Skip to content

Commit

Permalink
CNB_BUILDPACK_DIR (RFC #36)
Browse files Browse the repository at this point in the history
This change causes the buildpack's path to be determined first by reading
CNB_BUILDPACK_DIR if it exists.  If it does not, fall back to trimming
argv[0].  The fallback is temporary and can be removed once the lifecycle
supports the primary behavior.

[buildpacks/rfcs#49]

Signed-off-by: Ben Hale <[email protected]>
  • Loading branch information
nebhale committed Apr 22, 2020
1 parent 8b98016 commit f24419c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
6 changes: 5 additions & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ func Build(builder Builder, options ...Option) {
logger.Debug(ApplicationPathFormatter(ctx.Application.Path))
}

ctx.Buildpack.Path = filepath.Clean(strings.TrimSuffix(config.arguments[0], filepath.Join("bin", "build")))
if s, ok := os.LookupEnv("CNB_BUILDPACK_DIR"); ok {
ctx.Buildpack.Path = filepath.Clean(s)
} else { // TODO: Remove branch once lifecycle has been updated to support this
ctx.Buildpack.Path = filepath.Clean(strings.TrimSuffix(config.arguments[0], filepath.Join("bin", "build")))
}
if logger.IsDebugEnabled() {
logger.Debug(BuildpackPathFormatter(ctx.Buildpack.Path))
}
Expand Down
18 changes: 17 additions & 1 deletion build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

buildpackPath, err = ioutil.TempDir("", "build-buildpack-path")
Expect(err).NotTo(HaveOccurred())
Expect(os.Setenv("CNB_BUILDPACK_DIR", buildpackPath)).To(Succeed())

Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(`
Expand Down Expand Up @@ -105,7 +106,7 @@ test-key = "test-value"
0644),
).To(Succeed())

commandPath = filepath.Join(buildpackPath, "bin", "build")
commandPath = filepath.Join("bin", "build")

environmentWriter = &mocks.EnvironmentWriter{}
environmentWriter.On("Write", mock.Anything, mock.Anything).Return(nil)
Expand Down Expand Up @@ -158,6 +159,7 @@ test-key = "test-value"

it.After(func() {
Expect(os.Chdir(workingDir)).To(Succeed())
Expect(os.Unsetenv("CNB_BUILDPACK_DIR")).To(Succeed())
Expect(os.Unsetenv("CNB_STACK_ID")).To(Succeed())

Expect(os.RemoveAll(applicationPath)).To(Succeed())
Expand Down Expand Up @@ -256,6 +258,20 @@ test-key = "test-value"
Expect(ctx.StackID).To(Equal("test-stack-id"))
})

it("extracts buildpack path from command path if CNB_BUILDPACK_PATH is not set", func() {
Expect(os.Unsetenv("CNB_BUILDPACK_DIR")).To(Succeed())

builder.On("Build", mock.Anything).Return(libcnb.NewBuildResult(), nil)

libcnb.Build(builder,
libcnb.WithArguments([]string{filepath.Join(buildpackPath, commandPath), layersPath, platformPath, buildpackPlanPath}),
)

ctx := builder.Calls[0].Arguments[0].(libcnb.BuildContext)

Expect(ctx.Buildpack.Path).To(Equal(buildpackPath))
})

it("handles error from BuildFunc", func() {
builder.On("Build", mock.Anything).Return(libcnb.NewBuildResult(), fmt.Errorf("test-error"))

Expand Down
6 changes: 5 additions & 1 deletion detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ func Detect(detector Detector, options ...Option) {
logger.Debug(ApplicationPathFormatter(ctx.Application.Path))
}

ctx.Buildpack.Path = filepath.Clean(strings.TrimSuffix(config.arguments[0], filepath.Join("bin", "detect")))
if s, ok := os.LookupEnv("CNB_BUILDPACK_DIR"); ok {
ctx.Buildpack.Path = filepath.Clean(s)
} else { // TODO: Remove branch once lifecycle has been updated to support this
ctx.Buildpack.Path = filepath.Clean(strings.TrimSuffix(config.arguments[0], filepath.Join("bin", "detect")))
}
if logger.IsDebugEnabled() {
logger.Debug(BuildpackPathFormatter(ctx.Buildpack.Path))
}
Expand Down
19 changes: 18 additions & 1 deletion detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {

buildpackPath, err = ioutil.TempDir("", "detect-buildpack-path")
Expect(err).NotTo(HaveOccurred())
Expect(os.Setenv("CNB_BUILDPACK_DIR", buildpackPath)).To(Succeed())

Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(`
Expand Down Expand Up @@ -88,7 +89,7 @@ test-key = "test-value"
Expect(f.Close()).NotTo(HaveOccurred())
buildPlanPath = f.Name()

commandPath = filepath.Join(buildpackPath, "bin", "detect")
commandPath = filepath.Join("bin", "detect")

detector = &mocks.Detector{}

Expand Down Expand Up @@ -129,6 +130,7 @@ test-key = "test-value"

it.After(func() {
Expect(os.Chdir(workingDir)).To(Succeed())
Expect(os.Unsetenv("CNB_BUILDPACK_DIR")).To(Succeed())
Expect(os.Unsetenv("CNB_STACK_ID")).To(Succeed())

Expect(os.RemoveAll(applicationPath)).To(Succeed())
Expand Down Expand Up @@ -216,6 +218,21 @@ test-key = "test-value"
Expect(ctx.StackID).To(Equal("test-stack-id"))
})

it("extracts buildpack path from command path if CNB_BUILDPACK_PATH is not set", func() {
Expect(os.Unsetenv("CNB_BUILDPACK_DIR")).To(Succeed())

detector.On("Detect", mock.Anything).Return(libcnb.DetectResult{}, nil)

libcnb.Detect(detector,
libcnb.WithArguments([]string{filepath.Join(buildpackPath, commandPath), platformPath, buildPlanPath}),
libcnb.WithExitHandler(exitHandler),
)

ctx := detector.Calls[0].Arguments[0].(libcnb.DetectContext)

Expect(ctx.Buildpack.Path).To(Equal(buildpackPath))
})

it("handles error from DetectFunc", func() {
detector.On("Detect", mock.Anything).Return(libcnb.DetectResult{}, fmt.Errorf("test-error"))

Expand Down

0 comments on commit f24419c

Please sign in to comment.