diff --git a/build.go b/build.go index 353ede9..c6be7da 100644 --- a/build.go +++ b/build.go @@ -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)) } diff --git a/build_test.go b/build_test.go index 264fcc4..9316044 100644 --- a/build_test.go +++ b/build_test.go @@ -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(` @@ -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) @@ -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()) @@ -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")) diff --git a/detect.go b/detect.go index 0ab69b6..0e1af3e 100644 --- a/detect.go +++ b/detect.go @@ -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)) } diff --git a/detect_test.go b/detect_test.go index 6a8bf9c..833436b 100644 --- a/detect_test.go +++ b/detect_test.go @@ -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(` @@ -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{} @@ -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()) @@ -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"))