Skip to content

Commit

Permalink
Adds support for setting default process
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Moran authored and ForestEckhardt committed Sep 21, 2021
1 parent 71254cc commit 3d878bb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 56 deletions.
15 changes: 13 additions & 2 deletions build.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package packit

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -131,6 +132,7 @@ func Build(f BuildFunc, options ...Option) {
}

apiV05, _ := semver.NewVersion("0.5")
apiV06, _ := semver.NewVersion("0.6")
apiVersion, err := semver.NewVersion(buildpackInfo.APIVersion)
if err != nil {
config.exitHandler.Error(err)
Expand All @@ -157,7 +159,7 @@ func Build(f BuildFunc, options ...Option) {

if len(result.Plan.Entries) > 0 {
if apiVersion.GreaterThan(apiV05) || apiVersion.Equal(apiV05) {
config.exitHandler.Error(fmt.Errorf(`buildpack plan is read only since BuildPack API v0.5`))
config.exitHandler.Error(errors.New("buildpack plan is read only since Buildpack API v0.5"))
return
}

Expand Down Expand Up @@ -220,7 +222,7 @@ func Build(f BuildFunc, options ...Option) {

if !result.Launch.isEmpty() {
if apiVersion.LessThan(apiV05) && len(result.Launch.BOM) > 0 {
config.exitHandler.Error(fmt.Errorf("BOM entries in launch.toml is only supported with Buildpack API v0.5 or higher"))
config.exitHandler.Error(errors.New("BOM entries in launch.toml is only supported with Buildpack API v0.5 or higher"))
return
}

Expand All @@ -237,6 +239,15 @@ func Build(f BuildFunc, options ...Option) {
}

launch.Processes = result.Launch.Processes
if apiVersion.LessThan(apiV06) {
for _, process := range launch.Processes {
if process.Default {
config.exitHandler.Error(errors.New("processes can only be marked as default with Buildpack API v0.6 or higher"))
return
}
}
}

launch.Slices = result.Launch.Slices
launch.BOM = result.Launch.BOM
if len(result.Launch.Labels) > 0 {
Expand Down
92 changes: 38 additions & 54 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())

bpTOML := []byte(`
api = "0.5"
api = "0.6"
[buildpack]
id = "some-id"
name = "some-name"
Expand Down Expand Up @@ -175,6 +175,7 @@ api = "0.4"
`))
})
})

context("when the api version is greater or equal to 0.5", func() {
it("throws an error", func() {
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
Expand Down Expand Up @@ -543,7 +544,7 @@ api = "0.4"
`))
})

context("when using the deprecated api", func() {
context("when the process is the default", func() {
it("persists a launch.toml", func() {
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
return packit.BuildResult{
Expand All @@ -554,6 +555,7 @@ api = "0.4"
Command: "some-command",
Args: []string{"some-arg"},
Direct: true,
Default: true,
},
},
},
Expand All @@ -569,9 +571,43 @@ api = "0.4"
command = "some-command"
args = ["some-arg"]
direct = true
default = true
`))
})
})

context("when the api version is less than 0.6", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), []byte(`
api = "0.5"
[buildpack]
id = "some-id"
name = "some-name"
version = "some-version"
clear-env = false
`), 0600)).To(Succeed())
})

it("errors", func() {
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
return packit.BuildResult{
Launch: packit.LaunchMetadata{
Processes: []packit.Process{
{
Type: "some-type",
Command: "some-command",
Args: []string{"some-arg"},
Direct: true,
Default: true,
},
},
},
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, platformDir, planPath}), packit.WithExitHandler(exitHandler))

Expect(exitHandler.ErrorCall.Receives.Error).To(MatchError(ContainSubstring("processes can only be marked as default with Buildpack API v0.6 or higher")))
})
})
})

context("when there are slices in the result", func() {
Expand All @@ -596,30 +632,6 @@ api = "0.4"
paths = ["some-slice"]
`))
})

context("when using the deprecated api", func() {
it("persists a launch.toml", func() {
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
return packit.BuildResult{
Launch: packit.LaunchMetadata{
Slices: []packit.Slice{
{
Paths: []string{"some-slice"},
},
},
},
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, platformDir, planPath}))

contents, err := os.ReadFile(filepath.Join(layersDir, "launch.toml"))
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(MatchTOML(`
[[slices]]
paths = ["some-slice"]
`))
})
})
})

context("when there are labels in the result", func() {
Expand Down Expand Up @@ -648,34 +660,6 @@ api = "0.4"
value = "some-other-value"
`))
})

context("when using the deprecated api", func() {
it("persists a launch.toml", func() {
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
return packit.BuildResult{
Launch: packit.LaunchMetadata{
Labels: map[string]string{
"some key": "some value",
"some-other-key": "some-other-value",
},
},
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, platformDir, planPath}))

contents, err := os.ReadFile(filepath.Join(layersDir, "launch.toml"))
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(MatchTOML(`
[[labels]]
key = "some key"
value = "some value"
[[labels]]
key = "some-other-key"
value = "some-other-value"
`))
})
})
})

context("when there are no processes, slices, bom or labels in the result", func() {
Expand Down
3 changes: 3 additions & 0 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ type Process struct {

// Direct indicates whether the process should bypass the shell when invoked.
Direct bool `toml:"direct"`

// Default indicates if this process should be the default when launched.
Default bool `toml:"default,omitempty"`
}

0 comments on commit 3d878bb

Please sign in to comment.