-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzs_build_test.go
72 lines (64 loc) · 1.3 KB
/
zs_build_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
package main
import (
"crypto/md5"
"encoding/hex"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
const TESTDIR = ".test"
func TestBuild(t *testing.T) {
files, _ := ioutil.ReadDir("testdata")
for _, f := range files {
if f.IsDir() {
testBuild(filepath.Join("testdata", f.Name()), t)
}
}
}
func testBuild(path string, t *testing.T) {
wd, _ := os.Getwd()
os.Chdir(path)
args := os.Args[:]
os.Args = []string{"zs", "build"}
t.Log("--- BUILD", path)
main()
compare(PUBDIR, TESTDIR, t)
os.Chdir(wd)
os.Args = args
}
func compare(pub, test string, t *testing.T) {
a := md5dir(pub)
b := md5dir(test)
for k, v := range a {
if s, ok := b[k]; !ok {
t.Error("Unexpected file:", k, v)
} else if s != v {
t.Error("Different file:", k, v, s)
} else {
t.Log("Matching file", k, v)
}
}
for k, v := range b {
if _, ok := a[k]; !ok {
t.Error("Missing file:", k, v)
}
}
}
func md5dir(path string) map[string]string {
files := map[string]string{}
filepath.Walk(path, func(s string, info os.FileInfo, err error) error {
if err == nil && !info.IsDir() {
if f, err := os.Open(s); err == nil {
defer f.Close()
hash := md5.New()
io.Copy(hash, f)
files[strings.TrimPrefix(s, path)] = hex.EncodeToString(hash.Sum(nil))
}
}
return nil
})
return files
}