github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/fakes/fake_buildpack.go (about) 1 package fakes 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "path/filepath" 8 9 "github.com/BurntSushi/toml" 10 11 "github.com/buildpacks/pack/pkg/archive" 12 "github.com/buildpacks/pack/pkg/buildpack" 13 "github.com/buildpacks/pack/pkg/dist" 14 ) 15 16 type fakeBuildpack struct { 17 descriptor dist.BuildpackDescriptor 18 chmod int64 19 options []FakeBuildpackOption 20 } 21 22 type fakeBuildpackConfig struct { 23 // maping of extrafilename to stringified contents 24 ExtraFiles map[string]string 25 OpenError error 26 } 27 28 func newFakeBuildpackConfig() *fakeBuildpackConfig { 29 return &fakeBuildpackConfig{ExtraFiles: map[string]string{}} 30 } 31 32 type FakeBuildpackOption func(*fakeBuildpackConfig) 33 34 func WithExtraBuildpackContents(filename, contents string) FakeBuildpackOption { 35 return func(f *fakeBuildpackConfig) { 36 f.ExtraFiles[filename] = contents 37 } 38 } 39 40 func WithBpOpenError(err error) FakeBuildpackOption { 41 return func(f *fakeBuildpackConfig) { 42 f.OpenError = err 43 } 44 } 45 46 // NewFakeBuildpack creates a fake buildpack with contents: 47 // 48 // \_ /cnb/buildpacks/{ID} 49 // \_ /cnb/buildpacks/{ID}/{version} 50 // \_ /cnb/buildpacks/{ID}/{version}/buildpack.toml 51 // \_ /cnb/buildpacks/{ID}/{version}/bin 52 // \_ /cnb/buildpacks/{ID}/{version}/bin/build 53 // build-contents 54 // \_ /cnb/buildpacks/{ID}/{version}/bin/detect 55 // detect-contents 56 func NewFakeBuildpack(descriptor dist.BuildpackDescriptor, chmod int64, options ...FakeBuildpackOption) (buildpack.BuildModule, error) { 57 return &fakeBuildpack{ 58 descriptor: descriptor, 59 chmod: chmod, 60 options: options, 61 }, nil 62 } 63 64 func (b *fakeBuildpack) Descriptor() buildpack.Descriptor { 65 return &b.descriptor 66 } 67 68 func (b *fakeBuildpack) Open() (io.ReadCloser, error) { 69 fConfig := newFakeBuildpackConfig() 70 for _, option := range b.options { 71 option(fConfig) 72 } 73 74 if fConfig.OpenError != nil { 75 return nil, fConfig.OpenError 76 } 77 78 buf := &bytes.Buffer{} 79 if err := toml.NewEncoder(buf).Encode(b.descriptor); err != nil { 80 return nil, err 81 } 82 83 tarBuilder := archive.TarBuilder{} 84 ts := archive.NormalizedDateTime 85 tarBuilder.AddDir(fmt.Sprintf("/cnb/buildpacks/%s", b.descriptor.EscapedID()), b.chmod, ts) 86 bpDir := fmt.Sprintf("/cnb/buildpacks/%s/%s", b.descriptor.EscapedID(), b.descriptor.Info().Version) 87 tarBuilder.AddDir(bpDir, b.chmod, ts) 88 tarBuilder.AddFile(bpDir+"/buildpack.toml", b.chmod, ts, buf.Bytes()) 89 90 if len(b.descriptor.Order()) == 0 { 91 tarBuilder.AddDir(bpDir+"/bin", b.chmod, ts) 92 tarBuilder.AddFile(bpDir+"/bin/build", b.chmod, ts, []byte("build-contents")) 93 tarBuilder.AddFile(bpDir+"/bin/detect", b.chmod, ts, []byte("detect-contents")) 94 } 95 96 for extraFilename, extraContents := range fConfig.ExtraFiles { 97 tarBuilder.AddFile(filepath.Join(bpDir, extraFilename), b.chmod, ts, []byte(extraContents)) 98 } 99 100 return tarBuilder.Reader(archive.DefaultTarWriterFactory()), nil 101 }