Skip to content

Commit

Permalink
Moves core logic into its own package
Browse files Browse the repository at this point in the history
This PR moves the core Include/Exclude logic into its own package. This allows the core logic to be imported and reused by other buildpacks or libraries. This core logic package does not depend on packit or libcnb/libpak so it can be used in either style buildpack.

Summary:

- Creates 'logic' package (not tied to that name)
- Moves exclude, include and tests into that package
- Updates buildpack & tests to use logic package

Signed-off-by: Daniel Mikusa <[email protected]>
  • Loading branch information
Daniel Mikusa authored and ForestEckhardt committed Jul 7, 2022
1 parent c90c315 commit 0f66735
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 16 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ $ ./scripts/package.sh

This builds the buildpack's Go source using `GOOS=linux` by default. You can
supply another value as the first argument to `package.sh`.

## Library Usage

The logic to implement this buildpack is isolated to a package called `logic`, which can be used if you want to incorporate the same logic in your buildpack or library. This package does reference packit or libcnb/libpak so it can be used from either style buildpack.

```
import "github.com/paketo-buildpacks/source-removal/logic"
```

Then you can run `logic.Exclude("foo/*")` or `logic.Include("foo/*")`.
6 changes: 4 additions & 2 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ package sourceremoval
import (
"os"

"github.com/paketo-buildpacks/source-removal/logic"

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

func Build() packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
if includeVal, ok := os.LookupEnv("BP_INCLUDE_FILES"); ok {
err := Include(context.WorkingDir, includeVal)
err := logic.Include(context.WorkingDir, includeVal)
if err != nil {
return packit.BuildResult{}, err
}
}

if excludeVal, ok := os.LookupEnv("BP_EXCLUDE_FILES"); ok {
err := Exclude(context.WorkingDir, excludeVal)
err := logic.Exclude(context.WorkingDir, excludeVal)
if err != nil {
return packit.BuildResult{}, err
}
Expand Down
2 changes: 0 additions & 2 deletions init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ func TestUnitSourceRemoval(t *testing.T) {
suite := spec.New("source-removal", spec.Report(report.Terminal{}))
suite("Build", testBuild)
suite("Detect", testDetect)
suite("Exclude", testExclude)
suite("Include", testInclude)
suite.Run(t)
}
2 changes: 1 addition & 1 deletion exclude.go → logic/exclude.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sourceremoval
package logic

import (
"os"
Expand Down
10 changes: 5 additions & 5 deletions exclude_test.go → logic/exclude_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package sourceremoval_test
package logic_test

import (
"os"
"path/filepath"
"testing"

sourceremoval "github.com/paketo-buildpacks/source-removal"
"github.com/paketo-buildpacks/source-removal/logic"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -44,7 +44,7 @@ func testExclude(t *testing.T, context spec.G, it spec.S) {
})

it("returns a result that deletes the contents of the working directroy that were specified", func() {
err := sourceremoval.Exclude(workingDir, "some-dir/some-other-dir/*:some-file")
err := logic.Exclude(workingDir, "some-dir/some-other-dir/*:some-file")
Expect(err).NotTo(HaveOccurred())

Expect(workingDir).To(BeADirectory())
Expand All @@ -67,14 +67,14 @@ func testExclude(t *testing.T, context spec.G, it spec.S) {
})

it("returns an error", func() {
err := sourceremoval.Exclude(workingDir, "some-dir/some-other-dir/*:some-file")
err := logic.Exclude(workingDir, "some-dir/some-other-dir/*:some-file")
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})

context("when there is a malformed glob in include", func() {
it("returns an error", func() {
err := sourceremoval.Exclude(workingDir, `\`)
err := logic.Exclude(workingDir, `\`)
Expect(err).To(MatchError(ContainSubstring("syntax error in pattern")))
})
})
Expand Down
2 changes: 1 addition & 1 deletion include.go → logic/include.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sourceremoval
package logic

import (
"fmt"
Expand Down
10 changes: 5 additions & 5 deletions include_test.go → logic/include_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package sourceremoval_test
package logic_test

import (
"os"
"path/filepath"
"testing"

sourceremoval "github.com/paketo-buildpacks/source-removal"
"github.com/paketo-buildpacks/source-removal/logic"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -36,7 +36,7 @@ func testInclude(t *testing.T, context spec.G, it spec.S) {

context("Include", func() {
it("returns a result that deletes the contents of the working directroy except for the file that are meant to kept", func() {
err := sourceremoval.Include(workingDir, "some-dir/some-other-dir/*:some-file")
err := logic.Include(workingDir, "some-dir/some-other-dir/*:some-file")
Expect(err).NotTo(HaveOccurred())

Expect(workingDir).To(BeADirectory())
Expand All @@ -59,14 +59,14 @@ func testInclude(t *testing.T, context spec.G, it spec.S) {
})

it("returns an error", func() {
err := sourceremoval.Include(workingDir, "some-dir/some-other-dir/*:some-file")
err := logic.Include(workingDir, "some-dir/some-other-dir/*:some-file")
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})

context("when there is a malformed glob in include", func() {
it("returns an error", func() {
err := sourceremoval.Include(workingDir, `\`)
err := logic.Include(workingDir, `\`)
Expect(err).To(MatchError(ContainSubstring("syntax error in pattern")))
})
})
Expand Down
15 changes: 15 additions & 0 deletions logic/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package logic_test

import (
"testing"

"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
)

func TestUnitSourceRemoval(t *testing.T) {
suite := spec.New("source-removal", spec.Report(report.Terminal{}))
suite("Exclude", testExclude)
suite("Include", testInclude)
suite.Run(t)
}

0 comments on commit 0f66735

Please sign in to comment.