From 3473766d8afb07cb0685694656947883cbbd1138 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Mon, 5 Apr 2021 11:20:02 +0200 Subject: [PATCH] tests: add and use testutil package to reduce duplication This is not as explicit as the previous code but still good enough. And it gets rid of quite some boilerplate. --- cmd/safcm-remote/ainsl/ainsl_test.go | 21 ++----- cmd/safcm-remote/sync/commands_test.go | 20 ++----- cmd/safcm-remote/sync/files_test.go | 57 ++++--------------- cmd/safcm-remote/sync/packages_debian_test.go | 20 ++----- .../sync/services_systemd_test.go | 20 ++----- cmd/safcm-remote/sync/triggers_test.go | 7 +-- cmd/safcm/config/files_test.go | 15 +---- cmd/safcm/config/groups_test.go | 24 ++------ cmd/safcm/config/hosts_test.go | 14 +---- cmd/safcm/config/permissions_test.go | 14 +---- cmd/safcm/config/templates_test.go | 14 +---- cmd/safcm/config/triggers_test.go | 14 +---- cmd/safcm/sync_changes_test.go | 19 ++----- cmd/safcm/sync_info_test.go | 8 +-- cmd/safcm/sync_sync_test.go | 21 ++----- cmd/safcm/sync_test.go | 14 +---- cmd/safcm/term_test.go | 6 +- testutil/testutil.go | 42 ++++++++++++++ 18 files changed, 111 insertions(+), 239 deletions(-) create mode 100644 testutil/testutil.go diff --git a/cmd/safcm-remote/ainsl/ainsl_test.go b/cmd/safcm-remote/ainsl/ainsl_test.go index 564bc37..2526dca 100644 --- a/cmd/safcm-remote/ainsl/ainsl_test.go +++ b/cmd/safcm-remote/ainsl/ainsl_test.go @@ -20,13 +20,11 @@ import ( "io/fs" "os" "path/filepath" - "reflect" "syscall" "testing" - "github.com/google/go-cmp/cmp" - ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest" + "ruderich.org/simon/safcm/testutil" ) func TestHandle(t *testing.T) { @@ -310,24 +308,15 @@ func TestHandle(t *testing.T) { } changes, err := handle(tc.path, tc.line, tc.create) - if !reflect.DeepEqual(tc.expChanges, changes) { - t.Errorf("changes: %s", - cmp.Diff(tc.expChanges, changes)) - } - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertEqual(t, "changes", + changes, tc.expChanges) + testutil.AssertErrorEqual(t, "err", err, tc.expErr) files, err := ft.WalkDir(path) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(tc.expFiles, files) { - t.Errorf("files: %s", - cmp.Diff(tc.expFiles, files)) - } + testutil.AssertEqual(t, "files", files, tc.expFiles) }) } diff --git a/cmd/safcm-remote/sync/commands_test.go b/cmd/safcm-remote/sync/commands_test.go index 3c47c17..142a254 100644 --- a/cmd/safcm-remote/sync/commands_test.go +++ b/cmd/safcm-remote/sync/commands_test.go @@ -20,12 +20,10 @@ import ( "io/fs" "os" "os/exec" - "reflect" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm" + "ruderich.org/simon/safcm/testutil" ) func TestSyncCommands(t *testing.T) { @@ -508,21 +506,11 @@ func TestSyncCommands(t *testing.T) { s.triggers = tc.triggers err := s.syncCommands() - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertErrorEqual(t, "err", err, tc.expErr) dbg := res.Wait() - if !reflect.DeepEqual(tc.expResp, s.resp) { - t.Errorf("resp: %s", - cmp.Diff(tc.expResp, s.resp)) - } - if !reflect.DeepEqual(tc.expDbg, dbg) { - t.Errorf("dbg: %s", - cmp.Diff(tc.expDbg, dbg)) - } + testutil.AssertEqual(t, "resp", s.resp, tc.expResp) + testutil.AssertEqual(t, "dbg", dbg, tc.expDbg) }) } } diff --git a/cmd/safcm-remote/sync/files_test.go b/cmd/safcm-remote/sync/files_test.go index e57401e..e7864ba 100644 --- a/cmd/safcm-remote/sync/files_test.go +++ b/cmd/safcm-remote/sync/files_test.go @@ -21,14 +21,12 @@ import ( "math/rand" "os" "path/filepath" - "reflect" "regexp" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm" ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest" + "ruderich.org/simon/safcm/testutil" ) var randFilesRegexp = regexp.MustCompile(`\d+"$`) @@ -863,38 +861,23 @@ func TestSyncFiles(t *testing.T) { s.setDefaults() err := s.syncFiles() - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertErrorEqual(t, "err", err, tc.expErr) dbg := res.Wait() // Remove random file names from result for i, x := range dbg { dbg[i] = randFilesRegexp.ReplaceAllString(x, `RND"`) } - if !reflect.DeepEqual(tc.expDbg, dbg) { - t.Errorf("dbg: %s", - cmp.Diff(tc.expDbg, dbg)) - } + testutil.AssertEqual(t, "dbg", dbg, tc.expDbg) files, err := ft.WalkDir(path) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(tc.expFiles, files) { - t.Errorf("files: %s", - cmp.Diff(tc.expFiles, files)) - } + testutil.AssertEqual(t, "files", files, tc.expFiles) - if !reflect.DeepEqual(tc.expResp, s.resp) { - t.Errorf("resp: %s", - cmp.Diff(tc.expResp, s.resp)) - } - if !reflect.DeepEqual(tc.triggers, s.triggers) { - t.Errorf("triggers: %s", - cmp.Diff(tc.triggers, s.triggers)) - } + testutil.AssertEqual(t, "resp", s.resp, tc.expResp) + testutil.AssertEqual(t, "triggers", + s.triggers, tc.triggers) }) } @@ -2422,38 +2405,22 @@ file var changed bool err := s.syncFile(tc.file, &changed) - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertErrorEqual(t, "err", err, tc.expErr) dbg := res.Wait() // Remove random file names from result for i, x := range dbg { dbg[i] = randFilesRegexp.ReplaceAllString(x, `RND"`) } - if !reflect.DeepEqual(tc.expDbg, dbg) { - t.Errorf("dbg: %s", - cmp.Diff(tc.expDbg, dbg)) - } + testutil.AssertEqual(t, "dbg", dbg, tc.expDbg) files, err := ft.WalkDir(path) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(tc.expFiles, files) { - t.Errorf("files: %s", - cmp.Diff(tc.expFiles, files)) - } + testutil.AssertEqual(t, "files", files, tc.expFiles) - if tc.expChanged != changed { - t.Errorf("changed = %#v, want %#v", - changed, tc.expChanged) - } - if !reflect.DeepEqual(tc.expResp, s.resp) { - t.Errorf("resp: %s", - cmp.Diff(tc.expResp, s.resp)) - } + testutil.AssertEqual(t, "changed", changed, tc.expChanged) + testutil.AssertEqual(t, "resp", s.resp, tc.expResp) }) } diff --git a/cmd/safcm-remote/sync/packages_debian_test.go b/cmd/safcm-remote/sync/packages_debian_test.go index ff0e066..0e3411d 100644 --- a/cmd/safcm-remote/sync/packages_debian_test.go +++ b/cmd/safcm-remote/sync/packages_debian_test.go @@ -20,12 +20,10 @@ import ( "fmt" "os" "os/exec" - "reflect" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm" + "ruderich.org/simon/safcm/testutil" ) func TestSyncPackagesDebian(t *testing.T) { @@ -310,21 +308,11 @@ install ok installed golang-1.16-src }) err := s.syncPackagesDebian() - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertErrorEqual(t, "err", err, tc.expErr) dbg := res.Wait() - if !reflect.DeepEqual(tc.expResp, s.resp) { - t.Errorf("resp: %s", - cmp.Diff(tc.expResp, s.resp)) - } - if !reflect.DeepEqual(tc.expDbg, dbg) { - t.Errorf("dbg: %s", - cmp.Diff(tc.expDbg, dbg)) - } + testutil.AssertEqual(t, "resp", s.resp, tc.expResp) + testutil.AssertEqual(t, "dbg", dbg, tc.expDbg) }) } } diff --git a/cmd/safcm-remote/sync/services_systemd_test.go b/cmd/safcm-remote/sync/services_systemd_test.go index 40dd2e0..0460bb7 100644 --- a/cmd/safcm-remote/sync/services_systemd_test.go +++ b/cmd/safcm-remote/sync/services_systemd_test.go @@ -19,12 +19,10 @@ import ( "bytes" "fmt" "os/exec" - "reflect" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm" + "ruderich.org/simon/safcm/testutil" ) func TestSyncServicesSystemd(t *testing.T) { @@ -517,21 +515,11 @@ LoadError= }) err := s.syncServicesSystemd() - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertErrorEqual(t, "err", err, tc.expErr) dbg := res.Wait() - if !reflect.DeepEqual(tc.expResp, s.resp) { - t.Errorf("resp: %s", - cmp.Diff(tc.expResp, s.resp)) - } - if !reflect.DeepEqual(tc.expDbg, dbg) { - t.Errorf("dbg: %s", - cmp.Diff(tc.expDbg, dbg)) - } + testutil.AssertEqual(t, "resp", s.resp, tc.expResp) + testutil.AssertEqual(t, "dbg", dbg, tc.expDbg) }) } } diff --git a/cmd/safcm-remote/sync/triggers_test.go b/cmd/safcm-remote/sync/triggers_test.go index 0b8bbe8..9775ed5 100644 --- a/cmd/safcm-remote/sync/triggers_test.go +++ b/cmd/safcm-remote/sync/triggers_test.go @@ -16,10 +16,9 @@ package sync import ( - "reflect" "testing" - "github.com/google/go-cmp/cmp" + "ruderich.org/simon/safcm/testutil" ) func TestTriggerPaths(t *testing.T) { @@ -57,9 +56,7 @@ func TestTriggerPaths(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { res := triggerPaths(tc.path) - if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("res: %s", cmp.Diff(tc.exp, res)) - } + testutil.AssertEqual(t, "res", res, tc.exp) }) } } diff --git a/cmd/safcm/config/files_test.go b/cmd/safcm/config/files_test.go index c9af0f2..1313fdf 100644 --- a/cmd/safcm/config/files_test.go +++ b/cmd/safcm/config/files_test.go @@ -19,13 +19,11 @@ import ( "fmt" "io/fs" "os" - "reflect" "syscall" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm" + "ruderich.org/simon/safcm/testutil" ) func chmod(name string, perm int) { @@ -190,15 +188,8 @@ This is FreeBSD host for _, tc := range tests { t.Run(tc.group, func(t *testing.T) { res, err := LoadFiles(tc.group) - - if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("res: %s", cmp.Diff(tc.exp, res)) - } - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertEqual(t, "res", res, tc.exp) + testutil.AssertErrorEqual(t, "err", err, tc.expErr) }) } } diff --git a/cmd/safcm/config/groups_test.go b/cmd/safcm/config/groups_test.go index cf6cd6e..9c0acda 100644 --- a/cmd/safcm/config/groups_test.go +++ b/cmd/safcm/config/groups_test.go @@ -19,10 +19,9 @@ import ( "fmt" "os" "path/filepath" - "reflect" "testing" - "github.com/google/go-cmp/cmp" + "ruderich.org/simon/safcm/testutil" ) func TestLoadGroups(t *testing.T) { @@ -193,15 +192,8 @@ func TestLoadGroups(t *testing.T) { } res, err := LoadGroups(tc.cfg, tc.hosts) - - if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("res: %s", cmp.Diff(tc.exp, res)) - } - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertEqual(t, "res", res, tc.exp) + testutil.AssertErrorEqual(t, "err", err, tc.expErr) }) } } @@ -315,14 +307,8 @@ func TestResolveHostGroups(t *testing.T) { t.Run(tc.name, func(t *testing.T) { res, err := ResolveHostGroups(tc.host, allGroups, tc.detected) - if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("res: %s", cmp.Diff(tc.exp, res)) - } - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertEqual(t, "res", res, tc.exp) + testutil.AssertErrorEqual(t, "err", err, tc.expErr) }) } } diff --git a/cmd/safcm/config/hosts_test.go b/cmd/safcm/config/hosts_test.go index 95f4e75..ecbc37e 100644 --- a/cmd/safcm/config/hosts_test.go +++ b/cmd/safcm/config/hosts_test.go @@ -19,10 +19,9 @@ import ( "fmt" "os" "path/filepath" - "reflect" "testing" - "github.com/google/go-cmp/cmp" + "ruderich.org/simon/safcm/testutil" ) func TestLoadHosts(t *testing.T) { @@ -90,15 +89,8 @@ func TestLoadHosts(t *testing.T) { } res, err := LoadHosts() - - if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("res: %s", cmp.Diff(tc.exp, res)) - } - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertEqual(t, "res", res, tc.exp) + testutil.AssertErrorEqual(t, "err", err, tc.expErr) }) } } diff --git a/cmd/safcm/config/permissions_test.go b/cmd/safcm/config/permissions_test.go index ba1d505..98b82b6 100644 --- a/cmd/safcm/config/permissions_test.go +++ b/cmd/safcm/config/permissions_test.go @@ -19,12 +19,10 @@ import ( "fmt" "io/fs" "os" - "reflect" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm" + "ruderich.org/simon/safcm/testutil" ) func TestLoadPermissions(t *testing.T) { @@ -235,14 +233,8 @@ This is FreeBSD host } err = LoadPermissions(tc.group, files) - if !reflect.DeepEqual(tc.exp, files) { - t.Errorf("res: %s", cmp.Diff(tc.exp, files)) - } - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertEqual(t, "res", files, tc.exp) + testutil.AssertErrorEqual(t, "err", err, tc.expErr) }) } } diff --git a/cmd/safcm/config/templates_test.go b/cmd/safcm/config/templates_test.go index 8097af7..36b53dc 100644 --- a/cmd/safcm/config/templates_test.go +++ b/cmd/safcm/config/templates_test.go @@ -19,12 +19,10 @@ import ( "fmt" "io/fs" "os" - "reflect" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm" + "ruderich.org/simon/safcm/testutil" ) func TestLoadTemplates(t *testing.T) { @@ -252,14 +250,8 @@ This is GNU/Linux host err = LoadTemplates(tc.group, files, host, groups, allHosts, allGroups) - if !reflect.DeepEqual(tc.exp, files) { - t.Errorf("res: %s", cmp.Diff(tc.exp, files)) - } - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertEqual(t, "res", files, tc.exp) + testutil.AssertErrorEqual(t, "err", err, tc.expErr) }) } } diff --git a/cmd/safcm/config/triggers_test.go b/cmd/safcm/config/triggers_test.go index 7de0322..43c3a55 100644 --- a/cmd/safcm/config/triggers_test.go +++ b/cmd/safcm/config/triggers_test.go @@ -19,12 +19,10 @@ import ( "fmt" "io/fs" "os" - "reflect" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm" + "ruderich.org/simon/safcm/testutil" ) func TestLoadTriggers(t *testing.T) { @@ -142,14 +140,8 @@ This is FreeBSD host } err = LoadTriggers(tc.group, files) - if !reflect.DeepEqual(tc.exp, files) { - t.Errorf("res: %s", cmp.Diff(tc.exp, files)) - } - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertEqual(t, "res", files, tc.exp) + testutil.AssertErrorEqual(t, "err", err, tc.expErr) }) } } diff --git a/cmd/safcm/sync_changes_test.go b/cmd/safcm/sync_changes_test.go index dda4873..e5efe1d 100644 --- a/cmd/safcm/sync_changes_test.go +++ b/cmd/safcm/sync_changes_test.go @@ -19,10 +19,9 @@ import ( "io/fs" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm" "ruderich.org/simon/safcm/cmd/safcm/config" + "ruderich.org/simon/safcm/testutil" ) func TestFormatFileChanges(t *testing.T) { @@ -291,9 +290,7 @@ func TestFormatFileChanges(t *testing.T) { } res := s.formatFileChanges(tc.changes) - if tc.exp != res { - t.Errorf("res: %s", cmp.Diff(tc.exp, res)) - } + testutil.AssertEqual(t, "res", res, tc.exp) }) } } @@ -363,9 +360,7 @@ func TestFormatPackageChanges(t *testing.T) { } res := s.formatPackageChanges(tc.changes) - if tc.exp != res { - t.Errorf("res: %s", cmp.Diff(tc.exp, res)) - } + testutil.AssertEqual(t, "res", res, tc.exp) }) } } @@ -457,9 +452,7 @@ func TestFormatServiceChanges(t *testing.T) { } res := s.formatServiceChanges(tc.changes) - if tc.exp != res { - t.Errorf("res: %s", cmp.Diff(tc.exp, res)) - } + testutil.AssertEqual(t, "res", res, tc.exp) }) } } @@ -669,9 +662,7 @@ func TestFormatCommandChanges(t *testing.T) { } res := s.formatCommandChanges(tc.changes) - if tc.exp != res { - t.Errorf("res: %s", cmp.Diff(tc.exp, res)) - } + testutil.AssertEqual(t, "res", res, tc.exp) }) } } diff --git a/cmd/safcm/sync_info_test.go b/cmd/safcm/sync_info_test.go index e01083b..7ad13c9 100644 --- a/cmd/safcm/sync_info_test.go +++ b/cmd/safcm/sync_info_test.go @@ -16,12 +16,10 @@ package main import ( - "reflect" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm" + "ruderich.org/simon/safcm/testutil" ) func TestHostInfoRespToGroups(t *testing.T) { @@ -70,9 +68,7 @@ func TestHostInfoRespToGroups(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { res := hostInfoRespToGroups(tc.resp) - if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("res: %s", cmp.Diff(tc.exp, res)) - } + testutil.AssertEqual(t, "res", res, tc.exp) }) } } diff --git a/cmd/safcm/sync_sync_test.go b/cmd/safcm/sync_sync_test.go index f3d046a..b5e8ce2 100644 --- a/cmd/safcm/sync_sync_test.go +++ b/cmd/safcm/sync_sync_test.go @@ -22,12 +22,10 @@ import ( "log" "os" "path/filepath" - "reflect" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm" + "ruderich.org/simon/safcm/testutil" ) func TestHostSyncReq(t *testing.T) { @@ -470,21 +468,14 @@ func TestHostSyncReq(t *testing.T) { } res, err := s.hostSyncReq(tc.detected) - if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("res: %s", cmp.Diff(tc.exp, res)) - } - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + + testutil.AssertEqual(t, "res", res, tc.exp) + testutil.AssertErrorEqual(t, "err", err, tc.expErr) close(ch) <-done - if !reflect.DeepEqual(tc.expEvents, events) { - t.Errorf("events: %s", - cmp.Diff(tc.expEvents, events)) - } + testutil.AssertEqual(t, "events", + events, tc.expEvents) }) } } diff --git a/cmd/safcm/sync_test.go b/cmd/safcm/sync_test.go index f904711..4616f71 100644 --- a/cmd/safcm/sync_test.go +++ b/cmd/safcm/sync_test.go @@ -18,12 +18,10 @@ package main import ( "fmt" "os" - "reflect" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm/cmd/safcm/config" + "ruderich.org/simon/safcm/testutil" ) func TestHostsToSync(t *testing.T) { @@ -168,14 +166,8 @@ func TestHostsToSync(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { res, err := hostsToSync(tc.names, allHosts, allGroups) - if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("res: %s", cmp.Diff(tc.exp, res)) - } - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + testutil.AssertEqual(t, "res", res, tc.exp) + testutil.AssertErrorEqual(t, "err", err, tc.expErr) }) } } diff --git a/cmd/safcm/term_test.go b/cmd/safcm/term_test.go index e5faf17..479d7e8 100644 --- a/cmd/safcm/term_test.go +++ b/cmd/safcm/term_test.go @@ -18,7 +18,7 @@ package main import ( "testing" - "github.com/google/go-cmp/cmp" + "ruderich.org/simon/safcm/testutil" ) func TestEscapeControlCharacters(t *testing.T) { @@ -84,9 +84,7 @@ func TestEscapeControlCharacters(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { res := EscapeControlCharacters(tc.isTTY, tc.x) - if tc.exp != res { - t.Errorf("res: %s", cmp.Diff(tc.exp, res)) - } + testutil.AssertEqual(t, "res", res, tc.exp) }) } } diff --git a/testutil/testutil.go b/testutil/testutil.go new file mode 100644 index 0000000..8d2e86b --- /dev/null +++ b/testutil/testutil.go @@ -0,0 +1,42 @@ +// Utility functions useful for tests + +// Copyright (C) 2021 Simon Ruderich +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package testutil + +import ( + "fmt" + "reflect" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func AssertEqual(t *testing.T, name string, act, exp interface{}) { + if !reflect.DeepEqual(act, exp) { + t.Errorf("%s: %s", name, cmp.Diff(exp, act)) + } +} + +func AssertErrorEqual(t *testing.T, name string, act, exp error) { + // Ugly but the simplest way to compare errors (including nil) + actStr := fmt.Sprintf("%s", act) + expStr := fmt.Sprintf("%s", exp) + if actStr != expStr { + t.Errorf("err = %s (%#v), want %s (%#v)", + actStr, act, expStr, exp) + } +} -- 2.44.1