Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding in ability to templatize sub directories and unit tests #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions examples/json/json-example
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
{{end}}
6 changes: 5 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,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()))
Expand Down
64 changes: 64 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
@@ -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)))
}
1 change: 1 addition & 0 deletions test/expected/one.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
key1=one;
15 changes: 15 additions & 0 deletions test/expected/rendered-json-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

NAME0=service1
PORT0=8000
NAME1=service2
PORT1=9000

Index: 0
Name: service1
Port: 8000
---

Index: 1
Name: service2
Port: 9000
---
1 change: 1 addition & 0 deletions test/expected/two.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
key2=two
1 change: 1 addition & 0 deletions test/fixtures/etc/conf/two.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
key2={{ .Env.VAL_2 }}
1 change: 1 addition & 0 deletions test/fixtures/etc/sample/one.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
key1={{ .Env.VAL_1 }};