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

     1  package commands_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	"github.com/sclevine/spec"
     9  	"github.com/sclevine/spec/report"
    10  	"github.com/spf13/cobra"
    11  
    12  	"github.com/buildpacks/pack/internal/commands"
    13  	"github.com/buildpacks/pack/internal/commands/testmocks"
    14  	"github.com/buildpacks/pack/internal/config"
    15  	"github.com/buildpacks/pack/pkg/client"
    16  	"github.com/buildpacks/pack/pkg/logging"
    17  	h "github.com/buildpacks/pack/testhelpers"
    18  )
    19  
    20  func TestPullBuildpackCommand(t *testing.T) {
    21  	spec.Run(t, "PullBuildpackCommand", testPullBuildpackCommand, spec.Parallel(), spec.Report(report.Terminal{}))
    22  }
    23  
    24  func testPullBuildpackCommand(t *testing.T, when spec.G, it spec.S) {
    25  	var (
    26  		command        *cobra.Command
    27  		logger         logging.Logger
    28  		outBuf         bytes.Buffer
    29  		mockController *gomock.Controller
    30  		mockClient     *testmocks.MockPackClient
    31  		cfg            config.Config
    32  	)
    33  
    34  	it.Before(func() {
    35  		logger = logging.NewLogWithWriters(&outBuf, &outBuf)
    36  		mockController = gomock.NewController(t)
    37  		mockClient = testmocks.NewMockPackClient(mockController)
    38  		cfg = config.Config{}
    39  
    40  		command = commands.BuildpackPull(logger, cfg, mockClient)
    41  	})
    42  
    43  	when("#BuildpackPullCommand", func() {
    44  		when("no buildpack is provided", func() {
    45  			it("fails to run", func() {
    46  				err := command.Execute()
    47  				h.AssertError(t, err, "accepts 1 arg")
    48  			})
    49  		})
    50  
    51  		when("buildpack uri is provided", func() {
    52  			it("should work for required args", func() {
    53  				buildpackImage := "buildpack/image"
    54  				opts := client.PullBuildpackOptions{
    55  					URI:          buildpackImage,
    56  					RegistryName: "official",
    57  				}
    58  
    59  				mockClient.EXPECT().
    60  					PullBuildpack(gomock.Any(), opts).
    61  					Return(nil)
    62  
    63  				command.SetArgs([]string{buildpackImage})
    64  				h.AssertNil(t, command.Execute())
    65  			})
    66  		})
    67  	})
    68  }