Skip to content

Commit 2d4c000

Browse files
authored
Add testify linter and address fixes (#1334)
While reviewing #1330 we had a discussion about the ability to generally prevent the use of `requires` within `Eventually` calls to prevent future flakes, and that led us to evaluate the `testifylint`-er to give us some cautionary guidelines. It did _not_ resolve the requires-within-eventually issue, but we may try to tackle that with some custom AST in the future. Signed-off-by: Jordan Keister <[email protected]>
1 parent 814bf63 commit 2d4c000

File tree

10 files changed

+106
-122
lines changed

10 files changed

+106
-122
lines changed

.golangci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ linters:
3030
- nonamedreturns
3131
- prealloc
3232
- stylecheck
33+
- testifylint
3334
- tparallel
3435
- unconvert
3536
- unparam

internal/action/helm_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/mock"
11+
"github.com/stretchr/testify/require"
1112
"helm.sh/helm/v3/pkg/chart"
1213
"helm.sh/helm/v3/pkg/release"
1314
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -130,12 +131,12 @@ func TestActionClientFor(t *testing.T) {
130131

131132
// Test the successful case
132133
actionClient, err := acg.ActionClientFor(ctx, obj)
133-
assert.NoError(t, err)
134+
require.NoError(t, err)
134135
assert.NotNil(t, actionClient)
135136
assert.IsType(t, &ActionClient{}, actionClient)
136137

137138
// Test the error case
138139
actionClient, err = acg.ActionClientFor(ctx, obj)
139-
assert.Error(t, err)
140+
require.Error(t, err)
140141
assert.Nil(t, actionClient)
141142
}

internal/catalogmetadata/cache/cache_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,26 @@ func TestFilesystemCachePutAndGet(t *testing.T) {
7979

8080
t.Log("Get empty v1 cache")
8181
actualFSGet, err := c.Get(catalogName, resolvedRef1)
82-
assert.NoError(t, err)
82+
require.NoError(t, err)
8383
assert.Nil(t, actualFSGet)
8484

8585
t.Log("Put v1 content into cache")
8686
actualFSPut, err := c.Put(catalogName, resolvedRef1, defaultContent(), nil)
87-
assert.NoError(t, err)
87+
require.NoError(t, err)
8888
require.NotNil(t, actualFSPut)
89-
assert.NoError(t, equalFilesystems(defaultFS(), actualFSPut))
89+
require.NoError(t, equalFilesystems(defaultFS(), actualFSPut))
9090

9191
t.Log("Get v1 content from cache")
9292
actualFSGet, err = c.Get(catalogName, resolvedRef1)
93-
assert.NoError(t, err)
93+
require.NoError(t, err)
9494
require.NotNil(t, actualFSGet)
9595
assert.NoError(t, equalFilesystems(defaultFS(), actualFSPut))
9696
assert.NoError(t, equalFilesystems(actualFSPut, actualFSGet))
9797

9898
t.Log("Put v1 error into cache")
9999
actualFSPut, err = c.Put(catalogName, resolvedRef1, nil, errors.New("fake put error"))
100100
// Errors do not override previously successfully populated cache
101-
assert.NoError(t, err)
101+
require.NoError(t, err)
102102
require.NotNil(t, actualFSPut)
103103
assert.NoError(t, equalFilesystems(defaultFS(), actualFSPut))
104104
assert.NoError(t, equalFilesystems(actualFSPut, actualFSGet))
@@ -115,13 +115,13 @@ func TestFilesystemCachePutAndGet(t *testing.T) {
115115

116116
t.Log("Put v2 content into cache")
117117
actualFSPut, err = c.Put(catalogName, resolvedRef2, defaultContent(), nil)
118-
assert.NoError(t, err)
118+
require.NoError(t, err)
119119
require.NotNil(t, actualFSPut)
120-
assert.NoError(t, equalFilesystems(defaultFS(), actualFSPut))
120+
require.NoError(t, equalFilesystems(defaultFS(), actualFSPut))
121121

122122
t.Log("Get v2 content from cache")
123123
actualFSGet, err = c.Get(catalogName, resolvedRef2)
124-
assert.NoError(t, err)
124+
require.NoError(t, err)
125125
require.NotNil(t, actualFSGet)
126126
assert.NoError(t, equalFilesystems(defaultFS(), actualFSPut))
127127
assert.NoError(t, equalFilesystems(actualFSPut, actualFSGet))
@@ -130,7 +130,7 @@ func TestFilesystemCachePutAndGet(t *testing.T) {
130130
// Cache should be empty and no error because
131131
// Put with a new version overrides the old version
132132
actualFSGet, err = c.Get(catalogName, resolvedRef1)
133-
assert.NoError(t, err)
133+
require.NoError(t, err)
134134
assert.Nil(t, actualFSGet)
135135
}
136136

internal/catalogmetadata/client/client_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing/fstest"
1212

1313
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1415
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1516

1617
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
@@ -77,7 +78,7 @@ func TestClientGetPackage(t *testing.T) {
7778
pkgName: "pkg-missing",
7879
cache: &fakeCache{getFS: testFS},
7980
assert: func(t *testing.T, fbc *declcfg.DeclarativeConfig, err error) {
80-
assert.NoError(t, err)
81+
require.NoError(t, err)
8182
assert.Equal(t, &declcfg.DeclarativeConfig{}, fbc)
8283
},
8384
},
@@ -89,7 +90,7 @@ func TestClientGetPackage(t *testing.T) {
8990
"invalid-pkg-present/olm.package/invalid-pkg-present.json": &fstest.MapFile{Data: []byte(`{"schema": "olm.package","name": 12345}`)},
9091
}},
9192
assert: func(t *testing.T, fbc *declcfg.DeclarativeConfig, err error) {
92-
assert.ErrorContains(t, err, `error loading package "invalid-pkg-present"`)
93+
require.ErrorContains(t, err, `error loading package "invalid-pkg-present"`)
9394
assert.Nil(t, fbc)
9495
},
9596
},
@@ -99,7 +100,7 @@ func TestClientGetPackage(t *testing.T) {
99100
pkgName: "pkg-present",
100101
cache: &fakeCache{getFS: testFS},
101102
assert: func(t *testing.T, fbc *declcfg.DeclarativeConfig, err error) {
102-
assert.NoError(t, err)
103+
require.NoError(t, err)
103104
assert.Equal(t, &declcfg.DeclarativeConfig{Packages: []declcfg.Package{{Schema: declcfg.SchemaPackage, Name: "pkg-present"}}}, fbc)
104105
},
105106
},
@@ -111,7 +112,7 @@ func TestClientGetPackage(t *testing.T) {
111112
return testFS, nil
112113
}},
113114
assert: func(t *testing.T, fbc *declcfg.DeclarativeConfig, err error) {
114-
assert.NoError(t, err)
115+
require.NoError(t, err)
115116
assert.Equal(t, &declcfg.DeclarativeConfig{Packages: []declcfg.Package{{Schema: declcfg.SchemaPackage, Name: "pkg-present"}}}, fbc)
116117
},
117118
},
@@ -170,7 +171,7 @@ func TestClientPopulateCache(t *testing.T) {
170171
}, nil
171172
},
172173
assert: func(t *testing.T, fs fs.FS, err error) {
173-
assert.NoError(t, err)
174+
require.NoError(t, err)
174175
assert.Equal(t, testFS, fs)
175176
},
176177
putFuncConstructor: func(t *testing.T) func(source string, errToCache error) (fs.FS, error) {

internal/catalogmetadata/filter/bundle_predicates_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
mmsemver "github.com/Masterminds/semver/v3"
88
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
910

1011
"github.com/operator-framework/operator-registry/alpha/declcfg"
1112
"github.com/operator-framework/operator-registry/alpha/property"
@@ -40,7 +41,7 @@ func TestInMastermindsSemverRange(t *testing.T) {
4041
}
4142

4243
vRange, err := mmsemver.NewConstraint(">=1.0.0")
43-
assert.NoError(t, err)
44+
require.NoError(t, err)
4445

4546
f := filter.InMastermindsSemverRange(vRange)
4647

internal/catalogmetadata/filter/successors_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func TestSuccessorsPredicateWithForceSemverUpgradeConstraintsEnabled(t *testing.
171171
} {
172172
t.Run(tt.name, func(t *testing.T) {
173173
successors, err := SuccessorsOf(tt.installedBundle, channelSet[testPackageName])
174-
assert.NoError(t, err)
174+
require.NoError(t, err)
175175

176176
allBundles := make([]declcfg.Bundle, 0, len(bundleSet))
177177
for _, bundle := range bundleSet {
@@ -328,7 +328,7 @@ func TestSuccessorsPredicateWithForceSemverUpgradeConstraintsDisabled(t *testing
328328
} {
329329
t.Run(tt.name, func(t *testing.T) {
330330
successors, err := SuccessorsOf(tt.installedBundle, channelSet[testPackageName])
331-
assert.NoError(t, err)
331+
require.NoError(t, err)
332332

333333
allBundles := make([]declcfg.Bundle, 0, len(bundleSet))
334334
for _, bundle := range bundleSet {
@@ -380,7 +380,7 @@ func TestLegacySuccessor(t *testing.T) {
380380
emptyBundle := declcfg.Bundle{}
381381

382382
f, err := legacySuccessor(installedBundle, fakeChannel)
383-
assert.NoError(t, err)
383+
require.NoError(t, err)
384384

385385
assert.True(t, f(b2))
386386
assert.False(t, f(b3))

internal/controllers/clusterextension_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func TestClusterExtensionResolutionSuccessfulUnpackFails(t *testing.T) {
167167

168168
isTerminal := errors.Is(err, reconcile.TerminalError(nil))
169169
assert.Equal(t, tc.expectTerminal, isTerminal, "expected terminal error: %v, got: %v", tc.expectTerminal, isTerminal)
170-
assert.ErrorContains(t, err, tc.unpackErr.Error())
170+
require.ErrorContains(t, err, tc.unpackErr.Error())
171171

172172
t.Log("By fetching updated cluster extension after reconcile")
173173
require.NoError(t, cl.Get(ctx, extKey, clusterExtension))

internal/resolve/catalog_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ func TestPackageVariationsBetweenCatalogs(t *testing.T) {
397397
gotBundle, gotVersion, gotDeprecation, err := r.Resolve(context.Background(), ce, nil)
398398
require.Error(t, err)
399399
// We will not make a decision on which catalog to use
400-
assert.ErrorContains(t, err, "in multiple catalogs with the same priority [b c]")
400+
require.ErrorContains(t, err, "in multiple catalogs with the same priority [b c]")
401401
assert.Nil(t, gotBundle)
402402
assert.Nil(t, gotVersion)
403403
assert.Nil(t, gotDeprecation)
@@ -408,7 +408,7 @@ func TestPackageVariationsBetweenCatalogs(t *testing.T) {
408408
gotBundle, gotVersion, gotDeprecation, err := r.Resolve(context.Background(), ce, nil)
409409
require.Error(t, err)
410410
// We will not make a decision on which catalog to use
411-
assert.ErrorContains(t, err, "in multiple catalogs with the same priority [d f]")
411+
require.ErrorContains(t, err, "in multiple catalogs with the same priority [d f]")
412412
assert.Nil(t, gotBundle)
413413
assert.Nil(t, gotVersion)
414414
assert.Nil(t, gotDeprecation)
@@ -635,7 +635,7 @@ func TestCatalogWalker(t *testing.T) {
635635
seenCatalogs = append(seenCatalogs, cat.Name)
636636
return nil
637637
}
638-
assert.NoError(t, w(context.Background(), "", walkFunc))
638+
require.NoError(t, w(context.Background(), "", walkFunc))
639639
assert.Equal(t, []string{"a", "b"}, seenCatalogs)
640640
})
641641
}
@@ -936,7 +936,7 @@ func TestMultiplePriority(t *testing.T) {
936936
ce := buildFooClusterExtension(pkgName, []string{}, ">=1.0.0 <=1.0.1", ocv1alpha1.UpgradeConstraintPolicyCatalogProvided)
937937
gotBundle, gotVersion, gotDeprecation, err := r.Resolve(context.Background(), ce, nil)
938938
require.Error(t, err)
939-
assert.ErrorContains(t, err, "in multiple catalogs with the same priority [a b c]")
939+
require.ErrorContains(t, err, "in multiple catalogs with the same priority [a b c]")
940940
assert.Nil(t, gotBundle)
941941
assert.Nil(t, gotVersion)
942942
assert.Nil(t, gotDeprecation)

internal/rukpak/source/containers_image_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ func TestUnpackValidInsecure(t *testing.T) {
5353
result, err := unpacker.Unpack(context.Background(), bundleSource)
5454
require.NoError(t, err)
5555
require.NotNil(t, result)
56-
assert.Equal(t, result.State, source.StateUnpacked)
56+
assert.Equal(t, source.StateUnpacked, result.State)
5757

5858
require.NoDirExists(t, oldBundlePath)
5959

6060
unpackedFile, err := fs.ReadFile(result.Bundle, testFileName)
61-
assert.NoError(t, err)
61+
require.NoError(t, err)
6262
// Ensure the unpacked file matches the source content
6363
assert.Equal(t, []byte(testFileContents), unpackedFile)
6464
assert.NoError(t, unpacker.Cleanup(context.Background(), bundleSource))
@@ -87,9 +87,9 @@ func TestUnpackValidUsesCache(t *testing.T) {
8787

8888
// Attempt to pull and unpack the image
8989
result, err := unpacker.Unpack(context.Background(), bundleSource)
90-
assert.NoError(t, err)
90+
require.NoError(t, err)
9191
require.NotNil(t, result)
92-
assert.Equal(t, result.State, source.StateUnpacked)
92+
assert.Equal(t, source.StateUnpacked, result.State)
9393

9494
// Make sure the original contents of the cache are still present. If the cached contents
9595
// were not used, we would expect the original contents to be removed.
@@ -144,7 +144,7 @@ func TestUnpackNameOnlyImageReference(t *testing.T) {
144144

145145
// Attempt to pull and unpack the image
146146
_, err := unpacker.Unpack(context.Background(), bundleSource)
147-
assert.ErrorContains(t, err, "tag or digest is needed")
147+
require.ErrorContains(t, err, "tag or digest is needed")
148148
assert.ErrorIs(t, err, reconcile.TerminalError(nil))
149149
}
150150

@@ -226,8 +226,8 @@ func TestUnpackInvalidNilImage(t *testing.T) {
226226
// Attempt to unpack
227227
result, err := unpacker.Unpack(context.Background(), bundleSource)
228228
assert.Nil(t, result)
229-
assert.ErrorContains(t, err, "nil image source")
230-
assert.ErrorIs(t, err, reconcile.TerminalError(nil))
229+
require.ErrorContains(t, err, "nil image source")
230+
require.ErrorIs(t, err, reconcile.TerminalError(nil))
231231
assert.NoDirExists(t, filepath.Join(unpacker.BaseCachePath, bundleSource.Name))
232232
}
233233

@@ -245,8 +245,8 @@ func TestUnpackInvalidImageRef(t *testing.T) {
245245
// Attempt to unpack
246246
result, err := unpacker.Unpack(context.Background(), bundleSource)
247247
assert.Nil(t, result)
248-
assert.ErrorContains(t, err, "error parsing image reference")
249-
assert.ErrorIs(t, err, reconcile.TerminalError(nil))
248+
require.ErrorContains(t, err, "error parsing image reference")
249+
require.ErrorIs(t, err, reconcile.TerminalError(nil))
250250
assert.NoDirExists(t, filepath.Join(unpacker.BaseCachePath, bundleSource.Name))
251251
}
252252

@@ -322,7 +322,7 @@ func TestCleanup(t *testing.T) {
322322

323323
// Clean up the bundle
324324
err := unpacker.Cleanup(context.Background(), bundleSource)
325-
assert.NoError(t, err)
325+
require.NoError(t, err)
326326
assert.NoDirExists(t, bundleDir)
327327
}
328328

0 commit comments

Comments
 (0)