From 06e4051c91e4919df51aa9b982c52daa221ab862 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Sun, 10 Nov 2024 09:17:03 +0100 Subject: [PATCH] enable testifylint linter Signed-off-by: Matthieu MOREL --- .golangci.yml | 7 ++++++ mem/mem_linux_test.go | 11 +++++---- mem/mem_test.go | 6 ++--- net/net_linux_test.go | 33 ++++++++++++------------- process/process_linux_test.go | 22 ++++++++--------- process/process_test.go | 45 +++++++++++++++++------------------ 6 files changed, 66 insertions(+), 58 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 5db0a31e8..00ceb01af 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -35,6 +35,7 @@ linters: - nolintlint - predeclared - revive + - testifylint - typecheck - unparam disable: @@ -56,3 +57,9 @@ linters-settings: recommandations: - io - os + testifylint: + disable: + - expected-actual + - float-compare + - require-error + enabel-all: true \ No newline at end of file diff --git a/mem/mem_linux_test.go b/mem/mem_linux_test.go index 390621d5e..b71b71c79 100644 --- a/mem/mem_linux_test.go +++ b/mem/mem_linux_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestExVirtualMemory(t *testing.T) { @@ -152,19 +153,19 @@ const invalidFile = `INVALID Type Size Used Priority func TestParseSwapsFile_ValidFile(t *testing.T) { assert := assert.New(t) stats, err := parseSwapsFile(context.Background(), strings.NewReader(validFile)) - assert.NoError(err) + require.NoError(t, err) - assert.Equal(*stats[0], SwapDevice{ + assert.Equal(SwapDevice{ Name: "/dev/dm-2", UsedBytes: 502566912, FreeBytes: 68128825344, - }) + }, *stats[0]) - assert.Equal(*stats[1], SwapDevice{ + assert.Equal(SwapDevice{ Name: "/swapfile", UsedBytes: 1024, FreeBytes: 1024, - }) + }, *stats[1]) } func TestParseSwapsFile_InvalidFile(t *testing.T) { diff --git a/mem/mem_test.go b/mem/mem_test.go index c069a4a10..8d449dafa 100644 --- a/mem/mem_test.go +++ b/mem/mem_test.go @@ -34,9 +34,9 @@ func TestVirtualMemory(t *testing.T) { } t.Log(v) - assert.True(t, v.Total > 0) - assert.True(t, v.Available > 0) - assert.True(t, v.Used > 0) + assert.Positive(t, v.Total) + assert.Positive(t, v.Available) + assert.Positive(t, v.Used) total := v.Used + v.Free + v.Buffers + v.Cached totalStr := "used + free + buffers + cached" diff --git a/net/net_linux_test.go b/net/net_linux_test.go index 0dba51e4a..9552dd063 100644 --- a/net/net_linux_test.go +++ b/net/net_linux_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/shirou/gopsutil/v4/internal/common" ) @@ -20,7 +21,7 @@ func TestIOCountersByFileParsing(t *testing.T) { tmpfile, err := os.CreateTemp("", "proc_dev_net") defer os.Remove(tmpfile.Name()) // clean up - assert.Nil(t, err, "Temporary file creation failed: ", err) + require.NoError(t, err, "Temporary file creation failed: ", err) cases := [4][2]string{ {"eth0: ", "eth1: "}, @@ -30,7 +31,7 @@ func TestIOCountersByFileParsing(t *testing.T) { } for _, testCase := range cases { err = tmpfile.Truncate(0) - assert.Nil(t, err, "Temporary file truncating problem: ", err) + require.NoError(t, err, "Temporary file truncating problem: ", err) // Parse interface name for assertion interface0 := strings.TrimSpace(testCase[0]) @@ -44,13 +45,13 @@ func TestIOCountersByFileParsing(t *testing.T) { // Write /proc/net/dev sample output _, err = tmpfile.Write(proc) - assert.Nil(t, err, "Temporary file writing failed: ", err) + require.NoError(t, err, "Temporary file writing failed: ", err) counters, err := IOCountersByFile(true, tmpfile.Name()) - assert.Nil(t, err) + require.NoError(t, err) assert.NotEmpty(t, counters) - assert.Equal(t, 2, len(counters)) + assert.Len(t, counters, 2) assert.Equal(t, interface0, counters[0].Name) assert.Equal(t, uint64(1), counters[0].BytesRecv) assert.Equal(t, uint64(2), counters[0].PacketsRecv) @@ -76,7 +77,7 @@ func TestIOCountersByFileParsing(t *testing.T) { } err = tmpfile.Close() - assert.Nil(t, err, "Temporary file closing failed: ", err) + assert.NoError(t, err, "Temporary file closing failed: ", err) } func TestGetProcInodesAll(t *testing.T) { @@ -104,7 +105,7 @@ func TestGetProcInodesAll(t *testing.T) { root := common.HostProcWithContext(context.Background(), "") v, err := getProcInodesAll(root, 0) - assert.Nil(t, err) + require.NoError(t, err) assert.NotEmpty(t, v) } @@ -115,7 +116,7 @@ func TestConnectionsMax(t *testing.T) { maxConn := 10 v, err := ConnectionsMax("tcp", maxConn) - assert.Nil(t, err) + require.NoError(t, err) assert.NotEmpty(t, v) cxByPid := map[int32]int{} @@ -125,7 +126,7 @@ func TestConnectionsMax(t *testing.T) { } } for _, c := range cxByPid { - assert.True(t, c <= maxConn) + assert.LessOrEqual(t, c, maxConn) } } @@ -180,9 +181,9 @@ func TestDecodeAddress(t *testing.T) { } addr, err := decodeAddress(uint32(family), src) if dst.Error { - assert.NotNil(err, src) + assert.Error(err, src) } else { - assert.Nil(err, src) + require.NoError(t, err, src) assert.Equal(dst.IP, addr.IP, src) assert.Equal(dst.Port, int(addr.Port), src) } @@ -197,7 +198,7 @@ func TestReverse(t *testing.T) { func TestConntrackStatFileParsing(t *testing.T) { tmpfile, err := os.CreateTemp("", "proc_net_stat_conntrack") defer os.Remove(tmpfile.Name()) - assert.Nil(t, err, "Temporary file creation failed: ", err) + require.NoError(t, err, "Temporary file creation failed: ", err) data := []byte(` entries searched found new invalid ignore delete deleteList insert insertFailed drop earlyDrop icmpError expectNew expectCreate expectDelete searchRestart @@ -246,12 +247,12 @@ entries searched found new invalid ignore delete deleteList insert insertFailed // Write data to tempfile _, err = tmpfile.Write(data) - assert.Nil(t, err, "Temporary file writing failed: ", err) + require.NoError(t, err, "Temporary file writing failed: ", err) // Function under test stats, err := conntrackStatsFromFile(tmpfile.Name(), true) - assert.Nil(t, err) - assert.Equal(t, 8, len(stats), "Expected 8 results") + require.NoError(t, err) + assert.Len(t, stats, 8, "Expected 8 results") summary := &ConntrackStat{} for i, exp := range slist.Items() { @@ -311,7 +312,7 @@ entries searched found new invalid ignore delete deleteList insert insertFailed // Test summary grouping totals, err := conntrackStatsFromFile(tmpfile.Name(), false) - assert.Nil(t, err) + require.NoError(t, err) for i, st := range totals { assert.Equal(t, summary.Entries, st.Entries) assert.Equal(t, summary.Searched, st.Searched) diff --git a/process/process_linux_test.go b/process/process_linux_test.go index 19e42a172..71364676b 100644 --- a/process/process_linux_test.go +++ b/process/process_linux_test.go @@ -73,23 +73,23 @@ func TestSplitProcStat_fromFile(t *testing.T) { continue } contents, err := os.ReadFile(statFile) - assert.NoError(t, err) + require.NoError(t, err) pidStr := strconv.Itoa(int(pid)) ppid := "68044" // TODO: how to pass ppid to test? fields := splitProcStat(contents) - assert.Equal(t, fields[1], pidStr) - assert.Equal(t, fields[2], "test(cmd).sh") - assert.Equal(t, fields[3], "S") - assert.Equal(t, fields[4], ppid) - assert.Equal(t, fields[5], pidStr) // pgrp - assert.Equal(t, fields[6], ppid) // session - assert.Equal(t, fields[8], pidStr) // tpgrp - assert.Equal(t, fields[18], "20") // priority - assert.Equal(t, fields[20], "1") // num threads - assert.Equal(t, fields[52], "0") // exit code + assert.Equal(t, pidStr, fields[1]) + assert.Equal(t, "test(cmd).sh", fields[2]) + assert.Equal(t, "S", fields[3]) + assert.Equal(t, ppid, fields[4]) + assert.Equal(t, pidStr, fields[5]) // pgrp + assert.Equal(t, ppid, fields[6]) // session + assert.Equal(t, pidStr, fields[8]) // tpgrp + assert.Equal(t, "20", fields[18]) // priority + assert.Equal(t, "1", fields[20]) // num threads + assert.Equal(t, "0", fields[52]) // exit code } } diff --git a/process/process_test.go b/process/process_test.go index abc3dbdf0..225b411a3 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -336,11 +336,11 @@ func TestLong_Name_With_Spaces(t *testing.T) { cmd := exec.Command(tmpfile.Name() + ".exe") - assert.Nil(t, cmd.Start()) + require.NoError(t, cmd.Start()) time.Sleep(100 * time.Millisecond) p, err := NewProcess(int32(cmd.Process.Pid)) skipIfNotImplementedErr(t, err) - assert.Nil(t, err) + require.NoError(t, err) n, err := p.Name() skipIfNotImplementedErr(t, err) @@ -383,11 +383,11 @@ func TestLong_Name(t *testing.T) { cmd := exec.Command(tmpfile.Name() + ".exe") - assert.Nil(t, cmd.Start()) + require.NoError(t, cmd.Start()) time.Sleep(100 * time.Millisecond) p, err := NewProcess(int32(cmd.Process.Pid)) skipIfNotImplementedErr(t, err) - assert.Nil(t, err) + require.NoError(t, err) n, err := p.Name() skipIfNotImplementedErr(t, err) @@ -642,7 +642,7 @@ func TestChildren(t *testing.T) { } else { cmd = exec.Command("sleep", "3") } - assert.Nil(t, cmd.Start()) + require.NoError(t, cmd.Start()) time.Sleep(100 * time.Millisecond) c, err := p.Children() @@ -682,23 +682,23 @@ func TestCPUTimes(t *testing.T) { pid := os.Getpid() process, err := NewProcess(int32(pid)) skipIfNotImplementedErr(t, err) - assert.Nil(t, err) + require.NoError(t, err) spinSeconds := 0.2 cpuTimes0, err := process.Times() skipIfNotImplementedErr(t, err) - assert.Nil(t, err) + require.NoError(t, err) // Spin for a duration of spinSeconds t0 := time.Now() tGoal := t0.Add(time.Duration(spinSeconds*1000) * time.Millisecond) - assert.Nil(t, err) + require.NoError(t, err) for time.Now().Before(tGoal) { // This block intentionally left blank } cpuTimes1, err := process.Times() - assert.Nil(t, err) + require.NoError(t, err) if cpuTimes0 == nil || cpuTimes1 == nil { t.FailNow() @@ -706,26 +706,25 @@ func TestCPUTimes(t *testing.T) { measuredElapsed := cpuTimes1.Total() - cpuTimes0.Total() message := fmt.Sprintf("Measured %fs != spun time of %fs\ncpuTimes0=%v\ncpuTimes1=%v", measuredElapsed, spinSeconds, cpuTimes0, cpuTimes1) - assert.True(t, measuredElapsed > float64(spinSeconds)/5, message) - assert.True(t, measuredElapsed < float64(spinSeconds)*5, message) + assert.Greater(t, measuredElapsed, float64(spinSeconds)/5, message) + assert.Less(t, measuredElapsed, float64(spinSeconds)*5, message) } func TestOpenFiles(t *testing.T) { fp, err := os.Open("process_test.go") - assert.Nil(t, err) + require.NoError(t, err) defer func() { - err := fp.Close() - assert.Nil(t, err) + assert.NoError(t, fp.Close()) }() pid := os.Getpid() p, err := NewProcess(int32(pid)) skipIfNotImplementedErr(t, err) - assert.Nil(t, err) + require.NoError(t, err) v, err := p.OpenFiles() skipIfNotImplementedErr(t, err) - assert.Nil(t, err) + require.NoError(t, err) assert.NotEmpty(t, v) // test always open files. for _, vv := range v { @@ -740,14 +739,14 @@ func TestKill(t *testing.T) { } else { cmd = exec.Command("sleep", "3") } - assert.Nil(t, cmd.Start()) + require.NoError(t, cmd.Start()) time.Sleep(100 * time.Millisecond) p, err := NewProcess(int32(cmd.Process.Pid)) skipIfNotImplementedErr(t, err) - assert.Nil(t, err) + require.NoError(t, err) err = p.Kill() skipIfNotImplementedErr(t, err) - assert.Nil(t, err) + require.NoError(t, err) cmd.Wait() } @@ -761,7 +760,7 @@ func TestIsRunning(t *testing.T) { cmd.Start() p, err := NewProcess(int32(cmd.Process.Pid)) skipIfNotImplementedErr(t, err) - assert.Nil(t, err) + require.NoError(t, err) running, err := p.IsRunning() skipIfNotImplementedErr(t, err) if err != nil { @@ -812,12 +811,12 @@ func TestEnviron(t *testing.T) { cmd.Env = []string{"testkey=envvalue"} - assert.Nil(t, cmd.Start()) + require.NoError(t, cmd.Start()) defer cmd.Process.Kill() time.Sleep(100 * time.Millisecond) p, err := NewProcess(int32(cmd.Process.Pid)) skipIfNotImplementedErr(t, err) - assert.Nil(t, err) + require.NoError(t, err) envs, err := p.Environ() skipIfNotImplementedErr(t, err) @@ -877,6 +876,6 @@ func BenchmarkProcesses(b *testing.B) { for i := 0; i < b.N; i++ { ps, err := Processes() require.NoError(b, err) - require.Greater(b, len(ps), 0) + require.NotEmpty(b, ps) } }