Skip to content

Commit 784625e

Browse files
authored
Merge pull request #2880 from alexandear/test/refactor-to-use-assert
test: refactor to use assert pkg instead of t.Fatal
2 parents 97ef064 + e4073ad commit 784625e

File tree

9 files changed

+41
-60
lines changed

9 files changed

+41
-60
lines changed

.golangci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ linters-settings:
8888
deny:
8989
- pkg: "golang.org/x/net/context"
9090
desc: "use the 'context' package from the standard library"
91+
forbidigo:
92+
analyze-types: true
93+
forbid:
94+
- p: ^print(ln)?$
95+
msg: Do not use builtin print functions. It's for bootstrapping only and may be removed in the future.
96+
- p: ^fmt\.Print.*$
97+
msg: Do not use fmt.Print statements.
98+
- p: ^testing.T.Fatal.*$
99+
msg: Use assert functions from the gotest.tools/v3/assert package instead.
91100
gocritic:
92101
# See "Tags" section in https://github.com/go-critic/go-critic#usage
93102
enabled-tags:

pkg/downloader/fuzz_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/opencontainers/go-digest"
10+
"gotest.tools/v3/assert"
1011
)
1112

1213
var algorithm = digest.Algorithm("sha256")
@@ -16,9 +17,7 @@ func FuzzDownload(f *testing.F) {
1617
localFile := filepath.Join(t.TempDir(), "localFile")
1718
remoteFile := filepath.Join(t.TempDir(), "remoteFile")
1819
err := os.WriteFile(remoteFile, fileContents, 0o600)
19-
if err != nil {
20-
t.Fatal(err)
21-
}
20+
assert.NilError(t, err)
2221
testLocalFileURL := "file://" + remoteFile
2322
if checkDigest {
2423
d := algorithm.FromBytes(fileContents)

pkg/guestagent/iptables/iptables_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package iptables
33
import (
44
"strings"
55
"testing"
6+
7+
"gotest.tools/v3/assert"
68
)
79

810
// data is from a run of `iptables -t nat -S` with two containers running (started
@@ -74,14 +76,10 @@ func TestParsePortsFromRules(t *testing.T) {
7476
}
7577

7678
res, err := parsePortsFromRules(rules)
77-
if err != nil {
78-
t.Errorf("parsing iptables ports failed with error: %s", err)
79-
}
79+
assert.NilError(t, err, "parsing iptables ports failed")
8080

8181
l := len(res)
82-
if l != 2 {
83-
t.Fatalf("expected 2 ports parsed from iptables but parsed %d", l)
84-
}
82+
assert.Equal(t, l, 2, "unexpected number of ports parsed from iptables")
8583

8684
if res[0].IP.String() != "0.0.0.0" || res[0].Port != 8082 || res[0].TCP != true {
8785
t.Errorf("expected port 8082 on IP 0.0.0.0 with TCP true but got port %d on IP %s with TCP %t", res[0].Port, res[0].IP.String(), res[0].TCP)

pkg/iso9660util/fuzz_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import (
44
"os"
55
"path/filepath"
66
"testing"
7+
8+
"gotest.tools/v3/assert"
79
)
810

911
func FuzzIsISO9660(f *testing.F) {
1012
f.Fuzz(func(t *testing.T, fileContents []byte) {
1113
imageFile := filepath.Join(t.TempDir(), "fuzz.iso")
1214
err := os.WriteFile(imageFile, fileContents, 0o600)
13-
if err != nil {
14-
t.Fatal(err)
15-
}
15+
assert.NilError(t, err)
1616
_, _ = IsISO9660(imageFile)
1717
})
1818
}

pkg/limayaml/limayaml_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import (
1010

1111
func dumpJSON(t *testing.T, d interface{}) string {
1212
b, err := json.Marshal(d)
13-
if err != nil {
14-
t.Fatal(err)
15-
}
13+
assert.NilError(t, err)
1614
return string(b)
1715
}
1816

pkg/lockutil/lockutil_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"path/filepath"
88
"strings"
99
"testing"
10+
11+
"gotest.tools/v3/assert"
1012
)
1113

1214
const parallel = 20
@@ -46,11 +48,7 @@ func TestWithDirLock(t *testing.T) {
4648
}
4749

4850
data, err := os.ReadFile(log)
49-
if err != nil {
50-
t.Fatal(err)
51-
}
51+
assert.NilError(t, err)
5252
lines := strings.Split(strings.Trim(string(data), "\n"), "\n")
53-
if len(lines) != 1 {
54-
t.Errorf("Expected one writer, got %v", lines)
55-
}
53+
assert.Equal(t, len(lines), 1, "unexpected number of writers")
5654
}

pkg/nativeimgutil/fuzz_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import (
44
"os"
55
"path/filepath"
66
"testing"
7+
8+
"gotest.tools/v3/assert"
79
)
810

911
func FuzzConvertToRaw(f *testing.F) {
1012
f.Fuzz(func(t *testing.T, imgData []byte, withBacking bool, size int64) {
1113
srcPath := filepath.Join(t.TempDir(), "src.img")
1214
destPath := filepath.Join(t.TempDir(), "dest.img")
1315
err := os.WriteFile(srcPath, imgData, 0o600)
14-
if err != nil {
15-
t.Fatal(err)
16-
}
16+
assert.NilError(t, err)
1717
_ = ConvertToRaw(srcPath, destPath, &size, withBacking)
1818
})
1919
}

pkg/store/fuzz_test.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import (
66
"testing"
77

88
"github.com/lima-vm/lima/pkg/store/filenames"
9+
"gotest.tools/v3/assert"
910
)
1011

1112
func FuzzLoadYAMLByFilePath(f *testing.F) {
1213
f.Fuzz(func(t *testing.T, fileContents []byte) {
1314
localFile := filepath.Join(t.TempDir(), "yaml_file.yml")
1415
err := os.WriteFile(localFile, fileContents, 0o600)
15-
if err != nil {
16-
t.Fatal(err)
17-
}
16+
assert.NilError(t, err)
1817
_, _ = LoadYAMLByFilePath(localFile)
1918
})
2019
}
@@ -24,19 +23,13 @@ func FuzzInspect(f *testing.F) {
2423
limaDir := t.TempDir()
2524
t.Setenv("LIMA_HOME", limaDir)
2625
err := os.MkdirAll(filepath.Join(limaDir, "fuzz-instance"), 0o700)
27-
if err != nil {
28-
t.Fatal(err)
29-
}
26+
assert.NilError(t, err)
3027
ymlFile := filepath.Join(limaDir, "fuzz-instance", filenames.LimaYAML)
3128
limaVersionFile := filepath.Join(limaDir, filenames.LimaVersion)
3229
err = os.WriteFile(ymlFile, yml, 0o600)
33-
if err != nil {
34-
t.Fatal(err)
35-
}
30+
assert.NilError(t, err)
3631
err = os.WriteFile(limaVersionFile, limaVersion, 0o600)
37-
if err != nil {
38-
t.Fatal(err)
39-
}
32+
assert.NilError(t, err)
4033
_, _ = Inspect("fuzz-instance")
4134
})
4235
}

pkg/vz/network_darwin_test.go

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"net"
99
"path/filepath"
1010
"testing"
11+
12+
"gotest.tools/v3/assert"
1113
)
1214

1315
const (
@@ -17,9 +19,7 @@ const (
1719

1820
func TestDialQemu(t *testing.T) {
1921
listener, err := listenUnix(t.TempDir())
20-
if err != nil {
21-
t.Fatal(err)
22-
}
22+
assert.NilError(t, err)
2323
defer listener.Close()
2424
t.Logf("Listening at %q", listener.Addr())
2525

@@ -34,15 +34,11 @@ func TestDialQemu(t *testing.T) {
3434

3535
// Connect to the fake vmnet server.
3636
client, err := DialQemu(listener.Addr().String())
37-
if err != nil {
38-
t.Fatal(err)
39-
}
37+
assert.NilError(t, err)
4038
t.Log("Connected to fake vment server")
4139

4240
dgramConn, err := net.FileConn(client)
43-
if err != nil {
44-
t.Fatal(err)
45-
}
41+
assert.NilError(t, err)
4642

4743
vzConn := packetConn{Conn: dgramConn}
4844
defer vzConn.Close()
@@ -85,31 +81,21 @@ func TestDialQemu(t *testing.T) {
8581
t.Logf("Receiving and verifying data packets...")
8682
for i := 0; i < packetsCount; i++ {
8783
n, err := vzConn.Read(buf)
88-
if err != nil {
89-
t.Fatal(err)
90-
}
91-
if n < vmnetMaxPacketSize {
92-
t.Fatalf("Expected %d bytes, got %d", vmnetMaxPacketSize, n)
93-
}
84+
assert.NilError(t, err)
85+
assert.Assert(t, n >= vmnetMaxPacketSize, "unexpected number of bytes")
9486

9587
number := binary.BigEndian.Uint32(buf[:4])
96-
if number != uint32(i) {
97-
t.Fatalf("Expected packet %d, got packet %d", i, number)
98-
}
88+
assert.Equal(t, number, uint32(i), "unexpected packet")
9989

10090
for j := 4; j < vmnetMaxPacketSize; j++ {
101-
if buf[j] != 0x55 {
102-
t.Fatalf("Expected byte 0x55 at offset %d, got 0x%02x", j, buf[j])
103-
}
91+
assert.Equal(t, buf[j], byte(0x55), "unexpected byte at offset %d", j)
10492
}
10593
}
10694
t.Logf("Received and verified %d data packets", packetsCount)
10795

10896
for i := 0; i < 2; i++ {
10997
err := <-errc
110-
if err != nil {
111-
t.Fatal(err)
112-
}
98+
assert.NilError(t, err)
11399
}
114100
}
115101

0 commit comments

Comments
 (0)