Skip to content

Commit

Permalink
enable testifylint linter
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
  • Loading branch information
mmorel-35 committed Nov 10, 2024
1 parent aa9796d commit 06e4051
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 58 deletions.
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ linters:
- nolintlint
- predeclared
- revive
- testifylint
- typecheck
- unparam
disable:
Expand All @@ -56,3 +57,9 @@ linters-settings:
recommandations:
- io
- os
testifylint:
disable:
- expected-actual
- float-compare
- require-error
enabel-all: true
11 changes: 6 additions & 5 deletions mem/mem_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

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

func TestExVirtualMemory(t *testing.T) {
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions mem/mem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 17 additions & 16 deletions net/net_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

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

"github.com/shirou/gopsutil/v4/internal/common"
)
Expand All @@ -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: "},
Expand All @@ -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])
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand All @@ -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{}
Expand All @@ -125,7 +126,7 @@ func TestConnectionsMax(t *testing.T) {
}
}
for _, c := range cxByPid {
assert.True(t, c <= maxConn)
assert.LessOrEqual(t, c, maxConn)
}
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions process/process_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
45 changes: 22 additions & 23 deletions process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -682,50 +682,49 @@ 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()
}
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 {
Expand All @@ -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()
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 06e4051

Please sign in to comment.