]> Nishi Git Mirror - aya.git/commitdiff
added integration test for build procedure
authorSerge A. Zaitsev <zaitsev.serge@gmail.com>
Sat, 29 Aug 2015 13:28:15 +0000 (15:28 +0200)
committerSerge A. Zaitsev <zaitsev.serge@gmail.com>
Sat, 29 Aug 2015 13:28:15 +0000 (15:28 +0200)
.gitignore
testdata/empty/.empty [new file with mode: 0644]
testdata/page/.test/index.html [new file with mode: 0644]
testdata/page/index.html [new file with mode: 0644]
zs_build_test.go [new file with mode: 0644]

index 470733c168855617532de5f6ca5b1b0b3dfce42b..b6577528d7eed1a47745a2ff82753190b4e7462d 100644 (file)
@@ -1 +1,2 @@
 zs
+.pub
diff --git a/testdata/empty/.empty b/testdata/empty/.empty
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/testdata/page/.test/index.html b/testdata/page/.test/index.html
new file mode 100644 (file)
index 0000000..2149a48
--- /dev/null
@@ -0,0 +1,6 @@
+<html>
+       <body>
+               <h1>Hello
+</h1>
+       </body>
+</html>
diff --git a/testdata/page/index.html b/testdata/page/index.html
new file mode 100644 (file)
index 0000000..ee49424
--- /dev/null
@@ -0,0 +1,5 @@
+<html>
+       <body>
+               <h1>{{ echo Hello }}</h1>
+       </body>
+</html>
diff --git a/zs_build_test.go b/zs_build_test.go
new file mode 100644 (file)
index 0000000..78076b8
--- /dev/null
@@ -0,0 +1,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
+}