From c324e05bfd8759e291621d9edb5d2317e586f257 Mon Sep 17 00:00:00 2001 From: Josh Mahowald Date: Fri, 2 Dec 2016 14:48:54 -0600 Subject: [PATCH] Adding in ability to templatize sub directories and unit tests --- examples/json/json-example | 5 +-- template.go | 6 ++- template_test.go | 64 +++++++++++++++++++++++++++++ test/expected/one.conf | 1 + test/expected/rendered-json-example | 15 +++++++ test/expected/two.txt | 1 + test/fixtures/etc/conf/two.txt | 1 + test/fixtures/etc/sample/one.conf | 1 + 8 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 template_test.go create mode 100644 test/expected/one.conf create mode 100644 test/expected/rendered-json-example create mode 100644 test/expected/two.txt create mode 100644 test/fixtures/etc/conf/two.txt create mode 100644 test/fixtures/etc/sample/one.conf diff --git a/examples/json/json-example b/examples/json/json-example index 3ad2628..12902bd 100644 --- a/examples/json/json-example +++ b/examples/json/json-example @@ -9,17 +9,14 @@ } ] }` }} - NAME0={{ jsonQuery $jsonDoc "services.[0].name" }} PORT0={{ jsonQuery $jsonDoc "services.[0].port" }} NAME1={{ jsonQuery $jsonDoc "services.[1].name" }} PORT1={{ jsonQuery $jsonDoc "services.[1].port" }} - {{ range $index, $value := jsonQuery $jsonDoc "services" }} Index: {{ printf "%v" $index }} Name: {{ printf "%v" $value.name }} Port: {{ printf "%v" $value.port }} --- {{end}} - -{{end}} \ No newline at end of file +{{end}} diff --git a/template.go b/template.go index 46bf8c6..5d6d7c6 100644 --- a/template.go +++ b/template.go @@ -158,7 +158,11 @@ func generateDir(templateDir, destDir string) bool { } for _, file := range files { - if destDir == "" { + if file.IsDir() { + nextDestination := filepath.Join(destDir, file.Name()) + os.Mkdir(nextDestination, file.Mode()) + generateDir(filepath.Join(templateDir, file.Name()), nextDestination) + } else if destDir == "" { generateFile(filepath.Join(templateDir, file.Name()), "") } else { generateFile(filepath.Join(templateDir, file.Name()), filepath.Join(destDir, file.Name())) diff --git a/template_test.go b/template_test.go new file mode 100644 index 0000000..b0e778b --- /dev/null +++ b/template_test.go @@ -0,0 +1,64 @@ +package main + +import ( + "io/ioutil" + "os" + "path/filepath" + "strings" + "testing" + + // I use this instead of base testing Suite + // to bring back warm fuzzies of junit + . "gopkg.in/check.v1" +) + +// Hook up gocheck into the "go test" runner. +func Test(t *testing.T) { TestingT(t) } + +type MySuite struct { + dir string +} + +var _ = Suite(&MySuite{}) + +func (s *MySuite) SetUpSuite(c *C) { + os.Setenv("VAL_1", "one") + os.Setenv("VAL_2", "two") + s.dir = c.MkDir() +} + +func (s *MySuite) TestJsonTemplate(c *C) { + + templatePath := filepath.Join("examples", "json", "json-example") + answerFile := filepath.Join("test", "expected", "rendered-json-example") + destPath := filepath.Join(s.dir, "output-json-example") + generateFile(templatePath, destPath) + fileCompare(answerFile, destPath, c) +} + +// Test walking through a tree of templates +func (s *MySuite) TestDirTemplates(c *C) { + dirpath := filepath.Join("test", "fixtures") + generateDir(dirpath, s.dir) + + fileCompare(filepath.Join("test", "expected", "one.conf"), + filepath.Join(s.dir, + "etc", "sample", "one.conf"), c) + fileCompare(filepath.Join("test", "expected", "two.txt"), + filepath.Join(s.dir, + "etc", "conf", "two.txt"), c) + +} + +func fileCompare(expectedFile, actualFile string, c *C) { + expectedResult, err := ioutil.ReadFile(expectedFile) + if err != nil { + c.Errorf("No file %s", expectedFile) + } + actualResult, err := ioutil.ReadFile(actualFile) + if err != nil { + c.Errorf("No file %s", actualFile) + } + c.Assert(strings.TrimSpace(string(actualResult)), + Equals, strings.TrimSpace(string(expectedResult))) +} diff --git a/test/expected/one.conf b/test/expected/one.conf new file mode 100644 index 0000000..525c02c --- /dev/null +++ b/test/expected/one.conf @@ -0,0 +1 @@ +key1=one; diff --git a/test/expected/rendered-json-example b/test/expected/rendered-json-example new file mode 100644 index 0000000..6f557c8 --- /dev/null +++ b/test/expected/rendered-json-example @@ -0,0 +1,15 @@ + + NAME0=service1 + PORT0=8000 + NAME1=service2 + PORT1=9000 + + Index: 0 + Name: service1 + Port: 8000 + --- + + Index: 1 + Name: service2 + Port: 9000 + --- diff --git a/test/expected/two.txt b/test/expected/two.txt new file mode 100644 index 0000000..92d0ce9 --- /dev/null +++ b/test/expected/two.txt @@ -0,0 +1 @@ +key2=two diff --git a/test/fixtures/etc/conf/two.txt b/test/fixtures/etc/conf/two.txt new file mode 100644 index 0000000..56ae246 --- /dev/null +++ b/test/fixtures/etc/conf/two.txt @@ -0,0 +1 @@ +key2={{ .Env.VAL_2 }} diff --git a/test/fixtures/etc/sample/one.conf b/test/fixtures/etc/sample/one.conf new file mode 100644 index 0000000..266ddff --- /dev/null +++ b/test/fixtures/etc/sample/one.conf @@ -0,0 +1 @@ +key1={{ .Env.VAL_1 }};