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

Fix project.toml build env to match spec #1479

Merged
merged 6 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 37 additions & 1 deletion pkg/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ id = "example/lua"
version = "1.0"
[[io.buildpacks.group]]
uri = "https://example.com/buildpack"
[[io.buildpacks.env.build]]
[[io.buildpacks.build.env]]
name = "JAVA_OPTS"
value = "-Xmx300m"
[[io.buildpacks.env.build]]
name = "JAVA_OPTS"
value = "this-should-get-overridden-because-its-deprecated"
`
tmpProjectToml, err := createTmpProjectTomlFile(projectToml)
if err != nil {
Expand Down Expand Up @@ -114,6 +117,39 @@ value = "-Xmx300m"
expected, projectDescriptor.Metadata["pipeline"])
}
})
it("should be backwards compatible with older v0.2 project.toml file", func() {
projectToml := `
[_]
name = "gallant 0.2"
schema-version="0.2"
[[io.buildpacks.env.build]]
name = "JAVA_OPTS"
value = "-Xmx300m"
`
tmpProjectToml, err := createTmpProjectTomlFile(projectToml)
if err != nil {
t.Fatal(err)
}

projectDescriptor, err := ReadProjectDescriptor(tmpProjectToml.Name())
if err != nil {
t.Fatal(err)
}

var expected string

expected = "JAVA_OPTS"
if projectDescriptor.Build.Env[0].Name != expected {
t.Fatalf("Expected\n-----\n%#v\n-----\nbut got\n-----\n%#v\n",
expected, projectDescriptor.Build.Env[0].Name)
}

expected = "-Xmx300m"
if projectDescriptor.Build.Env[0].Value != expected {
t.Fatalf("Expected\n-----\n%#v\n-----\nbut got\n-----\n%#v\n",
expected, projectDescriptor.Build.Env[0].Value)
}
})
it("should parse a valid v0.1 project.toml file", func() {
projectToml := `
[project]
Expand Down
14 changes: 13 additions & 1 deletion pkg/project/v02/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ type Buildpacks struct {
Exclude []string `toml:"exclude"`
Group []types.Buildpack `toml:"group"`
Env Env `toml:"env"`
Build Build `toml:"build"`
Builder string `toml:"builder"`
}

type Build struct {
Env []types.EnvVar `toml:"env"`
}

// Deprecated: use `[[io.buildpacks.build.env]]` instead. see https://github.com/buildpacks/pack/pull/1479
type Env struct {
Build []types.EnvVar `toml:"build"`
}
Expand Down Expand Up @@ -42,6 +48,12 @@ func NewDescriptor(projectTomlContents string) (types.Descriptor, error) {
return types.Descriptor{}, err
}

// backward compatibility for incorrect key
env := versionedDescriptor.IO.Buildpacks.Build.Env
if env == nil {
env = versionedDescriptor.IO.Buildpacks.Env.Build
}

return types.Descriptor{
Project: types.Project{
Name: versionedDescriptor.Project.Name,
Expand All @@ -51,7 +63,7 @@ func NewDescriptor(projectTomlContents string) (types.Descriptor, error) {
Include: versionedDescriptor.IO.Buildpacks.Include,
Exclude: versionedDescriptor.IO.Buildpacks.Exclude,
Buildpacks: versionedDescriptor.IO.Buildpacks.Group,
Env: versionedDescriptor.IO.Buildpacks.Env.Build,
Env: env,
Builder: versionedDescriptor.IO.Buildpacks.Builder,
},
Metadata: versionedDescriptor.Project.Metadata,
Expand Down