Skip to content

Commit afea6a7

Browse files
committed
test: Avoid reptitive/duplicate error logging and quitting
1 parent 63c576c commit afea6a7

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

zfs_test.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,37 @@ func pow2(x int) int64 {
2424

2525
// https://github.com/benbjohnson/testing
2626
// assert fails the test if the condition is false.
27-
func assert(t *testing.T, condition bool, msg string, v ...interface{}) {
27+
func _assert(t *testing.T, condition bool, msg string, v ...interface{}) {
2828
t.Helper()
2929

3030
if !condition {
31-
_, file, line, _ := runtime.Caller(1)
31+
_, file, line, _ := runtime.Caller(2)
3232
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
3333
t.FailNow()
3434
}
3535
}
3636

37+
func assert(t *testing.T, condition bool, msg string, v ...interface{}) {
38+
t.Helper()
39+
_assert(t, condition, msg, v...)
40+
}
41+
3742
// ok fails the test if an err is not nil.
3843
func ok(t *testing.T, err error) {
3944
t.Helper()
40-
41-
if err != nil {
42-
_, file, line, _ := runtime.Caller(1)
43-
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
44-
t.FailNow()
45-
}
45+
_assert(t, err == nil, "unexpected error: %v", err)
4646
}
4747

4848
// nok fails the test if an err is nil.
4949
func nok(t *testing.T, err error) {
5050
t.Helper()
51-
52-
if err == nil {
53-
_, file, line, _ := runtime.Caller(1)
54-
fmt.Printf("\033[31m%s:%d: expected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
55-
t.FailNow()
56-
}
51+
_assert(t, err != nil, "expected error, got nil")
5752
}
5853

5954
// equals fails the test if exp is not equal to act.
6055
func equals(t *testing.T, exp, act interface{}) {
6156
t.Helper()
62-
63-
if !reflect.DeepEqual(exp, act) {
64-
_, file, line, _ := runtime.Caller(1)
65-
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
66-
t.FailNow()
67-
}
57+
_assert(t, reflect.DeepEqual(exp, act), "exp: %#v\n\ngot: %#v", exp, act)
6858
}
6959

7060
func zpoolTest(t *testing.T, fn func()) {

0 commit comments

Comments
 (0)