Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint: fix some testifylint warnings #4932

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ linters:
- nolintlint
- revive
- staticcheck
# - testifylint
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this commented out here? Is it just because it doesn't fix everything?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is only the first batch

- typecheck
- unused
- whitespace
Expand Down Expand Up @@ -74,6 +75,12 @@ linters-settings:
- G601 # Implicit memory aliasing in for loop (false positives)
config:
G306: "0644"
testifylint:
disable:
# disable rules that reduce the test condition
- "empty"
- "bool-compare"
- "len"

issues:
exclude-files:
Expand Down
8 changes: 4 additions & 4 deletions cache/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func TestManager(t *testing.T) {

fi, err := os.Stat(target)
require.NoError(t, err)
require.Equal(t, fi.IsDir(), true)
require.Equal(t, true, fi.IsDir())

err = lm.Unmount()
require.NoError(t, err)
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestManager(t *testing.T) {

checkDiskUsage(ctx, t, cm, 0, 0)

require.Equal(t, len(buf.all), 2)
require.Equal(t, 2, len(buf.all))

err = cm.Close()
require.NoError(t, err)
Expand Down Expand Up @@ -506,7 +506,7 @@ func TestSnapshotExtract(t *testing.T) {

checkDiskUsage(ctx, t, cm, 2, 0)

require.Equal(t, len(buf.all), 0)
require.Equal(t, 0, len(buf.all))

dirs, err = os.ReadDir(filepath.Join(tmpdir, "snapshots/snapshots"))
require.NoError(t, err)
Expand Down Expand Up @@ -1823,7 +1823,7 @@ func TestGetRemotes(t *testing.T) {
eg.Go(func() error {
remotes, err := ir.GetRemotes(egctx, false, refCfg, true, nil)
require.NoError(t, err)
require.True(t, len(remotes) > 0, "for %s : %d", compressionType, len(remotes))
require.Greater(t, len(remotes), 0, "for %s : %d", compressionType, len(remotes))
gotMain, gotVariants := remotes[0], remotes[1:]

// Check the main blob is compatible with all == false
Expand Down
8 changes: 4 additions & 4 deletions cache/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ func TestIndexes(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 2, len(sis))

require.Equal(t, sis[0].ID(), "foo1")
require.Equal(t, sis[1].ID(), "foo3")
require.Equal(t, "foo1", sis[0].ID())
require.Equal(t, "foo3", sis[1].ID())

sis, err = s.Search(ctx, "tag:bax")
require.NoError(t, err)
require.Equal(t, 1, len(sis))

require.Equal(t, sis[0].ID(), "foo2")
require.Equal(t, "foo2", sis[0].ID())

err = s.Clear("foo1")
require.NoError(t, err)
Expand All @@ -162,7 +162,7 @@ func TestIndexes(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 1, len(sis))

require.Equal(t, sis[0].ID(), "foo3")
require.Equal(t, "foo3", sis[0].ID())
}

func TestExternalData(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ func testPushByDigest(t *testing.T, sb integration.Sandbox) {

require.Equal(t, resp.ExporterResponse[exptypes.ExporterImageDigestKey], desc.Digest.String())
require.Equal(t, images.MediaTypeDockerSchema2Manifest, desc.MediaType)
require.True(t, desc.Size > 0)
require.Greater(t, desc.Size, int64(0))
}

func testSecurityMode(t *testing.T, sb integration.Sandbox) {
Expand Down Expand Up @@ -3354,7 +3354,7 @@ func testSourceDateEpochClamp(t *testing.T, sb integration.Sandbox) {
require.NoError(t, err)
busyboxTms := busyboxTmsX.FromImage

require.True(t, len(busyboxTms) > 1)
require.Greater(t, len(busyboxTms), 1)
bboxLayerLen := len(busyboxTms) - 1

tm, err := time.Parse(time.RFC3339Nano, busyboxTms[1])
Expand Down Expand Up @@ -4475,7 +4475,7 @@ func testBuildPushAndValidate(t *testing.T, sb integration.Sandbox) {
require.Equal(t, "layers", ociimg.RootFS.Type)
require.Equal(t, 3, len(ociimg.RootFS.DiffIDs))
require.NotNil(t, ociimg.Created)
require.True(t, time.Since(*ociimg.Created) < 2*time.Minute)
require.Less(t, time.Since(*ociimg.Created), 2*time.Minute)
require.Condition(t, func() bool {
for _, env := range ociimg.Config.Env {
if strings.HasPrefix(env, "PATH=") {
Expand Down Expand Up @@ -7766,7 +7766,7 @@ func checkAllReleasable(t *testing.T, c *Client, sb integration.Sandbox, checkCo
retries := 0
loop0:
for {
require.True(t, 20 > retries)
require.Greater(t, 20, retries)
retries++
du, err := c.DiskUsage(sb.Context())
require.NoError(t, err)
Expand Down Expand Up @@ -7814,7 +7814,7 @@ loop0:
if count == 0 {
break
}
require.True(t, 20 > retries)
require.Less(t, retries, 20)
retries++
time.Sleep(500 * time.Millisecond)
}
Expand Down
2 changes: 1 addition & 1 deletion client/llb/fileop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ func parseDef(t *testing.T, def [][]byte) (map[digest.Digest]pb.Op, []pb.Op) {
}

func last(t *testing.T, arr []pb.Op) (digest.Digest, int) {
require.True(t, len(arr) > 1)
require.Greater(t, len(arr), 1)

op := arr[len(arr)-1]
require.Equal(t, 1, len(op.Inputs))
Expand Down
2 changes: 1 addition & 1 deletion client/llb/llbbuild/llbbuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestMarshal(t *testing.T) {
require.NoError(t, err)

buildop := op.GetBuild()
require.NotEqual(t, buildop, nil)
require.NotNil(t, buildop)

require.Equal(t, len(op.Inputs), 1)
require.Equal(t, buildop.Builder, pb.LLBBuilder)
Expand Down
6 changes: 3 additions & 3 deletions client/llb/llbtest/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestCustomPlatform(t *testing.T) {
e, err := llbsolver.Load(context.TODO(), def.ToPB(), nil)
require.NoError(t, err)

require.Equal(t, depth(e), 5)
require.Equal(t, 5, depth(e))

expected := ocispecs.Platform{OS: "windows", Architecture: "amd64"}
require.Equal(t, expected, platform(e))
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestDefaultPlatform(t *testing.T) {
e, err := llbsolver.Load(context.TODO(), def.ToPB(), nil)
require.NoError(t, err)

require.Equal(t, depth(e), 2)
require.Equal(t, 2, depth(e))

// needs extra normalize for default spec
// https://github.com/moby/buildkit/pull/2427#issuecomment-952301867
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestPlatformMixed(t *testing.T) {
e, err := llbsolver.Load(context.TODO(), def.ToPB(), nil)
require.NoError(t, err)

require.Equal(t, depth(e), 4)
require.Equal(t, 4, depth(e))

expectedAmd := ocispecs.Platform{OS: "linux", Architecture: "amd64"}
require.Equal(t, []string{"cmd-main"}, args(e))
Expand Down
4 changes: 2 additions & 2 deletions control/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ func TestParseCacheExportIgnoreError(t *testing.T) {
t.Run(ignoreErrStr, func(t *testing.T) {
ignoreErr, supported := parseCacheExportIgnoreError(ignoreErrStr)
t.Log("checking expectedIgnoreError")
require.Equal(t, ignoreErr, test.expectedIgnoreError)
require.Equal(t, test.expectedIgnoreError, ignoreErr)
t.Log("checking expectedSupported")
require.Equal(t, supported, test.expectedSupported)
require.Equal(t, test.expectedSupported, supported)
})
}
}
2 changes: 1 addition & 1 deletion executor/oci/mounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestWithRemovedMounts(t *testing.T) {

oldLen := len(s.Mounts)
err := withRemovedMount("/run")(appcontext.Context(), nil, nil, &s)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, oldLen-1, len(s.Mounts))
}

Expand Down
7 changes: 4 additions & 3 deletions executor/resources/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

resourcestypes "github.com/moby/buildkit/executor/resources/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParseIOStat(t *testing.T) {
Expand All @@ -15,15 +16,15 @@ func TestParseIOStat(t *testing.T) {
ioStatContents := `8:0 rbytes=1024 wbytes=2048 dbytes=4096 rios=16 wios=32 dios=64
8:1 rbytes=512 wbytes=1024 dbytes=2048 rios=8 wios=16 dios=32`
err := os.WriteFile(filepath.Join(testDir, "io.stat"), []byte(ioStatContents), 0644)
assert.NoError(t, err)
require.NoError(t, err)

ioPressureContents := `some avg10=1.23 avg60=4.56 avg300=7.89 total=3031
full avg10=0.12 avg60=0.34 avg300=0.56 total=9876`
err = os.WriteFile(filepath.Join(testDir, "io.pressure"), []byte(ioPressureContents), 0644)
assert.NoError(t, err)
require.NoError(t, err)

ioStat, err := getCgroupIOStat(testDir)
assert.NoError(t, err)
require.NoError(t, err)

var expectedPressure = &resourcestypes.Pressure{
Some: &resourcestypes.PressureValues{
Expand Down
17 changes: 9 additions & 8 deletions frontend/dockerfile/dockerfile2llb/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/moby/buildkit/util/appcontext"
digest "github.com/opencontainers/go-digest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func toEnvMap(args []instructions.KeyValuePairOptional, env []string) map[string]string {
Expand All @@ -35,7 +36,7 @@ COPY f1 f2 /sub/
RUN ls -l
`
_, _, _, _, err := Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
assert.NoError(t, err)
require.NoError(t, err)

df = `FROM scratch AS foo
ENV FOO bar
Expand All @@ -44,7 +45,7 @@ COPY --from=foo f1 /
COPY --from=0 f2 /
`
_, _, _, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
assert.NoError(t, err)
require.NoError(t, err)

df = `FROM scratch AS foo
ENV FOO bar
Expand All @@ -57,20 +58,20 @@ COPY --from=0 f2 /
Target: "Foo",
},
})
assert.NoError(t, err)
require.NoError(t, err)

_, _, _, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{
Config: dockerui.Config{
Target: "nosuch",
},
})
assert.Error(t, err)
require.Error(t, err)

df = `FROM scratch
ADD http://github.com/moby/buildkit/blob/master/README.md /
`
_, _, _, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
assert.NoError(t, err)
require.NoError(t, err)

df = `FROM scratch
COPY http://github.com/moby/buildkit/blob/master/README.md /
Expand All @@ -80,11 +81,11 @@ COPY --from=0 f2 /

df = `FROM "" AS foo`
_, _, _, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
assert.Error(t, err)
require.Error(t, err)

df = `FROM ${BLANK} AS foo`
_, _, _, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
assert.Error(t, err)
require.Error(t, err)
}

func TestDockerfileParsingMarshal(t *testing.T) {
Expand Down Expand Up @@ -219,7 +220,7 @@ FROM foo AS bar
RUN echo bar
`
_, _, baseImg, _, err := Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
assert.NoError(t, err)
require.NoError(t, err)
t.Logf("baseImg=%+v", baseImg)
assert.Equal(t, []digest.Digest{"sha256:2e112031b4b923a873c8b3d685d48037e4d5ccd967b658743d93a6e56c3064b9"}, baseImg.RootFS.DiffIDs)
assert.Equal(t, "2024-01-17 21:49:12 +0000 UTC", baseImg.Created.String())
Expand Down
2 changes: 1 addition & 1 deletion frontend/dockerfile/dockerfile_outline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ COPY Dockerfile Dockerfile
reqs, err := subrequests.Describe(ctx, c)
require.NoError(t, err)

require.True(t, len(reqs) > 0)
require.Greater(t, len(reqs), 0)

hasOutline := false

Expand Down
4 changes: 2 additions & 2 deletions frontend/dockerfile/dockerfile_provenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ RUN echo "ok" > /foo
require.Equal(t, "dockerfile", pred.Invocation.Parameters.Locals[1].Name)

require.NotNil(t, pred.Metadata.BuildFinishedOn)
require.True(t, time.Since(*pred.Metadata.BuildFinishedOn) < 5*time.Minute)
require.Less(t, time.Since(*pred.Metadata.BuildFinishedOn), 5*time.Minute)
require.NotNil(t, pred.Metadata.BuildStartedOn)
require.True(t, time.Since(*pred.Metadata.BuildStartedOn) < 5*time.Minute)
require.Less(t, time.Since(*pred.Metadata.BuildStartedOn), 5*time.Minute)
require.True(t, pred.Metadata.BuildStartedOn.Before(*pred.Metadata.BuildFinishedOn))

require.True(t, pred.Metadata.Completeness.Environment)
Expand Down
2 changes: 1 addition & 1 deletion frontend/dockerfile/dockerfile_targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ COPY Dockerfile Dockerfile
reqs, err := subrequests.Describe(ctx, c)
require.NoError(t, err)

require.True(t, len(reqs) > 0)
require.Greater(t, len(reqs), 0)

hasTargets := false

Expand Down
4 changes: 2 additions & 2 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5075,7 +5075,7 @@ COPY Dockerfile Dockerfile
reqs, err := subrequests.Describe(ctx, c)
require.NoError(t, err)

require.True(t, len(reqs) > 0)
require.Greater(t, len(reqs), 0)

hasDescribe := false

Expand All @@ -5084,7 +5084,7 @@ COPY Dockerfile Dockerfile
hasDescribe = true
require.Equal(t, subrequests.RequestType("rpc"), req.Type)
require.NotEqual(t, req.Version, "")
require.True(t, len(req.Metadata) > 0)
require.Greater(t, len(req.Metadata), 0)
require.Equal(t, "result.json", req.Metadata[0].Name)
}
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/dockerfile/instructions/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func TestRunCmdFlagsUsed(t *testing.T) {
n := ast.AST.Children[0]
c, err := ParseInstruction(n)
require.NoError(t, err)
require.IsType(t, c, &RunCommand{})
require.IsType(t, &RunCommand{}, c)
require.Equal(t, []string{"mount"}, c.(*RunCommand).FlagsUsed)
}

Expand Down
15 changes: 12 additions & 3 deletions session/filesync/filesync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/testutil"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tonistiigi/fsutil"
Expand Down Expand Up @@ -47,6 +48,13 @@ func TestFileSyncIncludePatterns(t *testing.T) {
})

g.Go(func() (reterr error) {
defer func() {
err := s.Close()
if reterr == nil {
reterr = err
}
}()

c, err := m.Get(ctx, s.ID(), false)
if err != nil {
return err
Expand All @@ -59,15 +67,16 @@ func TestFileSyncIncludePatterns(t *testing.T) {
return err
}

_, err = os.ReadFile(filepath.Join(destDir, "foo"))
assert.Error(t, err)
if _, err := os.ReadFile(filepath.Join(destDir, "foo")); err == nil {
return errors.Errorf("expected error reading foo")
}

dt, err := os.ReadFile(filepath.Join(destDir, "bar"))
if err != nil {
return err
}
assert.Equal(t, "content2", string(dt))
return s.Close()
return nil
})

err = g.Wait()
Expand Down
Loading
Loading