add readfile e2e test
Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
parent
70b874a596
commit
33c66a6f46
|
|
@ -5,3 +5,12 @@ type Context struct {
|
|||
basePath string
|
||||
readFile func(string) ([]byte, error)
|
||||
}
|
||||
|
||||
// SetBasePath sets the base path for the template
|
||||
func (c *Context) SetBasePath(path string) {
|
||||
c.basePath = path
|
||||
}
|
||||
|
||||
func (c *Context) SetReadFile(f func(string) ([]byte, error)) {
|
||||
c.readFile = f
|
||||
}
|
||||
|
|
|
|||
|
|
@ -172,6 +172,10 @@ func (c *Context) ReadFile(filename string) (string, error) {
|
|||
path = filepath.Join(c.basePath, filename)
|
||||
}
|
||||
|
||||
if c.readFile == nil {
|
||||
return "", fmt.Errorf("readFile is not implemented")
|
||||
}
|
||||
|
||||
bytes, err := c.readFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ func TestHelmfileTemplateWithBuildCommand(t *testing.T) {
|
|||
_, filename, _, _ := runtime.Caller(0)
|
||||
projectRoot := filepath.Join(filepath.Dir(filename), "..", "..", "..", "..")
|
||||
helmfileBin := filepath.Join(projectRoot, "helmfile")
|
||||
testdataDir := "testdata"
|
||||
testdataDir := "testdata/snapshot"
|
||||
|
||||
entries, err := os.ReadDir(testdataDir)
|
||||
require.NoError(t, err)
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
test
|
||||
|
|
@ -77,13 +77,36 @@ var envExecTestCases = []tmplTestCase{
|
|||
{
|
||||
name: "envExec",
|
||||
tmplString: `{{ envExec (dict) "bash" (list "-c" "echo -n $testkey" ) }}`,
|
||||
output: "",
|
||||
},
|
||||
{
|
||||
name: "envExecInvalidEnvType",
|
||||
wantErr: true,
|
||||
tmplString: `{{ envExec "dict" "bash" (list "-c" "echo -n $testkey" ) }}`,
|
||||
output: "",
|
||||
},
|
||||
}
|
||||
|
||||
var execTestCases = []tmplTestCase{
|
||||
{
|
||||
name: "exec",
|
||||
tmplString: `{{ exec "bash" (list "-c" "echo -n $testkey" ) }}`,
|
||||
},
|
||||
{
|
||||
name: "execWithError",
|
||||
wantErr: true,
|
||||
tmplString: `{{ exec "bash" (list "-c" "exit 1" ) }}`,
|
||||
},
|
||||
}
|
||||
|
||||
var readFileTestCases = []tmplTestCase{
|
||||
{
|
||||
name: "readFile",
|
||||
tmplString: `{{ readFile "./testdata/tmpl/readfile.txt" }}`,
|
||||
output: "test",
|
||||
},
|
||||
{
|
||||
name: "readFileWithError",
|
||||
tmplString: `{{ readFile "./testdata/tmpl/readfile_error.txt" }}`,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -102,6 +125,8 @@ func (t *tmplE2e) load() {
|
|||
t.append(requireEnvTestCases...)
|
||||
t.append(requiredTestCases...)
|
||||
t.append(envExecTestCases...)
|
||||
t.append(execTestCases...)
|
||||
t.append(readFileTestCases...)
|
||||
}
|
||||
|
||||
var tmplE2eTest = tmplE2e{}
|
||||
|
|
@ -109,6 +134,8 @@ var tmplE2eTest = tmplE2e{}
|
|||
// TestTmplStrings tests the template string
|
||||
func TestTmplStrings(t *testing.T) {
|
||||
c := &tmpl.Context{}
|
||||
c.SetBasePath(".")
|
||||
c.SetReadFile(os.ReadFile)
|
||||
tmpl := template.New("stringTemplateTest").Funcs(c.CreateFuncMap())
|
||||
|
||||
tmplE2eTest.load()
|
||||
|
|
|
|||
Loading…
Reference in New Issue