Skip to content

Commit

Permalink
[renderer] Fix variable handling in output templates (#453)
Browse files Browse the repository at this point in the history
* Add test for bug; add outputs to fixtures with deps
* Get a PackTemplateContext from renderer's parsedVariables
  • Loading branch information
angrycub authored Oct 26, 2023
1 parent d4f4e46 commit 013d853
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions fixtures/v2/test_registry/packs/deps_test_1/outputs.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"job_name": [[ var "job_name" . | quote ]],
"child1.job_name": [[ var "job_name" .child1 | quote ]],
"child1.gc.job_name": [[ var "job_name" .child1.gc | quote ]],
"child2.job_name": [[ var "job_name" .child2 | quote ]],
"child2.gc.job_name": [[ var "job_name" .child2.gc | quote ]]
}
1 change: 1 addition & 0 deletions fixtures/v2/test_registry/packs/my_alias_test/outputs.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[var "job_name" .]],[[var "job_name" .child1]],[[var "job_name" .child2]]
34 changes: 34 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,40 @@ func TestCLI_PackRender_SetDepVarWithFlag(t *testing.T) {
must.SliceContainsAll(t, expected, elems)
}

func TestCLI_PackRender_VarsInOutputTemplate(t *testing.T) {
t.Parallel()
// This test has to do some extra shenanigans because dependent pack template
// output is not guaranteed to be ordered. This requires that the test handle
// either order.
expected := []string{
"deps_test/child1/child1.nomad=override",
"deps_test/child2/child2.nomad=child2",
"deps_test/deps_test.nomad=deps_test",
"outputs.tpl=deps_test,override,child2",
}

result := runPackCmd(t, []string{
"render",
"--no-format=true",
"--var", "child1.job_name=override",
"--render-output-template=true",
getTestPackPath(t, "my_alias_test"),
})

must.Eq(t, result.cmdErr.String(), "", must.Sprintf("cmdErr should be empty, but was %q", result.cmdErr.String()))
must.Eq(t, 0, result.exitCode, must.Sprintf("incorrect exit code.\ncmdOut:\n%v\ns", result.cmdOut.String()))
must.StrNotContains(t, result.cmdOut.String(), "Error")

// Performing a little clever string manipulation on the render output to
// prepare it for splitting into a slice of string enables us to use
// require.ElementsMatch to validate goodness.
outStr := strings.TrimSpace(result.cmdOut.String())
outStr = strings.ReplaceAll(outStr, ":\n\n", "=")
elems := strings.Split(outStr, "\n")

must.SliceContainsAll(t, expected, elems, must.Sprintf("unexpected returned value.\nexpected: %v\nelems: %v\nstdout:\n%v\n", expected, elems, result.cmdOut.String()))
}

func TestCLI_CLIFlag_Namespace(t *testing.T) {
testCases := []struct {
desc string
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ func (r *Renderer) RenderOutput() (string, error) {
return "", err
}

ptc, _ := r.pv.ToPackTemplateContext(r.pack)
var buf strings.Builder
if err := r.tpl.ExecuteTemplate(&buf, r.pack.OutputTemplateFile.Name, r.pv); err != nil {
if err := r.tpl.ExecuteTemplate(&buf, r.pack.OutputTemplateFile.Name, ptc); err != nil {
return "", fmt.Errorf("failed to render %s: %v", r.pack.OutputTemplateFile.Name, err)
}

Expand Down

0 comments on commit 013d853

Please sign in to comment.