Skip to content

Commit 8c1a718

Browse files
committed
chore: use testify instead of testing
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 62da883 commit 8c1a718

25 files changed

+356
-899
lines changed

cpu/cpu_darwin_test.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"os"
88
"runtime"
99
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
1013
)
1114

1215
func TestInfo_AppleSilicon(t *testing.T) {
@@ -15,19 +18,13 @@ func TestInfo_AppleSilicon(t *testing.T) {
1518
}
1619

1720
v, err := Info()
18-
if err != nil {
19-
t.Errorf("cpu info should be implemented on darwin systems")
20-
}
21+
require.NoErrorf(t, err, "cpu info should be implemented on darwin systems")
2122

2223
for _, vv := range v {
23-
if vv.ModelName == "" {
24-
t.Errorf("could not get CPU info: %v", vv)
25-
}
24+
assert.NotEmptyf(t, vv.ModelName, "could not get CPU info: %v", vv)
2625
if vv.Mhz <= 0 && os.Getenv("CI") != "true" {
2726
t.Errorf("could not get frequency of: %s", vv.ModelName)
2827
}
29-
if vv.Mhz > 6000 {
30-
t.Errorf("cpu frequency is absurdly high value: %f MHz", vv.Mhz)
31-
}
28+
assert.LessOrEqualf(t, vv.Mhz, float64(6000), "cpu frequency is absurdly high value: %f MHz", vv.Mhz)
3229
}
3330
}

cpu/cpu_freebsd_test.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"runtime"
77
"testing"
88

9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
912
"github.com/shirou/gopsutil/v4/internal/common"
1013
)
1114

@@ -25,17 +28,9 @@ func TestParseDmesgBoot(t *testing.T) {
2528
}
2629
for _, tt := range cpuTests {
2730
v, num, err := parseDmesgBoot(filepath.Join("testdata", "freebsd", tt.file))
28-
if err != nil {
29-
t.Errorf("parseDmesgBoot failed(%s), %v", tt.file, err)
30-
}
31-
if num != tt.cpuNum {
32-
t.Errorf("parseDmesgBoot wrong length(%s), %v", tt.file, err)
33-
}
34-
if v.Cores != tt.cores {
35-
t.Errorf("parseDmesgBoot wrong core(%s), %v", tt.file, err)
36-
}
37-
if !common.StringsContains(v.Flags, "fpu") {
38-
t.Errorf("parseDmesgBoot fail to parse features(%s), %v", tt.file, err)
39-
}
31+
require.NoErrorf(t, err, "parseDmesgBoot failed(%s), %v", tt.file, err)
32+
assert.Equalf(t, num, tt.cpuNum, "parseDmesgBoot wrong length(%s), %v", tt.file, err)
33+
assert.Equalf(t, v.Cores, tt.cores, "parseDmesgBoot wrong core(%s), %v", tt.file, err)
34+
assert.Truef(t, common.StringsContains(v.Flags, "fpu"), "parseDmesgBoot fail to parse features(%s), %v", tt.file, err)
4035
}
4136
}

cpu/cpu_linux_test.go

+11-24
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ import (
77
"strconv"
88
"strings"
99
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
1013
)
1114

1215
func TestTimesEmpty(t *testing.T) {
1316
t.Setenv("HOST_PROC", "testdata/linux/times_empty")
1417
_, err := Times(true)
15-
if err != nil {
16-
t.Error("Times(true) failed")
17-
}
18+
require.NoErrorf(t, err, "Times(true) failed")
1819
_, err = Times(false)
19-
if err != nil {
20-
t.Error("Times(false) failed")
21-
}
20+
assert.NoErrorf(t, err, "Times(false) failed")
2221
}
2322

2423
func TestParseStatLine_424(t *testing.T) {
@@ -78,31 +77,19 @@ func TestCountsAgainstLscpu(t *testing.T) {
7877
expectedLogical := expectedPhysical * threadsPerCore
7978
physical, err := Counts(false)
8079
skipIfNotImplementedErr(t, err)
81-
if err != nil {
82-
t.Errorf("error %v", err)
83-
}
80+
require.NoError(t, err)
8481
logical, err := Counts(true)
8582
skipIfNotImplementedErr(t, err)
86-
if err != nil {
87-
t.Errorf("error %v", err)
88-
}
89-
if expectedPhysical != physical {
90-
t.Errorf("expected %v, got %v", expectedPhysical, physical)
91-
}
92-
if expectedLogical != logical {
93-
t.Errorf("expected %v, got %v", expectedLogical, logical)
94-
}
83+
require.NoError(t, err)
84+
assert.Equalf(t, expectedPhysical, physical, "expected %v, got %v", expectedPhysical, physical)
85+
assert.Equalf(t, expectedLogical, logical, "expected %v, got %v", expectedLogical, logical)
9586
}
9687

9788
func TestCountsLogicalAndroid_1037(t *testing.T) { // https://github.com/shirou/gopsutil/issues/1037
9889
t.Setenv("HOST_PROC", "testdata/linux/1037/proc")
9990

10091
count, err := Counts(true)
101-
if err != nil {
102-
t.Errorf("error %v", err)
103-
}
92+
require.NoError(t, err)
10493
expected := 8
105-
if count != expected {
106-
t.Errorf("expected %v, got %v", expected, count)
107-
}
94+
assert.Equalf(t, expected, count, "expected %v, got %v", expected, count)
10895
}

cpu/cpu_plan9_test.go

+4-6
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+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1214
)
1315

1416
var timesTests = []struct {
@@ -34,13 +36,9 @@ func TestTimesPlan9(t *testing.T) {
3436
t.Setenv("HOST_ROOT", filepath.Join("testdata/plan9", tt.mockedRootFS))
3537
stats, err := Times(false)
3638
skipIfNotImplementedErr(t, err)
37-
if err != nil {
38-
t.Errorf("error %v", err)
39-
}
39+
require.NoError(t, err)
4040
eps := cmpopts.EquateApprox(0, 0.00000001)
41-
if !cmp.Equal(stats, tt.stats, eps) {
42-
t.Errorf("got: %+v\nwant: %+v", stats, tt.stats)
43-
}
41+
assert.Truef(t, cmp.Equal(stats, tt.stats, eps), "got: %+v\nwant: %+v", stats, tt.stats)
4442
})
4543
}
4644
}

cpu/cpu_solaris_test.go

+8-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"reflect"
88
"sort"
99
"testing"
10+
11+
"github.com/stretchr/testify/require"
1012
)
1113

1214
func TestParseISAInfo(t *testing.T) {
@@ -51,20 +53,14 @@ func TestParseISAInfo(t *testing.T) {
5153

5254
for _, tc := range cases {
5355
content, err := os.ReadFile(filepath.Join("testdata", "solaris", tc.filename))
54-
if err != nil {
55-
t.Errorf("cannot read test case: %s", err)
56-
}
56+
require.NoErrorf(t, err, "cannot read test case: %s", err)
5757

5858
sort.Strings(tc.expected)
5959

6060
flags, err := parseISAInfo(string(content))
61-
if err != nil {
62-
t.Fatalf("parseISAInfo: %s", err)
63-
}
61+
require.NoErrorf(t, err, "parseISAInfo: %s", err)
6462

65-
if !reflect.DeepEqual(tc.expected, flags) {
66-
t.Fatalf("Bad flags\nExpected: %v\n Actual: %v", tc.expected, flags)
67-
}
63+
require.Truef(t, reflect.DeepEqual(tc.expected, flags), "Bad flags\nExpected: %v\n Actual: %v", tc.expected, flags)
6864
}
6965
}
7066

@@ -140,17 +136,11 @@ func TestParseProcessorInfo(t *testing.T) {
140136

141137
for _, tc := range cases {
142138
content, err := os.ReadFile(filepath.Join("testdata", "solaris", tc.filename))
143-
if err != nil {
144-
t.Errorf("cannot read test case: %s", err)
145-
}
139+
require.NoErrorf(t, err, "cannot read test case: %s", err)
146140

147141
cpus, err := parseProcessorInfo(string(content))
148-
if err != nil {
149-
t.Errorf("cannot parse processor info: %s", err)
150-
}
142+
require.NoErrorf(t, err, "cannot parse processor info: %s", err)
151143

152-
if !reflect.DeepEqual(tc.expected, cpus) {
153-
t.Fatalf("Bad Processor Info\nExpected: %v\n Actual: %v", tc.expected, cpus)
154-
}
144+
require.Truef(t, reflect.DeepEqual(tc.expected, cpus), "Bad Processor Info\nExpected: %v\n Actual: %v", tc.expected, cpus)
155145
}
156146
}

cpu/cpu_test.go

+20-57
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1314

1415
"github.com/shirou/gopsutil/v4/internal/common"
1516
)
@@ -23,36 +24,22 @@ func skipIfNotImplementedErr(t *testing.T, err error) {
2324
func TestTimes(t *testing.T) {
2425
v, err := Times(false)
2526
skipIfNotImplementedErr(t, err)
26-
if err != nil {
27-
t.Errorf("error %v", err)
28-
}
29-
if len(v) == 0 {
30-
t.Error("could not get CPUs ", err)
31-
}
27+
require.NoError(t, err)
28+
assert.NotEmptyf(t, v, "could not get CPUs: %s", err)
3229
empty := TimesStat{}
3330
for _, vv := range v {
34-
if vv == empty {
35-
t.Errorf("could not get CPU User: %v", vv)
36-
}
31+
assert.NotEqualf(t, vv, empty, "could not get CPU User: %v", vv)
3732
}
3833

3934
// test sum of per cpu stats is within margin of error for cpu total stats
4035
cpuTotal, err := Times(false)
4136
skipIfNotImplementedErr(t, err)
42-
if err != nil {
43-
t.Errorf("error %v", err)
44-
}
45-
if len(cpuTotal) == 0 {
46-
t.Error("could not get CPUs", err)
47-
}
37+
require.NoError(t, err)
38+
assert.NotEmptyf(t, cpuTotal, "could not get CPUs: %s", err)
4839
perCPU, err := Times(true)
4940
skipIfNotImplementedErr(t, err)
50-
if err != nil {
51-
t.Errorf("error %v", err)
52-
}
53-
if len(perCPU) == 0 {
54-
t.Error("could not get CPUs", err)
55-
}
41+
require.NoError(t, err)
42+
assert.NotEmptyf(t, perCPU, "could not get CPUs: %s", err)
5643
var perCPUUserTimeSum float64
5744
var perCPUSystemTimeSum float64
5845
var perCPUIdleTimeSum float64
@@ -81,21 +68,13 @@ func TestTimes(t *testing.T) {
8168
func TestCounts(t *testing.T) {
8269
v, err := Counts(true)
8370
skipIfNotImplementedErr(t, err)
84-
if err != nil {
85-
t.Errorf("error %v", err)
86-
}
87-
if v == 0 {
88-
t.Errorf("could not get logical CPU counts: %v", v)
89-
}
71+
require.NoError(t, err)
72+
assert.NotZerof(t, v, "could not get logical CPU counts: %v", v)
9073
t.Logf("logical cores: %d", v)
9174
v, err = Counts(false)
9275
skipIfNotImplementedErr(t, err)
93-
if err != nil {
94-
t.Errorf("error %v", err)
95-
}
96-
if v == 0 {
97-
t.Errorf("could not get physical CPU counts: %v", v)
98-
}
76+
require.NoError(t, err)
77+
assert.NotZerof(t, v, "could not get physical CPU counts: %v", v)
9978
t.Logf("physical cores: %d", v)
10079
}
10180

@@ -107,24 +86,16 @@ func TestTimeStat_String(t *testing.T) {
10786
Idle: 300.1,
10887
}
10988
e := `{"cpu":"cpu0","user":100.1,"system":200.1,"idle":300.1,"nice":0.0,"iowait":0.0,"irq":0.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0}`
110-
if e != fmt.Sprintf("%v", v) {
111-
t.Errorf("CPUTimesStat string is invalid: %v", v)
112-
}
89+
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "CPUTimesStat string is invalid: %v", v)
11390
}
11491

11592
func TestInfo(t *testing.T) {
11693
v, err := Info()
11794
skipIfNotImplementedErr(t, err)
118-
if err != nil {
119-
t.Errorf("error %v", err)
120-
}
121-
if len(v) == 0 {
122-
t.Errorf("could not get CPU Info")
123-
}
95+
require.NoError(t, err)
96+
assert.NotEmptyf(t, v, "could not get CPU Info")
12497
for _, vv := range v {
125-
if vv.ModelName == "" {
126-
t.Errorf("could not get CPU Info: %v", vv)
127-
}
98+
assert.NotEmptyf(t, vv.ModelName, "could not get CPU Info: %v", vv)
12899
}
129100
}
130101

@@ -136,9 +107,7 @@ func testPercent(t *testing.T, percpu bool) {
136107
testCount = 100
137108
v, err := Percent(time.Millisecond, percpu)
138109
skipIfNotImplementedErr(t, err)
139-
if err != nil {
140-
t.Errorf("error %v", err)
141-
}
110+
require.NoError(t, err)
142111
// Skip CI which CPU num is different
143112
if os.Getenv("CI") != "true" {
144113
if (percpu && len(v) != numcpu) || (!percpu && len(v) != 1) {
@@ -150,9 +119,7 @@ func testPercent(t *testing.T, percpu bool) {
150119
duration := time.Duration(10) * time.Microsecond
151120
v, err := Percent(duration, percpu)
152121
skipIfNotImplementedErr(t, err)
153-
if err != nil {
154-
t.Errorf("error %v", err)
155-
}
122+
require.NoError(t, err)
156123
for _, percent := range v {
157124
// Check for slightly greater then 100% to account for any rounding issues.
158125
if percent < 0.0 || percent > 100.0001*float64(numcpu) {
@@ -170,9 +137,7 @@ func testPercentLastUsed(t *testing.T, percpu bool) {
170137
testCount = 2
171138
v, err := Percent(time.Millisecond, percpu)
172139
skipIfNotImplementedErr(t, err)
173-
if err != nil {
174-
t.Errorf("error %v", err)
175-
}
140+
require.NoError(t, err)
176141
// Skip CI which CPU num is different
177142
if os.Getenv("CI") != "true" {
178143
if (percpu && len(v) != numcpu) || (!percpu && len(v) != 1) {
@@ -183,9 +148,7 @@ func testPercentLastUsed(t *testing.T, percpu bool) {
183148
for i := 0; i < testCount; i++ {
184149
v, err := Percent(0, percpu)
185150
skipIfNotImplementedErr(t, err)
186-
if err != nil {
187-
t.Errorf("error %v", err)
188-
}
151+
require.NoError(t, err)
189152
time.Sleep(1 * time.Millisecond)
190153
for _, percent := range v {
191154
// Check for slightly greater then 100% to account for any rounding issues.

0 commit comments

Comments
 (0)