Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy Up #235

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/.bin
.idea/
.DS_Store
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ file that looks like the following:
# for deprecation.
name = "yarn"

# The version of the Yarn 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 "1.*", "1.22.*", or even
# "1.22.4".
version = "1.22.4"

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

# The version of the Yarn 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 "1.*", "1.22.*", or even
# "1.22.4".
version = "1.22.4"

# Setting the build flag to true will ensure that the yarn
# depdendency is available on the $PATH for subsequent buildpacks during
# their build phase. If you are writing a buildpack that needs to run yarn
Expand All @@ -50,15 +50,18 @@ file that looks like the following:

To package this buildpack for consumption:

```
```shell
$ ./scripts/package.sh --version <version-number>
```

This will create a `buildpackage.cnb` file under the `build` directory which you
can use to build your app as follows:
```
pack build <app-name> -p <path-to-app> -b <path/to/node-engine.cnb> -b build/buildpackage.cnb \
-b <path/to/node-and-yarn-requiring-cnb>
```shell
pack build <app-name> \
--path <path-to-app> \
--buildpack <path/to/node-engine.cnb> \
--buildpack build/buildpackage.cnb \
--buildpack <path/to/cnb-that-requires-node-and-yarn>
```

Though the API of this buildpack does not require `node`, yarn is unusable without node.
Expand All @@ -70,11 +73,11 @@ There are no extra configurations for this buildpack based on `buildpack.yml`.
## Run Tests

To run all unit tests, run:
```
```shell
./scripts/unit.sh
```

To run all integration tests, run:
```
```shell
/scripts/integration.sh
```
13 changes: 4 additions & 9 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@ import (

"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
type EntryResolver interface {
Resolve(name string, entries []packit.BuildpackPlanEntry, priorites []interface{}) (packit.BuildpackPlanEntry, []packit.BuildpackPlanEntry)
MergeLayerTypes(name string, entries []packit.BuildpackPlanEntry) (launch, build bool)
}

//go:generate faux --interface DependencyManager --output fakes/dependency_manager.go
type DependencyManager interface {
Resolve(path, id, version, stack string) (postal.Dependency, error)
Expand All @@ -33,7 +28,6 @@ type SBOMGenerator interface {
}

func Build(
entryResolver EntryResolver,
dependencyManager DependencyManager,
sbomGenerator SBOMGenerator,
clock chronos.Clock,
Expand All @@ -47,7 +41,8 @@ func Build(
return packit.BuildResult{}, err
}

entry, _ := entryResolver.Resolve("yarn", context.Plan.Entries, nil)
planner := draft.NewPlanner()
entry, _ := planner.Resolve("yarn", context.Plan.Entries, nil)
version, ok := entry.Metadata["version"].(string)
if !ok {
version = "default"
Expand All @@ -64,7 +59,7 @@ func Build(

bom := dependencyManager.GenerateBillOfMaterials(dependency)

launch, build := entryResolver.MergeLayerTypes("yarn", context.Plan.Entries)
launch, build := planner.MergeLayerTypes("yarn", context.Plan.Entries)

var buildMetadata = packit.BuildMetadata{}
var launchMetadata = packit.LaunchMetadata{}
Expand Down
157 changes: 37 additions & 120 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
layersDir string
workingDir string
cnbDir string
entryResolver *fakes.EntryResolver
dependencyManager *fakes.DependencyManager
sbomGenerator *fakes.SBOMGenerator

buffer *bytes.Buffer

build packit.BuildFunc
buildContext packit.BuildContext
build packit.BuildFunc
)

it.Before(func() {
Expand All @@ -49,11 +49,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())

entryResolver = &fakes.EntryResolver{}
entryResolver.ResolveCall.Returns.BuildpackPlanEntry = packit.BuildpackPlanEntry{
Name: "yarn",
}

dependencyManager = &fakes.DependencyManager{}
dependencyManager.ResolveCall.Returns.Dependency = postal.Dependency{
ID: "yarn",
Expand Down Expand Up @@ -81,21 +76,8 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
sbomGenerator.GenerateFromDependencyCall.Returns.SBOM = sbom.SBOM{}

buffer = bytes.NewBuffer(nil)
build = yarn.Build(entryResolver,
dependencyManager,
sbomGenerator,
chronos.DefaultClock,
scribe.NewEmitter(buffer))
})

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

it("returns a result that installs yarn", func() {
result, err := build(packit.BuildContext{
buildContext = packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
Expand All @@ -113,7 +95,22 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
},
Platform: packit.Platform{Path: "platform"},
Layers: packit.Layers{Path: layersDir},
})
}

build = yarn.Build(dependencyManager,
sbomGenerator,
chronos.DefaultClock,
scribe.NewEmitter(buffer))
})

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

it("returns a result that installs yarn", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(1))
Expand All @@ -136,16 +133,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
},
}))

Expect(entryResolver.ResolveCall.Receives.Name).To(Equal("yarn"))
Expect(entryResolver.ResolveCall.Receives.Entries).To(Equal([]packit.BuildpackPlanEntry{
{Name: "yarn"},
}))

Expect(entryResolver.MergeLayerTypesCall.Receives.Name).To(Equal("yarn"))
Expect(entryResolver.MergeLayerTypesCall.Receives.Entries).To(Equal([]packit.BuildpackPlanEntry{
{Name: "yarn"},
}))

Expect(dependencyManager.ResolveCall.Receives.Path).To(Equal(filepath.Join(cnbDir, "buildpack.toml")))
Expect(dependencyManager.ResolveCall.Receives.Id).To(Equal("yarn"))
Expect(dependencyManager.ResolveCall.Receives.Stack).To(Equal("some-stack"))
Expand Down Expand Up @@ -190,27 +177,14 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

context("when the plan entry requires the dependency during the build and launch phases", func() {
it.Before(func() {
entryResolver.MergeLayerTypesCall.Returns.Launch = true
entryResolver.MergeLayerTypesCall.Returns.Build = true
buildContext.Plan.Entries[0].Metadata = map[string]interface{}{
"build": true,
"launch": true,
}
})

it("makes the layer available in those phases", func() {
result, err := build(packit.BuildContext{
CNBPath: cnbDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{
Name: "yarn",
Metadata: map[string]interface{}{
"build": true,
"launch": true,
},
},
},
},
Layers: packit.Layers{Path: layersDir},
Stack: "some-stack",
})
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(1))
Expand All @@ -235,16 +209,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})

it("returns an error", func() {
_, err := build(packit.BuildContext{
CNBPath: cnbDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: "yarn"},
},
},
Layers: packit.Layers{Path: layersDir},
Stack: "some-stack",
})
_, err := build(buildContext)
Expect(err).To(MatchError(ContainSubstring("failed to parse layer content metadata")))
})
})
Expand All @@ -255,16 +220,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})

it("returns an error", func() {
_, err := build(packit.BuildContext{
CNBPath: cnbDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: "yarn"},
},
},
Layers: packit.Layers{Path: layersDir},
Stack: "some-stack",
})
_, err := build(buildContext)
Expect(err).To(MatchError("failed to resolve dependency"))
})
})
Expand All @@ -279,15 +235,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})

it("returns an error", func() {
_, err := build(packit.BuildContext{
CNBPath: cnbDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: "yarn"},
},
},
Layers: packit.Layers{Path: layersDir},
})
_, err := build(buildContext)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
Expand All @@ -298,33 +246,18 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})

it("returns an error", func() {
_, err := build(packit.BuildContext{
CNBPath: cnbDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: "yarn"},
},
},
Layers: packit.Layers{Path: layersDir},
Stack: "some-stack",
})
_, err := build(buildContext)
Expect(err).To(MatchError("failed to install dependency"))
})
})

context("when generating the SBOM returns an error", func() {
it.Before(func() {
buildContext.BuildpackInfo = packit.BuildpackInfo{SBOMFormats: []string{"random-format"}}
})

it("returns an error", func() {
_, err := build(packit.BuildContext{
BuildpackInfo: packit.BuildpackInfo{SBOMFormats: []string{"random-format"}},
CNBPath: cnbDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: "yarn"},
},
},
Layers: packit.Layers{Path: layersDir},
Stack: "some-stack",
})
_, err := build(buildContext)
Expect(err).To(MatchError("unsupported SBOM format: 'random-format'"))
})
})
Expand All @@ -335,38 +268,22 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})

it("returns an error", func() {
_, err := build(packit.BuildContext{
CNBPath: cnbDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: "yarn"},
},
},
Layers: packit.Layers{Path: layersDir},
Stack: "some-stack",
})
_, err := build(buildContext)
Expect(err).To(MatchError(ContainSubstring("failed to generate SBOM")))
})
})

context("when BP_DISABLE_SBOM is set incorrectly", func() {
it.Before(func() {
os.Setenv("BP_DISABLE_SBOM", "not-a-bool")
Expect(os.Setenv("BP_DISABLE_SBOM", "not-a-bool")).To(Succeed())
})

it.After(func() {
os.Unsetenv("BP_DISABLE_SBOM")
Expect(os.Unsetenv("BP_DISABLE_SBOM")).To(Succeed())
})

it("returns an error", func() {
_, err := build(packit.BuildContext{
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
SBOMFormats: []string{sbom.CycloneDXFormat, sbom.SPDXFormat},
},
})

_, err := build(buildContext)
Expect(err).To(MatchError(ContainSubstring("failed to parse BP_DISABLE_SBOM")))
})
})
Expand Down
Loading