2020-11-22 05:25:24 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-11-24 06:10:46 +00:00
|
|
|
tmplContents = `{{title name}} {{if .bool}}{{.year}}{{end}}`
|
|
|
|
tmplTemplate = `
|
|
|
|
name = "john olheiser"
|
2020-11-22 05:25:24 +00:00
|
|
|
|
|
|
|
[year]
|
2020-11-24 06:10:46 +00:00
|
|
|
default = ${TMPL_TEST} # 2020
|
2020-11-22 05:25:24 +00:00
|
|
|
|
|
|
|
[package]
|
2020-11-24 06:10:46 +00:00
|
|
|
default = "pkg"
|
|
|
|
|
|
|
|
[bool]
|
|
|
|
default = true
|
|
|
|
`
|
2020-11-22 05:25:24 +00:00
|
|
|
tmplGold = "John Olheiser 2020"
|
|
|
|
tmplNewGold = "DO NOT OVERWRITE!"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testExecute(t *testing.T) {
|
2020-11-23 05:02:16 +00:00
|
|
|
// Set environment variable
|
|
|
|
if err := os.Setenv("TMPL_TEST", "2020"); err != nil {
|
|
|
|
t.Logf("could not set environment: %v", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
// Get template
|
|
|
|
tmpl, err := reg.GetTemplate("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("could not get template")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute template
|
|
|
|
if err := tmpl.Execute(destDir, true, true); err != nil {
|
|
|
|
t.Logf("could not execute template: %v\n", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check contents of file
|
|
|
|
testPath := filepath.Join(destDir, "TEST")
|
|
|
|
contents, err := ioutil.ReadFile(testPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("could not read file: %v\n", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(contents) != tmplGold {
|
|
|
|
t.Logf("contents did not match:\n\tExpected: %s\n\tGot: %s", tmplGold, string(contents))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if directory was created
|
|
|
|
pkgPath := filepath.Join(destDir, "PKG")
|
|
|
|
if _, err := os.Lstat(pkgPath); err != nil {
|
|
|
|
t.Logf("expected a directory at %s: %v\n", pkgPath, err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change file to test non-overwrite
|
|
|
|
if err := ioutil.WriteFile(testPath, []byte(tmplNewGold), os.ModePerm); err != nil {
|
|
|
|
t.Logf("could not write file: %v\n", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(destDir, true, false); err != nil {
|
|
|
|
t.Logf("could not execute template: %v\n", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
contents, err = ioutil.ReadFile(testPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("could not read file: %v\n", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(contents) != tmplNewGold {
|
|
|
|
t.Logf("contents did not match:\n\tExpected: %s\n\tGot: %s", tmplNewGold, string(contents))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|