github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/commands/buildpack_new_test.go (about) 1 package commands_test 2 3 import ( 4 "bytes" 5 "os" 6 "path/filepath" 7 "runtime" 8 "testing" 9 10 "github.com/buildpacks/pack/pkg/client" 11 "github.com/buildpacks/pack/pkg/dist" 12 "github.com/buildpacks/pack/pkg/logging" 13 14 "github.com/golang/mock/gomock" 15 "github.com/heroku/color" 16 "github.com/sclevine/spec" 17 "github.com/sclevine/spec/report" 18 "github.com/spf13/cobra" 19 20 "github.com/buildpacks/pack/internal/commands" 21 "github.com/buildpacks/pack/internal/commands/testmocks" 22 h "github.com/buildpacks/pack/testhelpers" 23 ) 24 25 func TestBuildpackNewCommand(t *testing.T) { 26 color.Disable(true) 27 defer color.Disable(false) 28 spec.Run(t, "BuildpackNewCommand", testBuildpackNewCommand, spec.Parallel(), spec.Report(report.Terminal{})) 29 } 30 31 func testBuildpackNewCommand(t *testing.T, when spec.G, it spec.S) { 32 var ( 33 command *cobra.Command 34 logger *logging.LogWithWriters 35 outBuf bytes.Buffer 36 mockController *gomock.Controller 37 mockClient *testmocks.MockPackClient 38 tmpDir string 39 ) 40 targets := []dist.Target{{ 41 OS: runtime.GOOS, 42 Arch: runtime.GOARCH, 43 }} 44 45 it.Before(func() { 46 var err error 47 tmpDir, err = os.MkdirTemp("", "build-test") 48 h.AssertNil(t, err) 49 50 logger = logging.NewLogWithWriters(&outBuf, &outBuf) 51 mockController = gomock.NewController(t) 52 mockClient = testmocks.NewMockPackClient(mockController) 53 54 command = commands.BuildpackNew(logger, mockClient) 55 }) 56 57 it.After(func() { 58 os.RemoveAll(tmpDir) 59 }) 60 61 when("BuildpackNew#Execute", func() { 62 it("uses the args to generate artifacts", func() { 63 mockClient.EXPECT().NewBuildpack(gomock.Any(), client.NewBuildpackOptions{ 64 API: "0.8", 65 ID: "example/some-cnb", 66 Path: filepath.Join(tmpDir, "some-cnb"), 67 Version: "1.0.0", 68 Targets: targets, 69 }).Return(nil).MaxTimes(1) 70 71 path := filepath.Join(tmpDir, "some-cnb") 72 command.SetArgs([]string{"--path", path, "example/some-cnb"}) 73 74 err := command.Execute() 75 h.AssertNil(t, err) 76 }) 77 78 it("stops if the directory already exists", func() { 79 err := os.MkdirAll(tmpDir, 0600) 80 h.AssertNil(t, err) 81 82 command.SetArgs([]string{"--path", tmpDir, "example/some-cnb"}) 83 err = command.Execute() 84 h.AssertNotNil(t, err) 85 h.AssertContains(t, outBuf.String(), "ERROR: directory") 86 }) 87 88 when("target flag is specified, ", func() { 89 it("it uses target to generate artifacts", func() { 90 mockClient.EXPECT().NewBuildpack(gomock.Any(), client.NewBuildpackOptions{ 91 API: "0.8", 92 ID: "example/targets", 93 Path: filepath.Join(tmpDir, "targets"), 94 Version: "1.0.0", 95 Targets: []dist.Target{{ 96 OS: "linux", 97 Arch: "arm", 98 ArchVariant: "v6", 99 Distributions: []dist.Distribution{{ 100 Name: "ubuntu", 101 Version: "14.04", 102 }}, 103 }}, 104 }).Return(nil).MaxTimes(1) 105 106 path := filepath.Join(tmpDir, "targets") 107 command.SetArgs([]string{"--path", path, "example/targets", "--targets", "linux/arm/v6:ubuntu@14.04"}) 108 109 err := command.Execute() 110 h.AssertNil(t, err) 111 }) 112 it("it should show error when invalid [os]/[arch] passed", func() { 113 mockClient.EXPECT().NewBuildpack(gomock.Any(), client.NewBuildpackOptions{ 114 API: "0.8", 115 ID: "example/targets", 116 Path: filepath.Join(tmpDir, "targets"), 117 Version: "1.0.0", 118 Targets: []dist.Target{{ 119 OS: "os", 120 Arch: "arm", 121 ArchVariant: "v6", 122 Distributions: []dist.Distribution{{ 123 Name: "ubuntu", 124 Version: "14.04", 125 }, { 126 Name: "ubuntu", 127 Version: "16.04", 128 }}, 129 }}, 130 }).Return(nil).MaxTimes(1) 131 132 path := filepath.Join(tmpDir, "targets") 133 command.SetArgs([]string{"--path", path, "example/targets", "--targets", "os/arm/v6:ubuntu@14.04@16.04"}) 134 135 err := command.Execute() 136 h.AssertNotNil(t, err) 137 }) 138 when("it should", func() { 139 it("support format [os][/arch][/variant]:[name@version];[some-name@version]", func() { 140 mockClient.EXPECT().NewBuildpack(gomock.Any(), client.NewBuildpackOptions{ 141 API: "0.8", 142 ID: "example/targets", 143 Path: filepath.Join(tmpDir, "targets"), 144 Version: "1.0.0", 145 Targets: []dist.Target{ 146 { 147 OS: "linux", 148 Arch: "arm", 149 ArchVariant: "v6", 150 Distributions: []dist.Distribution{ 151 { 152 Name: "ubuntu", 153 Version: "14.04", 154 }, 155 { 156 Name: "debian", 157 Version: "8.10", 158 }, 159 }, 160 }, 161 { 162 OS: "windows", 163 Arch: "amd64", 164 Distributions: []dist.Distribution{ 165 { 166 Name: "windows-nano", 167 Version: "10.0.19041.1415", 168 }, 169 }, 170 }, 171 }, 172 }).Return(nil).MaxTimes(1) 173 174 path := filepath.Join(tmpDir, "targets") 175 command.SetArgs([]string{"--path", path, "example/targets", "--targets", "linux/arm/v6:ubuntu@14.04;debian@8.10", "-t", "windows/amd64:windows-nano@10.0.19041.1415"}) 176 177 err := command.Execute() 178 h.AssertNil(t, err) 179 }) 180 }) 181 when("stacks ", func() { 182 it("flag should show deprecated message when used", func() { 183 mockClient.EXPECT().NewBuildpack(gomock.Any(), client.NewBuildpackOptions{ 184 API: "0.8", 185 ID: "example/stacks", 186 Path: filepath.Join(tmpDir, "stacks"), 187 Version: "1.0.0", 188 Stacks: []dist.Stack{{ 189 ID: "io.buildpacks.stacks.jammy", 190 Mixins: []string{}, 191 }}, 192 }).Return(nil).MaxTimes(1) 193 194 path := filepath.Join(tmpDir, "stacks") 195 output := new(bytes.Buffer) 196 command.SetOut(output) 197 command.SetErr(output) 198 command.SetArgs([]string{"--path", path, "example/stacks", "--stacks", "io.buildpacks.stacks.jammy"}) 199 200 err := command.Execute() 201 h.AssertNil(t, err) 202 h.AssertContains(t, output.String(), "Flag --stacks has been deprecated,") 203 }) 204 }) 205 }) 206 }) 207 }