github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/client/new_buildpack_test.go (about) 1 package client_test 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "runtime" 8 "testing" 9 10 "github.com/heroku/color" 11 "github.com/pelletier/go-toml" 12 "github.com/sclevine/spec" 13 "github.com/sclevine/spec/report" 14 15 "github.com/buildpacks/pack/pkg/client" 16 "github.com/buildpacks/pack/pkg/dist" 17 h "github.com/buildpacks/pack/testhelpers" 18 ) 19 20 func TestNewBuildpack(t *testing.T) { 21 color.Disable(true) 22 defer color.Disable(false) 23 spec.Run(t, "NewBuildpack", testNewBuildpack, spec.Parallel(), spec.Report(report.Terminal{})) 24 } 25 26 func testNewBuildpack(t *testing.T, when spec.G, it spec.S) { 27 var ( 28 subject *client.Client 29 tmpDir string 30 ) 31 32 it.Before(func() { 33 var err error 34 35 tmpDir, err = os.MkdirTemp("", "new-buildpack-test") 36 h.AssertNil(t, err) 37 38 subject, err = client.NewClient() 39 h.AssertNil(t, err) 40 }) 41 42 it.After(func() { 43 h.AssertNil(t, os.RemoveAll(tmpDir)) 44 }) 45 46 when("#NewBuildpack", func() { 47 it("should create bash scripts", func() { 48 err := subject.NewBuildpack(context.TODO(), client.NewBuildpackOptions{ 49 API: "0.4", 50 Path: tmpDir, 51 ID: "example/my-cnb", 52 Version: "0.0.0", 53 Stacks: []dist.Stack{ 54 { 55 ID: "some-stack", 56 Mixins: []string{"some-mixin"}, 57 }, 58 }, 59 }) 60 h.AssertNil(t, err) 61 62 info, err := os.Stat(filepath.Join(tmpDir, "bin/build")) 63 h.AssertFalse(t, os.IsNotExist(err)) 64 if runtime.GOOS != "windows" { 65 h.AssertTrue(t, info.Mode()&0100 != 0) 66 } 67 68 info, err = os.Stat(filepath.Join(tmpDir, "bin/detect")) 69 h.AssertFalse(t, os.IsNotExist(err)) 70 if runtime.GOOS != "windows" { 71 h.AssertTrue(t, info.Mode()&0100 != 0) 72 } 73 74 assertBuildpackToml(t, tmpDir, "example/my-cnb") 75 }) 76 77 when("files exist", func() { 78 it.Before(func() { 79 var err error 80 81 err = os.MkdirAll(filepath.Join(tmpDir, "bin"), 0755) 82 h.AssertNil(t, err) 83 err = os.WriteFile(filepath.Join(tmpDir, "buildpack.toml"), []byte("expected value"), 0655) 84 h.AssertNil(t, err) 85 err = os.WriteFile(filepath.Join(tmpDir, "bin", "build"), []byte("expected value"), 0755) 86 h.AssertNil(t, err) 87 err = os.WriteFile(filepath.Join(tmpDir, "bin", "detect"), []byte("expected value"), 0755) 88 h.AssertNil(t, err) 89 }) 90 91 it("should not clobber files that exist", func() { 92 err := subject.NewBuildpack(context.TODO(), client.NewBuildpackOptions{ 93 API: "0.4", 94 Path: tmpDir, 95 ID: "example/my-cnb", 96 Version: "0.0.0", 97 Stacks: []dist.Stack{ 98 { 99 ID: "some-stack", 100 Mixins: []string{"some-mixin"}, 101 }, 102 }, 103 }) 104 h.AssertNil(t, err) 105 106 content, err := os.ReadFile(filepath.Join(tmpDir, "buildpack.toml")) 107 h.AssertNil(t, err) 108 h.AssertEq(t, content, []byte("expected value")) 109 110 content, err = os.ReadFile(filepath.Join(tmpDir, "bin", "build")) 111 h.AssertNil(t, err) 112 h.AssertEq(t, content, []byte("expected value")) 113 114 content, err = os.ReadFile(filepath.Join(tmpDir, "bin", "detect")) 115 h.AssertNil(t, err) 116 h.AssertEq(t, content, []byte("expected value")) 117 }) 118 }) 119 }) 120 } 121 122 func assertBuildpackToml(t *testing.T, path string, id string) { 123 buildpackTOML := filepath.Join(path, "buildpack.toml") 124 _, err := os.Stat(buildpackTOML) 125 h.AssertFalse(t, os.IsNotExist(err)) 126 127 f, err := os.Open(buildpackTOML) 128 h.AssertNil(t, err) 129 var buildpackDescriptor dist.BuildpackDescriptor 130 err = toml.NewDecoder(f).Decode(&buildpackDescriptor) 131 h.AssertNil(t, err) 132 defer f.Close() 133 134 h.AssertEq(t, buildpackDescriptor.Info().ID, "example/my-cnb") 135 }