changeset 16:be58ace6edae draft

added integration test for build procedure
author zaitsev.serge
date Sat, 29 Aug 2015 13:28:15 +0000
parents a9c42bd52f64
children 0214b1b5f5eb
files .gitignore testdata/empty/.empty testdata/page/.test/index.html testdata/page/index.html zs_build_test.go
diffstat 4 files changed, 84 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.gitignore	Sat Aug 29 13:17:13 2015 +0000
+++ b/.gitignore	Sat Aug 29 13:28:15 2015 +0000
@@ -1,1 +1,2 @@
 zs
+.pub
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/testdata/page/.test/index.html	Sat Aug 29 13:28:15 2015 +0000
@@ -0,0 +1,6 @@
+<html>
+	<body>
+		<h1>Hello
+</h1>
+	</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/testdata/page/index.html	Sat Aug 29 13:28:15 2015 +0000
@@ -0,0 +1,5 @@
+<html>
+	<body>
+		<h1>{{ echo Hello }}</h1>
+	</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/zs_build_test.go	Sat Aug 29 13:28:15 2015 +0000
@@ -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
+}