github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/commands/manifest_remove_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	"github.com/heroku/color"
     9  	"github.com/pkg/errors"
    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/commands/testmocks"
    16  	"github.com/buildpacks/pack/pkg/logging"
    17  	h "github.com/buildpacks/pack/testhelpers"
    18  )
    19  
    20  func TestManifestDeleteCommand(t *testing.T) {
    21  	color.Disable(true)
    22  	defer color.Disable(false)
    23  
    24  	spec.Run(t, "Commands", testManifestDeleteCommand, spec.Random(), spec.Report(report.Terminal{}))
    25  }
    26  
    27  func testManifestDeleteCommand(t *testing.T, when spec.G, it spec.S) {
    28  	var (
    29  		command        *cobra.Command
    30  		logger         *logging.LogWithWriters
    31  		outBuf         bytes.Buffer
    32  		mockController *gomock.Controller
    33  		mockClient     *testmocks.MockPackClient
    34  	)
    35  
    36  	it.Before(func() {
    37  		logger = logging.NewLogWithWriters(&outBuf, &outBuf)
    38  		mockController = gomock.NewController(t)
    39  		mockClient = testmocks.NewMockPackClient(mockController)
    40  		command = commands.ManifestDelete(logger, mockClient)
    41  	})
    42  
    43  	when("args are valid", func() {
    44  		var indexRepoName string
    45  		it.Before(func() {
    46  			indexRepoName = h.NewRandomIndexRepoName()
    47  		})
    48  
    49  		when("index exists", func() {
    50  			when("no extra flags are provided", func() {
    51  				it.Before(func() {
    52  					mockClient.EXPECT().DeleteManifest(
    53  						gomock.Eq([]string{indexRepoName}),
    54  					).Return(nil)
    55  				})
    56  
    57  				it("should delete index", func() {
    58  					command.SetArgs([]string{indexRepoName})
    59  					h.AssertNil(t, command.Execute())
    60  				})
    61  			})
    62  
    63  			when("--help", func() {
    64  				it("should have help flag", func() {
    65  					command.SetArgs([]string{"--help"})
    66  					h.AssertNil(t, command.Execute())
    67  				})
    68  			})
    69  		})
    70  
    71  		when("index does not exist", func() {
    72  			it.Before(func() {
    73  				mockClient.EXPECT().DeleteManifest(
    74  					gomock.Eq([]string{"some-none-existent-index"}),
    75  				).Return(errors.New("image index doesn't exists"))
    76  			})
    77  
    78  			it("should return an error", func() {
    79  				command.SetArgs([]string{"some-none-existent-index"})
    80  				h.AssertNotNil(t, command.Execute())
    81  			})
    82  		})
    83  	})
    84  }