github.com/YousefHaggyHeroku/pack@v1.5.5/internal/commands/remove_registry_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/heroku/color"
    11  	"github.com/sclevine/spec"
    12  	"github.com/sclevine/spec/report"
    13  
    14  	"github.com/YousefHaggyHeroku/pack/internal/commands"
    15  	"github.com/YousefHaggyHeroku/pack/internal/config"
    16  	ilogging "github.com/YousefHaggyHeroku/pack/internal/logging"
    17  	h "github.com/YousefHaggyHeroku/pack/testhelpers"
    18  )
    19  
    20  func TestRemoveRegistry(t *testing.T) {
    21  	color.Disable(true)
    22  	defer color.Disable(false)
    23  
    24  	spec.Run(t, "RemoveRegistryCommand", testRemoveRegistryCommand, spec.Parallel(), spec.Report(report.Terminal{}))
    25  }
    26  
    27  func testRemoveRegistryCommand(t *testing.T, when spec.G, it spec.S) {
    28  	when("#RemoveRegistry", func() {
    29  		var (
    30  			outBuf     bytes.Buffer
    31  			logger     = ilogging.NewLogWithWriters(&outBuf, &outBuf)
    32  			tmpDir     string
    33  			configFile string
    34  			cfg        config.Config
    35  			assert     = h.NewAssertionManager(t)
    36  		)
    37  
    38  		it.Before(func() {
    39  			var err error
    40  			tmpDir, err = ioutil.TempDir("", "pack-home-*")
    41  			assert.Nil(err)
    42  
    43  			cfg = config.Config{
    44  				DefaultRegistryName: "buildpack-registry",
    45  				Registries: []config.Registry{
    46  					{
    47  						Name: "buildpack-registry",
    48  						URL:  "https://github.com/buildpacks/registry-index",
    49  						Type: "github",
    50  					},
    51  					{
    52  						Name: "elbandito-registry",
    53  						URL:  "https://github.com/elbandito/registry-index",
    54  						Type: "github",
    55  					},
    56  				},
    57  			}
    58  
    59  			configFile = filepath.Join(tmpDir, "config.toml")
    60  			err = config.Write(cfg, configFile)
    61  			assert.Nil(err)
    62  		})
    63  
    64  		it.After(func() {
    65  			_ = os.RemoveAll(tmpDir)
    66  		})
    67  
    68  		it("should remove the registry", func() {
    69  			command := commands.RemoveRegistry(logger, cfg, configFile)
    70  			command.SetArgs([]string{"elbandito-registry"})
    71  			assert.Succeeds(command.Execute())
    72  
    73  			newCfg, err := config.Read(configFile)
    74  			assert.Nil(err)
    75  
    76  			assert.Equal(newCfg, config.Config{
    77  				DefaultRegistryName: "buildpack-registry",
    78  				Registries: []config.Registry{
    79  					{
    80  						Name: "buildpack-registry",
    81  						URL:  "https://github.com/buildpacks/registry-index",
    82  						Type: "github",
    83  					},
    84  				},
    85  			})
    86  		})
    87  
    88  		it("should remove the registry and matching default registry name", func() {
    89  			command := commands.RemoveRegistry(logger, cfg, configFile)
    90  			command.SetArgs([]string{"buildpack-registry"})
    91  			assert.Succeeds(command.Execute())
    92  
    93  			newCfg, err := config.Read(configFile)
    94  			assert.Nil(err)
    95  
    96  			assert.Equal(newCfg, config.Config{
    97  				DefaultRegistryName: config.OfficialRegistryName,
    98  				Registries: []config.Registry{
    99  					{
   100  						Name: "elbandito-registry",
   101  						URL:  "https://github.com/elbandito/registry-index",
   102  						Type: "github",
   103  					},
   104  				},
   105  			})
   106  		})
   107  
   108  		it("should return error when registry does NOT already exist", func() {
   109  			command := commands.RemoveRegistry(logger, cfg, configFile)
   110  			command.SetArgs([]string{"missing-registry"})
   111  			assert.Error(command.Execute())
   112  
   113  			output := outBuf.String()
   114  			h.AssertContains(t, output, "registry 'missing-registry' does not exist")
   115  		})
   116  
   117  		it("should throw error when registry name is official", func() {
   118  			command := commands.RemoveRegistry(logger, config.Config{}, configFile)
   119  			command.SetArgs([]string{"official"})
   120  			assert.Error(command.Execute())
   121  
   122  			output := outBuf.String()
   123  			h.AssertContains(t, output, "'official' is a reserved registry name, please provide a different registry")
   124  		})
   125  	})
   126  }