Skip to content

Commit f733a3a

Browse files
committed
changing the logic to print the warning message, only will be printed for tables in the project.toml we own
Signed-off-by: Juan Bustamante <jbustamante@vmware.com>
1 parent 884dd18 commit f733a3a

2 files changed

Lines changed: 54 additions & 13 deletions

File tree

pkg/project/project.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ func ReadProjectDescriptor(pathToFile string, logger logging.Logger) (types.Desc
6868
func warnIfTomlContainsKeysNotSupportedBySchema(schemaVersion string, tomlMetaData toml.MetaData, logger logging.Logger) {
6969
unsupportedKeys := []string{}
7070

71-
// filter out any keys from [_]
7271
for _, undecodedKey := range tomlMetaData.Undecoded() {
7372
keyName := undecodedKey.String()
74-
if keyName != "_" && !strings.HasPrefix(keyName, "_.schema-version") {
73+
if unsupportedKey(keyName, schemaVersion) {
7574
unsupportedKeys = append(unsupportedKeys, keyName)
7675
}
7776
}
@@ -85,6 +84,17 @@ func warnIfTomlContainsKeysNotSupportedBySchema(schemaVersion string, tomlMetaDa
8584
}
8685
}
8786

87+
func unsupportedKey(keyName, schemaVersion string) bool {
88+
if schemaVersion == "0.1" {
89+
// filter out any keys from [metadata] and any other custom table defined by end-users
90+
return strings.HasPrefix(keyName, "project.") || strings.HasPrefix(keyName, "build.") || strings.Contains(keyName, "io.buildpacks")
91+
} else if schemaVersion == "0.2" {
92+
// filter out any keys from [_.metadata] and any other custom table defined by end-users
93+
return strings.Contains(keyName, "io.buildpacks") || (strings.HasPrefix(keyName, "_.") && !strings.HasPrefix(keyName, "_.metadata"))
94+
}
95+
return true
96+
}
97+
8898
func validate(p types.Descriptor) error {
8999
if p.Build.Exclude != nil && p.Build.Include != nil {
90100
return errors.New("project.toml: cannot have both include and exclude defined")

pkg/project/project_test.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,24 @@ name = "licenses should have either a type or uri defined"
401401
h.AssertContains(t, readStdout(), "Warning: No schema version declared in project.toml, defaulting to schema version 0.1\n")
402402
})
403403

404-
it("should warn when unsupported keys are declared with schema v0.1", func() {
404+
it("should warn when unsupported keys, on tables the project owns, are declared with schema v0.1", func() {
405405
projectToml := `
406-
[_]
407-
schema-version = "0.1"
406+
# try to use some schema 0.2 configuration with 0.1 version - warning message expected
407+
[project]
408+
authors = ["foo", "bar"]
408409
409-
[unsupported-table]
410-
unsupported-key = "some value"
410+
# try to use buildpack.io table with version 0.1 - warning message expected
411+
[[io.buildpacks.build.env]]
412+
name = "JAVA_OPTS"
413+
value = "-Xmx1g"
414+
415+
# something else defined by end-users - no warning message expected
416+
[io.docker]
417+
file = "./Dockerfile"
418+
419+
# some metadata - no warning message expected
420+
[metadata]
421+
foo = "bar"
411422
`
412423
tmpProjectToml, err := createTmpProjectTomlFile(projectToml)
413424
if err != nil {
@@ -416,17 +427,36 @@ unsupported-key = "some value"
416427

417428
_, err = ReadProjectDescriptor(tmpProjectToml.Name(), logger)
418429
h.AssertNil(t, err)
419-
420-
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")
430+
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")
421431
})
422432

423-
it("should warn when unsupported keys are declared with schema v0.2", func() {
433+
it("should warn when unsupported keys, on tables the project owns, are declared with schema v0.2", func() {
424434
projectToml := `
425435
[_]
426436
schema-version = "0.2"
437+
# typo in a key under valid table - warning message expected
438+
versions = "0.1"
439+
440+
[[_.licenses]]
441+
type = "foo"
442+
# invalid key under a valid table - warning message expected
443+
foo = "bar"
444+
445+
# try to use an invalid key under io.buildpacks - warning message expected
446+
[[io.buildpacks.build.foo]]
447+
name = "something"
448+
449+
# something else defined by end-users - no warning message expected
450+
[io.docker]
451+
file = "./Dockerfile"
452+
453+
# some metadata defined the end-user - no warning message expected
454+
[_.metadata]
455+
foo = "bar"
427456
428-
[unsupported-table]
429-
unsupported-key = "some value"
457+
# more metadata defined the end-user - no warning message expected
458+
[_.metadata.fizz]
459+
buzz = ["a", "b", "c"]
430460
`
431461
tmpProjectToml, err := createTmpProjectTomlFile(projectToml)
432462
if err != nil {
@@ -436,7 +466,8 @@ unsupported-key = "some value"
436466
_, err = ReadProjectDescriptor(tmpProjectToml.Name(), logger)
437467
h.AssertNil(t, err)
438468

439-
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")
469+
// Assert we only warn
470+
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")
440471
})
441472
})
442473
}

0 commit comments

Comments
 (0)