forked from microsoft/cobalt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
71 lines (62 loc) · 1.78 KB
/
magefile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Build a script to format and run tests of a Terraform module project
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// The default target when the command executes `mage` in Cloud Shell
var Default = RunAllTargets
func main() {
Default()
}
// A build step that runs Clean, Format, Unit and Integration in sequence
func RunAllTargets() {
mg.Deps(RunUnitTests)
mg.Deps(RunIntegrationTests)
}
// A build step that runs unit tests
func RunUnitTests() error {
mg.Deps(CleanAll)
mg.Deps(FormatGolangFiles)
fmt.Println("INFO: Running unit tests...")
return sh.RunV("go", "test", "./...", "-run", "TestUT", "-v")
}
// A build step that runs integration tests
func RunIntegrationTests() error {
mg.Deps(CleanAll)
mg.Deps(FormatGolangFiles)
fmt.Println("INFO: Running integration tests...")
return sh.RunV("go", "test", "./...", "-run", "TestIT", "-v", "-timeout", "99999s")
}
// A build step that formats both Terraform code and Go code
func FormatGolangFiles() error {
fmt.Println("INFO: Formatting...")
return sh.RunV("go", "fmt", "./...")
}
// A build step that removes temporary build and test files
func CleanAll() error {
fmt.Println("INFO: Cleaning...")
return filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && info.Name() == "vendor" {
return filepath.SkipDir
}
if info.IsDir() && info.Name() == ".terraform" {
os.RemoveAll(path)
fmt.Printf("Removed \"%v\"\n", path)
return filepath.SkipDir
}
if !info.IsDir() && (info.Name() == "terraform.tfstate" ||
info.Name() == "terraform.tfplan" ||
info.Name() == "terraform.tfstate.backup") {
os.Remove(path)
fmt.Printf("Removed \"%v\"\n", path)
}
return nil
})
}