diff --git a/Makefile b/Makefile index f337e94..a64e5db 100644 --- a/Makefile +++ b/Makefile @@ -44,13 +44,11 @@ lint: golint ./... build: - go fmt ./... godep go build -ldflags $(LDFLAGS) $(BINARY) install: - go fmt ./... godep go install -ldflags $(LDFLAGS) $(BINARIES) clean: go clean ./... - rm -rf tnator + rm -rf t2 diff --git a/stack/stack_test.go b/stack/stack_test.go index f0510ef..4a409ad 100644 --- a/stack/stack_test.go +++ b/stack/stack_test.go @@ -1,31 +1,31 @@ package stack import ( - "testing" + "testing" ) var stacktests = []struct { - list []interface{} + list []interface{} }{ - {[]interface{}{"one", "two", "three"}}, - {[]interface{}{1, 2, 3}}, + {[]interface{}{"one", "two", "three"}}, + {[]interface{}{1, 2, 3}}, } func TestStack(t *testing.T) { - for _, st := range stacktests { - i, s := 0, NewStack() - for _, in := range st.list { - if s.Len() != i { - t.Errorf("stack.Len(): %d, want %d", s.Len(), i) - } - s.Push(in) - i++ - } - for j := 2; j >= 0; j-- { - item := s.Pop() - if st.list[j] != item { - t.Errorf("stack.Pop(): %v, want %v", item, st.list[j]) - } - } - } + for _, st := range stacktests { + i, s := 0, NewStack() + for _, in := range st.list { + if s.Len() != i { + t.Errorf("stack.Len(): %d, want %d", s.Len(), i) + } + s.Push(in) + i++ + } + for j := 2; j >= 0; j-- { + item := s.Pop() + if st.list[j] != item { + t.Errorf("stack.Pop(): %v, want %v", item, st.list[j]) + } + } + } } diff --git a/template/template.go b/template/template.go index 410b2e9..7aeecfe 100644 --- a/template/template.go +++ b/template/template.go @@ -1,115 +1,115 @@ package template import ( - "crypto/sha1" - "fmt" - "github.com/albertrdixon/tmplnator/stack" - "github.com/oxtoacart/bpool" - "io/ioutil" - "os" - "path/filepath" - "strings" - "text/template" + "crypto/sha1" + "fmt" + "github.com/albertrdixon/tmplnator/stack" + "github.com/oxtoacart/bpool" + "io/ioutil" + "os" + "path/filepath" + "strings" + "text/template" ) type Template struct { - template *template.Template - Sha1 string - Vars map[string]string - Src string + template *template.Template + Sha1 string + Vars map[string]string + Src string } var ( - files map[string]*File - varMap map[string]string - bp *bpool.BufferPool + files map[string]*File + varMap map[string]string + bp *bpool.BufferPool ) func (t Template) Write() error { - b := bp.Get() - defer bp.Put(b) + b := bp.Get() + defer bp.Put(b) - err := t.template.Execute(b, t) - if err != nil { - return err - } + err := t.template.Execute(b, t) + if err != nil { + return err + } - file := files[t.Sha1] - fmt.Printf("==> Generating %q from %q\n", file.Destination(), t.Src) - if err := os.MkdirAll(file.DestinationDir(), 0755); err != nil { - return err - } - f, err := os.OpenFile(file.Destination(), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.FileMode(file.Mode)) - if err != nil { - return err - } - defer f.Close() - _, err = f.Write(b.Bytes()) - if err != nil { - return err - } - return nil + file := files[t.Sha1] + fmt.Printf("==> Generating %q from %q\n", file.Destination(), t.Src) + if err := os.MkdirAll(file.DestinationDir(), 0755); err != nil { + return err + } + f, err := os.OpenFile(file.Destination(), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.FileMode(file.Mode)) + if err != nil { + return err + } + defer f.Close() + _, err = f.Write(b.Bytes()) + if err != nil { + return err + } + return nil } func ParseDirectory(dir string, prefix string) (*stack.Stack, error) { - st := stack.NewStack() - return st, filepath.Walk(dir, walkfunc(st, prefix)) + st := stack.NewStack() + return st, filepath.Walk(dir, walkfunc(st, prefix)) } func walkfunc(tmplStack *stack.Stack, prefix string) filepath.WalkFunc { - return func(path string, info os.FileInfo, err error) error { - if info.Mode().IsRegular() { - return ParseTemplate(path, prefix, tmplStack) - } - return nil - } + return func(path string, info os.FileInfo, err error) error { + if info.Mode().IsRegular() { + return ParseTemplate(path, prefix, tmplStack) + } + return nil + } } func ParseTemplate(path string, prefix string, st *stack.Stack) error { - base := filepath.Base(path) - t := new(Template) - t.Src = path - t.Vars = varMap - contents, err := ioutil.ReadFile(path) - if err != nil { - return err - } + base := filepath.Base(path) + t := new(Template) + t.Src = path + t.Vars = varMap + contents, err := ioutil.ReadFile(path) + if err != nil { + return err + } - t.Sha1 = fmt.Sprintf("%x", sha1.New().Sum(contents)) - template, err := newTemplate(path).Parse(string(contents)) - if err != nil { - return err - } + t.Sha1 = fmt.Sprintf("%x", sha1.New().Sum(contents)) + template, err := newTemplate(path).Parse(string(contents)) + if err != nil { + return err + } - t.template = template - st.Push(t) - files[t.Sha1] = newFile(prefix, base) - return nil + t.template = template + st.Push(t) + files[t.Sha1] = newFile(prefix, base) + return nil } func newTemplate(path string) *template.Template { - return template.New(path).Funcs(newFuncMap()) + return template.New(path).Funcs(newFuncMap()) } func envMap() map[string]string { - env := make(map[string]string, len(os.Environ())) - for _, val := range os.Environ() { - index := strings.Index(val, "=") - env[val[:index]] = val[index+1:] - } - return env + env := make(map[string]string, len(os.Environ())) + for _, val := range os.Environ() { + index := strings.Index(val, "=") + env[val[:index]] = val[index+1:] + } + return env } func iface(list []string) []interface{} { - vals := make([]interface{}, len(list)) - for i, v := range list { - vals[i] = v - } - return vals + vals := make([]interface{}, len(list)) + for i, v := range list { + vals[i] = v + } + return vals } func init() { - bp = bpool.NewBufferPool(48) - varMap = envMap() - files = make(map[string]*File) + bp = bpool.NewBufferPool(48) + varMap = envMap() + files = make(map[string]*File) } diff --git a/template/template_funcs_test.go b/template/template_funcs_test.go index 412cda6..19bac3e 100644 --- a/template/template_funcs_test.go +++ b/template/template_funcs_test.go @@ -1,90 +1,90 @@ package template import ( - "os" - "path/filepath" - "testing" + "os" + "path/filepath" + "testing" ) func TestFileExists(t *testing.T) { - tmpFile := filepath.Join(os.TempDir(), "sg-test") - os.Create(tmpFile) - defer os.Remove(tmpFile) - fakeFile := "path/to/fake_file" + tmpFile := filepath.Join(os.TempDir(), "sg-test") + os.Create(tmpFile) + defer os.Remove(tmpFile) + fakeFile := "path/to/fake_file" - // Test fileExists on a real file - shouldBeTrue, err := fileExists(tmpFile) - if err != nil { - t.Errorf("fileExists returned an error with %q: %v", tmpFile, err) - } - if !shouldBeTrue { - t.Errorf("Could not find %q even though it was created", tmpFile) - } + // Test fileExists on a real file + shouldBeTrue, err := fileExists(tmpFile) + if err != nil { + t.Errorf("fileExists returned an error with %q: %v", tmpFile, err) + } + if !shouldBeTrue { + t.Errorf("Could not find %q even though it was created", tmpFile) + } - // Test fileExists on a fake file - shouldBeFalse, err := fileExists(fakeFile) - if err != nil { - t.Errorf("fileExists returned an error with %q: %v", fakeFile, err) - } - if shouldBeFalse { - t.Error("Fake file returned true, but hsould be false") - } + // Test fileExists on a fake file + shouldBeFalse, err := fileExists(fakeFile) + if err != nil { + t.Errorf("fileExists returned an error with %q: %v", fakeFile, err) + } + if shouldBeFalse { + t.Error("Fake file returned true, but hsould be false") + } } func TestParseURL(t *testing.T) { - goodURL := "http://my.domain.net:8888" - expectedHost := "my.domain.net:8888" + goodURL := "http://my.domain.net:8888" + expectedHost := "my.domain.net:8888" - good, err := parseURL(goodURL) - if err != nil { - t.Errorf("Should not have gotten an error parsing %q: %v", goodURL, err) - } - if good == nil { - t.Errorf("Should have parsed %q correctly, but got nil", good) - } - if good.Host != expectedHost { - t.Errorf("Expected Host to be %q but got %q", expectedHost, good.Host) - } + good, err := parseURL(goodURL) + if err != nil { + t.Errorf("Should not have gotten an error parsing %q: %v", goodURL, err) + } + if good == nil { + t.Errorf("Should have parsed %q correctly, but got nil", good) + } + if good.Host != expectedHost { + t.Errorf("Expected Host to be %q but got %q", expectedHost, good.Host) + } } func TestDefaultValue(t *testing.T) { - var defaulttests = []struct { - in interface{} - def interface{} - expected interface{} - }{ - {nil, "default", "default"}, - {nil, 2, 2}, - {"", "default", "default"}, - {"", 3, 3}, - {"realoutput", "shouldnotshow", "realoutput"}, - } + var defaulttests = []struct { + in interface{} + def interface{} + expected interface{} + }{ + {nil, "default", "default"}, + {nil, 2, 2}, + {"", "default", "default"}, + {"", 3, 3}, + {"realoutput", "shouldnotshow", "realoutput"}, + } - for _, dt := range defaulttests { - out := defaultValue(dt.in, dt.def) - if out != dt.expected { - t.Errorf("defaultValue(%v, %v): %q, want %q", dt.in, dt.def, out, dt.expected) - } - } + for _, dt := range defaulttests { + out := defaultValue(dt.in, dt.def) + if out != dt.expected { + t.Errorf("defaultValue(%v, %v): %q, want %q", dt.in, dt.def, out, dt.expected) + } + } } func TestJSON(t *testing.T) { - goodJSON := "{\"Key1\":\"Value1\",\"Key2\":\"Value2\"}" - badJSON := "{\"Key3\":\"Blah\"" + goodJSON := "{\"Key1\":\"Value1\",\"Key2\":\"Value2\"}" + badJSON := "{\"Key3\":\"Blah\"" - out, err := UnmarshalJSON(goodJSON) - if err != nil { - t.Errorf("UnmarshalJSON(good): expected no error, got %v", err) - } - if out["Key2"] != "Value2" { - t.Error("UnmarshalJSON(good): didn't get expected structure back") - } + out, err := UnmarshalJSON(goodJSON) + if err != nil { + t.Errorf("UnmarshalJSON(good): expected no error, got %v", err) + } + if out["Key2"] != "Value2" { + t.Error("UnmarshalJSON(good): didn't get expected structure back") + } - out, err = UnmarshalJSON(badJSON) - if err == nil { - t.Errorf("UnmarshalJSON(bad): expected error, got %v", err) - } - if out != nil { - t.Error("UnmarshalJSON(bad): expected out to be nil") - } + out, err = UnmarshalJSON(badJSON) + if err == nil { + t.Errorf("UnmarshalJSON(bad): expected error, got %v", err) + } + if out != nil { + t.Error("UnmarshalJSON(bad): expected out to be nil") + } } diff --git a/template/template_test.go b/template/template_test.go index 463d59d..9e25500 100644 --- a/template/template_test.go +++ b/template/template_test.go @@ -1,11 +1,11 @@ package template import ( - "github.com/albertrdixon/tmplnator/stack" - "io/ioutil" - "os" - "path/filepath" - "testing" + "github.com/albertrdixon/tmplnator/stack" + "io/ioutil" + "os" + "path/filepath" + "testing" ) var tmpl = ` @@ -18,46 +18,46 @@ Foo = Baz ` func TestTemplate(t *testing.T) { - os.Setenv("BAR", "Baz") - varMap = envMap() - - prefix, err := ioutil.TempDir("", "tmpltest") - defer os.RemoveAll(prefix) - if err != nil { - t.Errorf("ParseTemplate(): Could not create tmp dir: %v", err) - } - - file, targetDir := filepath.Join(prefix, "test"), filepath.Join(prefix, "out") - os.Mkdir(targetDir, 0755) - f, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) - if err != nil { - t.Errorf("ParseTemplate(): Could not create testFile: %v", err) - } - f.WriteString(tmpl) - f.Close() - - st := stack.NewStack() - err = ParseTemplate(file, prefix, st) - if err != nil { - t.Errorf("ParseTemplate(): did not expect an error: %v", err) - } - - tm, ok := st.Pop().(*Template) - if !ok { - t.Error("ParseTemplate(): Could not cast stack item as Template") - } - - err = tm.Write() - if err != nil { - t.Errorf("template.Write(): did not expect error: %v", err) - } - - out, err := ioutil.ReadFile(filepath.Join(targetDir, "test")) - if err != nil { - t.Errorf("template.Write(): problems reading written file: %v", err) - } - - if string(out) != expected { - t.Errorf("expected %q, got %q", expected, out) - } + os.Setenv("BAR", "Baz") + varMap = envMap() + + prefix, err := ioutil.TempDir("", "tmpltest") + defer os.RemoveAll(prefix) + if err != nil { + t.Errorf("ParseTemplate(): Could not create tmp dir: %v", err) + } + + file, targetDir := filepath.Join(prefix, "test"), filepath.Join(prefix, "out") + os.Mkdir(targetDir, 0755) + f, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) + if err != nil { + t.Errorf("ParseTemplate(): Could not create testFile: %v", err) + } + f.WriteString(tmpl) + f.Close() + + st := stack.NewStack() + err = ParseTemplate(file, prefix, st) + if err != nil { + t.Errorf("ParseTemplate(): did not expect an error: %v", err) + } + + tm, ok := st.Pop().(*Template) + if !ok { + t.Error("ParseTemplate(): Could not cast stack item as Template") + } + + err = tm.Write() + if err != nil { + t.Errorf("template.Write(): did not expect error: %v", err) + } + + out, err := ioutil.ReadFile(filepath.Join(targetDir, "test")) + if err != nil { + t.Errorf("template.Write(): problems reading written file: %v", err) + } + + if string(out) != expected { + t.Errorf("expected %q, got %q", expected, out) + } } diff --git a/version/version.go b/version/version.go index 174e65c..31375a7 100644 --- a/version/version.go +++ b/version/version.go @@ -5,7 +5,7 @@ import ( ) const ( - CodeVersion = "v0.0.2" + CodeVersion = "v0.0.3" ) var Build string