2020-11-18 05:28:07 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
tmplDir string
|
|
|
|
regDir string
|
|
|
|
destDir string
|
|
|
|
reg *Registry
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
var err error
|
2020-12-29 03:14:28 +00:00
|
|
|
destDir, err = ioutil.TempDir(os.TempDir(), "tmpl-dest")
|
2020-11-18 05:28:07 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up template
|
|
|
|
setupTemplate()
|
|
|
|
|
|
|
|
// Set up registry
|
|
|
|
setupRegistry()
|
|
|
|
|
|
|
|
status := m.Run()
|
|
|
|
|
|
|
|
if err = os.RemoveAll(destDir); err != nil {
|
|
|
|
fmt.Printf("could not clean up temp directory %s\n", destDir)
|
|
|
|
}
|
|
|
|
if err = os.RemoveAll(tmplDir); err != nil {
|
|
|
|
fmt.Printf("could not clean up temp directory %s\n", tmplDir)
|
|
|
|
}
|
|
|
|
if err = os.RemoveAll(regDir); err != nil {
|
|
|
|
fmt.Printf("could not clean up temp directory %s\n", regDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplate(t *testing.T) {
|
|
|
|
t.Run("save", testSave)
|
|
|
|
t.Run("get", testGet)
|
|
|
|
t.Run("get-fail", testGetFail)
|
|
|
|
t.Run("execute", testExecute)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSave(t *testing.T) {
|
|
|
|
if _, err := reg.SaveTemplate("test", tmplDir); err != nil {
|
|
|
|
t.Log("could not save template")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGet(t *testing.T) {
|
|
|
|
_, err := reg.GetTemplate("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("could not get template")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGetFail(t *testing.T) {
|
|
|
|
_, err := reg.GetTemplate("fail")
|
|
|
|
if !IsErrTemplateNotFound(err) {
|
|
|
|
t.Logf("template should not exist")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupTemplate() {
|
|
|
|
var err error
|
2020-12-29 03:14:28 +00:00
|
|
|
tmplDir, err = ioutil.TempDir(os.TempDir(), "tmpl-setup")
|
2020-11-18 05:28:07 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Template config
|
|
|
|
fi, err := os.Create(filepath.Join(tmplDir, "template.toml"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
_, err = fi.WriteString(tmplTemplate)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := fi.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
// Template directories
|
|
|
|
pkgPath := filepath.Join(tmplDir, "template", "{{upper package}}")
|
|
|
|
if err := os.MkdirAll(pkgPath, os.ModePerm); err != nil {
|
2020-11-18 05:28:07 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-22 05:25:24 +00:00
|
|
|
fi, err = os.Create(filepath.Join(pkgPath, ".keep"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := fi.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Template file
|
2020-11-18 05:28:07 +00:00
|
|
|
fi, err = os.Create(filepath.Join(tmplDir, "template", "TEST"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
_, err = fi.WriteString(tmplContents)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := fi.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupRegistry() {
|
|
|
|
var err error
|
2020-12-29 03:14:28 +00:00
|
|
|
regDir, err = ioutil.TempDir(os.TempDir(), "tmpl-reg")
|
2020-11-18 05:28:07 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reg, err = Open(regDir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|