From 1f55feabdeceed59f9c5721922cb0f0cf744498a Mon Sep 17 00:00:00 2001 From: Varsha Munishwar Date: Fri, 29 Apr 2022 13:15:23 -0700 Subject: [PATCH 1/4] WIP - data.list(/) bug Signed-off-by: Garrett Cheadle --- pkg/cmd/template/cmd_test.go | 128 ++++++++++++++++++++ pkg/workspace/data_values_pre_processing.go | 4 +- pkg/workspace/library_execution.go | 1 + 3 files changed, 132 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/template/cmd_test.go b/pkg/cmd/template/cmd_test.go index 9965afe3..4fe37c7a 100644 --- a/pkg/cmd/template/cmd_test.go +++ b/pkg/cmd/template/cmd_test.go @@ -260,6 +260,134 @@ libdata2: #@ data.read("/other") assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) } +func TestDataListRelativeToLibraryRootWithinALibraryDataValues(t *testing.T) { + yamlTplData := []byte(` +#@ load("@ytt:template", "template") +#@ load("@ytt:library", "library") +#@ lib = library.get("lib") + +--- #@ template.replace(lib.eval())`) + + expectedYAMLTplData := `Files_in_values_dir: +- name: values.yml +Files_in_values: +- name: /other +- name: /config.yml +- name: /values/values.yml +Files_in_template: +- name: /other +- name: /config.yml +- name: /values/values.yml +` + + yamlLibDataValues := []byte(`#@data/values +--- +#@ load("@ytt:yaml", "yaml") +#@ load("@ytt:data", "data") + +#@ file = data.list("") +Files_in_values_dir: + #@ for/end file in file: + - name: #@ file +#@ rootFiles = data.list("/") +Files_in_values: + #@ for/end file in rootFiles: + - name: #@ file +`) + + yamlLibConfigData := []byte(` +#@ load("@ytt:data", "data") +#@ load("@ytt:template", "template") + +_: #@ template.replace(data.values) +#@ files = data.list("/") +Files_in_template: + #@ for/end file in files: + - name: #@ file`) + + filesToProcess := files.NewSortedFiles([]*files.File{ + files.MustNewFileFromSource(files.NewBytesSource("tpl.yml", yamlTplData)), + files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/other", []byte("lib1\ndata"))), + files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/values/values.yml", yamlLibDataValues)), + files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/config.yml", yamlLibConfigData)), + }) + + ui := ui.NewTTY(false) + opts := cmdtpl.NewOptions() + + out := opts.RunWithFiles(cmdtpl.Input{Files: filesToProcess}, ui) + + require.NoError(t, out.Err) + require.Len(t, out.Files, 1, "unexpected number of output files") + + file := out.Files[0] + + assert.Equal(t, "tpl.yml", file.RelativePath()) + assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) +} + +func TestDataListRelativeToRootWithinDataValues(t *testing.T) { + yamlTplData := []byte(` +#@ load("@ytt:data", "data") +#@ load("@ytt:template", "template") +_: #@ template.replace(data.values) +--- +#@ files = data.list("/") +Files_in_template: + #@ for/end file in files: + - name: #@ file`) + + expectedYAMLTplData := `Files_in_root_values: +- name: /config.yml +- name: /other +- name: /values.yml +Files_in_values: +- name: config.yml +- name: other +- name: values.yml +--- +Files_in_template: +- name: /config.yml +- name: /other +- name: /values.yml +` + + yamlDataValuesData := []byte(` +#@data/values +--- + +#@ load("@ytt:yaml", "yaml") +#@ load("@ytt:data", "data") + +#@ rootFiles = data.list("/") +Files_in_root_values: + #@ for/end file in rootFiles: + - name: #@ file +#@ files = data.list("") +Files_in_values: + #@ for/end file in files: + - name: #@ file`) + + filesToProcess := files.NewSortedFiles([]*files.File{ + files.MustNewFileFromSource(files.NewBytesSource("config.yml", yamlTplData)), + files.MustNewFileFromSource(files.NewBytesSource("other", []byte("lib1\ndata"))), + files.MustNewFileFromSource(files.NewBytesSource("values.yml", yamlDataValuesData)), + }) + + ui := ui.NewTTY(false) + opts := cmdtpl.NewOptions() + + out := opts.RunWithFiles(cmdtpl.Input{Files: filesToProcess}, ui) + + require.NoError(t, out.Err) + require.Len(t, out.Files, 1, "unexpected number of output files") + + file := out.Files[0] + + assert.Equal(t, "config.yml", file.RelativePath()) + assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) +} + func TestBacktraceAcrossFiles(t *testing.T) { yamlTplData := []byte(` #@ load("funcs/funcs.lib.yml", "some_data") diff --git a/pkg/workspace/data_values_pre_processing.go b/pkg/workspace/data_values_pre_processing.go index 403e660f..39565e48 100644 --- a/pkg/workspace/data_values_pre_processing.go +++ b/pkg/workspace/data_values_pre_processing.go @@ -20,6 +20,7 @@ type DataValuesPreProcessing struct { valuesOverlays []*datavalues.Envelope schema *datavalues.Schema loader *TemplateLoader + rootContext *LibraryExecutionContext } // Apply executes the pre-processing of data values for all libraries. @@ -116,7 +117,8 @@ func (pp DataValuesPreProcessing) typeAndCheck(dataValuesDoc *yamlmeta.Document) } func (pp DataValuesPreProcessing) extractDataValueDocs(dvFile *FileInLibrary) ([]*yamlmeta.Document, error) { - libraryCtx := LibraryExecutionContext{Current: dvFile.Library, Root: NewRootLibrary(nil)} + //libraryCtx := LibraryExecutionContext{Current: dvFile.Library, Root: NewRootLibrary(nil)} + libraryCtx := LibraryExecutionContext{Current: dvFile.Library, Root: pp.rootContext.Root} _, resultDocSet, err := pp.loader.EvalYAML(libraryCtx, dvFile.File) if err != nil { diff --git a/pkg/workspace/library_execution.go b/pkg/workspace/library_execution.go index 052e1f65..a9265d6e 100644 --- a/pkg/workspace/library_execution.go +++ b/pkg/workspace/library_execution.go @@ -86,6 +86,7 @@ func (ll *LibraryExecution) Values(valuesOverlays []*datavalues.Envelope, schema valuesOverlays: valuesOverlays, schema: schema, loader: loader, + rootContext: &ll.libraryCtx, } values, libValues, err := dvpp.Apply() From c60fccee4001752923cbe19b7f7d2a48f5f92c63 Mon Sep 17 00:00:00 2001 From: Garrett Cheadle Date: Mon, 2 May 2022 15:22:18 -0700 Subject: [PATCH 2/4] include root library when evaluating schema and data values yaml --- pkg/cmd/template/cmd_test.go | 193 ++++++++++++------ pkg/workspace/data_values_pre_processing.go | 4 +- .../data_values_schema_pre_processing.go | 3 +- pkg/workspace/library_execution.go | 3 +- 4 files changed, 131 insertions(+), 72 deletions(-) diff --git a/pkg/cmd/template/cmd_test.go b/pkg/cmd/template/cmd_test.go index 4fe37c7a..ce746d88 100644 --- a/pkg/cmd/template/cmd_test.go +++ b/pkg/cmd/template/cmd_test.go @@ -210,6 +210,94 @@ data: #@ data.read("/funcs/data")`) assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) } +func TestDataListRelativeToRootInDataValuesAndSchema(t *testing.T) { + yamlTplData := []byte(` +#@ load("@ytt:data", "data") +#@ load("@ytt:template", "template") +_: #@ template.replace(data.values) +`) + + expectedYAMLTplData := `Files_in_root_schema: + /config.yml: /config.yml + /other: /other + /schema.yml: /schema.yml + /values.yml: /values.yml +Files_in_schema: + config.yml: config.yml + other: other + schema.yml: schema.yml + values.yml: values.yml +Files_in_root_values: +- name: /config.yml +- name: /other +- name: /schema.yml +- name: /values.yml +Files_in_values: +- name: config.yml +- name: other +- name: schema.yml +- name: values.yml +` + + yamlSchemaData := []byte(` +#@ load("@ytt:yaml", "yaml") +#@ load("@ytt:data", "data") +#@data/values-schema +--- + +#@ rootFiles = data.list("/") +Files_in_root_schema: + #@ for/end file in rootFiles: + #@yaml/text-templated-strings + (@= file @): #@ file +#@ files = data.list("") +Files_in_schema: + #@ for/end file in files: + #@yaml/text-templated-strings + (@= file @): #@ file +Files_in_root_values: +- name: "" +Files_in_values: +- name: "" +`) + + yamlDataValuesData := []byte(` +#@data/values +--- + +#@ load("@ytt:yaml", "yaml") +#@ load("@ytt:data", "data") + +#@ rootFiles = data.list("/") +Files_in_root_values: + #@ for/end file in rootFiles: + - name: #@ file +#@ files = data.list("") +Files_in_values: + #@ for/end file in files: + - name: #@ file`) + + filesToProcess := files.NewSortedFiles([]*files.File{ + files.MustNewFileFromSource(files.NewBytesSource("config.yml", yamlTplData)), + files.MustNewFileFromSource(files.NewBytesSource("other", []byte("lib1\ndata"))), + files.MustNewFileFromSource(files.NewBytesSource("schema.yml", yamlSchemaData)), + files.MustNewFileFromSource(files.NewBytesSource("values.yml", yamlDataValuesData)), + }) + + ui := ui.NewTTY(false) + opts := cmdtpl.NewOptions() + + out := opts.RunWithFiles(cmdtpl.Input{Files: filesToProcess}, ui) + + require.NoError(t, out.Err) + require.Len(t, out.Files, 1, "unexpected number of output files") + + file := out.Files[0] + + assert.Equal(t, "config.yml", file.RelativePath()) + assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) +} + func TestDataListRelativeToLibraryRootWithinALibrary(t *testing.T) { yamlTplData := []byte(` #@ load("@lib1:funcs/funcs.lib.yml", "lib_data_list", "lib_data_read") @@ -260,7 +348,7 @@ libdata2: #@ data.read("/other") assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) } -func TestDataListRelativeToLibraryRootWithinALibraryDataValues(t *testing.T) { +func TestDataListRelativeToLibraryRootWithinALibraryDataValuesAndSchema(t *testing.T) { yamlTplData := []byte(` #@ load("@ytt:template", "template") #@ load("@ytt:library", "library") @@ -268,29 +356,59 @@ func TestDataListRelativeToLibraryRootWithinALibraryDataValues(t *testing.T) { --- #@ template.replace(lib.eval())`) - expectedYAMLTplData := `Files_in_values_dir: -- name: values.yml -Files_in_values: + expectedYAMLTplData := `Files_in_root_schema: + /other: /other + /config.yml: /config.yml + /schema/schema.yml: /schema/schema.yml + /values/values.yml: /values/values.yml +Files_in_schema: + schema.yml: schema.yml +Files_in_root_values: - name: /other - name: /config.yml +- name: /schema/schema.yml - name: /values/values.yml +Files_in_values: +- name: values.yml Files_in_template: - name: /other - name: /config.yml +- name: /schema/schema.yml - name: /values/values.yml ` + yamlSchemaData := []byte(` +#@ load("@ytt:yaml", "yaml") +#@ load("@ytt:data", "data") +#@data/values-schema +--- + +#@ rootFiles = data.list("/") +Files_in_root_schema: + #@ for/end file in rootFiles: + #@yaml/text-templated-strings + (@= file @): #@ file +#@ files = data.list("") +Files_in_schema: + #@ for/end file in files: + #@yaml/text-templated-strings + (@= file @): #@ file +Files_in_root_values: +- name: "" +Files_in_values: +- name: "" +`) yamlLibDataValues := []byte(`#@data/values --- #@ load("@ytt:yaml", "yaml") #@ load("@ytt:data", "data") #@ file = data.list("") -Files_in_values_dir: +Files_in_values: #@ for/end file in file: - name: #@ file #@ rootFiles = data.list("/") -Files_in_values: +Files_in_root_values: #@ for/end file in rootFiles: - name: #@ file `) @@ -308,6 +426,7 @@ Files_in_template: filesToProcess := files.NewSortedFiles([]*files.File{ files.MustNewFileFromSource(files.NewBytesSource("tpl.yml", yamlTplData)), files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/other", []byte("lib1\ndata"))), + files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/schema/schema.yml", yamlSchemaData)), files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/values/values.yml", yamlLibDataValues)), files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/config.yml", yamlLibConfigData)), }) @@ -326,68 +445,6 @@ Files_in_template: assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) } -func TestDataListRelativeToRootWithinDataValues(t *testing.T) { - yamlTplData := []byte(` -#@ load("@ytt:data", "data") -#@ load("@ytt:template", "template") -_: #@ template.replace(data.values) ---- -#@ files = data.list("/") -Files_in_template: - #@ for/end file in files: - - name: #@ file`) - - expectedYAMLTplData := `Files_in_root_values: -- name: /config.yml -- name: /other -- name: /values.yml -Files_in_values: -- name: config.yml -- name: other -- name: values.yml ---- -Files_in_template: -- name: /config.yml -- name: /other -- name: /values.yml -` - - yamlDataValuesData := []byte(` -#@data/values ---- - -#@ load("@ytt:yaml", "yaml") -#@ load("@ytt:data", "data") - -#@ rootFiles = data.list("/") -Files_in_root_values: - #@ for/end file in rootFiles: - - name: #@ file -#@ files = data.list("") -Files_in_values: - #@ for/end file in files: - - name: #@ file`) - - filesToProcess := files.NewSortedFiles([]*files.File{ - files.MustNewFileFromSource(files.NewBytesSource("config.yml", yamlTplData)), - files.MustNewFileFromSource(files.NewBytesSource("other", []byte("lib1\ndata"))), - files.MustNewFileFromSource(files.NewBytesSource("values.yml", yamlDataValuesData)), - }) - - ui := ui.NewTTY(false) - opts := cmdtpl.NewOptions() - - out := opts.RunWithFiles(cmdtpl.Input{Files: filesToProcess}, ui) - - require.NoError(t, out.Err) - require.Len(t, out.Files, 1, "unexpected number of output files") - - file := out.Files[0] - - assert.Equal(t, "config.yml", file.RelativePath()) - assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) -} - func TestBacktraceAcrossFiles(t *testing.T) { yamlTplData := []byte(` #@ load("funcs/funcs.lib.yml", "some_data") diff --git a/pkg/workspace/data_values_pre_processing.go b/pkg/workspace/data_values_pre_processing.go index 39565e48..f6e32f46 100644 --- a/pkg/workspace/data_values_pre_processing.go +++ b/pkg/workspace/data_values_pre_processing.go @@ -20,7 +20,7 @@ type DataValuesPreProcessing struct { valuesOverlays []*datavalues.Envelope schema *datavalues.Schema loader *TemplateLoader - rootContext *LibraryExecutionContext + rootLibrary *Library } // Apply executes the pre-processing of data values for all libraries. @@ -118,7 +118,7 @@ func (pp DataValuesPreProcessing) typeAndCheck(dataValuesDoc *yamlmeta.Document) func (pp DataValuesPreProcessing) extractDataValueDocs(dvFile *FileInLibrary) ([]*yamlmeta.Document, error) { //libraryCtx := LibraryExecutionContext{Current: dvFile.Library, Root: NewRootLibrary(nil)} - libraryCtx := LibraryExecutionContext{Current: dvFile.Library, Root: pp.rootContext.Root} + libraryCtx := LibraryExecutionContext{Current: dvFile.Library, Root: pp.rootLibrary} _, resultDocSet, err := pp.loader.EvalYAML(libraryCtx, dvFile.File) if err != nil { diff --git a/pkg/workspace/data_values_schema_pre_processing.go b/pkg/workspace/data_values_schema_pre_processing.go index 98117f58..d88709a0 100644 --- a/pkg/workspace/data_values_schema_pre_processing.go +++ b/pkg/workspace/data_values_schema_pre_processing.go @@ -18,6 +18,7 @@ type DataValuesSchemaPreProcessing struct { schemaFiles []*FileInLibrary schemaOverlays []*datavalues.SchemaEnvelope loader *TemplateLoader + rootLibrary *Library } // Apply executes the pre-processing of schema for data values for all libraries. @@ -97,7 +98,7 @@ func (pp DataValuesSchemaPreProcessing) collectSchemaDocs(schemaFiles []*FileInL } func (pp DataValuesSchemaPreProcessing) extractSchemaDocs(schemaFile *FileInLibrary) ([]*yamlmeta.Document, error) { - libraryCtx := LibraryExecutionContext{Current: schemaFile.Library, Root: NewRootLibrary(nil)} + libraryCtx := LibraryExecutionContext{Current: schemaFile.Library, Root: pp.rootLibrary} _, resultDocSet, err := pp.loader.EvalYAML(libraryCtx, schemaFile.File) if err != nil { diff --git a/pkg/workspace/library_execution.go b/pkg/workspace/library_execution.go index a9265d6e..539bedf5 100644 --- a/pkg/workspace/library_execution.go +++ b/pkg/workspace/library_execution.go @@ -62,6 +62,7 @@ func (ll *LibraryExecution) Schemas(overlays []*datavalues.SchemaEnvelope) (*dat schemaFiles: files, schemaOverlays: overlays, loader: loader, + rootLibrary: ll.libraryCtx.Root, } return spp.Apply() @@ -86,7 +87,7 @@ func (ll *LibraryExecution) Values(valuesOverlays []*datavalues.Envelope, schema valuesOverlays: valuesOverlays, schema: schema, loader: loader, - rootContext: &ll.libraryCtx, + rootLibrary: ll.libraryCtx.Root, } values, libValues, err := dvpp.Apply() From e9775d1aad4f4e3995388c29cb5e199f54fcfe56 Mon Sep 17 00:00:00 2001 From: Garrett Cheadle Date: Mon, 2 May 2022 15:33:15 -0700 Subject: [PATCH 3/4] remove comment --- pkg/workspace/data_values_pre_processing.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/workspace/data_values_pre_processing.go b/pkg/workspace/data_values_pre_processing.go index f6e32f46..750b1196 100644 --- a/pkg/workspace/data_values_pre_processing.go +++ b/pkg/workspace/data_values_pre_processing.go @@ -117,7 +117,6 @@ func (pp DataValuesPreProcessing) typeAndCheck(dataValuesDoc *yamlmeta.Document) } func (pp DataValuesPreProcessing) extractDataValueDocs(dvFile *FileInLibrary) ([]*yamlmeta.Document, error) { - //libraryCtx := LibraryExecutionContext{Current: dvFile.Library, Root: NewRootLibrary(nil)} libraryCtx := LibraryExecutionContext{Current: dvFile.Library, Root: pp.rootLibrary} _, resultDocSet, err := pp.loader.EvalYAML(libraryCtx, dvFile.File) From 62008071a3d822cb1535352f6ae1e6794d52cf1e Mon Sep 17 00:00:00 2001 From: Garrett Cheadle Date: Tue, 3 May 2022 15:42:42 -0700 Subject: [PATCH 4/4] refactor tests location --- pkg/cmd/template/cmd_data_values_test.go | 185 +++++++++++++++++++++++ pkg/cmd/template/cmd_test.go | 185 ----------------------- 2 files changed, 185 insertions(+), 185 deletions(-) diff --git a/pkg/cmd/template/cmd_data_values_test.go b/pkg/cmd/template/cmd_data_values_test.go index 23ffe776..7301a2ed 100644 --- a/pkg/cmd/template/cmd_data_values_test.go +++ b/pkg/cmd/template/cmd_data_values_test.go @@ -528,6 +528,191 @@ data: assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) } +func TestDataValuesDataListRelativeToRoot(t *testing.T) { + yamlTplData := []byte(` +#@ load("@ytt:data", "data") +#@ load("@ytt:template", "template") +_: #@ template.replace(data.values) +`) + + expectedYAMLTplData := `Files_in_root_schema: + /config.yml: /config.yml + /other: /other + /schema.yml: /schema.yml + /values.yml: /values.yml +Files_in_schema: + config.yml: config.yml + other: other + schema.yml: schema.yml + values.yml: values.yml +Files_in_root_values: +- name: /config.yml +- name: /other +- name: /schema.yml +- name: /values.yml +Files_in_values: +- name: config.yml +- name: other +- name: schema.yml +- name: values.yml +` + + yamlSchemaData := []byte(` +#@ load("@ytt:yaml", "yaml") +#@ load("@ytt:data", "data") +#@data/values-schema +--- + +#@ rootFiles = data.list("/") +Files_in_root_schema: + #@ for/end file in rootFiles: + #@yaml/text-templated-strings + (@= file @): #@ file +#@ files = data.list("") +Files_in_schema: + #@ for/end file in files: + #@yaml/text-templated-strings + (@= file @): #@ file +Files_in_root_values: +- name: "" +Files_in_values: +- name: "" +`) + + yamlDataValuesData := []byte(` +#@data/values +--- + +#@ load("@ytt:yaml", "yaml") +#@ load("@ytt:data", "data") + +#@ rootFiles = data.list("/") +Files_in_root_values: + #@ for/end file in rootFiles: + - name: #@ file +#@ files = data.list("") +Files_in_values: + #@ for/end file in files: + - name: #@ file`) + + filesToProcess := files.NewSortedFiles([]*files.File{ + files.MustNewFileFromSource(files.NewBytesSource("config.yml", yamlTplData)), + files.MustNewFileFromSource(files.NewBytesSource("other", []byte("lib1\ndata"))), + files.MustNewFileFromSource(files.NewBytesSource("schema.yml", yamlSchemaData)), + files.MustNewFileFromSource(files.NewBytesSource("values.yml", yamlDataValuesData)), + }) + + ui := ui.NewTTY(false) + opts := cmdtpl.NewOptions() + + out := opts.RunWithFiles(cmdtpl.Input{Files: filesToProcess}, ui) + + require.NoError(t, out.Err) + require.Len(t, out.Files, 1, "unexpected number of output files") + + file := out.Files[0] + + assert.Equal(t, "config.yml", file.RelativePath()) + assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) +} + +func TestDataValuesDataListRelativeToLibraryRoot(t *testing.T) { + yamlTplData := []byte(` +#@ load("@ytt:template", "template") +#@ load("@ytt:library", "library") +#@ lib = library.get("lib") + +--- #@ template.replace(lib.eval())`) + + expectedYAMLTplData := `Files_in_root_schema: + /other: /other + /config.yml: /config.yml + /schema/schema.yml: /schema/schema.yml + /values/values.yml: /values/values.yml +Files_in_schema: + schema.yml: schema.yml +Files_in_root_values: +- name: /other +- name: /config.yml +- name: /schema/schema.yml +- name: /values/values.yml +Files_in_values: +- name: values.yml +Files_in_template: +- name: /other +- name: /config.yml +- name: /schema/schema.yml +- name: /values/values.yml +` + + yamlSchemaData := []byte(` +#@ load("@ytt:yaml", "yaml") +#@ load("@ytt:data", "data") +#@data/values-schema +--- + +#@ rootFiles = data.list("/") +Files_in_root_schema: + #@ for/end file in rootFiles: + #@yaml/text-templated-strings + (@= file @): #@ file +#@ files = data.list("") +Files_in_schema: + #@ for/end file in files: + #@yaml/text-templated-strings + (@= file @): #@ file +Files_in_root_values: +- name: "" +Files_in_values: +- name: "" +`) + yamlLibDataValues := []byte(`#@data/values +--- +#@ load("@ytt:yaml", "yaml") +#@ load("@ytt:data", "data") + +#@ file = data.list("") +Files_in_values: + #@ for/end file in file: + - name: #@ file +#@ rootFiles = data.list("/") +Files_in_root_values: + #@ for/end file in rootFiles: + - name: #@ file +`) + + yamlLibConfigData := []byte(` +#@ load("@ytt:data", "data") +#@ load("@ytt:template", "template") + +_: #@ template.replace(data.values) +#@ files = data.list("/") +Files_in_template: + #@ for/end file in files: + - name: #@ file`) + + filesToProcess := files.NewSortedFiles([]*files.File{ + files.MustNewFileFromSource(files.NewBytesSource("tpl.yml", yamlTplData)), + files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/other", []byte("lib1\ndata"))), + files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/schema/schema.yml", yamlSchemaData)), + files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/values/values.yml", yamlLibDataValues)), + files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/config.yml", yamlLibConfigData)), + }) + + ui := ui.NewTTY(false) + opts := cmdtpl.NewOptions() + + out := opts.RunWithFiles(cmdtpl.Input{Files: filesToProcess}, ui) + + require.NoError(t, out.Err) + require.Len(t, out.Files, 1, "unexpected number of output files") + + file := out.Files[0] + + assert.Equal(t, "tpl.yml", file.RelativePath()) + assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) +} + func TestDataValuesFromEnv(t *testing.T) { tmplBytes := []byte(` #@ load("@ytt:template", "template") diff --git a/pkg/cmd/template/cmd_test.go b/pkg/cmd/template/cmd_test.go index ce746d88..9965afe3 100644 --- a/pkg/cmd/template/cmd_test.go +++ b/pkg/cmd/template/cmd_test.go @@ -210,94 +210,6 @@ data: #@ data.read("/funcs/data")`) assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) } -func TestDataListRelativeToRootInDataValuesAndSchema(t *testing.T) { - yamlTplData := []byte(` -#@ load("@ytt:data", "data") -#@ load("@ytt:template", "template") -_: #@ template.replace(data.values) -`) - - expectedYAMLTplData := `Files_in_root_schema: - /config.yml: /config.yml - /other: /other - /schema.yml: /schema.yml - /values.yml: /values.yml -Files_in_schema: - config.yml: config.yml - other: other - schema.yml: schema.yml - values.yml: values.yml -Files_in_root_values: -- name: /config.yml -- name: /other -- name: /schema.yml -- name: /values.yml -Files_in_values: -- name: config.yml -- name: other -- name: schema.yml -- name: values.yml -` - - yamlSchemaData := []byte(` -#@ load("@ytt:yaml", "yaml") -#@ load("@ytt:data", "data") -#@data/values-schema ---- - -#@ rootFiles = data.list("/") -Files_in_root_schema: - #@ for/end file in rootFiles: - #@yaml/text-templated-strings - (@= file @): #@ file -#@ files = data.list("") -Files_in_schema: - #@ for/end file in files: - #@yaml/text-templated-strings - (@= file @): #@ file -Files_in_root_values: -- name: "" -Files_in_values: -- name: "" -`) - - yamlDataValuesData := []byte(` -#@data/values ---- - -#@ load("@ytt:yaml", "yaml") -#@ load("@ytt:data", "data") - -#@ rootFiles = data.list("/") -Files_in_root_values: - #@ for/end file in rootFiles: - - name: #@ file -#@ files = data.list("") -Files_in_values: - #@ for/end file in files: - - name: #@ file`) - - filesToProcess := files.NewSortedFiles([]*files.File{ - files.MustNewFileFromSource(files.NewBytesSource("config.yml", yamlTplData)), - files.MustNewFileFromSource(files.NewBytesSource("other", []byte("lib1\ndata"))), - files.MustNewFileFromSource(files.NewBytesSource("schema.yml", yamlSchemaData)), - files.MustNewFileFromSource(files.NewBytesSource("values.yml", yamlDataValuesData)), - }) - - ui := ui.NewTTY(false) - opts := cmdtpl.NewOptions() - - out := opts.RunWithFiles(cmdtpl.Input{Files: filesToProcess}, ui) - - require.NoError(t, out.Err) - require.Len(t, out.Files, 1, "unexpected number of output files") - - file := out.Files[0] - - assert.Equal(t, "config.yml", file.RelativePath()) - assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) -} - func TestDataListRelativeToLibraryRootWithinALibrary(t *testing.T) { yamlTplData := []byte(` #@ load("@lib1:funcs/funcs.lib.yml", "lib_data_list", "lib_data_read") @@ -348,103 +260,6 @@ libdata2: #@ data.read("/other") assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) } -func TestDataListRelativeToLibraryRootWithinALibraryDataValuesAndSchema(t *testing.T) { - yamlTplData := []byte(` -#@ load("@ytt:template", "template") -#@ load("@ytt:library", "library") -#@ lib = library.get("lib") - ---- #@ template.replace(lib.eval())`) - - expectedYAMLTplData := `Files_in_root_schema: - /other: /other - /config.yml: /config.yml - /schema/schema.yml: /schema/schema.yml - /values/values.yml: /values/values.yml -Files_in_schema: - schema.yml: schema.yml -Files_in_root_values: -- name: /other -- name: /config.yml -- name: /schema/schema.yml -- name: /values/values.yml -Files_in_values: -- name: values.yml -Files_in_template: -- name: /other -- name: /config.yml -- name: /schema/schema.yml -- name: /values/values.yml -` - - yamlSchemaData := []byte(` -#@ load("@ytt:yaml", "yaml") -#@ load("@ytt:data", "data") -#@data/values-schema ---- - -#@ rootFiles = data.list("/") -Files_in_root_schema: - #@ for/end file in rootFiles: - #@yaml/text-templated-strings - (@= file @): #@ file -#@ files = data.list("") -Files_in_schema: - #@ for/end file in files: - #@yaml/text-templated-strings - (@= file @): #@ file -Files_in_root_values: -- name: "" -Files_in_values: -- name: "" -`) - yamlLibDataValues := []byte(`#@data/values ---- -#@ load("@ytt:yaml", "yaml") -#@ load("@ytt:data", "data") - -#@ file = data.list("") -Files_in_values: - #@ for/end file in file: - - name: #@ file -#@ rootFiles = data.list("/") -Files_in_root_values: - #@ for/end file in rootFiles: - - name: #@ file -`) - - yamlLibConfigData := []byte(` -#@ load("@ytt:data", "data") -#@ load("@ytt:template", "template") - -_: #@ template.replace(data.values) -#@ files = data.list("/") -Files_in_template: - #@ for/end file in files: - - name: #@ file`) - - filesToProcess := files.NewSortedFiles([]*files.File{ - files.MustNewFileFromSource(files.NewBytesSource("tpl.yml", yamlTplData)), - files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/other", []byte("lib1\ndata"))), - files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/schema/schema.yml", yamlSchemaData)), - files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/values/values.yml", yamlLibDataValues)), - files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/config.yml", yamlLibConfigData)), - }) - - ui := ui.NewTTY(false) - opts := cmdtpl.NewOptions() - - out := opts.RunWithFiles(cmdtpl.Input{Files: filesToProcess}, ui) - - require.NoError(t, out.Err) - require.Len(t, out.Files, 1, "unexpected number of output files") - - file := out.Files[0] - - assert.Equal(t, "tpl.yml", file.RelativePath()) - assert.Equal(t, expectedYAMLTplData, string(file.Bytes())) -} - func TestBacktraceAcrossFiles(t *testing.T) { yamlTplData := []byte(` #@ load("funcs/funcs.lib.yml", "some_data")