Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: have data.list("/") read from root of library #661

Merged
merged 4 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 185 additions & 0 deletions pkg/cmd/template/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,94 @@ data: #@ data.read("/funcs/data")`)
assert.Equal(t, expectedYAMLTplData, string(file.Bytes()))
}

func TestDataListRelativeToRootInDataValuesAndSchema(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests give me a lot of confidence that our "absolute path" loading works in both schema and data values -flavored templates. 👍🏻 Thank you for investing in this.

As I look around at the test files in pkg/cmd/template ... their ranks are growing. cmd_test.go is almost a KLOC.

If you haven't already thought about this, I'm wondering if these tests would be more "at home" in one of these test files:

  • cmd_root_library_test.go
  • cmd_data_values_test.go

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")
Expand Down Expand Up @@ -260,6 +348,103 @@ 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")
Expand Down
3 changes: 2 additions & 1 deletion pkg/workspace/data_values_pre_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/workspace/data_values_schema_pre_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about how minimizing the differences between Data Values and Data Values Schema processing makes this sweetly obvious that making the exact same change in both flows is the right move? ⭐


_, resultDocSet, err := pp.loader.EvalYAML(libraryCtx, schemaFile.File)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/workspace/library_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down