github.com/AlekSi/nut@v0.3.1-0.20130607203728-cce108d4135e/integration_test/hook_for_test.go (about) 1 package integration_test 2 3 import ( 4 "bytes" 5 "fmt" 6 "os" 7 "os/exec" 8 "path/filepath" 9 "runtime" 10 "strings" 11 "syscall" 12 "testing" 13 14 . "launchpad.net/gocheck" 15 ) 16 17 // Global gocheck hook. 18 func TestIntegration(t *testing.T) { TestingT(t) } 19 20 var ( 21 TestNut1 = "../../test_nut1" 22 TestNut2 = "../../test_nut2" 23 TestNut3 = "../../test_nut3" 24 Wd string 25 nutBin string 26 ) 27 28 func init() { 29 var err error 30 Wd, err = os.Getwd() 31 if err != nil { 32 panic(err) 33 } 34 35 nutBin = filepath.Join(Wd, "..", "gonut.exe") 36 TestNut1 = filepath.Join(Wd, TestNut1) 37 TestNut2 = filepath.Join(Wd, TestNut2) 38 TestNut3 = filepath.Join(Wd, TestNut3) 39 } 40 41 func setupTest(c *C) { 42 for _, dir := range []string{TestNut1, TestNut2, TestNut3} { 43 runCommand(c, dir, "git", "reset --hard origin/master") 44 runCommand(c, dir, "git", "clean -xdf") 45 } 46 47 oa := fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH) 48 for _, dir := range []string{ 49 filepath.Join(Wd, "../../../../gonuts.io/"), 50 filepath.Join(Wd, "../../../../localhost/"), 51 filepath.Join(Wd, "../../../../../pkg/"+oa+"/gonuts.io/"), 52 filepath.Join(Wd, "../../../../../pkg/"+oa+"/localhost/"), 53 filepath.Join(Wd, "../../../../../bin/"), 54 filepath.Join(Wd, "../../../../../nut/"), 55 } { 56 // c.Logf("Removing %s", dir) 57 c.Assert(os.RemoveAll(dir), IsNil) 58 } 59 } 60 61 func runCommand(c *C, dir, command string, args string, exitCode ...int) (stdout, stderr string) { 62 var expectedCode int 63 switch len(exitCode) { 64 case 0: 65 case 1: 66 expectedCode = exitCode[0] 67 default: 68 c.Fatal("Invalid invocation of runCommand") 69 } 70 71 o, e := bytes.Buffer{}, bytes.Buffer{} 72 cmd := exec.Command(command, strings.Split(args, " ")...) 73 cmd.Dir = dir 74 cmd.Stdout, cmd.Stderr = &o, &e 75 err := cmd.Run() 76 stdout, stderr = strings.TrimSpace(o.String()), strings.TrimSpace(e.String()) 77 c.Logf("%s: %s %s", dir, command, args) 78 if stdout != "" { 79 c.Logf("stdout: %s", stdout) 80 } 81 if stderr != "" { 82 c.Logf("stderr: %s", stderr) 83 } 84 85 if err == nil { 86 if expectedCode == 0 { 87 return 88 } else { 89 c.Fatalf("Expected exit code %d, got 0.", expectedCode) 90 } 91 } 92 93 ee, ok := err.(*exec.ExitError) 94 if !ok { 95 c.Fatal(err) 96 } 97 actualCode := ee.Sys().(syscall.WaitStatus).ExitStatus() // why it's so hard?.. 98 if expectedCode != actualCode { 99 c.Fatalf("Expected exit code %d, got %d.", expectedCode, actualCode) 100 } 101 102 return 103 } 104 105 func runNut(c *C, dir string, args string, exitCode ...int) (stdout, stderr string) { 106 return runCommand(c, dir, nutBin, args, exitCode...) 107 } 108 109 func runGo(c *C, dir string, args string, exitCode ...int) (stdout, stderr string) { 110 return runCommand(c, dir, "go", args, exitCode...) 111 } 112 113 func gitNoDiff(c *C, dir string) { 114 runCommand(c, dir, "git", "diff --exit-code") 115 }