This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
/
hook_for_test.go
106 lines (90 loc) · 2.32 KB
/
hook_for_test.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package integration_test
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
"testing"
. "launchpad.net/gocheck"
)
// Global gocheck hook.
func TestIntegration(t *testing.T) { TestingT(t) }
var (
TestNut1 = "../../test_nut1"
TestNut2 = "../../test_nut2"
TestNut3 = "../../test_nut3"
nutBin string
)
func init() {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
nutBin = filepath.Join(wd, "..", "gonut.exe")
}
func setupTest(c *C) {
for _, dir := range []string{TestNut1, TestNut2, TestNut3} {
runCommand(c, dir, "git", "reset --hard origin/master")
runCommand(c, dir, "git", "clean -xdf")
}
oa := fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH)
for _, dir := range []string{
"../../../../gonuts.io/",
"../../../../localhost/",
"../../../../../pkg/" + oa + "/gonuts.io/",
"../../../../../pkg/" + oa + "/localhost/",
"../../../../../bin/",
"../../../../../nut/",
} {
c.Logf("%s", dir)
c.Assert(os.RemoveAll(dir), IsNil)
}
}
func runCommand(c *C, dir, command string, args string, exitCode ...int) (stdout, stderr string) {
var expectedCode int
switch len(exitCode) {
case 0:
case 1:
expectedCode = exitCode[0]
default:
c.Fatal("Invalid invacation of runCommand")
}
o, e := bytes.Buffer{}, bytes.Buffer{}
cmd := exec.Command(command, strings.Split(args, " ")...)
cmd.Dir = dir
cmd.Stdout, cmd.Stderr = &o, &e
err := cmd.Run()
stdout, stderr = strings.TrimSpace(o.String()), strings.TrimSpace(e.String())
c.Logf("%s: %s %s", dir, command, args)
c.Logf("stdout: %s", stdout)
c.Logf("stderr: %s", stderr)
if err == nil {
if expectedCode == 0 {
return
} else {
c.Fatalf("Expected exit code %d, got 0.", expectedCode)
}
}
ee, ok := err.(*exec.ExitError)
if !ok {
c.Fatal(err)
}
actualCode := ee.Sys().(syscall.WaitStatus).ExitStatus() // why it's so hard?..
if expectedCode != actualCode {
c.Fatalf("Expected exit code %d, got %d.", expectedCode, actualCode)
}
return
}
func runNut(c *C, dir string, args string, exitCode ...int) (stdout, stderr string) {
return runCommand(c, dir, nutBin, args, exitCode...)
}
func runGo(c *C, dir string, args string, exitCode ...int) (stdout, stderr string) {
return runCommand(c, dir, "go", args, exitCode...)
}
func gitNoDiff(c *C, dir string) {
runCommand(c, dir, "git", "diff --exit-code")
}