Skip to content

Commit

Permalink
Merge pull request #1479 from buildpacks/jkutner/fix-proj-build-env
Browse files Browse the repository at this point in the history
Fix project.toml build env to match spec
  • Loading branch information
sambhav authored Aug 24, 2022
2 parents 2bed9bb + fc05166 commit d56f56a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
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

0 comments on commit d56f56a

Please sign in to comment.