Skip to content

Commit

Permalink
Tidy Up (#427)
Browse files Browse the repository at this point in the history
* Run 'go generate'

* Use draft.Planner from packit directly

* Prefer testing package API over os package API for tests

* Clarify README

* Bump Golang to 1.19

* Run 'go get -u all'
  • Loading branch information
joshuatcasey authored Oct 20, 2022
1 parent b92a32d commit aca3c8e
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 326 deletions.
39 changes: 21 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

## `gcr.io/paketo-buildpacks/bundler`

The Bundler CNB provides the Bundler binary. The buildpack installs the Bundler
onto the `$PATH` and `$GEM_PATH` which makes it available for subsequent buildpacks
and in the final running container.
The Bundler CNB provides the Bundler binary.

The buildpack installs Bundler onto the `$PATH` and `$GEM_PATH` which makes it available for subsequent buildpacks
and/or in the final running container.

## Integration

The Bundler CNB provides bundler as a dependency. Downstream buildpacks, can require the bundler dependency
by generating a [Build Plan
TOML](https://github.com/buildpacks/spec/blob/master/buildpack.md#build-plan-toml)
The Bundler CNB provides bundler as a dependency.
Downstream buildpacks can require the bundler dependency by generating a
[Build Plan TOML](https://github.com/buildpacks/spec/blob/master/buildpack.md#build-plan-toml)
file that looks like the following:

```toml
Expand All @@ -21,22 +22,24 @@ file that looks like the following:
# for deprecation.
name = "bundler"

# The version of the Bundler dependency is not required. In the case it
# is not specified, the buildpack will provide the default version, which can
# be seen in the buildpack.toml file.
# If you wish to request a specific version, the buildpack supports
# specifying a semver constraint in the form of "2.*", "2.1.*", or even
# "2.1.4".
version = "2.1.4"

# The Bundler buildpack supports some non-required metadata options.
[requires.metadata]

# Setting the build flag to true will ensure that the Bundler
# depdendency is available on the $PATH for subsequent buildpacks during
# their build phase. If you are writing a buildpack that needs to run Bundle
# during its build process, this flag should be set to true.
# Use `version` to request a specific version of `bundler`.
# This buildpack supports specifying a semver constraint in the form of "2.*", "2.1.*",
# or even "2.1.4".
# Optional, defaults to the latest version of `bundler` found in the `buildpack.toml` file.
version = "2.1.4"

# When `build` is true, this buildpack will ensure that `bundler` is available
# on the `$PATH` and `$GEM_PATH` for later buildpacks.
# Optional, default false.
build = true

# When `launch` is true, this buildpack will ensure that `bundler` is available
# on the `$PATH` and `$GEM_PATH` for the running application.
# Optional, default false.
launch = true
```
## Usage

Expand Down
14 changes: 5 additions & 9 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@ import (
"github.com/Masterminds/semver"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/draft"
"github.com/paketo-buildpacks/packit/v2/postal"
"github.com/paketo-buildpacks/packit/v2/sbom"
"github.com/paketo-buildpacks/packit/v2/scribe"
)

//go:generate faux --interface EntryResolver --output fakes/entry_resolver.go
//go:generate faux --interface DependencyManager --output fakes/dependency_manager.go
//go:generate faux --interface Shimmer --output fakes/shimmer.go
//go:generate faux --interface SBOMGenerator --output fakes/sbom_generator.go

type EntryResolver interface {
Resolve(string, []packit.BuildpackPlanEntry, []interface{}) (packit.BuildpackPlanEntry, []packit.BuildpackPlanEntry)
MergeLayerTypes(string, []packit.BuildpackPlanEntry) (launch, build bool)
}

type DependencyManager interface {
Resolve(path, id, version, stack string) (postal.Dependency, error)
Deliver(dependency postal.Dependency, cnbPath, layerPath, platformPath string) error
Expand All @@ -37,7 +32,6 @@ type SBOMGenerator interface {
}

func Build(
entries EntryResolver,
dependencies DependencyManager,
versionShimmer Shimmer,
sbomGenerator SBOMGenerator,
Expand All @@ -48,7 +42,9 @@ func Build(
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
logger.Process("Resolving Bundler version")

entry, allEntries := entries.Resolve("bundler", context.Plan.Entries, []interface{}{"BP_BUNDLER_VERSION", "buildpack.yml", "Gemfile.lock"})
planner := draft.NewPlanner()

entry, allEntries := planner.Resolve("bundler", context.Plan.Entries, []interface{}{"BP_BUNDLER_VERSION", "buildpack.yml", "Gemfile.lock"})
logger.Candidates(allEntries)

version, _ := entry.Metadata["version"].(string)
Expand All @@ -68,7 +64,7 @@ func Build(
}

legacySBOM := dependencies.GenerateBillOfMaterials(dependency)
launch, build := entries.MergeLayerTypes("bundler", context.Plan.Entries)
launch, build := planner.MergeLayerTypes("bundler", context.Plan.Entries)

var buildMetadata packit.BuildMetadata
if build {
Expand Down
160 changes: 6 additions & 154 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
layersDir string
cnbDir string

entryResolver *fakes.EntryResolver
dependencyManager *fakes.DependencyManager
versionShimmer *fakes.Shimmer
sbomGenerator *fakes.SBOMGenerator
Expand All @@ -42,23 +41,8 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
)

it.Before(func() {
var err error
layersDir, err = os.MkdirTemp("", "layers")
Expect(err).NotTo(HaveOccurred())

cnbDir, err = os.MkdirTemp("", "cnb")
Expect(err).NotTo(HaveOccurred())

entryResolver = &fakes.EntryResolver{}
entryResolver.ResolveCall.Returns.BuildpackPlanEntry = packit.BuildpackPlanEntry{
Name: "bundler",
Metadata: map[string]interface{}{
"version-source": "BP_BUNDLER_VERSION",
"version": "2.0.x",
"launch": true,
"build": true,
},
}
layersDir = t.TempDir()
cnbDir = t.TempDir()

// Legacy SBOM
dependencyManager = &fakes.DependencyManager{}
Expand Down Expand Up @@ -92,7 +76,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
versionShimmer = &fakes.Shimmer{}

build = bundler.Build(
entryResolver,
dependencyManager,
versionShimmer,
sbomGenerator,
Expand All @@ -115,8 +98,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Metadata: map[string]interface{}{
"version-source": "BP_BUNDLER_VERSION",
"version": "2.0.x",
"launch": true,
"build": true,
},
},
},
Expand All @@ -126,11 +107,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
}
})

it.After(func() {
Expect(os.RemoveAll(layersDir)).To(Succeed())
Expect(os.RemoveAll(cnbDir)).To(Succeed())
})

it("returns a result that installs bundler", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -170,31 +146,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

Expect(filepath.Join(layersDir, "bundler")).To(BeADirectory())

Expect(entryResolver.ResolveCall.Receives.BuildpackPlanEntrySlice).To(Equal([]packit.BuildpackPlanEntry{
{
Name: "bundler",
Metadata: map[string]interface{}{
"version-source": "BP_BUNDLER_VERSION",
"version": "2.0.x",
"launch": true,
"build": true,
},
},
}))
Expect(entryResolver.MergeLayerTypesCall.Receives.String).To(Equal("bundler"))
Expect(entryResolver.MergeLayerTypesCall.Receives.BuildpackPlanEntrySlice).To(Equal(
[]packit.BuildpackPlanEntry{
{
Name: "bundler",
Metadata: map[string]interface{}{
"version-source": "BP_BUNDLER_VERSION",
"version": "2.0.x",
"launch": true,
"build": true,
},
},
}))

Expect(dependencyManager.ResolveCall.Receives.Path).To(Equal(filepath.Join(cnbDir, "buildpack.toml")))
Expect(dependencyManager.ResolveCall.Receives.Id).To(Equal("bundler"))
Expect(dependencyManager.ResolveCall.Receives.Version).To(Equal("2.0.x"))
Expand Down Expand Up @@ -235,27 +186,8 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})

context("when the build plan entry includes the build flag", func() {
var workingDir string

it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())

entryResolver.ResolveCall.Returns.BuildpackPlanEntry = packit.BuildpackPlanEntry{
Name: "bundler",

Metadata: map[string]interface{}{
"version-source": "BP_BUNDLER_VERSION",
"version": "2.0.x",
"build": true,
},
}
entryResolver.MergeLayerTypesCall.Returns.Build = true
})

it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
buildContext.Plan.Entries[0].Metadata["build"] = true
})

it("marks the bundler layer as cached", func() {
Expand Down Expand Up @@ -290,26 +222,8 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})

context("when the build plan entry includes the launch flag", func() {
var workingDir string

it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())

entryResolver.ResolveCall.Returns.BuildpackPlanEntry = packit.BuildpackPlanEntry{
Name: "bundler",
Metadata: map[string]interface{}{
"version-source": "BP_BUNDLER_VERSION",
"version": "2.0.x",
"launch": true,
},
}
entryResolver.MergeLayerTypesCall.Returns.Launch = true
})

it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
buildContext.Plan.Entries[0].Metadata["launch"] = true
})

it("marks the bundler layer as launch", func() {
Expand Down Expand Up @@ -375,27 +289,8 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

context("when the build plan entry version source is from buildpack.yml", func() {
it.Before(func() {
buildContext.Plan.Entries = append(
buildContext.Plan.Entries,
packit.BuildpackPlanEntry{
Name: "bundler",
Metadata: map[string]interface{}{
"version-source": "buildpack.yml",
"version": "1.17.x",
"launch": true,
"build": true,
},
})

entryResolver.ResolveCall.Returns.BuildpackPlanEntry = packit.BuildpackPlanEntry{
Name: "bundler",
Metadata: map[string]interface{}{
"version-source": "buildpack.yml",
"version": "1.17.x",
"launch": true,
"build": true,
},
}
buildContext.Plan.Entries[0].Metadata["version-source"] = "buildpack.yml"
buildContext.Plan.Entries[0].Metadata["version"] = "1.17.x"

dependencyManager.ResolveCall.Returns.Dependency = postal.Dependency{
Name: "Bundler",
Expand All @@ -416,49 +311,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

Expect(filepath.Join(layersDir, "bundler")).To(BeADirectory())

Expect(entryResolver.ResolveCall.Receives.BuildpackPlanEntrySlice).To(Equal([]packit.BuildpackPlanEntry{
{
Name: "bundler",
Metadata: map[string]interface{}{
"version-source": "BP_BUNDLER_VERSION",
"version": "2.0.x",
"launch": true,
"build": true,
},
},
{
Name: "bundler",
Metadata: map[string]interface{}{
"version-source": "buildpack.yml",
"version": "1.17.x",
"launch": true,
"build": true,
},
},
}))
Expect(entryResolver.MergeLayerTypesCall.Receives.String).To(Equal("bundler"))
Expect(entryResolver.MergeLayerTypesCall.Receives.BuildpackPlanEntrySlice).To(Equal(
[]packit.BuildpackPlanEntry{
{
Name: "bundler",
Metadata: map[string]interface{}{
"version-source": "BP_BUNDLER_VERSION",
"version": "2.0.x",
"launch": true,
"build": true,
},
},
{
Name: "bundler",
Metadata: map[string]interface{}{
"version-source": "buildpack.yml",
"version": "1.17.x",
"launch": true,
"build": true,
},
},
}))

Expect(dependencyManager.ResolveCall.Receives.Path).To(Equal(filepath.Join(cnbDir, "buildpack.toml")))
Expect(dependencyManager.ResolveCall.Receives.Id).To(Equal("bundler"))
Expect(dependencyManager.ResolveCall.Receives.Version).To(Equal("1.17.x"))
Expand Down
7 changes: 1 addition & 6 deletions detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bundler_test

import (
"errors"
"os"
"testing"

"github.com/paketo-buildpacks/bundler"
Expand Down Expand Up @@ -43,14 +42,10 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {

context("when $BP_BUNDLER_VERSION is set and a buildpack.yml is present", func() {
it.Before(func() {
os.Setenv("BP_BUNDLER_VERSION", "1.2.3")
t.Setenv("BP_BUNDLER_VERSION", "1.2.3")
buildpackYMLParser.ParseVersionCall.Returns.Version = "1.17.3"
})

it.After(func() {
os.Unsetenv("BP_BUNDLER_VERSION")
})

it("returns a plan that provides and requires that version of bundler", func() {
result, err := detect(packit.DetectContext{
WorkingDir: "/working-dir",
Expand Down
2 changes: 1 addition & 1 deletion fakes/dependency_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package fakes
import (
"sync"

packit "github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/postal"
)

Expand Down
Loading

0 comments on commit aca3c8e

Please sign in to comment.