47
|
1 package main
|
|
2
|
|
3 import (
|
|
4 "crypto/md5"
|
|
5 "encoding/hex"
|
|
6 "io"
|
|
7 "io/ioutil"
|
|
8 "os"
|
|
9 "path/filepath"
|
|
10 "strings"
|
|
11 "testing"
|
|
12 )
|
|
13
|
|
14 const TESTDIR = ".test"
|
|
15
|
|
16 func TestBuild(t *testing.T) {
|
|
17 files, _ := ioutil.ReadDir("testdata")
|
|
18 for _, f := range files {
|
|
19 if f.IsDir() {
|
|
20 testBuild(filepath.Join("testdata", f.Name()), t)
|
|
21 }
|
|
22 }
|
|
23 }
|
|
24
|
|
25 func testBuild(path string, t *testing.T) {
|
|
26 wd, _ := os.Getwd()
|
|
27 os.Chdir(path)
|
|
28 args := os.Args[:]
|
|
29 os.Args = []string{"zs", "build"}
|
|
30 t.Log("--- BUILD", path)
|
|
31 main()
|
|
32
|
|
33 compare(PUBDIR, TESTDIR, t)
|
|
34
|
|
35 os.Chdir(wd)
|
|
36 os.Args = args
|
|
37 }
|
|
38
|
|
39 func compare(pub, test string, t *testing.T) {
|
|
40 a := md5dir(pub)
|
|
41 b := md5dir(test)
|
|
42 for k, v := range a {
|
|
43 if s, ok := b[k]; !ok {
|
|
44 t.Error("Unexpected file:", k, v)
|
|
45 } else if s != v {
|
|
46 t.Error("Different file:", k, v, s)
|
|
47 } else {
|
|
48 t.Log("Matching file", k, v)
|
|
49 }
|
|
50 }
|
|
51 for k, v := range b {
|
|
52 if _, ok := a[k]; !ok {
|
|
53 t.Error("Missing file:", k, v)
|
|
54 }
|
|
55 }
|
|
56 }
|
|
57
|
|
58 func md5dir(path string) map[string]string {
|
|
59 files := map[string]string{}
|
|
60 filepath.Walk(path, func(s string, info os.FileInfo, err error) error {
|
|
61 if err == nil && !info.IsDir() {
|
|
62 if f, err := os.Open(s); err == nil {
|
|
63 defer f.Close()
|
|
64 hash := md5.New()
|
|
65 io.Copy(hash, f)
|
|
66 files[strings.TrimPrefix(s, path)] = hex.EncodeToString(hash.Sum(nil))
|
|
67 }
|
|
68 }
|
|
69 return nil
|
|
70 })
|
|
71 return files
|
|
72 }
|