Skip to content

Commit

Permalink
Updates fetchers to create buildpackages
Browse files Browse the repository at this point in the history
  • Loading branch information
ForestEckhardt committed Jun 4, 2024
1 parent bc89e23 commit a57bf55
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 127 deletions.
18 changes: 9 additions & 9 deletions fakes/buildpack_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (

type BuildpackCache struct {
DirCall struct {
sync.Mutex
mutex sync.Mutex
CallCount int
Returns struct {
String string
}
Stub func() string
}
GetCall struct {
sync.Mutex
mutex sync.Mutex
CallCount int
Receives struct {
Key string
Expand All @@ -29,7 +29,7 @@ type BuildpackCache struct {
Stub func(string) (freezer.CacheEntry, bool, error)
}
SetCall struct {
sync.Mutex
mutex sync.Mutex
CallCount int
Receives struct {
Key string
Expand All @@ -43,17 +43,17 @@ type BuildpackCache struct {
}

func (f *BuildpackCache) Dir() string {
f.DirCall.Lock()
defer f.DirCall.Unlock()
f.DirCall.mutex.Lock()
defer f.DirCall.mutex.Unlock()
f.DirCall.CallCount++
if f.DirCall.Stub != nil {
return f.DirCall.Stub()
}
return f.DirCall.Returns.String
}
func (f *BuildpackCache) Get(param1 string) (freezer.CacheEntry, bool, error) {
f.GetCall.Lock()
defer f.GetCall.Unlock()
f.GetCall.mutex.Lock()
defer f.GetCall.mutex.Unlock()
f.GetCall.CallCount++
f.GetCall.Receives.Key = param1
if f.GetCall.Stub != nil {
Expand All @@ -62,8 +62,8 @@ func (f *BuildpackCache) Get(param1 string) (freezer.CacheEntry, bool, error) {
return f.GetCall.Returns.CacheEntry, f.GetCall.Returns.Bool, f.GetCall.Returns.Error
}
func (f *BuildpackCache) Set(param1 string, param2 freezer.CacheEntry) error {
f.SetCall.Lock()
defer f.SetCall.Unlock()
f.SetCall.mutex.Lock()
defer f.SetCall.mutex.Unlock()
f.SetCall.CallCount++
f.SetCall.Receives.Key = param1
f.SetCall.Receives.CachedEntry = param2
Expand Down
6 changes: 3 additions & 3 deletions fakes/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type Executable struct {
ExecuteCall struct {
sync.Mutex
mutex sync.Mutex
CallCount int
Receives struct {
Execution pexec.Execution
Expand All @@ -21,8 +21,8 @@ type Executable struct {
}

func (f *Executable) Execute(param1 pexec.Execution) error {
f.ExecuteCall.Lock()
defer f.ExecuteCall.Unlock()
f.ExecuteCall.mutex.Lock()
defer f.ExecuteCall.mutex.Unlock()
f.ExecuteCall.CallCount++
f.ExecuteCall.Receives.Execution = param1
if f.ExecuteCall.Stub != nil {
Expand Down
18 changes: 9 additions & 9 deletions fakes/git_release_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type GitReleaseFetcher struct {
GetCall struct {
sync.Mutex
mutex sync.Mutex
CallCount int
Receives struct {
Org string
Expand All @@ -22,7 +22,7 @@ type GitReleaseFetcher struct {
Stub func(string, string) (github.Release, error)
}
GetReleaseAssetCall struct {
sync.Mutex
mutex sync.Mutex
CallCount int
Receives struct {
Asset github.ReleaseAsset
Expand All @@ -34,7 +34,7 @@ type GitReleaseFetcher struct {
Stub func(github.ReleaseAsset) (io.ReadCloser, error)
}
GetReleaseTarballCall struct {
sync.Mutex
mutex sync.Mutex
CallCount int
Receives struct {
Url string
Expand All @@ -48,8 +48,8 @@ type GitReleaseFetcher struct {
}

func (f *GitReleaseFetcher) Get(param1 string, param2 string) (github.Release, error) {
f.GetCall.Lock()
defer f.GetCall.Unlock()
f.GetCall.mutex.Lock()
defer f.GetCall.mutex.Unlock()
f.GetCall.CallCount++
f.GetCall.Receives.Org = param1
f.GetCall.Receives.Repo = param2
Expand All @@ -59,8 +59,8 @@ func (f *GitReleaseFetcher) Get(param1 string, param2 string) (github.Release, e
return f.GetCall.Returns.Release, f.GetCall.Returns.Error
}
func (f *GitReleaseFetcher) GetReleaseAsset(param1 github.ReleaseAsset) (io.ReadCloser, error) {
f.GetReleaseAssetCall.Lock()
defer f.GetReleaseAssetCall.Unlock()
f.GetReleaseAssetCall.mutex.Lock()
defer f.GetReleaseAssetCall.mutex.Unlock()
f.GetReleaseAssetCall.CallCount++
f.GetReleaseAssetCall.Receives.Asset = param1
if f.GetReleaseAssetCall.Stub != nil {
Expand All @@ -69,8 +69,8 @@ func (f *GitReleaseFetcher) GetReleaseAsset(param1 github.ReleaseAsset) (io.Read
return f.GetReleaseAssetCall.Returns.ReadCloser, f.GetReleaseAssetCall.Returns.Error
}
func (f *GitReleaseFetcher) GetReleaseTarball(param1 string) (io.ReadCloser, error) {
f.GetReleaseTarballCall.Lock()
defer f.GetReleaseTarballCall.Unlock()
f.GetReleaseTarballCall.mutex.Lock()
defer f.GetReleaseTarballCall.mutex.Unlock()
f.GetReleaseTarballCall.CallCount++
f.GetReleaseTarballCall.Receives.Url = param1
if f.GetReleaseTarballCall.Stub != nil {
Expand Down
6 changes: 3 additions & 3 deletions fakes/namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "sync"

type Namer struct {
RandomNameCall struct {
sync.Mutex
mutex sync.Mutex
CallCount int
Receives struct {
Name string
Expand All @@ -18,8 +18,8 @@ type Namer struct {
}

func (f *Namer) RandomName(param1 string) (string, error) {
f.RandomNameCall.Lock()
defer f.RandomNameCall.Unlock()
f.RandomNameCall.mutex.Lock()
defer f.RandomNameCall.mutex.Unlock()
f.RandomNameCall.CallCount++
f.RandomNameCall.Receives.Name = param1
if f.RandomNameCall.Stub != nil {
Expand Down
6 changes: 3 additions & 3 deletions fakes/packager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "sync"

type Packager struct {
ExecuteCall struct {
sync.Mutex
mutex sync.Mutex
CallCount int
Receives struct {
BuildpackDir string
Expand All @@ -20,8 +20,8 @@ type Packager struct {
}

func (f *Packager) Execute(param1 string, param2 string, param3 string, param4 bool) error {
f.ExecuteCall.Lock()
defer f.ExecuteCall.Unlock()
f.ExecuteCall.mutex.Lock()
defer f.ExecuteCall.mutex.Unlock()
f.ExecuteCall.CallCount++
f.ExecuteCall.Receives.BuildpackDir = param1
f.ExecuteCall.Receives.Output = param2
Expand Down
15 changes: 0 additions & 15 deletions file_system.go

This file was deleted.

29 changes: 0 additions & 29 deletions file_system_test.go

This file was deleted.

1 change: 0 additions & 1 deletion init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var gitToken string
func TestFreezer(t *testing.T) {
suite := spec.New("freezer", spec.Report(report.Terminal{}))
suite("CacheManager", testCacheManager)
suite("FileSystem", testFileSystem)
suite("LocalFetcher", testLocalFetcher)
suite("PackingTools", testPackingTools)
suite("RandomName", testRandomName)
Expand Down
2 changes: 1 addition & 1 deletion local_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (l LocalFetcher) Get(buildpack LocalBuildpack) (string, error) {
return "", fmt.Errorf("random name generation failed: %w", err)
}

path := filepath.Join(buildpackCacheDir, fmt.Sprintf("%s.tgz", name))
path := filepath.Join(buildpackCacheDir, fmt.Sprintf("%s.cnb", name))

cachedEntry, exist, err := l.buildpackCache.Get(key)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions local_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func testLocalFetcher(t *testing.T, context spec.G, it spec.S) {
Expect(namer.RandomNameCall.Receives.Name).To(Equal("some-buildpack"))

Expect(packager.ExecuteCall.Receives.BuildpackDir).To(Equal("path/to/buildpack"))
Expect(packager.ExecuteCall.Receives.Output).To(Equal(filepath.Join(cacheDir, "some-buildpack", "some-buildpack-random-string.tgz")))
Expect(packager.ExecuteCall.Receives.Output).To(Equal(filepath.Join(cacheDir, "some-buildpack", "some-buildpack-random-string.cnb")))
Expect(packager.ExecuteCall.Receives.Version).To(Equal("some-version"))
Expect(packager.ExecuteCall.Receives.Cached).To(BeFalse())

Expect(buildpackCache.SetCall.CallCount).To(Equal(1))

Expect(uri).To(Equal(filepath.Join(cacheDir, "some-buildpack", "some-buildpack-random-string.tgz")))
Expect(uri).To(Equal(filepath.Join(cacheDir, "some-buildpack", "some-buildpack-random-string.cnb")))
})
})

Expand Down
47 changes: 43 additions & 4 deletions packing_tools.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package freezer

import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/paketo-buildpacks/packit/v2/pexec"
)
Expand All @@ -13,12 +15,16 @@ type Executable interface {
}

type PackingTools struct {
jam Executable
jam Executable
pack Executable
tempOutput func(dir string, pattern string) (string, error)
}

func NewPackingTools() PackingTools {
return PackingTools{
jam: pexec.NewExecutable("jam"),
jam: pexec.NewExecutable("jam"),
pack: pexec.NewExecutable("pack"),
tempOutput: os.MkdirTemp,
}
}

Expand All @@ -27,19 +33,52 @@ func (p PackingTools) WithExecutable(executable Executable) PackingTools {
return p
}

func (p PackingTools) WithPack(pack Executable) PackingTools {
p.pack = pack
return p
}

func (p PackingTools) WithTempOutput(tempOutput func(string, string) (string, error)) PackingTools {
p.tempOutput = tempOutput
return p
}

func (p PackingTools) Execute(buildpackDir, output, version string, cached bool) error {
jamOutput, err := p.tempOutput("", "")
if err != nil {
return err
}
defer os.RemoveAll(jamOutput)

args := []string{
"pack",
"--buildpack", filepath.Join(buildpackDir, "buildpack.toml"),
"--output", output,
"--output", filepath.Join(jamOutput, fmt.Sprintf("%s.tgz", version)),
"--version", version,
}

if cached {
args = append(args, "--offline")
}

return p.jam.Execute(pexec.Execution{
err = p.jam.Execute(pexec.Execution{
Args: args,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
if err != nil {
return err
}

args = []string{
"buildpack", "package",
output,
"--path", filepath.Join(jamOutput, fmt.Sprintf("%s.tgz", version)),
"--format", "file",
"--target", fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}

return p.pack.Execute(pexec.Execution{
Args: args,
Stdout: os.Stdout,
Stderr: os.Stderr,
Expand Down
Loading

0 comments on commit a57bf55

Please sign in to comment.