Skip to content

chore: use testify instead of testing #1815

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 15, 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
15 changes: 6 additions & 9 deletions cpu/cpu_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"os"
"runtime"
"testing"

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

func TestInfo_AppleSilicon(t *testing.T) {
Expand All @@ -15,19 +18,13 @@ func TestInfo_AppleSilicon(t *testing.T) {
}

v, err := Info()
if err != nil {
t.Errorf("cpu info should be implemented on darwin systems")
}
require.NoErrorf(t, err, "cpu info should be implemented on darwin systems")

for _, vv := range v {
if vv.ModelName == "" {
t.Errorf("could not get CPU info: %v", vv)
}
assert.NotEmptyf(t, vv.ModelName, "could not get CPU info: %v", vv)
if vv.Mhz <= 0 && os.Getenv("CI") != "true" {
t.Errorf("could not get frequency of: %s", vv.ModelName)
}
if vv.Mhz > 6000 {
t.Errorf("cpu frequency is absurdly high value: %f MHz", vv.Mhz)
}
assert.LessOrEqualf(t, vv.Mhz, float64(6000), "cpu frequency is absurdly high value: %f MHz", vv.Mhz)
}
}
19 changes: 7 additions & 12 deletions cpu/cpu_freebsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"runtime"
"testing"

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

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

Expand All @@ -25,17 +28,9 @@ func TestParseDmesgBoot(t *testing.T) {
}
for _, tt := range cpuTests {
v, num, err := parseDmesgBoot(filepath.Join("testdata", "freebsd", tt.file))
if err != nil {
t.Errorf("parseDmesgBoot failed(%s), %v", tt.file, err)
}
if num != tt.cpuNum {
t.Errorf("parseDmesgBoot wrong length(%s), %v", tt.file, err)
}
if v.Cores != tt.cores {
t.Errorf("parseDmesgBoot wrong core(%s), %v", tt.file, err)
}
if !common.StringsContains(v.Flags, "fpu") {
t.Errorf("parseDmesgBoot fail to parse features(%s), %v", tt.file, err)
}
require.NoErrorf(t, err, "parseDmesgBoot failed(%s), %v", tt.file, err)
assert.Equalf(t, num, tt.cpuNum, "parseDmesgBoot wrong length(%s), %v", tt.file, err)
assert.Equalf(t, v.Cores, tt.cores, "parseDmesgBoot wrong core(%s), %v", tt.file, err)
assert.Truef(t, common.StringsContains(v.Flags, "fpu"), "parseDmesgBoot fail to parse features(%s), %v", tt.file, err)
}
}
35 changes: 11 additions & 24 deletions cpu/cpu_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ import (
"strconv"
"strings"
"testing"

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

func TestTimesEmpty(t *testing.T) {
t.Setenv("HOST_PROC", "testdata/linux/times_empty")
_, err := Times(true)
if err != nil {
t.Error("Times(true) failed")
}
require.NoErrorf(t, err, "Times(true) failed")
_, err = Times(false)
if err != nil {
t.Error("Times(false) failed")
}
assert.NoErrorf(t, err, "Times(false) failed")
}

func TestParseStatLine_424(t *testing.T) {
Expand Down Expand Up @@ -78,31 +77,19 @@ func TestCountsAgainstLscpu(t *testing.T) {
expectedLogical := expectedPhysical * threadsPerCore
physical, err := Counts(false)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
require.NoError(t, err)
logical, err := Counts(true)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if expectedPhysical != physical {
t.Errorf("expected %v, got %v", expectedPhysical, physical)
}
if expectedLogical != logical {
t.Errorf("expected %v, got %v", expectedLogical, logical)
}
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)
}

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

count, err := Counts(true)
if err != nil {
t.Errorf("error %v", err)
}
require.NoError(t, err)
expected := 8
if count != expected {
t.Errorf("expected %v, got %v", expected, count)
}
assert.Equalf(t, expected, count, "expected %v, got %v", expected, count)
}
10 changes: 4 additions & 6 deletions cpu/cpu_plan9_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var timesTests = []struct {
Expand All @@ -34,13 +36,9 @@ func TestTimesPlan9(t *testing.T) {
t.Setenv("HOST_ROOT", filepath.Join("testdata/plan9", tt.mockedRootFS))
stats, err := Times(false)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
require.NoError(t, err)
eps := cmpopts.EquateApprox(0, 0.00000001)
if !cmp.Equal(stats, tt.stats, eps) {
t.Errorf("got: %+v\nwant: %+v", stats, tt.stats)
}
assert.Truef(t, cmp.Equal(stats, tt.stats, eps), "got: %+v\nwant: %+v", stats, tt.stats)
})
}
}
26 changes: 8 additions & 18 deletions cpu/cpu_solaris_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"reflect"
"sort"
"testing"

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

func TestParseISAInfo(t *testing.T) {
Expand Down Expand Up @@ -51,20 +53,14 @@ func TestParseISAInfo(t *testing.T) {

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

sort.Strings(tc.expected)

flags, err := parseISAInfo(string(content))
if err != nil {
t.Fatalf("parseISAInfo: %s", err)
}
require.NoErrorf(t, err, "parseISAInfo: %s", err)

if !reflect.DeepEqual(tc.expected, flags) {
t.Fatalf("Bad flags\nExpected: %v\n Actual: %v", tc.expected, flags)
}
require.Truef(t, reflect.DeepEqual(tc.expected, flags), "Bad flags\nExpected: %v\n Actual: %v", tc.expected, flags)
}
}

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

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

cpus, err := parseProcessorInfo(string(content))
if err != nil {
t.Errorf("cannot parse processor info: %s", err)
}
require.NoErrorf(t, err, "cannot parse processor info: %s", err)

if !reflect.DeepEqual(tc.expected, cpus) {
t.Fatalf("Bad Processor Info\nExpected: %v\n Actual: %v", tc.expected, cpus)
}
require.Truef(t, reflect.DeepEqual(tc.expected, cpus), "Bad Processor Info\nExpected: %v\n Actual: %v", tc.expected, cpus)
}
}
77 changes: 20 additions & 57 deletions cpu/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

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

"github.com/shirou/gopsutil/v4/internal/common"
)
Expand All @@ -23,36 +24,22 @@ func skipIfNotImplementedErr(t *testing.T, err error) {
func TestTimes(t *testing.T) {
v, err := Times(false)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if len(v) == 0 {
t.Error("could not get CPUs ", err)
}
require.NoError(t, err)
assert.NotEmptyf(t, v, "could not get CPUs: %s", err)
empty := TimesStat{}
for _, vv := range v {
if vv == empty {
t.Errorf("could not get CPU User: %v", vv)
}
assert.NotEqualf(t, vv, empty, "could not get CPU User: %v", vv)
}

// test sum of per cpu stats is within margin of error for cpu total stats
cpuTotal, err := Times(false)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if len(cpuTotal) == 0 {
t.Error("could not get CPUs", err)
}
require.NoError(t, err)
assert.NotEmptyf(t, cpuTotal, "could not get CPUs: %s", err)
perCPU, err := Times(true)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if len(perCPU) == 0 {
t.Error("could not get CPUs", err)
}
require.NoError(t, err)
assert.NotEmptyf(t, perCPU, "could not get CPUs: %s", err)
var perCPUUserTimeSum float64
var perCPUSystemTimeSum float64
var perCPUIdleTimeSum float64
Expand Down Expand Up @@ -81,21 +68,13 @@ func TestTimes(t *testing.T) {
func TestCounts(t *testing.T) {
v, err := Counts(true)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if v == 0 {
t.Errorf("could not get logical CPU counts: %v", v)
}
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)
if err != nil {
t.Errorf("error %v", err)
}
if v == 0 {
t.Errorf("could not get physical CPU counts: %v", v)
}
require.NoError(t, err)
assert.NotZerof(t, v, "could not get physical CPU counts: %v", v)
t.Logf("physical cores: %d", v)
}

Expand All @@ -107,24 +86,16 @@ func TestTimeStat_String(t *testing.T) {
Idle: 300.1,
}
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}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("CPUTimesStat string is invalid: %v", v)
}
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "CPUTimesStat string is invalid: %v", v)
}

func TestInfo(t *testing.T) {
v, err := Info()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if len(v) == 0 {
t.Errorf("could not get CPU Info")
}
require.NoError(t, err)
assert.NotEmptyf(t, v, "could not get CPU Info")
for _, vv := range v {
if vv.ModelName == "" {
t.Errorf("could not get CPU Info: %v", vv)
}
assert.NotEmptyf(t, vv.ModelName, "could not get CPU Info: %v", vv)
}
}

Expand All @@ -136,9 +107,7 @@ func testPercent(t *testing.T, percpu bool) {
testCount = 100
v, err := Percent(time.Millisecond, percpu)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
require.NoError(t, err)
// Skip CI which CPU num is different
if os.Getenv("CI") != "true" {
if (percpu && len(v) != numcpu) || (!percpu && len(v) != 1) {
Expand All @@ -150,9 +119,7 @@ func testPercent(t *testing.T, percpu bool) {
duration := time.Duration(10) * time.Microsecond
v, err := Percent(duration, percpu)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
require.NoError(t, err)
for _, percent := range v {
// Check for slightly greater then 100% to account for any rounding issues.
if percent < 0.0 || percent > 100.0001*float64(numcpu) {
Expand All @@ -170,9 +137,7 @@ func testPercentLastUsed(t *testing.T, percpu bool) {
testCount = 2
v, err := Percent(time.Millisecond, percpu)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
require.NoError(t, err)
// Skip CI which CPU num is different
if os.Getenv("CI") != "true" {
if (percpu && len(v) != numcpu) || (!percpu && len(v) != 1) {
Expand All @@ -183,9 +148,7 @@ func testPercentLastUsed(t *testing.T, percpu bool) {
for i := 0; i < testCount; i++ {
v, err := Percent(0, percpu)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
require.NoError(t, err)
time.Sleep(1 * time.Millisecond)
for _, percent := range v {
// Check for slightly greater then 100% to account for any rounding issues.
Expand Down
Loading
Loading