github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/commands/set_default_registry_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  
    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 TestSetDefaultRegistry(t *testing.T) {
    20  	color.Disable(true)
    21  	defer color.Disable(false)
    22  
    23  	spec.Run(t, "SetDefaultRegistryCommand", testSetDefaultRegistryCommand, spec.Parallel(), spec.Report(report.Terminal{}))
    24  }
    25  
    26  func testSetDefaultRegistryCommand(t *testing.T, when spec.G, it spec.S) {
    27  	when("#SetDefaultRegistry", func() {
    28  		var (
    29  			outBuf     bytes.Buffer
    30  			logger     = logging.NewLogWithWriters(&outBuf, &outBuf)
    31  			tmpDir     string
    32  			configFile string
    33  			assert     = h.NewAssertionManager(t)
    34  		)
    35  
    36  		it.Before(func() {
    37  			var err error
    38  			tmpDir, err = os.MkdirTemp("", "pack-home-*")
    39  			assert.Nil(err)
    40  
    41  			configFile = filepath.Join(tmpDir, "config.toml")
    42  		})
    43  
    44  		it.After(func() {
    45  			_ = os.RemoveAll(tmpDir)
    46  		})
    47  
    48  		it("should set the default registry", func() {
    49  			cfg := config.Config{
    50  				Registries: []config.Registry{
    51  					{
    52  						Name: "myregistry",
    53  						URL:  "https://github.com/buildpacks/registry-index",
    54  						Type: "github",
    55  					},
    56  				},
    57  			}
    58  			command := commands.SetDefaultRegistry(logger, cfg, configFile)
    59  			command.SetArgs([]string{"myregistry"})
    60  			assert.Succeeds(command.Execute())
    61  
    62  			cfg, err := config.Read(configFile)
    63  			assert.Nil(err)
    64  
    65  			assert.Equal(cfg.DefaultRegistryName, "myregistry")
    66  			assert.Contains(outBuf.String(), "has been deprecated, please use 'pack config registries default'")
    67  		})
    68  
    69  		it("should fail if no corresponding registry exists", func() {
    70  			command := commands.SetDefaultRegistry(logger, config.Config{}, configFile)
    71  			command.SetArgs([]string{"myregistry"})
    72  			assert.Error(command.Execute())
    73  
    74  			output := outBuf.String()
    75  			h.AssertContains(t, output, "no registry with the name 'myregistry' exists")
    76  		})
    77  	})
    78  }