Skip to content

Commit

Permalink
comply with latest golangci-lint requirements
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Braun (SAP) <maximilian.braun@sap.com>
  • Loading branch information
maximilianbraun committed Oct 24, 2023
1 parent 6d47ffc commit c87f63c
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 76 deletions.
1 change: 1 addition & 0 deletions move/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions move/internal/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSave(t *testing.T) {
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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)
}
})
}
Expand Down
24 changes: 12 additions & 12 deletions move/internal/xpkg/xpkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const csdProvider = `apiVersion: pkg.crossplane.io/v1
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -135,30 +135,30 @@ 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)
}
}
})
}
}

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)))
}
}
}
30 changes: 15 additions & 15 deletions move/pkg/envvar/envvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -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") })
})
}

Expand Down
14 changes: 7 additions & 7 deletions move/pkg/images/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -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)
})
})
Expand Down
6 changes: 3 additions & 3 deletions move/pkg/logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"flag"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestEnableVerboseLogging(t *testing.T) {
Expand All @@ -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(
Expand All @@ -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())
},
)
}
Expand Down
6 changes: 3 additions & 3 deletions move/pkg/setup/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var someName = "Bar"
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit c87f63c

Please sign in to comment.