2020-11-22 05:25:24 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2022-06-14 19:59:53 +00:00
|
|
|
|
|
|
|
"github.com/matryer/is"
|
2020-11-22 05:25:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-06-14 19:59:53 +00:00
|
|
|
tmplContents = `{{title name}} (@{{username}}) {{if .bool}}{{.year}}{{end}} {{org}}`
|
2020-11-24 06:10:46 +00:00
|
|
|
tmplTemplate = `
|
2022-06-14 19:59:53 +00:00
|
|
|
prompts:
|
|
|
|
- id: name
|
|
|
|
default: john olheiser
|
|
|
|
- id: year
|
|
|
|
default: ${TMPL_TEST} # 2020
|
|
|
|
- id: package
|
|
|
|
default: pkg
|
|
|
|
- id: bool
|
|
|
|
default: true
|
|
|
|
- id: username
|
|
|
|
default: username
|
|
|
|
- id: org
|
|
|
|
default: ${TMPL_PROMPT_USERNAME}/org
|
2020-11-24 06:10:46 +00:00
|
|
|
`
|
2022-06-14 19:59:53 +00:00
|
|
|
tmplGold = "John Olheiser (@jolheiser) 2020 jolheiser/org"
|
2020-11-22 05:25:24 +00:00
|
|
|
tmplNewGold = "DO NOT OVERWRITE!"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testExecute(t *testing.T) {
|
2022-06-14 19:59:53 +00:00
|
|
|
assert := is.New(t)
|
|
|
|
destDir := t.TempDir()
|
|
|
|
|
2020-11-23 05:02:16 +00:00
|
|
|
// Set environment variable
|
2022-06-14 19:59:53 +00:00
|
|
|
err := os.Setenv("TMPL_TEST", "2020")
|
|
|
|
assert.NoErr(err) // Should set TMPL_TEST env
|
|
|
|
|
|
|
|
err = os.Setenv("TMPL_VAR_USERNAME", "jolheiser")
|
|
|
|
assert.NoErr(err) // Should set TMPL_VAR_USERNAME env
|
2020-11-23 05:02:16 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
// Get template
|
|
|
|
tmpl, err := reg.GetTemplate("test")
|
2022-06-14 19:59:53 +00:00
|
|
|
assert.NoErr(err) // Should get template
|
2020-11-22 05:25:24 +00:00
|
|
|
|
|
|
|
// Execute template
|
2022-06-14 19:59:53 +00:00
|
|
|
err = tmpl.Execute(destDir, true, true)
|
|
|
|
assert.NoErr(err) // Should execute template
|
2020-11-22 05:25:24 +00:00
|
|
|
|
|
|
|
// Check contents of file
|
|
|
|
testPath := filepath.Join(destDir, "TEST")
|
2023-08-20 19:30:50 +00:00
|
|
|
contents, err := os.ReadFile(testPath)
|
2022-06-14 19:59:53 +00:00
|
|
|
assert.NoErr(err) // Should be able to read TEST file
|
|
|
|
assert.Equal(string(contents), tmplGold) // Template should match golden file
|
2020-11-22 05:25:24 +00:00
|
|
|
|
|
|
|
// Check if directory was created
|
|
|
|
pkgPath := filepath.Join(destDir, "PKG")
|
2022-06-14 19:59:53 +00:00
|
|
|
_, err = os.Lstat(pkgPath)
|
|
|
|
assert.NoErr(err) // PKG directory should exist
|
2020-11-22 05:25:24 +00:00
|
|
|
|
2021-01-03 03:06:32 +00:00
|
|
|
// Check for .tmplkeep
|
|
|
|
tmplKeep := filepath.Join(pkgPath, ".tmplkeep")
|
2022-06-14 19:59:53 +00:00
|
|
|
_, err = os.Lstat(tmplKeep)
|
|
|
|
assert.True(err != nil) // .tmplkeep file should NOT be retained
|
2021-01-03 03:06:32 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
// Change file to test non-overwrite
|
2023-08-20 19:30:50 +00:00
|
|
|
err = os.WriteFile(testPath, []byte(tmplNewGold), os.ModePerm)
|
2022-06-14 19:59:53 +00:00
|
|
|
assert.NoErr(err) // Writing file should succeed
|
2020-11-22 05:25:24 +00:00
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
err = tmpl.Execute(destDir, true, false)
|
|
|
|
assert.NoErr(err) // Should execute template
|
2020-11-22 05:25:24 +00:00
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
contents, err = os.ReadFile(testPath)
|
|
|
|
assert.NoErr(err) // Should be able to read file
|
|
|
|
assert.Equal(string(contents), tmplNewGold) // Template should match new golden file
|
2020-11-22 05:25:24 +00:00
|
|
|
}
|