-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain_test.go
55 lines (52 loc) · 1.52 KB
/
main_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
package main
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/aryann/difflib"
)
func TestMain(t *testing.T) {
build, _ := filepath.Abs(".")
filepath.Walk("test", func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".sh") {
cmd := exec.Command("bash", filepath.Base(path))
cmd.Dir = filepath.Dir(path)
cmd.Env = []string{"PATH=" + build}
stderr := new(bytes.Buffer)
cmd.Stderr = stderr
output, err := cmd.Output()
if err != nil {
t.Errorf("FAIL: execution failed: " + path + ": " + err.Error() + " " + stderr.String())
} else {
outfile := strings.TrimSuffix(path, filepath.Ext(path)) + ".txt"
expected, err := ioutil.ReadFile(outfile)
if err != nil {
t.Errorf("FAIL: error on reading output file: " + outfile)
} else {
diffs := difflib.Diff(strings.Split(stderr.String()+string(output), "\n"), strings.Split(string(expected), "\n"))
help := strings.Contains(string(output), "NAME:")
differs := false
for _, diff := range diffs {
differs = differs || (help && diff.Delta == difflib.RightOnly || !help && diff.Delta != difflib.Common)
}
if differs {
buf := bytes.NewBufferString("")
for _, diff := range diffs {
if diff.Delta != difflib.Common {
buf.WriteString(diff.String() + "\n")
}
}
t.Errorf("FAIL: output differs: " + path + "\n" + buf.String())
} else {
t.Logf("PASS: " + path + "\n")
}
}
}
}
return nil
})
}