Skip to content

Commit

Permalink
Use draft.Planner from packit directly
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuatcasey authored and robdimsdale committed May 9, 2022
1 parent cbd23a0 commit 2adf8a0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 107 deletions.
16 changes: 5 additions & 11 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,17 @@ 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
//go:generate faux --interface DependencyManager --output fakes/dependency_manager.go
//go:generate faux --interface InstallProcess --output fakes/install_process.go
//go:generate faux --interface SitePackageProcess --output fakes/site_package_process.go
//go:generate faux --interface SBOMGenerator --output fakes/sbom_generator.go

// EntryResolver defines the interface for picking the most relevant entry from
// the Buildpack Plan entries.
type EntryResolver interface {
Resolve(string, []packit.BuildpackPlanEntry, []interface{}) (packit.BuildpackPlanEntry, []packit.BuildpackPlanEntry)
MergeLayerTypes(string, []packit.BuildpackPlanEntry) (launch, build bool)
}

// DependencyManager defines the interface for picking the best matching
// dependency and installing it.
type DependencyManager interface {
Expand Down Expand Up @@ -56,7 +49,6 @@ type SBOMGenerator interface {
// layer, and generate Bill-of-Materials. It also makes use of the checksum of
// the dependency to reuse the layer when possible.
func Build(
entries EntryResolver,
dependencies DependencyManager,
installProcess InstallProcess,
siteProcess SitePackageProcess,
Expand All @@ -67,8 +59,10 @@ func Build(
return func(context packit.BuildContext) (packit.BuildResult, error) {
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)

planner := draft.NewPlanner()

logger.Process("Resolving Pip version")
entry, sortedEntries := entries.Resolve(Pip, context.Plan.Entries, Priorities)
entry, sortedEntries := planner.Resolve(Pip, context.Plan.Entries, Priorities)
logger.Candidates(sortedEntries)

version, _ := entry.Metadata["version"].(string)
Expand All @@ -82,7 +76,7 @@ func Build(
logger.SelectedDependency(entry, dependency, clock.Now())

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

var launchMetadata packit.LaunchMetadata
if launch {
Expand Down
37 changes: 7 additions & 30 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/paketo-buildpacks/packit/v2/postal"
"github.com/paketo-buildpacks/packit/v2/sbom"
"github.com/paketo-buildpacks/packit/v2/scribe"
pip "github.com/paketo-buildpacks/pip"
"github.com/paketo-buildpacks/pip"
"github.com/paketo-buildpacks/pip/fakes"
"github.com/sclevine/spec"

Expand All @@ -30,7 +30,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
layersDir string
cnbDir string

entryResolver *fakes.EntryResolver
dependencyManager *fakes.DependencyManager
installProcess *fakes.InstallProcess
sitePackageProcess *fakes.SitePackageProcess
Expand All @@ -52,11 +51,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
cnbDir, err = os.MkdirTemp("", "cnb")
Expect(err).NotTo(HaveOccurred())

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

dependencyManager = &fakes.DependencyManager{}
dependencyManager.ResolveCall.Returns.Dependency = postal.Dependency{
ID: "pip",
Expand Down Expand Up @@ -102,7 +96,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
logEmitter = scribe.NewEmitter(buffer)

build = pip.Build(
entryResolver,
dependencyManager,
installProcess,
sitePackageProcess,
Expand Down Expand Up @@ -173,24 +166,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
},
}))

Expect(entryResolver.ResolveCall.Receives.String).To(Equal("pip"))
Expect(entryResolver.ResolveCall.Receives.BuildpackPlanEntrySlice).To(Equal([]packit.BuildpackPlanEntry{
{
Name: "pip",
},
}))

Expect(entryResolver.ResolveCall.Receives.InterfaceSlice).To(Equal([]interface{}{"BP_PIP_VERSION"}))

Expect(entryResolver.MergeLayerTypesCall.Receives.String).To(Equal("pip"))
Expect(entryResolver.MergeLayerTypesCall.Receives.BuildpackPlanEntrySlice).To(Equal(
[]packit.BuildpackPlanEntry{
{
Name: "pip",
},
},
))

Expect(dependencyManager.ResolveCall.Receives.Path).To(Equal(filepath.Join(cnbDir, "buildpack.toml")))
Expect(dependencyManager.ResolveCall.Receives.Id).To(Equal("pip"))
Expect(dependencyManager.ResolveCall.Receives.Version).To(Equal(""))
Expand Down Expand Up @@ -222,8 +197,9 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

context("when build plan entries require pip at build/launch", func() {
it.Before(func() {
entryResolver.MergeLayerTypesCall.Returns.Build = true
entryResolver.MergeLayerTypesCall.Returns.Launch = true
buildContext.Plan.Entries[0].Metadata = make(map[string]interface{})
buildContext.Plan.Entries[0].Metadata["build"] = true
buildContext.Plan.Entries[0].Metadata["launch"] = true
})

it("makes the layer available at the right times", func() {
Expand Down Expand Up @@ -281,8 +257,9 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
`, pip.DependencySHAKey)), os.ModePerm)
Expect(err).NotTo(HaveOccurred())

entryResolver.MergeLayerTypesCall.Returns.Build = true
entryResolver.MergeLayerTypesCall.Returns.Launch = false
buildContext.Plan.Entries[0].Metadata = make(map[string]interface{})
buildContext.Plan.Entries[0].Metadata["build"] = true
buildContext.Plan.Entries[0].Metadata["launch"] = false
})

it("skips the build process if the cached dependency sha matches the selected dependency sha", func() {
Expand Down
64 changes: 0 additions & 64 deletions fakes/entry_resolver.go

This file was deleted.

2 changes: 0 additions & 2 deletions run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/cargo"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/draft"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/postal"
"github.com/paketo-buildpacks/packit/v2/sbom"
Expand All @@ -26,7 +25,6 @@ func main() {
packit.Run(
pip.Detect(),
pip.Build(
draft.NewPlanner(),
postal.NewService(cargo.NewTransport()),
pip.NewPipInstallProcess(pexec.NewExecutable("python")),
pip.NewSiteProcess(pexec.NewExecutable("python")),
Expand Down

0 comments on commit 2adf8a0

Please sign in to comment.