-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmake_env_test.go
82 lines (71 loc) · 1.87 KB
/
make_env_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
package repl_test
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"testing"
)
const slash = string(os.PathSeparator)
// Runs make_env and then compares output.
func TestMakeEnv(t *testing.T) {
got, err := exec.Command("go", "build", "-o", "make_env", "make_env.go").Output()
if err != nil {
fmt.Printf("Error running: go build -o make_env make_env.go: %s %s", got, err)
log.Fatal(err)
}
got, err = exec.Command("./make_env", "strings").Output()
if err != nil {
fmt.Printf("Error running ./make_env: %s", got)
log.Fatal(err)
}
rightFileName := fmt.Sprintf("testdata%sstring_imports.go", slash)
var rightFile *os.File
rightFile, err = os.Open(rightFileName) // For read access.
if err != nil {
log.Fatal(err)
}
data := make([]byte, 500000)
count, err := rightFile.Read(data)
if err != nil {
t.Errorf("Failed to read 'right' data file %s:", rightFileName)
log.Fatal(err)
}
want := string(data[0:count])
if string(got) != want {
gotName := fmt.Sprintf("testdata%sstring_imports.got", slash)
gotLines := strings.Split(string(got), "\n")
wantLines := strings.Split(string(want), "\n")
wantLen := len(wantLines)
for i, line := range(gotLines) {
if i == wantLen {
fmt.Println("want results are shorter than got results, line", i+1)
break
}
if line != wantLines[i] {
fmt.Println("results differ starting at line", i+1)
fmt.Println("got:\n", line)
fmt.Println("want:\n", wantLines[i])
break
}
}
if err := ioutil.WriteFile(gotName, got, 0666); err == nil {
fmt.Printf("Full results are in file %s\n", gotName)
}
t.Errorf("make_env comparison test failed")
}
// Print a helpful hint if we don't make it to the end.
hint := "Run manually"
defer func() {
if hint != "" {
fmt.Println("FAIL")
fmt.Println(hint)
} else {
fmt.Println("PASS")
}
}()
hint = "" // call off the hounds
return
}