diff --git a/move/.editorconfig b/move/.editorconfig index 288a6a1..af28a32 100644 --- a/move/.editorconfig +++ b/move/.editorconfig @@ -10,6 +10,7 @@ indent_size = 2 [{Makefile,go.mod,go.sum,*.go,.gitmodules}] indent_style = tab indent_size = 4 +ij_go_import_sorting = goimports [*.md] indent_size = 4 diff --git a/move/internal/docker/docker_test.go b/move/internal/docker/docker_test.go index 259bbcb..5082566 100644 --- a/move/internal/docker/docker_test.go +++ b/move/internal/docker/docker_test.go @@ -4,7 +4,7 @@ import ( "fmt" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestSave(t *testing.T) { @@ -74,17 +74,17 @@ func TestSave(t *testing.T) { for _, test := range tests { t.Run(test.description, func(t *testing.T) { runDocker = func(command string, options ...string) error { - assert.Equal(t, "save", command) - assert.Equal(t, test.expects.options, options) + require.Equal(t, "save", command) + require.Equal(t, test.expects.options, options) return test.args.returnError } err := Save(test.args.image, test.args.target) if len(test.expects.errorMessage) == 0 { - assert.NoError(t, err) + require.NoError(t, err) } else { - assert.EqualError(t, err, test.expects.errorMessage) + require.EqualError(t, err, test.expects.errorMessage) } }) } @@ -167,17 +167,17 @@ func TestCp(t *testing.T) { for _, test := range tests { t.Run(test.description, func(t *testing.T) { runDocker = func(command string, options ...string) error { - assert.Equal(t, "cp", command) - assert.Equal(t, test.expects.options, options) + require.Equal(t, "cp", command) + require.Equal(t, test.expects.options, options) return test.args.returnError } err := Cp(test.args.source, test.args.target) if len(test.expects.errorMessage) == 0 { - assert.NoError(t, err) + require.NoError(t, err) } else { - assert.EqualError(t, err, test.expects.errorMessage) + require.EqualError(t, err, test.expects.errorMessage) } }) } @@ -263,17 +263,17 @@ func TestExec(t *testing.T) { for _, test := range tests { t.Run(test.description, func(t *testing.T) { runDocker = func(command string, options ...string) error { - assert.Equal(t, "exec", command) - assert.Equal(t, test.expects.options, options) + require.Equal(t, "exec", command) + require.Equal(t, test.expects.options, options) return test.args.returnError } err := Exec(test.args.container, test.args.command, test.args.options...) if len(test.expects.errorMessage) == 0 { - assert.NoError(t, err) + require.NoError(t, err) } else { - assert.EqualError(t, err, test.expects.errorMessage) + require.EqualError(t, err, test.expects.errorMessage) } }) } diff --git a/move/internal/xpkg/xpkg_test.go b/move/internal/xpkg/xpkg_test.go index 3e699d9..117fb73 100644 --- a/move/internal/xpkg/xpkg_test.go +++ b/move/internal/xpkg/xpkg_test.go @@ -9,7 +9,7 @@ import ( "os" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const csdProvider = `apiVersion: pkg.crossplane.io/v1 @@ -22,7 +22,7 @@ spec: func returnStaticXPKG(t *testing.T, expectedImage string, returnContent string, returnError error) func(string, string) error { return func(image string, path string) error { - assert.Equal(t, expectedImage, image) + require.Equal(t, expectedImage, image) if returnError != nil { return returnError @@ -84,11 +84,11 @@ func TestFetchPackageContent(t *testing.T) { pkgContent, err := FetchPackageContent(test.args.crossplanePackage) if len(test.expects.errorMessage) == 0 { - if assert.NoError(t, err) { - assert.Equal(t, test.expects.crossplanePackageContent, pkgContent) + if err == nil { + require.Equal(t, test.expects.crossplanePackageContent, pkgContent) } } else { - assert.EqualError(t, err, test.expects.errorMessage) + require.EqualError(t, err, test.expects.errorMessage) } }) } @@ -135,17 +135,17 @@ func TestSavePackage(t *testing.T) { t.Run(test.description, func(t *testing.T) { extractContainerImage = returnStaticXPKG(t, test.args.crossplanePackage, test.args.returnContent, test.args.returnError) - if tmpFile, err := os.CreateTemp("", "test"); assert.NoError(t, err) { + if tmpFile, err := os.CreateTemp("", "test"); err == nil { defer os.Remove(tmpFile.Name()) - err := SavePackage(test.args.crossplanePackage, tmpFile.Name()) + err = SavePackage(test.args.crossplanePackage, tmpFile.Name()) if len(test.expects.errorMessage) == 0 { - if assert.NoError(t, err) { + if err == nil { assertSHA256(t, tmpFile.Name(), test.expects.fileChecksum) } } else { - assert.EqualError(t, err, test.expects.errorMessage) + require.EqualError(t, err, test.expects.errorMessage) } } }) @@ -153,12 +153,12 @@ func TestSavePackage(t *testing.T) { } func assertSHA256(t *testing.T, file string, expectedSHA256 string) { - if f, err := os.Open(file); assert.NoError(t, err) { + if f, err := os.Open(file); err == nil { defer f.Close() hasher := sha256.New() - if _, err := io.Copy(hasher, f); assert.NoError(t, err) { - assert.Equal(t, expectedSHA256, hex.EncodeToString(hasher.Sum(nil))) + if _, err := io.Copy(hasher, f); err == nil { + require.Equal(t, expectedSHA256, hex.EncodeToString(hasher.Sum(nil))) } } } diff --git a/move/pkg/envvar/envvar_test.go b/move/pkg/envvar/envvar_test.go index d41bc2f..87fe61c 100644 --- a/move/pkg/envvar/envvar_test.go +++ b/move/pkg/envvar/envvar_test.go @@ -5,7 +5,7 @@ import ( "strings" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -31,36 +31,36 @@ func (suite *EnvVarTestSuite) TearDownTest() { func (suite *EnvVarTestSuite) TestGet() { suite.T().Run("Returns existing env vars", func(t *testing.T) { - assert.Equal(t, "", Get("ENVVARTEST_EMPTY")) - assert.Equal(t, "This is a single line", Get("ENVVARTEST_SINGLE")) - assert.Equal(t, "This\nis\na multiline string!", Get("ENVVARTEST_MULTILINE")) + require.Equal(t, "", Get("ENVVARTEST_EMPTY")) + require.Equal(t, "This is a single line", Get("ENVVARTEST_SINGLE")) + require.Equal(t, "This\nis\na multiline string!", Get("ENVVARTEST_MULTILINE")) }) suite.T().Run("Returns empty string if env var can't be found", func(t *testing.T) { - assert.Empty(suite.T(), Get("ENVVARTEST_DOESNT_EXIST")) + suite.Require().Empty(Get("ENVVARTEST_DOESNT_EXIST")) }) } func (suite *EnvVarTestSuite) TestGetOrDefault() { suite.T().Run("Returns existing env vars", func(t *testing.T) { - assert.Equal(t, "", GetOrDefault("ENVVARTEST_EMPTY", "")) - assert.Equal(t, "This is a single line", GetOrDefault("ENVVARTEST_SINGLE", "")) - assert.Equal(t, "This\nis\na multiline string!", GetOrDefault("ENVVARTEST_MULTILINE", "")) + require.Equal(t, "", GetOrDefault("ENVVARTEST_EMPTY", "")) + require.Equal(t, "This is a single line", GetOrDefault("ENVVARTEST_SINGLE", "")) + require.Equal(t, "This\nis\na multiline string!", GetOrDefault("ENVVARTEST_MULTILINE", "")) }) suite.T().Run("Returns default value if env var can't be found", func(t *testing.T) { - assert.Equal(t, "a default value", GetOrDefault("ENVVARTEST_DOESNT_EXIST", "a default value")) - assert.Equal(t, "another default value", GetOrDefault("ENVVARTEST_DOESNT_EXIST", "another default value")) + require.Equal(t, "a default value", GetOrDefault("ENVVARTEST_DOESNT_EXIST", "a default value")) + require.Equal(t, "another default value", GetOrDefault("ENVVARTEST_DOESNT_EXIST", "another default value")) }) } func (suite *EnvVarTestSuite) TestGetOrPanic() { suite.T().Run("Returns existing env vars", func(t *testing.T) { - assert.Equal(t, "", GetOrPanic("ENVVARTEST_EMPTY")) - assert.Equal(t, "This is a single line", GetOrPanic("ENVVARTEST_SINGLE")) - assert.Equal(t, "This\nis\na multiline string!", GetOrPanic("ENVVARTEST_MULTILINE")) + require.Equal(t, "", GetOrPanic("ENVVARTEST_EMPTY")) + require.Equal(t, "This is a single line", GetOrPanic("ENVVARTEST_SINGLE")) + require.Equal(t, "This\nis\na multiline string!", GetOrPanic("ENVVARTEST_MULTILINE")) }) suite.T().Run("Panics if env var can't be found", func(t *testing.T) { - assert.Panics(t, func() { GetOrPanic("ENVVARTEST_DOESNT_EXIST") }) - assert.Panics(t, func() { GetOrPanic("ENVVARTEST_DOESNT_EXIST") }) + require.Panics(t, func() { GetOrPanic("ENVVARTEST_DOESNT_EXIST") }) + require.Panics(t, func() { GetOrPanic("ENVVARTEST_DOESNT_EXIST") }) }) } diff --git a/move/pkg/images/lookup_test.go b/move/pkg/images/lookup_test.go index e3a3623..1dba751 100644 --- a/move/pkg/images/lookup_test.go +++ b/move/pkg/images/lookup_test.go @@ -4,7 +4,7 @@ import ( "os" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -24,27 +24,27 @@ func (suite *LookupSuite) TestGetImagesFromJSONOrPanic() { err := os.Setenv("E2E_IMAGES", "{\"foo\": \"bar\", \"baz\": \"boom\"}") println(err) providerImages := GetImagesFromEnvironmentOrPanic(packageKey, &controllerKey) - assert.Equal(t, "bar", providerImages.Package) - assert.Equal(t, "boom", *providerImages.ControllerImage) + require.Equal(t, "bar", providerImages.Package) + require.Equal(t, "boom", *providerImages.ControllerImage) }) suite.T().Run("Returns existing env vars", func(t *testing.T) { os.Setenv("E2E_IMAGES", "{\"foo\": \"bar\"}") providerImages := GetImagesFromEnvironmentOrPanic(packageKey, nil) - assert.Equal(t, "bar", providerImages.Package) - assert.Nil(t, providerImages.ControllerImage) + require.Equal(t, "bar", providerImages.Package) + require.Nil(t, providerImages.ControllerImage) }) suite.T().Run("env var not set, will panic", func(t *testing.T) { os.Unsetenv("E2E_IMAGES") - assert.Panics(t, func() { + require.Panics(t, func() { GetImagesFromEnvironmentOrPanic(packageKey, nil) }) }) suite.T().Run("invalid json, will panic", func(t *testing.T) { os.Setenv("E2E_IMAGES", "//invalid.json") - assert.Panics(t, func() { + require.Panics(t, func() { GetImagesFromEnvironmentOrPanic(packageKey, nil) }) }) diff --git a/move/pkg/logging/logging_test.go b/move/pkg/logging/logging_test.go index aa3224d..bf02897 100644 --- a/move/pkg/logging/logging_test.go +++ b/move/pkg/logging/logging_test.go @@ -4,7 +4,7 @@ import ( "flag" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestEnableVerboseLogging(t *testing.T) { @@ -14,7 +14,7 @@ func TestEnableVerboseLogging(t *testing.T) { EnableVerboseLogging(nil) verbseFlag := flag.Lookup("v") - assert.Equal(t, verbseFlag.Value.String(), "4") + require.Equal(t, "4", verbseFlag.Value.String()) }, ) t.Run( @@ -24,7 +24,7 @@ func TestEnableVerboseLogging(t *testing.T) { EnableVerboseLogging(ptr(10)) verbseFlag := flag.Lookup("v") - assert.Equal(t, verbseFlag.Value.String(), "10") + require.Equal(t, "10", verbseFlag.Value.String()) }, ) } diff --git a/move/pkg/setup/setup_test.go b/move/pkg/setup/setup_test.go index e4de14a..0a52b32 100644 --- a/move/pkg/setup/setup_test.go +++ b/move/pkg/setup/setup_test.go @@ -6,7 +6,7 @@ import ( "strings" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) var someName = "Bar" @@ -66,9 +66,9 @@ func Test_clusterName(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.args.clusterNameEnvValue == nil { - assert.NoError(t, os.Unsetenv(clusterNameEnv)) + require.NoError(t, os.Unsetenv(clusterNameEnv)) } else { - assert.NoError(t, os.Setenv(clusterNameEnv, *tt.args.clusterNameEnvValue)) + require.NoError(t, os.Setenv(clusterNameEnv, *tt.args.clusterNameEnvValue)) } if got := clusterName(tt.args.reuseCluster); !tt.matcher(got) { t.Errorf("clusterName() = %v; matcher returned error", got) diff --git a/move/pkg/xpenvfuncs/xpenvfuncs_test.go b/move/pkg/xpenvfuncs/xpenvfuncs_test.go index d3e2d5b..e36cb3f 100644 --- a/move/pkg/xpenvfuncs/xpenvfuncs_test.go +++ b/move/pkg/xpenvfuncs/xpenvfuncs_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/pkg/errors" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "sigs.k8s.io/e2e-framework/pkg/env" "sigs.k8s.io/e2e-framework/pkg/envconf" ) @@ -31,8 +31,8 @@ func TestCompose(t *testing.T) { ctx, err := Compose()(pctx, nil) - assert.NoError(t, err) - assert.Equal(t, pctx, ctx) + require.NoError(t, err) + require.Equal(t, pctx, ctx) }, ) @@ -46,15 +46,15 @@ func TestCompose(t *testing.T) { ctx, err := Compose( func(ctx context.Context, cfg *envconf.Config) (context.Context, error) { invoked = true - assert.Equal(t, pctx, ctx) - assert.Equal(t, pcfg, cfg) + require.Equal(t, pctx, ctx) + require.Equal(t, pcfg, cfg) return ctx, nil }, )(pctx, pcfg) - assert.True(t, invoked) - assert.NoError(t, err) - assert.Equal(t, pctx, ctx) + require.True(t, invoked) + require.NoError(t, err) + require.Equal(t, pctx, ctx) }, ) @@ -68,8 +68,8 @@ func TestCompose(t *testing.T) { incEnvFunc(&invocations), )(context.Background(), &envconf.Config{}) - assert.NoError(t, err) - assert.Equal(t, 3, invocations) + require.NoError(t, err) + require.Equal(t, 3, invocations) }, ) @@ -84,14 +84,14 @@ func TestCompose(t *testing.T) { incEnvFunc(&invocations), )(context.Background(), &envconf.Config{}) - assert.EqualError(t, err, "stop here") - assert.Equal(t, 2, invocations) + require.EqualError(t, err, "stop here") + require.Equal(t, 2, invocations) }, ) } func TestGetClusterControlPlaneName(t *testing.T) { - assert.Equal(t, "my-cluster-control-plane", getClusterControlPlaneName("my-cluster")) + require.Equal(t, "my-cluster-control-plane", getClusterControlPlaneName("my-cluster")) } func TestRenderTemplate(t *testing.T) { @@ -238,15 +238,15 @@ spec: for _, test := range tests { t.Run( test.description, func(t *testing.T) { - assert.True(t, true) + require.True(t, true) rendered, err := renderTemplate(test.args.template, test.args.data) if len(test.expects.errorMessage) == 0 { - if assert.NoError(t, err) { - assert.Equal(t, test.expects.rendered, rendered) + if err == nil { + require.Equal(t, test.expects.rendered, rendered) } } else { - assert.Error(t, err) + require.Error(t, err) } }, ) @@ -261,7 +261,7 @@ func TestDummyErr(t *testing.T) { t.Run( "dummyErrFunc returns error", func(t *testing.T) { _, err := dummyErrFunc(nil, nil) - assert.Error(t, err) + require.Error(t, err) }, ) } @@ -271,14 +271,14 @@ func TestConditional(t *testing.T) { "executes", func(t *testing.T) { conditionalFn := Conditional(dummyErrFunc, true) _, err := conditionalFn(nil, nil) - assert.Error(t, err) + require.Error(t, err) }, ) t.Run( "doesnt execute", func(t *testing.T) { conditionalFn := Conditional(dummyErrFunc, false) _, err := conditionalFn(nil, nil) - assert.NoError(t, err) + require.NoError(t, err) }, ) } @@ -288,7 +288,7 @@ func TestIgnoreErr(t *testing.T) { "no error thown", func(t *testing.T) { conditionalFn := IgnoreErr(dummyErrFunc) _, err := conditionalFn(nil, nil) - assert.NoError(t, err) + require.NoError(t, err) }, ) } @@ -300,7 +300,7 @@ func TestIgnoreMatchedErr(t *testing.T) { return false }) _, err := conditionalFn(nil, nil) - assert.NoError(t, err) + require.NoError(t, err) }, ) @@ -310,7 +310,7 @@ func TestIgnoreMatchedErr(t *testing.T) { return true }) _, err := conditionalFn(nil, nil) - assert.NoError(t, err) + require.NoError(t, err) }, ) }