github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/commands/trust_builder_test.go (about) 1 package commands_test 2 3 import ( 4 "bytes" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/heroku/color" 10 "github.com/sclevine/spec" 11 "github.com/sclevine/spec/report" 12 "github.com/spf13/cobra" 13 14 "github.com/buildpacks/pack/internal/commands" 15 "github.com/buildpacks/pack/internal/config" 16 "github.com/buildpacks/pack/pkg/logging" 17 h "github.com/buildpacks/pack/testhelpers" 18 ) 19 20 func TestTrustBuilderCommand(t *testing.T) { 21 color.Disable(true) 22 defer color.Disable(false) 23 spec.Run(t, "Commands", testTrustBuilderCommand, spec.Random(), spec.Report(report.Terminal{})) 24 } 25 26 func testTrustBuilderCommand(t *testing.T, when spec.G, it spec.S) { 27 var ( 28 command *cobra.Command 29 logger logging.Logger 30 outBuf bytes.Buffer 31 tempPackHome string 32 configPath string 33 ) 34 35 it.Before(func() { 36 var err error 37 38 logger = logging.NewLogWithWriters(&outBuf, &outBuf) 39 tempPackHome, err = os.MkdirTemp("", "pack-home") 40 h.AssertNil(t, err) 41 configPath = filepath.Join(tempPackHome, "config.toml") 42 command = commands.TrustBuilder(logger, config.Config{}, configPath) 43 }) 44 45 it.After(func() { 46 h.AssertNil(t, os.RemoveAll(tempPackHome)) 47 }) 48 49 when("#TrustBuilder", func() { 50 when("no builder is provided", func() { 51 it("prints usage", func() { 52 command.SetArgs([]string{}) 53 h.AssertError(t, command.Execute(), "accepts 1 arg(s)") 54 }) 55 }) 56 57 when("builder is provided", func() { 58 when("builder is not already trusted", func() { 59 it("updates the config", func() { 60 command.SetArgs([]string{"some-builder"}) 61 h.AssertNil(t, command.Execute()) 62 63 b, err := os.ReadFile(configPath) 64 h.AssertNil(t, err) 65 h.AssertContains(t, string(b), `[[trusted-builders]] 66 name = "some-builder"`) 67 }) 68 }) 69 70 when("builder is already trusted", func() { 71 it("does nothing", func() { 72 command.SetArgs([]string{"some-already-trusted-builder"}) 73 h.AssertNil(t, command.Execute()) 74 oldContents, err := os.ReadFile(configPath) 75 h.AssertNil(t, err) 76 77 command.SetArgs([]string{"some-already-trusted-builder"}) 78 h.AssertNil(t, command.Execute()) 79 80 newContents, err := os.ReadFile(configPath) 81 h.AssertNil(t, err) 82 h.AssertEq(t, newContents, oldContents) 83 }) 84 }) 85 86 when("builder is a suggested builder", func() { 87 it("does nothing", func() { 88 h.AssertNil(t, os.WriteFile(configPath, []byte(""), os.ModePerm)) 89 90 command.SetArgs([]string{"paketobuildpacks/builder-jammy-base"}) 91 h.AssertNil(t, command.Execute()) 92 oldContents, err := os.ReadFile(configPath) 93 h.AssertNil(t, err) 94 h.AssertEq(t, string(oldContents), "") 95 }) 96 }) 97 }) 98 }) 99 }