diff --git a/src/cli/repo/repo.go b/src/cli/repo/repo.go index 49e4cdabc..4a5695c7b 100644 --- a/src/cli/repo/repo.go +++ b/src/cli/repo/repo.go @@ -4,12 +4,25 @@ import ( "github.com/spf13/cobra" ) +const ( + initCommand = "init" + connectCommand = "connect" +) + var repoCommands = []func(parent *cobra.Command) *cobra.Command{ addS3Commands, } // AddCommands attaches all `corso repo * *` commands to the parent. func AddCommands(parent *cobra.Command) { + var ( + // Get new instances so that setting the context during tests works + // properly. + repoCmd = repoCmd() + initCmd = initCmd() + connectCmd = connectCmd() + ) + parent.AddCommand(repoCmd) repoCmd.AddCommand(initCmd) repoCmd.AddCommand(connectCmd) @@ -22,12 +35,14 @@ func AddCommands(parent *cobra.Command) { // The repo category of commands. // `corso repo [] [...]` -var repoCmd = &cobra.Command{ - Use: "repo", - Short: "Manage your repositories", - Long: `Initialize, configure, and connect to your account backup repositories.`, - RunE: handleRepoCmd, - Args: cobra.NoArgs, +func repoCmd() *cobra.Command { + return &cobra.Command{ + Use: "repo", + Short: "Manage your repositories", + Long: `Initialize, configure, and connect to your account backup repositories.`, + RunE: handleRepoCmd, + Args: cobra.NoArgs, + } } // Handler for flat calls to `corso repo`. @@ -38,16 +53,15 @@ func handleRepoCmd(cmd *cobra.Command, args []string) error { // The repo init subcommand. // `corso repo init [...]` -var ( - initCommand = "init" - initCmd = &cobra.Command{ +func initCmd() *cobra.Command { + return &cobra.Command{ Use: initCommand, Short: "Initialize a repository.", Long: `Create a new repository to store your backups.`, RunE: handleInitCmd, Args: cobra.NoArgs, } -) +} // Handler for calls to `corso repo init`. func handleInitCmd(cmd *cobra.Command, args []string) error { @@ -56,16 +70,15 @@ func handleInitCmd(cmd *cobra.Command, args []string) error { // The repo connect subcommand. // `corso repo connect [...]` -var ( - connectCommand = "connect" - connectCmd = &cobra.Command{ +func connectCmd() *cobra.Command { + return &cobra.Command{ Use: connectCommand, Short: "Connect to a repository.", Long: `Connect to an existing repository.`, RunE: handleConnectCmd, Args: cobra.NoArgs, } -) +} // Handler for calls to `corso repo connect`. func handleConnectCmd(cmd *cobra.Command, args []string) error { diff --git a/src/cli/repo/s3_integration_test.go b/src/cli/repo/s3_integration_test.go index 9c0391ee2..476a4fca7 100644 --- a/src/cli/repo/s3_integration_test.go +++ b/src/cli/repo/s3_integration_test.go @@ -3,6 +3,7 @@ package repo_test import ( "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -38,27 +39,44 @@ func (suite *S3IntegrationSuite) SetupSuite() { } func (suite *S3IntegrationSuite) TestInitS3Cmd() { - ctx := tester.NewContext() - t := suite.T() + table := []struct { + name string + bucketPrefix string + }{ + { + name: "NoPrefix", + bucketPrefix: "", + }, + { + name: "S3Prefix", + bucketPrefix: "s3://", + }, + } - st := tester.NewPrefixedS3Storage(t) - cfg, err := st.S3Config() - require.NoError(t, err) + for _, test := range table { + suite.T().Run(test.name, func(t *testing.T) { + ctx := tester.NewContext() - vpr, configFP, err := tester.MakeTempTestConfigClone(t, nil) - require.NoError(t, err) + st := tester.NewPrefixedS3Storage(t) + cfg, err := st.S3Config() + require.NoError(t, err) - ctx = config.SetViper(ctx, vpr) + vpr, configFP, err := tester.MakeTempTestConfigClone(t, nil) + require.NoError(t, err) - cmd := tester.StubRootCmd( - "repo", "init", "s3", - "--config-file", configFP, - "--bucket", cfg.Bucket, - "--prefix", cfg.Prefix) - cli.BuildCommandTree(cmd) + ctx = config.SetViper(ctx, vpr) - // run the command - require.NoError(t, cmd.ExecuteContext(ctx)) + cmd := tester.StubRootCmd( + "repo", "init", "s3", + "--config-file", configFP, + "--bucket", test.bucketPrefix+cfg.Bucket, + "--prefix", cfg.Prefix) + cli.BuildCommandTree(cmd) + + // run the command + require.NoError(t, cmd.ExecuteContext(ctx)) + }) + } } func (suite *S3IntegrationSuite) TestInitS3Cmd_missingBucket() { @@ -85,37 +103,54 @@ func (suite *S3IntegrationSuite) TestInitS3Cmd_missingBucket() { } func (suite *S3IntegrationSuite) TestConnectS3Cmd() { - ctx := tester.NewContext() - t := suite.T() - - st := tester.NewPrefixedS3Storage(t) - cfg, err := st.S3Config() - require.NoError(t, err) - - force := map[string]string{ - tester.TestCfgAccountProvider: "M365", - tester.TestCfgStorageProvider: "S3", - tester.TestCfgPrefix: cfg.Prefix, + table := []struct { + name string + bucketPrefix string + }{ + { + name: "NoPrefix", + bucketPrefix: "", + }, + { + name: "S3Prefix", + bucketPrefix: "s3://", + }, } - vpr, configFP, err := tester.MakeTempTestConfigClone(t, force) - require.NoError(t, err) - ctx = config.SetViper(ctx, vpr) + for _, test := range table { + suite.T().Run(test.name, func(t *testing.T) { + ctx := tester.NewContext() + st := tester.NewPrefixedS3Storage(t) + cfg, err := st.S3Config() + require.NoError(t, err) - // init the repo first - _, err = repository.Initialize(ctx, account.Account{}, st, control.Options{}) - require.NoError(t, err) + force := map[string]string{ + tester.TestCfgAccountProvider: "M365", + tester.TestCfgStorageProvider: "S3", + tester.TestCfgPrefix: cfg.Prefix, + } + vpr, configFP, err := tester.MakeTempTestConfigClone(t, force) + require.NoError(t, err) - // then test it - cmd := tester.StubRootCmd( - "repo", "connect", "s3", - "--config-file", configFP, - "--bucket", cfg.Bucket, - "--prefix", cfg.Prefix) - cli.BuildCommandTree(cmd) + ctx = config.SetViper(ctx, vpr) - // run the command - require.NoError(t, cmd.ExecuteContext(ctx)) + // init the repo first + _, err = repository.Initialize(ctx, account.Account{}, st, control.Options{}) + require.NoError(t, err) + + // then test it + cmd := tester.StubRootCmd( + "repo", "connect", "s3", + "--config-file", configFP, + "--bucket", test.bucketPrefix+cfg.Bucket, + "--prefix", cfg.Prefix, + ) + cli.BuildCommandTree(cmd) + + // run the command + assert.NoError(t, cmd.ExecuteContext(ctx)) + }) + } } func (suite *S3IntegrationSuite) TestConnectS3Cmd_BadBucket() {