Skip to content

Commit a17e57a

Browse files
committed
chore: enable begin rule from thelper
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 62da883 commit a17e57a

18 files changed

+123
-168
lines changed

.golangci.yml

-17
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,6 @@ linters-settings:
102102
disabled: true
103103
testifylint:
104104
enable-all: true
105-
thelper:
106-
test:
107-
# Check t.Helper() begins helper function.
108-
# Default: true
109-
begin: false
110-
benchmark:
111-
# Check b.Helper() begins helper function.
112-
# Default: true
113-
begin: false
114-
tb:
115-
# Check tb.Helper() begins helper function.
116-
# Default: true
117-
begin: false
118-
fuzz:
119-
# Check f.Helper() begins helper function.
120-
# Default: true
121-
begin: false
122105
usetesting:
123106
os-create-temp: false
124107
os-mkdir-temp: false

cpu/cpu_linux_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"strconv"
88
"strings"
99
"testing"
10+
11+
"github.com/shirou/gopsutil/v4/internal/common"
1012
)
1113

1214
func TestTimesEmpty(t *testing.T) {
@@ -77,12 +79,12 @@ func TestCountsAgainstLscpu(t *testing.T) {
7779
expectedPhysical := coresPerSocket * sockets * books * drawers
7880
expectedLogical := expectedPhysical * threadsPerCore
7981
physical, err := Counts(false)
80-
skipIfNotImplementedErr(t, err)
82+
common.SkipIfNotImplementedErr(t, err)
8183
if err != nil {
8284
t.Errorf("error %v", err)
8385
}
8486
logical, err := Counts(true)
85-
skipIfNotImplementedErr(t, err)
87+
common.SkipIfNotImplementedErr(t, err)
8688
if err != nil {
8789
t.Errorf("error %v", err)
8890
}

cpu/cpu_plan9_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99

1010
"github.com/google/go-cmp/cmp"
1111
"github.com/google/go-cmp/cmp/cmpopts"
12+
13+
"github.com/shirou/gopsutil/v4/internal/common"
1214
)
1315

1416
var timesTests = []struct {
@@ -33,7 +35,7 @@ func TestTimesPlan9(t *testing.T) {
3335
t.Run(tt.mockedRootFS, func(t *testing.T) {
3436
t.Setenv("HOST_ROOT", filepath.Join("testdata/plan9", tt.mockedRootFS))
3537
stats, err := Times(false)
36-
skipIfNotImplementedErr(t, err)
38+
common.SkipIfNotImplementedErr(t, err)
3739
if err != nil {
3840
t.Errorf("error %v", err)
3941
}

cpu/cpu_test.go

+12-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package cpu
33

44
import (
5-
"errors"
65
"fmt"
76
"os"
87
"runtime"
@@ -14,15 +13,9 @@ import (
1413
"github.com/shirou/gopsutil/v4/internal/common"
1514
)
1615

17-
func skipIfNotImplementedErr(t *testing.T, err error) {
18-
if errors.Is(err, common.ErrNotImplementedError) {
19-
t.Skip("not implemented")
20-
}
21-
}
22-
2316
func TestTimes(t *testing.T) {
2417
v, err := Times(false)
25-
skipIfNotImplementedErr(t, err)
18+
common.SkipIfNotImplementedErr(t, err)
2619
if err != nil {
2720
t.Errorf("error %v", err)
2821
}
@@ -38,15 +31,15 @@ func TestTimes(t *testing.T) {
3831

3932
// test sum of per cpu stats is within margin of error for cpu total stats
4033
cpuTotal, err := Times(false)
41-
skipIfNotImplementedErr(t, err)
34+
common.SkipIfNotImplementedErr(t, err)
4235
if err != nil {
4336
t.Errorf("error %v", err)
4437
}
4538
if len(cpuTotal) == 0 {
4639
t.Error("could not get CPUs", err)
4740
}
4841
perCPU, err := Times(true)
49-
skipIfNotImplementedErr(t, err)
42+
common.SkipIfNotImplementedErr(t, err)
5043
if err != nil {
5144
t.Errorf("error %v", err)
5245
}
@@ -80,7 +73,7 @@ func TestTimes(t *testing.T) {
8073

8174
func TestCounts(t *testing.T) {
8275
v, err := Counts(true)
83-
skipIfNotImplementedErr(t, err)
76+
common.SkipIfNotImplementedErr(t, err)
8477
if err != nil {
8578
t.Errorf("error %v", err)
8679
}
@@ -89,7 +82,7 @@ func TestCounts(t *testing.T) {
8982
}
9083
t.Logf("logical cores: %d", v)
9184
v, err = Counts(false)
92-
skipIfNotImplementedErr(t, err)
85+
common.SkipIfNotImplementedErr(t, err)
9386
if err != nil {
9487
t.Errorf("error %v", err)
9588
}
@@ -114,7 +107,7 @@ func TestTimeStat_String(t *testing.T) {
114107

115108
func TestInfo(t *testing.T) {
116109
v, err := Info()
117-
skipIfNotImplementedErr(t, err)
110+
common.SkipIfNotImplementedErr(t, err)
118111
if err != nil {
119112
t.Errorf("error %v", err)
120113
}
@@ -129,13 +122,14 @@ func TestInfo(t *testing.T) {
129122
}
130123

131124
func testPercent(t *testing.T, percpu bool) {
125+
t.Helper()
132126
numcpu := runtime.NumCPU()
133127
testCount := 3
134128

135129
if runtime.GOOS != "windows" {
136130
testCount = 100
137131
v, err := Percent(time.Millisecond, percpu)
138-
skipIfNotImplementedErr(t, err)
132+
common.SkipIfNotImplementedErr(t, err)
139133
if err != nil {
140134
t.Errorf("error %v", err)
141135
}
@@ -149,7 +143,7 @@ func testPercent(t *testing.T, percpu bool) {
149143
for i := 0; i < testCount; i++ {
150144
duration := time.Duration(10) * time.Microsecond
151145
v, err := Percent(duration, percpu)
152-
skipIfNotImplementedErr(t, err)
146+
common.SkipIfNotImplementedErr(t, err)
153147
if err != nil {
154148
t.Errorf("error %v", err)
155149
}
@@ -163,13 +157,14 @@ func testPercent(t *testing.T, percpu bool) {
163157
}
164158

165159
func testPercentLastUsed(t *testing.T, percpu bool) {
160+
t.Helper()
166161
numcpu := runtime.NumCPU()
167162
testCount := 10
168163

169164
if runtime.GOOS != "windows" {
170165
testCount = 2
171166
v, err := Percent(time.Millisecond, percpu)
172-
skipIfNotImplementedErr(t, err)
167+
common.SkipIfNotImplementedErr(t, err)
173168
if err != nil {
174169
t.Errorf("error %v", err)
175170
}
@@ -182,7 +177,7 @@ func testPercentLastUsed(t *testing.T, percpu bool) {
182177
}
183178
for i := 0; i < testCount; i++ {
184179
v, err := Percent(0, percpu)
185-
skipIfNotImplementedErr(t, err)
180+
common.SkipIfNotImplementedErr(t, err)
186181
if err != nil {
187182
t.Errorf("error %v", err)
188183
}

disk/disk_test.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package disk
33

44
import (
5-
"errors"
65
"fmt"
76
"runtime"
87
"sync"
@@ -11,19 +10,13 @@ import (
1110
"github.com/shirou/gopsutil/v4/internal/common"
1211
)
1312

14-
func skipIfNotImplementedErr(t *testing.T, err error) {
15-
if errors.Is(err, common.ErrNotImplementedError) {
16-
t.Skip("not implemented")
17-
}
18-
}
19-
2013
func TestUsage(t *testing.T) {
2114
path := "/"
2215
if runtime.GOOS == "windows" {
2316
path = "C:"
2417
}
2518
v, err := Usage(path)
26-
skipIfNotImplementedErr(t, err)
19+
common.SkipIfNotImplementedErr(t, err)
2720
if err != nil {
2821
t.Errorf("error %v", err)
2922
}
@@ -34,7 +27,7 @@ func TestUsage(t *testing.T) {
3427

3528
func TestPartitions(t *testing.T) {
3629
ret, err := Partitions(false)
37-
skipIfNotImplementedErr(t, err)
30+
common.SkipIfNotImplementedErr(t, err)
3831
if err != nil || len(ret) == 0 {
3932
t.Errorf("error %v", err)
4033
}
@@ -52,7 +45,7 @@ func TestPartitions(t *testing.T) {
5245

5346
func TestIOCounters(t *testing.T) {
5447
ret, err := IOCounters()
55-
skipIfNotImplementedErr(t, err)
48+
common.SkipIfNotImplementedErr(t, err)
5649
if err != nil {
5750
t.Errorf("error %v", err)
5851
}

host/host_test.go

+10-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package host
33

44
import (
5-
"errors"
65
"fmt"
76
"os"
87
"sync"
@@ -11,15 +10,9 @@ import (
1110
"github.com/shirou/gopsutil/v4/internal/common"
1211
)
1312

14-
func skipIfNotImplementedErr(t *testing.T, err error) {
15-
if errors.Is(err, common.ErrNotImplementedError) {
16-
t.Skip("not implemented")
17-
}
18-
}
19-
2013
func TestHostID(t *testing.T) {
2114
v, err := HostID()
22-
skipIfNotImplementedErr(t, err)
15+
common.SkipIfNotImplementedErr(t, err)
2316
if err != nil {
2417
t.Errorf("error %v", err)
2518
}
@@ -31,7 +24,7 @@ func TestHostID(t *testing.T) {
3124

3225
func TestInfo(t *testing.T) {
3326
v, err := Info()
34-
skipIfNotImplementedErr(t, err)
27+
common.SkipIfNotImplementedErr(t, err)
3528
if err != nil {
3629
t.Errorf("error %v", err)
3730
}
@@ -51,7 +44,7 @@ func TestUptime(t *testing.T) {
5144
}
5245

5346
v, err := Uptime()
54-
skipIfNotImplementedErr(t, err)
47+
common.SkipIfNotImplementedErr(t, err)
5548
if err != nil {
5649
t.Errorf("error %v", err)
5750
}
@@ -65,7 +58,7 @@ func TestBootTime(t *testing.T) {
6558
t.Skip("Skip CI")
6659
}
6760
v, err := BootTime()
68-
skipIfNotImplementedErr(t, err)
61+
common.SkipIfNotImplementedErr(t, err)
6962
if err != nil {
7063
t.Errorf("error %v", err)
7164
}
@@ -78,7 +71,7 @@ func TestBootTime(t *testing.T) {
7871
t.Logf("first boot time: %d", v)
7972

8073
v2, err := BootTime()
81-
skipIfNotImplementedErr(t, err)
74+
common.SkipIfNotImplementedErr(t, err)
8275
if err != nil {
8376
t.Errorf("error %v", err)
8477
}
@@ -90,7 +83,7 @@ func TestBootTime(t *testing.T) {
9083

9184
func TestUsers(t *testing.T) {
9285
v, err := Users()
93-
skipIfNotImplementedErr(t, err)
86+
common.SkipIfNotImplementedErr(t, err)
9487
if err != nil {
9588
t.Errorf("error %v", err)
9689
}
@@ -138,7 +131,7 @@ func TestUserStat_String(t *testing.T) {
138131

139132
func TestGuid(t *testing.T) {
140133
id, err := HostID()
141-
skipIfNotImplementedErr(t, err)
134+
common.SkipIfNotImplementedErr(t, err)
142135
if err != nil {
143136
t.Error(err)
144137
}
@@ -157,7 +150,7 @@ func TestVirtualization(t *testing.T) {
157150
go func(j int) {
158151
system, role, err := Virtualization()
159152
wg.Done()
160-
skipIfNotImplementedErr(t, err)
153+
common.SkipIfNotImplementedErr(t, err)
161154
if err != nil {
162155
t.Errorf("Virtualization() failed, %v", err)
163156
}
@@ -172,7 +165,7 @@ func TestVirtualization(t *testing.T) {
172165

173166
func TestKernelVersion(t *testing.T) {
174167
version, err := KernelVersion()
175-
skipIfNotImplementedErr(t, err)
168+
common.SkipIfNotImplementedErr(t, err)
176169
if err != nil {
177170
t.Errorf("KernelVersion() failed, %v", err)
178171
}
@@ -185,7 +178,7 @@ func TestKernelVersion(t *testing.T) {
185178

186179
func TestPlatformInformation(t *testing.T) {
187180
platform, family, version, err := PlatformInformation()
188-
skipIfNotImplementedErr(t, err)
181+
common.SkipIfNotImplementedErr(t, err)
189182
if err != nil {
190183
t.Errorf("PlatformInformation() failed, %v", err)
191184
}

internal/common/common.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ import (
3131
)
3232

3333
var (
34-
Timeout = 3 * time.Second
35-
ErrTimeout = errors.New("command timed out")
34+
Timeout = 3 * time.Second
35+
ErrNotImplementedError = errors.New("not implemented yet")
36+
ErrTimeout = errors.New("command timed out")
3637
)
3738

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

100-
var ErrNotImplementedError = errors.New("not implemented yet")
101-
102101
// ReadFile reads contents from a file
103102
func ReadFile(filename string) (string, error) {
104103
content, err := os.ReadFile(filename)

internal/common/common_testing.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
package common
3+
4+
import (
5+
"errors"
6+
"testing"
7+
)
8+
9+
func SkipIfNotImplementedErr(tb testing.TB, err error) {
10+
tb.Helper()
11+
if errors.Is(err, ErrNotImplementedError) {
12+
tb.Skip("not implemented")
13+
}
14+
}

0 commit comments

Comments
 (0)