Skip to content

Commit

Permalink
Only prunes layer TOML files when api < 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Moran authored and sophiewigmore committed Oct 26, 2021
1 parent 6fab3bb commit 4287c36
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 87 deletions.
14 changes: 8 additions & 6 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,14 @@ func Build(f BuildFunc, options ...Option) {
return
}

for _, file := range layerTomls {
if filepath.Base(file) != "launch.toml" && filepath.Base(file) != "store.toml" && filepath.Base(file) != "build.toml" {
err = os.Remove(file)
if err != nil {
config.exitHandler.Error(fmt.Errorf("failed to remove layer toml: %w", err))
return
if apiVersion.LessThan(apiV06) {
for _, file := range layerTomls {
if filepath.Base(file) != "launch.toml" && filepath.Base(file) != "store.toml" && filepath.Base(file) != "build.toml" {
err = os.Remove(file)
if err != nil {
config.exitHandler.Error(fmt.Errorf("failed to remove layer toml: %w", err))
return
}
}
}
}
Expand Down
201 changes: 120 additions & 81 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,102 +252,141 @@ cache = true
Expect(os.WriteFile(filepath.Join(layersDir, "store.toml"), []byte{}, 0600)).To(Succeed())
})

it("removes them", func() {
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
return packit.BuildResult{
Layers: []packit.Layer{},
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, platformDir, planPath}))
Expect(obsoleteLayerPath).NotTo(BeARegularFile())
Expect(obsoleteLayerPath + ".toml").NotTo(BeARegularFile())
context("when the buildpack api version is less than 0.6", func() {
it.Before(func() {
bpTOML := []byte(`
api = "0.5"
[buildpack]
id = "some-id"
name = "some-name"
version = "some-version"
`)
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
})

Expect(filepath.Join(layersDir, "launch.toml")).To(BeARegularFile())
Expect(filepath.Join(layersDir, "store.toml")).To(BeARegularFile())
})
})
it("removes them", func() {
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
return packit.BuildResult{
Layers: []packit.Layer{},
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, platformDir, planPath}))
Expect(obsoleteLayerPath).NotTo(BeARegularFile())
Expect(obsoleteLayerPath + ".toml").NotTo(BeARegularFile())

context("when the CNB_BUILDPACK_DIR environment variable is set", func() {
it.Before(func() {
os.Setenv("CNB_BUILDPACK_DIR", envCnbDir)
Expect(filepath.Join(layersDir, "launch.toml")).To(BeARegularFile())
Expect(filepath.Join(layersDir, "store.toml")).To(BeARegularFile())
})

context("failures", func() {
context("when getting the layer toml list", func() {
var unremovableTOMLPath string

it.Before(func() {
unremovableTOMLPath = filepath.Join(layersDir, "unremovable.toml")
Expect(os.MkdirAll(filepath.Join(layersDir, "unremovable"), os.ModePerm)).To(Succeed())
Expect(os.WriteFile(unremovableTOMLPath, []byte{}, os.ModePerm)).To(Succeed())
Expect(os.Chmod(layersDir, 0666)).To(Succeed())
})

it.After(func() {
Expect(os.Chmod(layersDir, os.ModePerm)).To(Succeed())
})

it("returns an error", func() {
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
return packit.BuildResult{
Layers: []packit.Layer{},
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, platformDir, planPath}), packit.WithExitHandler(exitHandler))
Expect(exitHandler.ErrorCall.Receives.Error).To(MatchError(ContainSubstring("failed to remove layer toml:")))
})
})
})
})

it.After(func() {
os.Unsetenv("CNB_BUILDPACK_DIR")
context("when the buildpack api version is greater than or equal to 0.6", func() {
it.Before(func() {
bpTOML := []byte(`
api = "0.6"
[buildpack]
id = "some-id"
name = "some-name"
version = "some-version"
`)
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
})

it("leaves them in place", func() {
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
return packit.BuildResult{
Layers: []packit.Layer{},
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, platformDir, planPath}))

Expect(obsoleteLayerPath).To(BeADirectory())
Expect(obsoleteLayerPath + ".toml").To(BeARegularFile())

Expect(filepath.Join(layersDir, "launch.toml")).To(BeARegularFile())
Expect(filepath.Join(layersDir, "store.toml")).To(BeARegularFile())
})
})
})
})

it("sets the correct value for CNBdir in the Build context", func() {
context("when the CNB_BUILDPACK_DIR environment variable is set", func() {
it.Before(func() {
os.Setenv("CNB_BUILDPACK_DIR", envCnbDir)
})

var context packit.BuildContext
it.After(func() {
os.Unsetenv("CNB_BUILDPACK_DIR")
})

packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
context = ctx
it("sets the correct value for CNBdir in the Build context", func() {
var context packit.BuildContext

return packit.BuildResult{}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, platformDir, planPath}))
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
context = ctx

Expect(context).To(Equal(packit.BuildContext{
CNBPath: envCnbDir,
Platform: packit.Platform{
Path: platformDir,
},
Stack: "some-stack",
WorkingDir: tmpDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{
Name: "some-entry",
Metadata: map[string]interface{}{
"version": "some-version",
"some-key": "some-value",
},
return packit.BuildResult{}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, platformDir, planPath}))

Expect(context).To(Equal(packit.BuildContext{
CNBPath: envCnbDir,
Platform: packit.Platform{
Path: platformDir,
},
Stack: "some-stack",
WorkingDir: tmpDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{
Name: "some-entry",
Metadata: map[string]interface{}{
"version": "some-version",
"some-key": "some-value",
},
},
},
Layers: packit.Layers{
Path: layersDir,
},
BuildpackInfo: packit.BuildpackInfo{
ID: "some-id",
Name: "some-name",
Version: "some-version",
Homepage: "some-homepage",
Description: "some-description",
Keywords: []string{"some-keyword"},
Licenses: []packit.BuildpackInfoLicense{
{
Type: "some-license-type",
URI: "some-license-uri",
},
},
Layers: packit.Layers{
Path: layersDir,
},
BuildpackInfo: packit.BuildpackInfo{
ID: "some-id",
Name: "some-name",
Version: "some-version",
Homepage: "some-homepage",
Description: "some-description",
Keywords: []string{"some-keyword"},
Licenses: []packit.BuildpackInfoLicense{
{
Type: "some-license-type",
URI: "some-license-uri",
},
},
}))
})
})

context("failures", func() {
context("when getting the layer toml list", func() {
var unremovableTOMLPath string

it.Before(func() {
unremovableTOMLPath = filepath.Join(layersDir, "unremovable.toml")
Expect(os.MkdirAll(filepath.Join(layersDir, "unremovable"), os.ModePerm)).To(Succeed())
Expect(os.WriteFile(unremovableTOMLPath, []byte{}, os.ModePerm)).To(Succeed())
Expect(os.Chmod(layersDir, 0666)).To(Succeed())
})

it.After(func() {
Expect(os.Chmod(layersDir, os.ModePerm)).To(Succeed())
})

it("returns an error", func() {
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
return packit.BuildResult{
Layers: []packit.Layer{},
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, platformDir, planPath}), packit.WithExitHandler(exitHandler))
Expect(exitHandler.ErrorCall.Receives.Error).To(MatchError(ContainSubstring("failed to remove layer toml:")))
})
})
},
}))
})
})

Expand Down

0 comments on commit 4287c36

Please sign in to comment.