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

     1  package commands_test
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/heroku/color"
     9  	"github.com/sclevine/spec"
    10  	"github.com/sclevine/spec/report"
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/buildpacks/pack/internal/commands"
    14  	"github.com/buildpacks/pack/internal/config"
    15  	"github.com/buildpacks/pack/pkg/logging"
    16  	h "github.com/buildpacks/pack/testhelpers"
    17  )
    18  
    19  func TestListTrustedBuildersCommand(t *testing.T) {
    20  	color.Disable(true)
    21  	defer color.Disable(false)
    22  	spec.Run(t, "Commands", testListTrustedBuildersCommand, spec.Random(), spec.Report(report.Terminal{}))
    23  }
    24  
    25  func testListTrustedBuildersCommand(t *testing.T, when spec.G, it spec.S) {
    26  	var (
    27  		command      *cobra.Command
    28  		logger       logging.Logger
    29  		outBuf       bytes.Buffer
    30  		tempPackHome string
    31  	)
    32  
    33  	it.Before(func() {
    34  		var err error
    35  
    36  		logger = logging.NewLogWithWriters(&outBuf, &outBuf)
    37  		command = commands.ListTrustedBuilders(logger, config.Config{})
    38  
    39  		tempPackHome, err = os.MkdirTemp("", "pack-home")
    40  		h.AssertNil(t, err)
    41  		h.AssertNil(t, os.Setenv("PACK_HOME", tempPackHome))
    42  	})
    43  
    44  	it.After(func() {
    45  		h.AssertNil(t, os.Unsetenv("PACK_HOME"))
    46  		h.AssertNil(t, os.RemoveAll(tempPackHome))
    47  	})
    48  
    49  	when("#ListTrustedBuilders", func() {
    50  		it("succeeds", func() {
    51  			h.AssertNil(t, command.Execute())
    52  		})
    53  
    54  		it("shows header", func() {
    55  			h.AssertNil(t, command.Execute())
    56  
    57  			h.AssertContains(t, outBuf.String(), "Trusted Builders:")
    58  		})
    59  
    60  		it("shows suggested builders and locally trusted builder in alphabetical order", func() {
    61  			builderName := "great-builder-" + h.RandString(8)
    62  
    63  			h.AssertNil(t, command.Execute())
    64  			h.AssertNotContains(t, outBuf.String(), builderName)
    65  			h.AssertContainsAllInOrder(t,
    66  				outBuf,
    67  				"gcr.io/buildpacks/builder:v1",
    68  				"heroku/builder:20",
    69  				"heroku/builder:22",
    70  				"paketobuildpacks/builder-jammy-base",
    71  				"paketobuildpacks/builder-jammy-full",
    72  				"paketobuildpacks/builder-jammy-tiny",
    73  			)
    74  
    75  			listTrustedBuildersCommand := commands.ListTrustedBuilders(
    76  				logger,
    77  				config.Config{
    78  					TrustedBuilders: []config.TrustedBuilder{{Name: builderName}},
    79  				},
    80  			)
    81  
    82  			outBuf.Reset()
    83  
    84  			h.AssertNil(t, listTrustedBuildersCommand.Execute())
    85  
    86  			h.AssertContainsAllInOrder(t,
    87  				outBuf,
    88  				"gcr.io/buildpacks/builder:v1",
    89  				builderName,
    90  				"heroku/builder:20",
    91  				"heroku/builder:22",
    92  				"paketobuildpacks/builder-jammy-base",
    93  				"paketobuildpacks/builder-jammy-full",
    94  				"paketobuildpacks/builder-jammy-tiny",
    95  			)
    96  		})
    97  	})
    98  }