Skip to content

Commit

Permalink
changing the logic to print the warning message, only will be printed…
Browse files Browse the repository at this point in the history
… for tables in the project.toml we own

Signed-off-by: Juan Bustamante <jbustamante@vmware.com>
  • Loading branch information
jjbustamante committed May 15, 2024
1 parent 0a39de0 commit f833975
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
14 changes: 12 additions & 2 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ func ReadProjectDescriptor(pathToFile string, logger logging.Logger) (types.Desc
func warnIfTomlContainsKeysNotSupportedBySchema(schemaVersion string, tomlMetaData toml.MetaData, logger logging.Logger) {
unsupportedKeys := []string{}

// filter out any keys from [_]
for _, undecodedKey := range tomlMetaData.Undecoded() {
keyName := undecodedKey.String()
if keyName != "_" && !strings.HasPrefix(keyName, "_.schema-version") {
if unsopportedKey(keyName, schemaVersion) {
unsupportedKeys = append(unsupportedKeys, keyName)
}
}
Expand All @@ -85,6 +84,17 @@ func warnIfTomlContainsKeysNotSupportedBySchema(schemaVersion string, tomlMetaDa
}
}

func unsopportedKey(keyName, schemaVersion string) bool {
if schemaVersion == "0.1" {
// filter out any keys from [metadata] and any other custom table defined by end-users
return strings.HasPrefix(keyName, "project.") || strings.HasPrefix(keyName, "build.") || strings.Contains(keyName, "io.buildpacks")
} else if schemaVersion == "0.2" {
// filter out any keys from [_.metadata] and any other custom table defined by end-users
return strings.Contains(keyName, "io.buildpacks") || (strings.HasPrefix(keyName, "_.") && !strings.HasPrefix(keyName, "_.metadata"))
}
return true
}

func validate(p types.Descriptor) error {
if p.Build.Exclude != nil && p.Build.Include != nil {
return errors.New("project.toml: cannot have both include and exclude defined")
Expand Down
53 changes: 42 additions & 11 deletions pkg/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,24 @@ name = "licenses should have either a type or uri defined"
h.AssertContains(t, readStdout(), "Warning: No schema version declared in project.toml, defaulting to schema version 0.1\n")
})

it("should warn when unsupported keys are declared with schema v0.1", func() {
it("should warn when unsupported keys, on tables the project owns, are declared with schema v0.1", func() {
projectToml := `
[_]
schema-version = "0.1"
# try to use some schema 0.2 configuration with 0.1 version - warning message expected
[project]
authors = ["foo", "bar"]
[unsupported-table]
unsupported-key = "some value"
# try to use buildpack.io table with version 0.1 - warning message expected
[[io.buildpacks.build.env]]
name = "JAVA_OPTS"
value = "-Xmx1g"
# something else defined by end-users - no warning message expected
[io.docker]
file = "./Dockerfile"
# some metadata - no warning message expected
[metadata]
foo = "bar"
`
tmpProjectToml, err := createTmpProjectTomlFile(projectToml)
if err != nil {
Expand All @@ -416,17 +427,36 @@ unsupported-key = "some value"

_, err = ReadProjectDescriptor(tmpProjectToml.Name(), logger)
h.AssertNil(t, err)

h.AssertContains(t, readStdout(), "Warning: The following keys declared in project.toml are not supported in schema version 0.1:\nWarning: - unsupported-table\nWarning: - unsupported-table.unsupported-key\nWarning: The above keys will be ignored. If this is not intentional, maybe try updating your schema version.\n")
h.AssertContains(t, readStdout(), "Warning: The following keys declared in project.toml are not supported in schema version 0.1:\nWarning: - project.authors\nWarning: - io.buildpacks.build.env\nWarning: - io.buildpacks.build.env.name\nWarning: - io.buildpacks.build.env.value\nWarning: The above keys will be ignored. If this is not intentional, maybe try updating your schema version.\n")
})

it("should warn when unsupported keys are declared with schema v0.2", func() {
it("should warn when unsupported keys, on tables the project owns, are declared with schema v0.2", func() {
projectToml := `
[_]
schema-version = "0.2"
# typo in a key under valid table - warning message expected
versions = "0.1"
[[_.licenses]]
type = "foo"
# invalid key under a valid table - warning message expected
foo = "bar"
# try to use an invalid key under io.buildpacks - warning message expected
[[io.buildpacks.build.foo]]
name = "something"
# something else defined by end-users - no warning message expected
[io.docker]
file = "./Dockerfile"
# some metadata defined the end-user - no warning message expected
[_.metadata]
foo = "bar"
[unsupported-table]
unsupported-key = "some value"
# more metadata defined the end-user - no warning message expected
[_.metadata.fizz]
buzz = ["a", "b", "c"]
`
tmpProjectToml, err := createTmpProjectTomlFile(projectToml)
if err != nil {
Expand All @@ -436,7 +466,8 @@ unsupported-key = "some value"
_, err = ReadProjectDescriptor(tmpProjectToml.Name(), logger)
h.AssertNil(t, err)

h.AssertContains(t, readStdout(), "Warning: The following keys declared in project.toml are not supported in schema version 0.2:\nWarning: - unsupported-table\nWarning: - unsupported-table.unsupported-key\nWarning: The above keys will be ignored. If this is not intentional, maybe try updating your schema version.\n")
// Assert we only warn
h.AssertContains(t, readStdout(), "Warning: The following keys declared in project.toml are not supported in schema version 0.2:\nWarning: - _.versions\nWarning: - _.licenses.foo\nWarning: - io.buildpacks.build.foo\nWarning: - io.buildpacks.build.foo.name\nWarning: The above keys will be ignored. If this is not intentional, maybe try updating your schema version.\n")
})
})
}
Expand Down

0 comments on commit f833975

Please sign in to comment.