github.imxd.top/gopinath-langote/1build@v1.2.0/testing/cli_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 utils2 "github.com/gopinath-langote/1build/cmd/utils" 6 "os" 7 "os/exec" 8 "testing" 9 10 "github.com/gopinath-langote/1build/testing/fixtures" 11 "github.com/gopinath-langote/1build/testing/utils" 12 ) 13 14 var binaryName = "1build" 15 var binaryPath string 16 var testDirectory string 17 18 func TestAll(t *testing.T) { 19 Tests := fixtures.GetFixtures() 20 for _, tt := range Tests { 21 testResourceDirectory := utils.RecreateTestResourceDirectory(testDirectory) 22 23 t.Run(".:."+tt.Feature+".:."+tt.Name, func(t *testing.T) { 24 if tt.Setup != nil { 25 _ = tt.Setup(testResourceDirectory) 26 } 27 cmd := exec.Command(binaryPath, tt.CmdArgs...) 28 cmd.Dir = testResourceDirectory 29 out, _ := cmd.Output() 30 _ = tt.Assertion(testResourceDirectory, string(out), t) 31 if tt.Teardown != nil { 32 _ = tt.Teardown(testResourceDirectory) 33 } 34 }) 35 36 } 37 38 } 39 40 func TestMain(m *testing.M) { 41 testDir, _ := utils.CreateTempDir() 42 testDirectory = testDir 43 44 binaryPath = testDir + "/" + binaryName 45 buildBinary(binaryPath) 46 47 fmt.Println(utils2.DASH() + "\nBinary Path:- '" + binaryPath + "'\n" + utils2.DASH()) 48 49 exitCode := m.Run() 50 51 utils.RemoveAllFilesFromDir(testDir) 52 os.Exit(exitCode) 53 } 54 55 func buildBinary(path string) { 56 err := os.Chdir("..") 57 if err != nil { 58 fmt.Printf("could not make binary for %s: %v", binaryName, err.Error()) 59 os.Exit(1) 60 } 61 62 getDep := exec.Command("go", "build", "-o", path) 63 if err := getDep.Run(); err != nil { 64 fmt.Printf("could not make binary for %s: %v", binaryName, err.Error()) 65 os.Exit(1) 66 } 67 }