Skip to content

Commit

Permalink
Apply and fix testifylint linter
Browse files Browse the repository at this point in the history
Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Dec 19, 2024
1 parent ae1c5ba commit bc8370a
Show file tree
Hide file tree
Showing 40 changed files with 489 additions and 488 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ linters:
- tagalign
- tenv
- testableexamples
- testifylint
- typecheck
- unconvert
- unparam
Expand Down Expand Up @@ -120,7 +121,6 @@ linters:
# - nonamedreturns
# - paralleltest
# - tagliatelle
# - testifylint
# - testpackage
# - thelper
# - tparallel
Expand Down
2 changes: 1 addition & 1 deletion cmd/gcbuilder/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ import (

func TestRootCommand(t *testing.T) {
err := rootCmd.Usage()
require.Nil(t, err)
require.NoError(t, err)
}
50 changes: 25 additions & 25 deletions cmd/krel/cmd/changelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func (s *sut) getChangelogOptions(tag string) *changelog.Options {
func fileContains(t *testing.T, file, contains string) {
require.FileExists(t, file)
content, err := os.ReadFile(file)
require.Nil(t, err)
require.NoError(t, err)
require.Contains(t, string(content), contains)
}

func TestChangelogNoArgumentsOrFlags(t *testing.T) {
err := changelog.New(&changelog.Options{}).Run()
require.NotNil(t, err)
require.Error(t, err)
}

func TestNewPatchRelease(t *testing.T) {
Expand All @@ -66,12 +66,12 @@ func TestNewPatchRelease(t *testing.T) {

// When
cl := changelog.New(co)
require.Nil(t, cl.Run())
require.NoError(t, cl.Run())

// Then
// Verify local results
fileContains(t, "CHANGELOG-1.25.html", patchReleaseExpectedHTML)
require.Nil(t, os.RemoveAll("CHANGELOG-1.25.html"))
require.NoError(t, os.RemoveAll("CHANGELOG-1.25.html"))
for _, x := range []struct {
branch string
commitMessage string
Expand All @@ -80,7 +80,7 @@ func TestNewPatchRelease(t *testing.T) {
{git.DefaultBranch, "Update directory for v1.25.3 release"},
} {
// Switch to the test branch
require.Nil(t, s.repo.Checkout(x.branch))
require.NoError(t, s.repo.Checkout(x.branch))

// Verify commit message
lastCommit := s.lastCommit(t, x.branch)
Expand All @@ -91,7 +91,7 @@ func TestNewPatchRelease(t *testing.T) {
changelogPath := filepath.Join(s.repo.Dir(), changelog.RepoChangelogDir, "CHANGELOG-1.25.md")
result, err := os.ReadFile(changelogPath)

require.Nil(t, err)
require.NoError(t, err)
require.Contains(t, string(result), patchReleaseExpectedTOC)
require.Contains(t, string(result), patchReleaseExpectedContent)
}
Expand All @@ -105,13 +105,13 @@ func TestNewAlphaRelease(t *testing.T) {
co := s.getChangelogOptions("v1.18.0-alpha.3")

// When
require.Nil(t, changelog.New(co).Run())
require.NoError(t, changelog.New(co).Run())

// Then
// Verify local results
fileContains(t, "CHANGELOG-1.18.html", alphaReleaseExpectedHTMLHead)
fileContains(t, "CHANGELOG-1.18.html", alphaReleaseExpectedHTMLBottom)
require.Nil(t, os.RemoveAll("CHANGELOG-1.18.html"))
require.NoError(t, os.RemoveAll("CHANGELOG-1.18.html"))

// Verify commit message
lastCommit := s.lastCommit(t, git.DefaultBranch)
Expand All @@ -122,7 +122,7 @@ func TestNewAlphaRelease(t *testing.T) {
result, err := os.ReadFile(
filepath.Join(s.repo.Dir(), changelog.RepoChangelogDir, "CHANGELOG-1.18.md"),
)
require.Nil(t, err)
require.NoError(t, err)
require.Regexp(t, alphaReleaseExpectedTOC, string(result))
require.Contains(t, string(result), alphaReleaseExpectedContent)
}
Expand All @@ -135,12 +135,12 @@ func TestNewAlpha1Release(t *testing.T) {
co := s.getChangelogOptions("v1.19.0-alpha.1")

// When
require.Nil(t, changelog.New(co).Run())
require.NoError(t, changelog.New(co).Run())

// Then
// Verify local results
fileContains(t, "CHANGELOG-1.19.html", alpha1ExpectedHTML)
require.Nil(t, os.RemoveAll("CHANGELOG-1.19.html"))
require.NoError(t, os.RemoveAll("CHANGELOG-1.19.html"))

// Verify commit message
lastCommit := s.lastCommit(t, git.DefaultBranch)
Expand All @@ -151,7 +151,7 @@ func TestNewAlpha1Release(t *testing.T) {
result, err := os.ReadFile(
filepath.Join(s.repo.Dir(), changelog.RepoChangelogDir, "CHANGELOG-1.19.md"),
)
require.Nil(t, err)
require.NoError(t, err)
require.Regexp(t, alpha1ReleaseExpectedTOC, string(result))
}

Expand All @@ -173,9 +173,9 @@ func TestNewMinorRelease(t *testing.T) {
}
}

require.Nil(t, s.repo.Checkout(releaseBranch))
require.NoError(t, s.repo.Checkout(releaseBranch))
changelogIter(func(filename string) {
require.Nil(t,
require.NoError(t,
//nolint:gosec // TODO(gosec): G306: Expect WriteFile permissions
// to be 0600 or less
os.WriteFile(
Expand All @@ -184,24 +184,24 @@ func TestNewMinorRelease(t *testing.T) {
0o644,
),
)
require.Nil(t, s.repo.Add(filename))
require.NoError(t, s.repo.Add(filename))
})
require.Nil(t, s.repo.Commit("Adding other changelog files"))
require.Nil(t, s.repo.Checkout(git.DefaultBranch))
require.NoError(t, s.repo.Commit("Adding other changelog files"))
require.NoError(t, s.repo.Checkout(git.DefaultBranch))

// When
require.Nil(t, changelog.New(co).Run())
require.NoError(t, changelog.New(co).Run())

// Then
// Verify local results
require.Nil(t, s.repo.Checkout(releaseBranch))
require.NoError(t, s.repo.Checkout(releaseBranch))
changelogIter(func(filename string) {
_, err := os.Stat(filename)
require.True(t, os.IsNotExist(err))
})

fileContains(t, "CHANGELOG-1.21.html", minorReleaseExpectedHTML)
require.Nil(t, os.RemoveAll("CHANGELOG-1.21.html"))
require.NoError(t, os.RemoveAll("CHANGELOG-1.21.html"))
for _, x := range []struct {
branch string
commitMessage string
Expand All @@ -210,7 +210,7 @@ func TestNewMinorRelease(t *testing.T) {
{git.DefaultBranch, "Update directory for v1.21.0 release"},
} {
// Switch to the test branch
require.Nil(t, s.repo.Checkout(x.branch))
require.NoError(t, s.repo.Checkout(x.branch))

// Verify commit message
lastCommit := s.lastCommit(t, x.branch)
Expand All @@ -221,7 +221,7 @@ func TestNewMinorRelease(t *testing.T) {
result, err := os.ReadFile(
filepath.Join(s.repo.Dir(), changelog.RepoChangelogDir, "CHANGELOG-1.21.md"),
)
require.Nil(t, err)
require.NoError(t, err)
require.Contains(t, string(result), minorReleaseExpectedTOC)
require.Contains(t, string(result), minorReleaseExpectedContent)
}
Expand All @@ -237,15 +237,15 @@ func TestNewRCRelease(t *testing.T) {
co.Branch = releaseBranch

// When
require.Nil(t, changelog.New(co).Run())
require.NoError(t, changelog.New(co).Run())

// Then
// Verify local results
require.Nil(t, s.repo.Checkout(releaseBranch))
require.NoError(t, s.repo.Checkout(releaseBranch))

result, err := os.ReadFile(
filepath.Join(s.repo.Dir(), changelog.RepoChangelogDir, "CHANGELOG-1.16.md"),
)
require.Nil(t, err)
require.NoError(t, err)
require.Contains(t, string(result), rcReleaseExpectedTOC)
}
12 changes: 6 additions & 6 deletions cmd/krel/cmd/ff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestFfFailedWithoutReleaseBranch(t *testing.T) {
// When
err := fastforward.New(&fastforward.Options{}).Run()
// Then
require.NotNil(t, err)
require.Error(t, err)
}

func TestFfFailedNoReleaseBranch(t *testing.T) {
Expand All @@ -57,7 +57,7 @@ func TestFfFailedNoReleaseBranch(t *testing.T) {
err := fastforward.New(ffo).Run()

// Then
require.NotNil(t, err)
require.Error(t, err)
}

func TestFfFailedReleaseBranchDoesNotExist(t *testing.T) {
Expand All @@ -72,7 +72,7 @@ func TestFfFailedReleaseBranchDoesNotExist(t *testing.T) {
err := fastforward.New(ffo).Run()

// Then
require.NotNil(t, err)
require.Error(t, err)
}

func TestFfFailedOldReleaseBranch(t *testing.T) {
Expand All @@ -87,7 +87,7 @@ func TestFfFailedOldReleaseBranch(t *testing.T) {
err := fastforward.New(ffo).Run()

// Then
require.NotNil(t, err)
require.Error(t, err)
}

func TestFfSuccessDryRun(t *testing.T) {
Expand All @@ -102,7 +102,7 @@ func TestFfSuccessDryRun(t *testing.T) {
err := fastforward.New(ffo).Run()

// Then
require.Nil(t, err)
require.NoError(t, err)

// Local should contain the commit
lastLocalCommit := s.lastCommit(t, pseudoReleaseBranch)
Expand All @@ -127,7 +127,7 @@ func TestFfSuccess(t *testing.T) {
err := fastforward.New(ffo).Run()

// Then
require.Nil(t, err)
require.NoError(t, err)

// Local should contain the commit
lastLocalCommit := s.lastCommit(t, pseudoReleaseBranch)
Expand Down
30 changes: 15 additions & 15 deletions cmd/krel/cmd/sut_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,77 +54,77 @@ func newSUT(t *testing.T) *sut {

// A local k/k repo will be our test base
tempDir := filepath.Join(os.TempDir(), "k8s-test")
require.Nil(t, os.MkdirAll(tempDir, 0o755))
require.NoError(t, os.MkdirAll(tempDir, 0o755))

// The bare repo which is the pseudo remote base
bareDir := filepath.Join(tempDir, "bare")
const url = "https://github.com/kubernetes/kubernetes"
if _, err := os.Stat(bareDir); os.IsNotExist(err) {
require.Nil(t,
require.NoError(t,
command.New("git", "clone", "--bare", url, bareDir).RunSuccess(),
)
}

now := time.Now().Unix()
bareCopyDir := filepath.Join(tempDir, fmt.Sprintf("bare-%d", now))
require.Nil(t, command.New("cp", "-r", bareDir, bareCopyDir).RunSuccess())
require.NoError(t, command.New("cp", "-r", bareDir, bareCopyDir).RunSuccess())

// The base repo where every test case is inherited
baseDir := filepath.Join(tempDir, fmt.Sprintf("base-%d", now))

// Clone the repo from the bare, which is safe to modify
require.Nil(t,
require.NoError(t,
command.New("git", "clone", bareCopyDir, baseDir).RunSuccess(),
)

// Modify the bare repo with test content
require.Nil(t,
require.NoError(t,
command.NewWithWorkDir(baseDir,
"git", "checkout", "-b", pseudoReleaseBranch,
).RunSuccess(),
)
require.Nil(t,
require.NoError(t,
command.NewWithWorkDir(baseDir,
"git", "push", "-u", git.DefaultRemote, pseudoReleaseBranch,
).RunSuccess(),
)
require.Nil(t,
require.NoError(t,
command.NewWithWorkDir(baseDir,
"git", "checkout", git.DefaultBranch,
).RunSuccess(),
)
require.Nil(t,
require.NoError(t,
command.NewWithWorkDir(baseDir,
"git", "commit", "--allow-empty", "-m", testCommitMessage,
).RunSuccess(),
)
require.Nil(t,
require.NoError(t,
command.NewWithWorkDir(baseDir,
"git", "push",
).RunSuccess(),
)

// The sut repo dir
repoDir := filepath.Join(tempDir, fmt.Sprintf("test-%d", now))
require.Nil(t, command.New("cp", "-r", baseDir, repoDir).RunSuccess())
require.NoError(t, command.New("cp", "-r", baseDir, repoDir).RunSuccess())

opts := &gogit.CloneOptions{}
repo, err := git.CloneOrOpenRepo(repoDir, url, false, false, opts)
require.Nil(t, err)
require.NoError(t, err)

// Adapt the settings
return &sut{repo, baseDir, bareCopyDir, tempDir}
}

func (s *sut) cleanup(t *testing.T) {
require.Nil(t, os.RemoveAll(s.repo.Dir()))
require.Nil(t, os.RemoveAll(s.baseDir))
require.Nil(t, os.RemoveAll(s.bareCopyDir))
require.NoError(t, os.RemoveAll(s.repo.Dir()))
require.NoError(t, os.RemoveAll(s.baseDir))
require.NoError(t, os.RemoveAll(s.bareCopyDir))
}

func (s *sut) lastCommit(t *testing.T, branch string) string {
res, err := command.NewWithWorkDir(s.repo.Dir(),
"git", "log", "-1", branch).RunSilentSuccessOutput()
require.Nil(t, err)
require.NoError(t, err)
return res.OutputTrimNL()
}
24 changes: 12 additions & 12 deletions cmd/krel/cmd/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,38 @@ import (
"path/filepath"
"testing"

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

func TestRunValidateReleaseNotes(t *testing.T) {
testDataPath := "testdata/validation-data"

// Valid YAML returns no error
err := runValidateReleaseNotes(filepath.Join(testDataPath, "valid.yaml"))
assert.NoError(t, err, "Expected no error for valid YAML file")
require.NoError(t, err, "Expected no error for valid YAML file")

// Try a non-existent path
err = runValidateReleaseNotes("nonexistent/path")
assert.Error(t, err, "Expected error for non-existent path")
assert.Contains(t, err.Error(), "does not exist", "Error should be about non-existent path")
require.Error(t, err, "Expected error for non-existent path")
require.Contains(t, err.Error(), "does not exist", "Error should be about non-existent path")

// Missing punctuation YAML returns error
err = runValidateReleaseNotes(filepath.Join(testDataPath, "missing-punctuation.yaml"))
assert.Error(t, err, "Expected error for missing punctuation YAML file")
assert.Contains(t, err.Error(), "field does not end with valid punctuation", "Error should be about missing punctuation")
require.Error(t, err, "Expected error for missing punctuation YAML file")
require.Contains(t, err.Error(), "field does not end with valid punctuation", "Error should be about missing punctuation")

// Try invalid yaml starting with "`"
err = runValidateReleaseNotes(filepath.Join(testDataPath, "invalid-yaml-start.yaml"))
assert.Error(t, err, "Expected error for invalid yaml")
assert.Contains(t, err.Error(), "YAML unmarshaling testdata/validation-data/invalid-yaml-start", "Error should be about invalid yaml")
require.Error(t, err, "Expected error for invalid yaml")
require.Contains(t, err.Error(), "YAML unmarshaling testdata/validation-data/invalid-yaml-start", "Error should be about invalid yaml")

// Try invalid multi line yaml
err = runValidateReleaseNotes(filepath.Join(testDataPath, "invalid-multi-line.yaml"))
assert.Error(t, err, "Expected error for invalid yaml")
assert.Contains(t, err.Error(), "YAML unmarshaling testdata/validation-data/invalid-multi-line.yaml", "Error should be about invalid yaml")
require.Error(t, err, "Expected error for invalid yaml")
require.Contains(t, err.Error(), "YAML unmarshaling testdata/validation-data/invalid-multi-line.yaml", "Error should be about invalid yaml")

// Try invalid indent
err = runValidateReleaseNotes(filepath.Join(testDataPath, "invalid-indent.yaml"))
assert.Error(t, err, "Expected error for invalid yaml")
assert.Contains(t, err.Error(), "YAML unmarshaling testdata/validation-data/invalid-indent.yaml", "Error should be about invalid yaml")
require.Error(t, err, "Expected error for invalid yaml")
require.Contains(t, err.Error(), "YAML unmarshaling testdata/validation-data/invalid-indent.yaml", "Error should be about invalid yaml")
}
Loading

0 comments on commit bc8370a

Please sign in to comment.