Skip to content

chore: enable begin rule from thelper #1816

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

Merged
merged 1 commit into from
Mar 17, 2025
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
17 changes: 0 additions & 17 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,6 @@ linters-settings:
disabled: true
testifylint:
enable-all: true
thelper:
test:
# Check t.Helper() begins helper function.
# Default: true
begin: false
benchmark:
# Check b.Helper() begins helper function.
# Default: true
begin: false
tb:
# Check tb.Helper() begins helper function.
# Default: true
begin: false
fuzz:
# Check f.Helper() begins helper function.
# Default: true
begin: false
usetesting:
os-create-temp: false
os-mkdir-temp: false
Expand Down
6 changes: 4 additions & 2 deletions cpu/cpu_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

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

"github.com/shirou/gopsutil/v4/internal/common"
)

func TestTimesEmpty(t *testing.T) {
Expand Down Expand Up @@ -76,10 +78,10 @@ func TestCountsAgainstLscpu(t *testing.T) {
expectedPhysical := coresPerSocket * sockets * books * drawers
expectedLogical := expectedPhysical * threadsPerCore
physical, err := Counts(false)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
logical, err := Counts(true)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.Equalf(t, expectedPhysical, physical, "expected %v, got %v", expectedPhysical, physical)
assert.Equalf(t, expectedLogical, logical, "expected %v, got %v", expectedLogical, logical)
Expand Down
4 changes: 3 additions & 1 deletion cpu/cpu_plan9_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/shirou/gopsutil/v4/internal/common"
)

var timesTests = []struct {
Expand All @@ -35,7 +37,7 @@ func TestTimesPlan9(t *testing.T) {
t.Run(tt.mockedRootFS, func(t *testing.T) {
t.Setenv("HOST_ROOT", filepath.Join("testdata/plan9", tt.mockedRootFS))
stats, err := Times(false)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
eps := cmpopts.EquateApprox(0, 0.00000001)
assert.Truef(t, cmp.Equal(stats, tt.stats, eps), "got: %+v\nwant: %+v", stats, tt.stats)
Expand Down
29 changes: 12 additions & 17 deletions cpu/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package cpu

import (
"errors"
"fmt"
"os"
"runtime"
Expand All @@ -15,15 +14,9 @@ import (
"github.com/shirou/gopsutil/v4/internal/common"
)

func skipIfNotImplementedErr(t *testing.T, err error) {
if errors.Is(err, common.ErrNotImplementedError) {
t.Skip("not implemented")
}
}

func TestTimes(t *testing.T) {
v, err := Times(false)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.NotEmptyf(t, v, "could not get CPUs: %s", err)
empty := TimesStat{}
Expand All @@ -33,11 +26,11 @@ func TestTimes(t *testing.T) {

// test sum of per cpu stats is within margin of error for cpu total stats
cpuTotal, err := Times(false)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.NotEmptyf(t, cpuTotal, "could not get CPUs: %s", err)
perCPU, err := Times(true)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.NotEmptyf(t, perCPU, "could not get CPUs: %s", err)
var perCPUUserTimeSum float64
Expand Down Expand Up @@ -67,12 +60,12 @@ func TestTimes(t *testing.T) {

func TestCounts(t *testing.T) {
v, err := Counts(true)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.NotZerof(t, v, "could not get logical CPU counts: %v", v)
t.Logf("logical cores: %d", v)
v, err = Counts(false)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.NotZerof(t, v, "could not get physical CPU counts: %v", v)
t.Logf("physical cores: %d", v)
Expand All @@ -91,7 +84,7 @@ func TestTimeStat_String(t *testing.T) {

func TestInfo(t *testing.T) {
v, err := Info()
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.NotEmptyf(t, v, "could not get CPU Info")
for _, vv := range v {
Expand All @@ -100,13 +93,14 @@ func TestInfo(t *testing.T) {
}

func testPercent(t *testing.T, percpu bool) {
t.Helper()
numcpu := runtime.NumCPU()
testCount := 3

if runtime.GOOS != "windows" {
testCount = 100
v, err := Percent(time.Millisecond, percpu)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
// Skip CI which CPU num is different
if os.Getenv("CI") != "true" {
Expand All @@ -118,7 +112,7 @@ func testPercent(t *testing.T, percpu bool) {
for i := 0; i < testCount; i++ {
duration := time.Duration(10) * time.Microsecond
v, err := Percent(duration, percpu)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
for _, percent := range v {
// Check for slightly greater then 100% to account for any rounding issues.
Expand All @@ -130,13 +124,14 @@ func testPercent(t *testing.T, percpu bool) {
}

func testPercentLastUsed(t *testing.T, percpu bool) {
t.Helper()
numcpu := runtime.NumCPU()
testCount := 10

if runtime.GOOS != "windows" {
testCount = 2
v, err := Percent(time.Millisecond, percpu)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
// Skip CI which CPU num is different
if os.Getenv("CI") != "true" {
Expand All @@ -147,7 +142,7 @@ func testPercentLastUsed(t *testing.T, percpu bool) {
}
for i := 0; i < testCount; i++ {
v, err := Percent(0, percpu)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
time.Sleep(1 * time.Millisecond)
for _, percent := range v {
Expand Down
13 changes: 3 additions & 10 deletions disk/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package disk

import (
"errors"
"fmt"
"runtime"
"sync"
Expand All @@ -14,26 +13,20 @@ import (
"github.com/shirou/gopsutil/v4/internal/common"
)

func skipIfNotImplementedErr(t *testing.T, err error) {
if errors.Is(err, common.ErrNotImplementedError) {
t.Skip("not implemented")
}
}

func TestUsage(t *testing.T) {
path := "/"
if runtime.GOOS == "windows" {
path = "C:"
}
v, err := Usage(path)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.Equalf(t, v.Path, path, "error %v", err)
}

func TestPartitions(t *testing.T) {
ret, err := Partitions(false)
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
if err != nil || len(ret) == 0 {
t.Errorf("error %v", err)
}
Expand All @@ -47,7 +40,7 @@ func TestPartitions(t *testing.T) {

func TestIOCounters(t *testing.T) {
ret, err := IOCounters()
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.NotEmptyf(t, ret, "ret is empty")
empty := IOCountersStat{}
Expand Down
27 changes: 10 additions & 17 deletions host/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package host

import (
"errors"
"fmt"
"os"
"sync"
Expand All @@ -14,23 +13,17 @@ import (
"github.com/shirou/gopsutil/v4/internal/common"
)

func skipIfNotImplementedErr(t *testing.T, err error) {
if errors.Is(err, common.ErrNotImplementedError) {
t.Skip("not implemented")
}
}

func TestHostID(t *testing.T) {
v, err := HostID()
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.NotEmptyf(t, v, "Could not get host id %v", v)
t.Log(v)
}

func TestInfo(t *testing.T) {
v, err := Info()
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
empty := &InfoStat{}
assert.NotSamef(t, v, empty, "Could not get hostinfo %v", v)
Expand All @@ -44,7 +37,7 @@ func TestUptime(t *testing.T) {
}

v, err := Uptime()
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.NotZerof(t, v, "Could not get up time %v", v)
}
Expand All @@ -54,22 +47,22 @@ func TestBootTime(t *testing.T) {
t.Skip("Skip CI")
}
v, err := BootTime()
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.NotZerof(t, v, "Could not get boot time %v", v)
assert.GreaterOrEqualf(t, v, 946652400, "Invalid Boottime, older than 2000-01-01")
t.Logf("first boot time: %d", v)

v2, err := BootTime()
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.Equalf(t, v, v2, "cached boot time is different")
t.Logf("second boot time: %d", v2)
}

func TestUsers(t *testing.T) {
v, err := Users()
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
empty := UserStat{}
if len(v) == 0 {
Expand Down Expand Up @@ -109,7 +102,7 @@ func TestUserStat_String(t *testing.T) {

func TestGuid(t *testing.T) {
id, err := HostID()
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoError(t, err)
assert.NotEmptyf(t, id, "Host id is empty")
t.Logf("Host id value: %v", id)
Expand All @@ -123,7 +116,7 @@ func TestVirtualization(t *testing.T) {
go func(j int) {
system, role, err := Virtualization()
wg.Done()
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
assert.NoErrorf(t, err, "Virtualization() failed, %v", err)

if j == 9 {
Expand All @@ -136,7 +129,7 @@ func TestVirtualization(t *testing.T) {

func TestKernelVersion(t *testing.T) {
version, err := KernelVersion()
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoErrorf(t, err, "KernelVersion() failed, %v", err)
assert.NotEmptyf(t, version, "KernelVersion() returns empty: %s", version)

Expand All @@ -145,7 +138,7 @@ func TestKernelVersion(t *testing.T) {

func TestPlatformInformation(t *testing.T) {
platform, family, version, err := PlatformInformation()
skipIfNotImplementedErr(t, err)
common.SkipIfNotImplementedErr(t, err)
require.NoErrorf(t, err, "PlatformInformation() failed, %v", err)
assert.NotEmptyf(t, platform, "PlatformInformation() returns empty: %v", platform)

Expand Down
7 changes: 3 additions & 4 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ import (
)

var (
Timeout = 3 * time.Second
ErrTimeout = errors.New("command timed out")
Timeout = 3 * time.Second
ErrNotImplementedError = errors.New("not implemented yet")
ErrTimeout = errors.New("command timed out")
)

type Invoker interface {
Expand Down Expand Up @@ -97,8 +98,6 @@ func (i FakeInvoke) CommandWithContext(ctx context.Context, name string, arg ...
return i.Command(name, arg...)
}

var ErrNotImplementedError = errors.New("not implemented yet")

// ReadFile reads contents from a file
func ReadFile(filename string) (string, error) {
content, err := os.ReadFile(filename)
Expand Down
14 changes: 14 additions & 0 deletions internal/common/common_testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: BSD-3-Clause
package common

import (
"errors"
"testing"
)

func SkipIfNotImplementedErr(tb testing.TB, err error) {
tb.Helper()
if errors.Is(err, ErrNotImplementedError) {
tb.Skip("not implemented")
}
}
Loading