Skip to content

Commit

Permalink
Sets CNB_BUILDPACK_DIR in bp execution env during detect and build
Browse files Browse the repository at this point in the history
* Resolves #320

Signed-off-by: Emily Casey <ecasey@vmware.com>
  • Loading branch information
ekcasey committed Jul 1, 2020
1 parent a7e916f commit 880053c
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 6 deletions.
1 change: 1 addition & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (b *Builder) Build() (*BuildMetadata, error) {
return nil, err
}
}
cmd.Env = append(cmd.Env, EnvBuildpackDir+"="+bpInfo.Path)

if err := cmd.Run(); err != nil {
return nil, err
Expand Down
59 changes: 56 additions & 3 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,26 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
}
})

it("should set CNB_BUILDPACK_DIR", func() {
if _, err := builder.Build(); err != nil {
t.Fatalf("Unexpected error:\n%s\n", err)
}
bpsDir, err := filepath.Abs(builder.BuildpacksDir)
if err != nil {
t.Fatalf("Unexpected error:\n%s\n", err)
}
if s := cmp.Diff(rdfile(t, filepath.Join(appDir, "build-env-cnb-buildpack-dir-A-v1")),
filepath.Join(bpsDir, "A/v1"),
); s != "" {
t.Fatalf("Unexpected CNB_BUILDPACK_DIR:\n%s\n", s)
}
if s := cmp.Diff(rdfile(t, filepath.Join(appDir, "build-env-cnb-buildpack-dir-B-v2")),
filepath.Join(bpsDir, "B/v2"),
); s != "" {
t.Fatalf("Unexpected CNB_BUILDPACK_DIR:\n%s\n", s)
}
})

it("should connect stdout and stdin to the terminal", func() {
if _, err := builder.Build(); err != nil {
t.Fatalf("Unexpected error:\n%s\n", err)
Expand Down Expand Up @@ -363,13 +383,46 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
})

when("building succeeds with a clear env", func() {
it("should not apply user-provided env vars", func() {
env.EXPECT().List().Return(append(os.Environ(), "TEST_ENV=Av1.clear"))
env.EXPECT().WithPlatform(platformDir).Return(append(os.Environ(), "TEST_ENV=Bv1"), nil)
it.Before(func() {
env.EXPECT().List().Return(append(os.Environ(), "TEST_ENV=cleared"))
env.EXPECT().WithPlatform(platformDir).Return(append(os.Environ(), "TEST_ENV=with-platform"), nil)
builder.Group.Group[0].Version = "v1.clear"
})

it("should not apply user-provided env vars", func() {
if _, err := builder.Build(); err != nil {
t.Fatalf("Error: %s\n", err)
}
if s := cmp.Diff(rdfile(t, filepath.Join(appDir, "build-info-A-v1.clear")),
"TEST_ENV: cleared\n",
); s != "" {
t.Fatalf("Unexpected info:\n%s\n", s)
}
if s := cmp.Diff(rdfile(t, filepath.Join(appDir, "build-info-B-v2")),
"TEST_ENV: with-platform\n",
); s != "" {
t.Fatalf("Unexpected info:\n%s\n", s)
}
})

it("should set CNB_BUILDPACK_DIR", func() {
if _, err := builder.Build(); err != nil {
t.Fatalf("Unexpected error:\n%s\n", err)
}
bpsDir, err := filepath.Abs(builder.BuildpacksDir)
if err != nil {
t.Fatalf("Unexpected error:\n%s\n", err)
}
if s := cmp.Diff(rdfile(t, filepath.Join(appDir, "build-env-cnb-buildpack-dir-A-v1.clear")),
filepath.Join(bpsDir, "A/v1.clear"),
); s != "" {
t.Fatalf("Unexpected CNB_BUILDPACK_DIR:\n%s\n", s)
}
if s := cmp.Diff(rdfile(t, filepath.Join(appDir, "build-env-cnb-buildpack-dir-B-v2")),
filepath.Join(bpsDir, "B/v2"),
); s != "" {
t.Fatalf("Unexpected CNB_BUILDPACK_DIR:\n%s\n", s)
}
})
})

Expand Down
6 changes: 4 additions & 2 deletions detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

const (
CodeDetectPass = 0
CodeDetectFail = 100
CodeDetectPass = 0
CodeDetectFail = 100
EnvBuildpackDir = "CNB_BUILDPACK_DIR"
)

var ErrFail = errors.New("no buildpacks participating")
Expand Down Expand Up @@ -221,6 +222,7 @@ func (bp *buildpackTOML) Detect(c *DetectConfig) detectRun {
if bp.Buildpack.ClearEnv {
cmd.Env = c.ClearEnv
}
cmd.Env = append(cmd.Env, EnvBuildpackDir+"="+bp.Path)

if err := cmd.Run(); err != nil {
if err, ok := err.(*exec.ExitError); ok {
Expand Down
28 changes: 28 additions & 0 deletions detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,34 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
}
})

it("should set CNB_BUILDPACK_DIR in the environment", func() {
mkappfile("0", "detect-status-A-v1.clear", "detect-status-B-v1")

_, _, err := lifecycle.BuildpackOrder{{
Group: []lifecycle.Buildpack{
{ID: "A", Version: "v1.clear"},
{ID: "B", Version: "v2"},
},
}}.Detect(config)
if err != nil {
t.Fatalf("Unexpected error:\n%s\n", err)
}

bpsDir, err := filepath.Abs(config.BuildpacksDir)
if err != nil {
t.Fatalf("Unexpected error:\n%s\n", err)
}
expectedBpDir := filepath.Join(bpsDir, "A/v1.clear")
if bpDir := rdappfile("detect-env-cnb-buildpack-dir-A-v1.clear"); bpDir != expectedBpDir {
t.Fatalf("Unexpected buildpack dir:\n\twanted: %s\n\tgot: %s\n", expectedBpDir, bpDir)
}

expectedBpDir = filepath.Join(bpsDir, "B/v2")
if bpDir := rdappfile("detect-env-cnb-buildpack-dir-B-v2"); bpDir != expectedBpDir {
t.Fatalf("Unexpected buildpack dir:\n\twanted: %s\n\tgot: %s\n", expectedBpDir, bpDir)
}
})

when("a build plan is employed", func() {
it("should return a build plan with matched dependencies", func() {
mkappfile("100", "detect-status-C-v1")
Expand Down
3 changes: 2 additions & 1 deletion testdata/buildpack/bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ echo "build out: ${bp_id}@${bp_version}"
>&2 echo "build err: ${bp_id}@${bp_version}"

echo "TEST_ENV: ${TEST_ENV}" > "build-info-${bp_id}-${bp_version}"
echo -n "${CNB_BUILDPACK_DIR:-unset}" > "build-env-cnb-buildpack-dir-${bp_id}-${bp_version}"

cp -a "$platform_dir/env" "build-env-${bp_id}-${bp_version}"

Expand All @@ -38,4 +39,4 @@ if [[ -f build-status ]]; then
exit "$(cat build-status)"
fi

exit 0
exit 0
1 change: 1 addition & 0 deletions testdata/buildpack/bin/detect
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ echo "detect out: ${bp_id}@${bp_version}"

ls -1 "$platform_dir/env" > "detect-env-${bp_id}-${bp_version}"
echo -n "$ENV_TYPE" > "detect-env-type-${bp_id}-${bp_version}"
echo -n "${CNB_BUILDPACK_DIR:-unset}" > "detect-env-cnb-buildpack-dir-${bp_id}-${bp_version}"

if [[ -f detect-plan-${bp_id}-${bp_version}.toml ]]; then
cat "detect-plan-${bp_id}-${bp_version}.toml" > "$plan_path"
Expand Down

0 comments on commit 880053c

Please sign in to comment.