Skip to content

Commit 08f848b

Browse files
authored
Merge pull request #129 from NETWAYS/chore/testify
Rework to use stdlib testing
2 parents d6fe6bd + dd9f67f commit 08f848b

14 files changed

+497
-202
lines changed

config_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package check
33
import (
44
"os"
55
"testing"
6-
7-
"github.com/stretchr/testify/assert"
86
)
97

108
func ExampleConfig() {
@@ -37,11 +35,21 @@ func TestLoadFromEnv(t *testing.T) {
3735
err := os.Setenv("EXAMPLE", "foobar")
3836
defer os.Unsetenv("EXAMPLE") // just to not create any side effects
3937

40-
assert.NoError(t, err)
38+
if err != nil {
39+
t.Fatalf("expected no error, got %v", err)
40+
}
4141

4242
LoadFromEnv(&c)
4343

44-
assert.Equal(t, "foobar", c.Bearer)
45-
assert.Equal(t, "", c.Auth)
46-
assert.Equal(t, "", c.OneMoreThanTags)
44+
if c.Bearer != "foobar" {
45+
t.Fatalf("expected %v, got %v", c.Bearer, "foobar")
46+
}
47+
48+
if c.Auth != "" {
49+
t.Fatalf("expected %v, got %v", c.Auth, "")
50+
}
51+
52+
if c.OneMoreThanTags != "" {
53+
t.Fatalf("expected %v, got %v", c.OneMoreThanTags, "")
54+
}
4755
}

convert/bytes_common_test.go

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,77 @@
11
package convert
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
65
)
76

87
func TestParseBytes(t *testing.T) {
98
b, err := ParseBytes("1024")
10-
assert.NoError(t, err)
11-
assert.IsType(t, BytesIEC(0), b)
12-
assert.Equal(t, uint64(1024), b.Bytes())
9+
if err != nil {
10+
t.Fatalf("expected no error, got %v", err)
11+
}
12+
if _, ok := b.(BytesIEC); !ok {
13+
t.Fatalf("expected type BytesIEC, got %T", b)
14+
}
15+
if b.Bytes() != 1024 {
16+
t.Fatalf("expected 1024 bytes, got %d", b.Bytes())
17+
}
1318

1419
b, err = ParseBytes("1MB")
15-
assert.NoError(t, err)
16-
assert.IsType(t, BytesSI(0), b)
17-
assert.Equal(t, uint64(1000*1000), b.Bytes())
20+
if err != nil {
21+
t.Fatalf("expected no error, got %v", err)
22+
}
23+
if _, ok := b.(BytesSI); !ok {
24+
t.Fatalf("expected type BytesSI, got %T", b)
25+
}
26+
if b.Bytes() != 1000*1000 {
27+
t.Fatalf("expected 1000000 bytes, got %d", b.Bytes())
28+
}
1829

1930
b, err = ParseBytes("1 MiB")
20-
assert.NoError(t, err)
21-
assert.IsType(t, BytesIEC(0), b)
22-
assert.Equal(t, uint64(1024*1024), b.Bytes())
31+
if err != nil {
32+
t.Fatalf("expected no error, got %v", err)
33+
}
34+
if _, ok := b.(BytesIEC); !ok {
35+
t.Fatalf("expected type BytesIEC, got %T", b)
36+
}
37+
if b.Bytes() != 1024*1024 {
38+
t.Fatalf("expected 1048576 bytes, got %d", b.Bytes())
39+
}
2340

2441
b, err = ParseBytes("100MB")
25-
assert.NoError(t, err)
42+
if err != nil {
43+
t.Fatalf("expected no error, got %v", err)
44+
}
2645

2746
b, err = ParseBytes("100MiB")
28-
assert.NoError(t, err)
47+
if err != nil {
48+
t.Fatalf("expected no error, got %v", err)
49+
}
2950

3051
b, err = ParseBytes(" 23 GiB ")
31-
assert.NoError(t, err)
32-
assert.IsType(t, BytesIEC(0), b)
33-
assert.Equal(t, uint64(23*1024*1024*1024), b.Bytes())
52+
if err != nil {
53+
t.Fatalf("expected no error, got %v", err)
54+
}
55+
if _, ok := b.(BytesIEC); !ok {
56+
t.Fatalf("expected type BytesIEC, got %T", b)
57+
}
58+
if b.Bytes() != 23*1024*1024*1024 {
59+
t.Fatalf("expected 24742653952 bytes, got %d", b.Bytes())
60+
}
3461

3562
b, err = ParseBytes("1.2.3.4MB")
36-
assert.Error(t, err)
37-
assert.Nil(t, b)
63+
if err == nil {
64+
t.Fatalf("expected error, got nil")
65+
}
66+
if b != nil {
67+
t.Fatalf("expected nil, got %v", b)
68+
}
3869

3970
b, err = ParseBytes("1PHD")
40-
assert.Error(t, err)
41-
assert.Nil(t, b)
71+
if err == nil {
72+
t.Fatalf("expected error, got nil")
73+
}
74+
if b != nil {
75+
t.Fatalf("expected nil, got %v", b)
76+
}
4277
}

convert/bytes_iec_test.go

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
11
package convert
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
65
)
76

87
func TestBytesIEC_HumanReadable(t *testing.T) {
9-
assert.Equal(t, "0B", BytesIEC(0).HumanReadable())
10-
assert.Equal(t, "999B", BytesIEC(999).HumanReadable())
11-
assert.Equal(t, "999KiB", BytesIEC(999*1024).HumanReadable())
12-
assert.Equal(t, "999MiB", BytesIEC(999*1024*1024).HumanReadable())
13-
assert.Equal(t, "999GiB", BytesIEC(999*1024*1024*1024).HumanReadable())
14-
assert.Equal(t, "999TiB", BytesIEC(999*1024*1024*1024*1024).HumanReadable())
15-
assert.Equal(t, "4PiB", BytesIEC(4*1024*1024*1024*1024*1024).HumanReadable())
16-
assert.Equal(t, "4096PiB", BytesIEC(4*1024*1024*1024*1024*1024*1024).HumanReadable())
17-
18-
assert.Equal(t, "1263MiB", BytesIEC(1263*1024*1024).HumanReadable()) // and not 1.23GiB
19-
20-
assert.Equal(t, "100MiB", BytesIEC(100*1024*1024).HumanReadable())
21-
22-
assert.Equal(t, "123.05MiB", BytesIEC(129032519).HumanReadable())
23-
assert.Equal(t, "14.67GiB", BytesIEC(15756365824).HumanReadable())
24-
25-
assert.Equal(t, "1024KiB", BytesIEC(1024*1024).HumanReadable())
26-
assert.Equal(t, "2MiB", BytesIEC(2*1024*1024).HumanReadable())
8+
if BytesIEC(0).HumanReadable() != "0B" {
9+
t.Fatalf("expected '0B', got %s", BytesIEC(0).HumanReadable())
10+
}
11+
if BytesIEC(999).HumanReadable() != "999B" {
12+
t.Fatalf("expected '999B', got %s", BytesIEC(999).HumanReadable())
13+
}
14+
if BytesIEC(999*1024).HumanReadable() != "999KiB" {
15+
t.Fatalf("expected '999KiB', got %s", BytesIEC(999*1024).HumanReadable())
16+
}
17+
if BytesIEC(999*1024*1024).HumanReadable() != "999MiB" {
18+
t.Fatalf("expected '999MiB', got %s", BytesIEC(999*1024*1024).HumanReadable())
19+
}
20+
if BytesIEC(999*1024*1024*1024).HumanReadable() != "999GiB" {
21+
t.Fatalf("expected '999GiB', got %s", BytesIEC(999*1024*1024*1024).HumanReadable())
22+
}
23+
if BytesIEC(999*1024*1024*1024*1024).HumanReadable() != "999TiB" {
24+
t.Fatalf("expected '999TiB', got %s", BytesIEC(999*1024*1024*1024*1024).HumanReadable())
25+
}
26+
if BytesIEC(4*1024*1024*1024*1024*1024).HumanReadable() != "4PiB" {
27+
t.Fatalf("expected '4PiB', got %s", BytesIEC(4*1024*1024*1024*1024*1024).HumanReadable())
28+
}
29+
if BytesIEC(4*1024*1024*1024*1024*1024*1024).HumanReadable() != "4096PiB" {
30+
t.Fatalf("expected '4096PiB', got %s", BytesIEC(4*1024*1024*1024*1024*1024*1024).HumanReadable())
31+
}
32+
if BytesIEC(1263*1024*1024).HumanReadable() != "1263MiB" {
33+
t.Fatalf("expected '1263MiB', got %s", BytesIEC(1263*1024*1024).HumanReadable())
34+
}
35+
if BytesIEC(100*1024*1024).HumanReadable() != "100MiB" {
36+
t.Fatalf("expected '100MiB', got %s", BytesIEC(100*1024*1024).HumanReadable())
37+
}
38+
if BytesIEC(129032519).HumanReadable() != "123.05MiB" {
39+
t.Fatalf("expected '123.05MiB', got %s", BytesIEC(129032519).HumanReadable())
40+
}
41+
if BytesIEC(15756365824).HumanReadable() != "14.67GiB" {
42+
t.Fatalf("expected '14.67GiB', got %s", BytesIEC(15756365824).HumanReadable())
43+
}
44+
if BytesIEC(1024*1024).HumanReadable() != "1024KiB" {
45+
t.Fatalf("expected '1024KiB', got %s", BytesIEC(1024*1024).HumanReadable())
46+
}
47+
if BytesIEC(2*1024*1024).HumanReadable() != "2MiB" {
48+
t.Fatalf("expected '2MiB', got %s", BytesIEC(2*1024*1024).HumanReadable())
49+
}
2750
}

convert/bytes_si_test.go

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,56 @@
11
package convert
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
65
)
76

87
func TestBytesSI_HumanReadable(t *testing.T) {
9-
assert.Equal(t, "0B", BytesSI(0).HumanReadable())
10-
assert.Equal(t, "999B", BytesSI(999).HumanReadable())
11-
assert.Equal(t, "999KB", BytesSI(999*1000).HumanReadable())
12-
assert.Equal(t, "999MB", BytesSI(999*1000*1000).HumanReadable())
13-
assert.Equal(t, "999GB", BytesSI(999*1000*1000*1000).HumanReadable())
14-
assert.Equal(t, "999TB", BytesSI(999*1000*1000*1000*1000).HumanReadable())
15-
16-
assert.Equal(t, "4PB", BytesSI(4*1000*1000*1000*1000*1000).HumanReadable())
17-
assert.Equal(t, "4000PB", BytesSI(4*1000*1000*1000*1000*1000*1000).HumanReadable())
18-
19-
assert.Equal(t, "4TB", BytesSI(4*1000*1000*1000*1000).HumanReadable())
20-
assert.Equal(t, "4PB", BytesSI(4*1000*1000*1000*1000*1000).HumanReadable())
21-
22-
assert.Equal(t, "1263MB", BytesSI(1263*1000*1000).HumanReadable()) // and not 1.23GiB
23-
24-
assert.Equal(t, "123.05MB", BytesSI(123050*1000).HumanReadable())
25-
assert.Equal(t, "14.67GB", BytesSI(14670*1000*1000).HumanReadable())
26-
27-
assert.Equal(t, "1000KB", BytesSI(1000*1000).HumanReadable())
28-
assert.Equal(t, "2MB", BytesSI(2*1000*1000).HumanReadable())
29-
assert.Equal(t, "3MB", BytesSI(3*1000*1000).HumanReadable())
8+
if BytesSI(0).HumanReadable() != "0B" {
9+
t.Fatalf("expected '0B', got %s", BytesSI(0).HumanReadable())
10+
}
11+
if BytesSI(999).HumanReadable() != "999B" {
12+
t.Fatalf("expected '999B', got %s", BytesSI(999).HumanReadable())
13+
}
14+
if BytesSI(999*1000).HumanReadable() != "999KB" {
15+
t.Fatalf("expected '999KB', got %s", BytesSI(999*1000).HumanReadable())
16+
}
17+
if BytesSI(999*1000*1000).HumanReadable() != "999MB" {
18+
t.Fatalf("expected '999MB', got %s", BytesSI(999*1000*1000).HumanReadable())
19+
}
20+
if BytesSI(999*1000*1000*1000).HumanReadable() != "999GB" {
21+
t.Fatalf("expected '999GB', got %s", BytesSI(999*1000*1000*1000).HumanReadable())
22+
}
23+
if BytesSI(999*1000*1000*1000*1000).HumanReadable() != "999TB" {
24+
t.Fatalf("expected '999TB', got %s", BytesSI(999*1000*1000*1000*1000).HumanReadable())
25+
}
26+
if BytesSI(4*1000*1000*1000*1000*1000).HumanReadable() != "4PB" {
27+
t.Fatalf("expected '4PB', got %s", BytesSI(4*1000*1000*1000*1000*1000).HumanReadable())
28+
}
29+
if BytesSI(4*1000*1000*1000*1000*1000*1000).HumanReadable() != "4000PB" {
30+
t.Fatalf("expected '4000PB', got %s", BytesSI(4*1000*1000*1000*1000*1000*1000).HumanReadable())
31+
}
32+
if BytesSI(4*1000*1000*1000*1000).HumanReadable() != "4TB" {
33+
t.Fatalf("expected '4TB', got %s", BytesSI(4*1000*1000*1000*1000).HumanReadable())
34+
}
35+
if BytesSI(4*1000*1000*1000*1000*1000).HumanReadable() != "4PB" {
36+
t.Fatalf("expected '4PB', got %s", BytesSI(4*1000*1000*1000*1000*1000).HumanReadable())
37+
}
38+
if BytesSI(1263*1000*1000).HumanReadable() != "1263MB" {
39+
t.Fatalf("expected '1263MB', got %s", BytesSI(1263*1000*1000).HumanReadable())
40+
}
41+
if BytesSI(123050*1000).HumanReadable() != "123.05MB" {
42+
t.Fatalf("expected '123.05MB', got %s", BytesSI(123050*1000).HumanReadable())
43+
}
44+
if BytesSI(14670*1000*1000).HumanReadable() != "14.67GB" {
45+
t.Fatalf("expected '14.67GB', got %s", BytesSI(14670*1000*1000).HumanReadable())
46+
}
47+
if BytesSI(1000*1000).HumanReadable() != "1000KB" {
48+
t.Fatalf("expected '1000KB', got %s", BytesSI(1000*1000).HumanReadable())
49+
}
50+
if BytesSI(2*1000*1000).HumanReadable() != "2MB" {
51+
t.Fatalf("expected '2MB', got %s", BytesSI(2*1000*1000).HumanReadable())
52+
}
53+
if BytesSI(3*1000*1000).HumanReadable() != "3MB" {
54+
t.Fatalf("expected '3MB', got %s", BytesSI(3*1000*1000).HumanReadable())
55+
}
3056
}

examples/check_example/main_test.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,33 @@ package main
22

33
import (
44
"os"
5-
"regexp"
5+
"strings"
66
"testing"
77

88
"github.com/NETWAYS/go-check/testhelper"
9-
"github.com/stretchr/testify/assert"
109
)
1110

1211
func TestMyMain(t *testing.T) {
13-
stdout := testhelper.RunMainTest(main, "--help")
14-
assert.Regexp(t, regexp.MustCompile(`would exit with code 3`), stdout)
12+
actual := testhelper.RunMainTest(main, "--help")
13+
expected := `would exit with code 3`
1514

16-
stdout = testhelper.RunMainTest(main, "--warning", "20")
17-
assert.Regexp(t, regexp.MustCompile(`^\[OK\] - value is 10\nwould exit with code 0\n$`), stdout)
15+
if !strings.Contains(actual, expected) {
16+
t.Fatalf("expected %v, got %v", expected, actual)
17+
}
1818

19-
stdout = testhelper.RunMainTest(main, "--warning", "10", "--value", "11")
20-
assert.Regexp(t, regexp.MustCompile(`^\[WARNING\] - value is 11\nwould exit with code 1\n$`), stdout)
19+
actual = testhelper.RunMainTest(main, "--warning", "20")
20+
expected = "[OK] - value is 10"
21+
22+
if !strings.Contains(actual, expected) {
23+
t.Fatalf("expected %v, got %v", expected, actual)
24+
}
25+
26+
actual = testhelper.RunMainTest(main, "--warning", "10", "--value", "11")
27+
expected = "[WARNING] - value is 11"
28+
29+
if !strings.Contains(actual, expected) {
30+
t.Fatalf("expected %v, got %v", expected, actual)
31+
}
2132
}
2233

2334
func TestMain(m *testing.M) {

examples/check_example2/main_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ import (
55
"testing"
66

77
"github.com/NETWAYS/go-check/testhelper"
8-
"github.com/stretchr/testify/assert"
98
)
109

1110
func TestMyMain(t *testing.T) {
12-
stdout := testhelper.RunMainTest(main)
11+
actual := testhelper.RunMainTest(main)
1312

14-
resultString := `[WARNING] - states: warning=1 ok=1
13+
expected := `[WARNING] - states: warning=1 ok=1
1514
\_ [OK] Check1
1615
\_ [WARNING] Check2
1716
|foo=23 bar=42 'foo2 bar'=46
1817
1918
would exit with code 1
2019
`
2120

22-
assert.Equal(t, resultString, stdout)
21+
if actual != expected {
22+
t.Fatalf("expected %v, got %v", expected, actual)
23+
}
2324
}
2425

2526
func TestMain(m *testing.M) {

go.mod

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,4 @@ module github.com/NETWAYS/go-check
22

33
go 1.22
44

5-
require (
6-
github.com/spf13/pflag v1.0.6
7-
github.com/stretchr/testify v1.10.0
8-
)
9-
10-
require (
11-
github.com/davecgh/go-spew v1.1.1 // indirect
12-
github.com/pmezard/go-difflib v1.0.0 // indirect
13-
gopkg.in/yaml.v3 v3.0.1 // indirect
14-
)
5+
require github.com/spf13/pflag v1.0.6

go.sum

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,2 @@
1-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2-
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
51
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
62
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
7-
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
8-
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
9-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
10-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11-
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
12-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)