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/workspace/data_values_pre_processing.go b/pkg/workspace/data_values_pre_processing.go index 403e660f..750b1196 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 + rootLibrary *Library } // Apply executes the pre-processing of data values for all libraries. @@ -116,7 +117,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.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 052e1f65..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,6 +87,7 @@ func (ll *LibraryExecution) Values(valuesOverlays []*datavalues.Envelope, schema valuesOverlays: valuesOverlays, schema: schema, loader: loader, + rootLibrary: ll.libraryCtx.Root, } values, libValues, err := dvpp.Apply()