Skip to content

Commit

Permalink
Add BuildpackForKey helper method.
Browse files Browse the repository at this point in the history
- this enables consumers to avoid iterating through the buildpack array and/or hard-coding buildpack index.
  • Loading branch information
robdimsdale committed Sep 6, 2022
1 parent 9c124b4 commit 0618297
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
10 changes: 10 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,13 @@ func NewImageFromInspectOutput(output []byte) (Image, error) {
Labels: inspect[0].Config.Labels,
}, nil
}

func (i Image) BuildpackForKey(key string) (ImageBuildpackMetadata, error) {
for _, buildpack := range i.Buildpacks {
if buildpack.Key == key {
return buildpack, nil
}
}

return ImageBuildpackMetadata{}, fmt.Errorf("no buildpack found for key: %s", key)
}
68 changes: 68 additions & 0 deletions image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package occam_test

import (
"testing"

"github.com/paketo-buildpacks/occam"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
)

func testImage(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect

context("BuildpackForKey", func() {
it("returns the Buildpack with the key", func() {
image := occam.Image{
Buildpacks: []occam.ImageBuildpackMetadata{
{
Key: "first",
Layers: map[string]occam.ImageBuildpackMetadataLayer{
"first-layer-1": {
SHA: "first-layer-1-sha",
},
"first-layer-2": {
SHA: "first-layer-2-sha",
},
},
},
{
Key: "second",
Layers: map[string]occam.ImageBuildpackMetadataLayer{
"second-layer-1": {
SHA: "second-layer-1-sha",
},
"second-layer-2": {
SHA: "second-layer-2-sha",
},
},
},
},
}

firstBuildpack, err := image.BuildpackForKey("first")
Expect(err).NotTo(HaveOccurred())

Expect(firstBuildpack.Key).To(Equal("first"))
Expect(firstBuildpack.Layers["first-layer-2"].SHA).To(Equal("first-layer-2-sha"))

secondBuildpack, err := image.BuildpackForKey("second")
Expect(err).NotTo(HaveOccurred())

Expect(secondBuildpack.Key).To(Equal("second"))
Expect(secondBuildpack.Layers["second-layer-2"].SHA).To(Equal("second-layer-2-sha"))
})

context("failure cases", func() {
context("when no buildpack exists with the provided key", func() {
it("returns an error", func() {
image := occam.Image{}

_, err := image.BuildpackForKey("some-non-existent-key")
Expect(err).To(HaveOccurred())
})
})
})
})
}
1 change: 1 addition & 0 deletions init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestUnitOccam(t *testing.T) {
suite("CacheVolumeNames", testCacheVolumeNames)
suite("Container", testContainer)
suite("Docker", testDocker)
suite("Image", testImage)
suite("Pack", testPack)
suite("RandomName", testRandomName)
suite("Source", testSource)
Expand Down

0 comments on commit 0618297

Please sign in to comment.