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

     1  package commands_test
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/golang/mock/gomock"
    10  	"github.com/heroku/color"
    11  	"github.com/sclevine/spec"
    12  	"github.com/sclevine/spec/report"
    13  	"github.com/spf13/cobra"
    14  
    15  	"github.com/buildpacks/pack/internal/commands"
    16  	"github.com/buildpacks/pack/internal/commands/testmocks"
    17  	"github.com/buildpacks/pack/internal/config"
    18  	"github.com/buildpacks/pack/pkg/logging"
    19  	h "github.com/buildpacks/pack/testhelpers"
    20  )
    21  
    22  func TestConfigCommand(t *testing.T) {
    23  	color.Disable(true)
    24  	defer color.Disable(false)
    25  	spec.Run(t, "ConfigCommands", testConfigCommand, spec.Random(), spec.Report(report.Terminal{}))
    26  }
    27  
    28  func testConfigCommand(t *testing.T, when spec.G, it spec.S) {
    29  	var (
    30  		command      *cobra.Command
    31  		logger       logging.Logger
    32  		outBuf       bytes.Buffer
    33  		tempPackHome string
    34  		configPath   string
    35  		mockClient   *testmocks.MockPackClient
    36  	)
    37  
    38  	it.Before(func() {
    39  		var err error
    40  
    41  		mockController := gomock.NewController(t)
    42  		mockClient = testmocks.NewMockPackClient(mockController)
    43  
    44  		logger = logging.NewLogWithWriters(&outBuf, &outBuf)
    45  		tempPackHome, err = os.MkdirTemp("", "pack-home")
    46  		h.AssertNil(t, err)
    47  		configPath = filepath.Join(tempPackHome, "config.toml")
    48  
    49  		command = commands.NewConfigCommand(logger, config.Config{Experimental: true}, configPath, mockClient)
    50  		command.SetOut(logging.GetWriterForLevel(logger, logging.InfoLevel))
    51  	})
    52  
    53  	it.After(func() {
    54  		h.AssertNil(t, os.RemoveAll(tempPackHome))
    55  	})
    56  
    57  	when("config", func() {
    58  		it("prints help text", func() {
    59  			command.SetArgs([]string{})
    60  			h.AssertNil(t, command.Execute())
    61  			output := outBuf.String()
    62  			h.AssertContains(t, output, "Usage:")
    63  			for _, command := range []string{"trusted-builders", "run-image-mirrors", "default-builder", "experimental", "registries", "pull-policy", "registry-mirrors"} {
    64  				h.AssertContains(t, output, command)
    65  			}
    66  		})
    67  	})
    68  }
    69  
    70  type configManager struct {
    71  	testObject *testing.T
    72  	configPath string
    73  }
    74  
    75  func newConfigManager(t *testing.T, configPath string) configManager {
    76  	return configManager{
    77  		testObject: t,
    78  		configPath: configPath,
    79  	}
    80  }
    81  
    82  func (c configManager) configWithTrustedBuilders(trustedBuilders ...string) config.Config {
    83  	c.testObject.Helper()
    84  
    85  	cfg := config.Config{}
    86  	for _, builderName := range trustedBuilders {
    87  		cfg.TrustedBuilders = append(cfg.TrustedBuilders, config.TrustedBuilder{Name: builderName})
    88  	}
    89  	err := config.Write(cfg, c.configPath)
    90  	h.AssertNil(c.testObject, err)
    91  
    92  	return cfg
    93  }