]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
tests: add and use testutil package to reduce duplication
authorSimon Ruderich <simon@ruderich.org>
Mon, 5 Apr 2021 09:20:02 +0000 (11:20 +0200)
committerSimon Ruderich <simon@ruderich.org>
Mon, 5 Apr 2021 09:20:02 +0000 (11:20 +0200)
This is not as explicit as the previous code but still good enough. And
it gets rid of quite some boilerplate.

18 files changed:
cmd/safcm-remote/ainsl/ainsl_test.go
cmd/safcm-remote/sync/commands_test.go
cmd/safcm-remote/sync/files_test.go
cmd/safcm-remote/sync/packages_debian_test.go
cmd/safcm-remote/sync/services_systemd_test.go
cmd/safcm-remote/sync/triggers_test.go
cmd/safcm/config/files_test.go
cmd/safcm/config/groups_test.go
cmd/safcm/config/hosts_test.go
cmd/safcm/config/permissions_test.go
cmd/safcm/config/templates_test.go
cmd/safcm/config/triggers_test.go
cmd/safcm/sync_changes_test.go
cmd/safcm/sync_info_test.go
cmd/safcm/sync_sync_test.go
cmd/safcm/sync_test.go
cmd/safcm/term_test.go
testutil/testutil.go [new file with mode: 0644]

index 564bc37d99fde46e81ebbe305d27ee838d59b5f1..2526dca0860da6685453c29e6e36e6d5dddfac98 100644 (file)
@@ -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)
                })
        }
 
index 3c47c1757c6adf5f199dc4342a692127da263e03..142a2542a931d4191d56f0b5be641da6a2b00cd7 100644 (file)
@@ -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)
                })
        }
 }
index e57401e3ada3c13b5d3e0edca9b37f06ab19d306..e7864ba8b8080e489da5964663c1496308599bd1 100644 (file)
@@ -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)
                })
        }
 
index ff0e066ae0d8e8ad806bdd4d1fbfe1d7521c6dc0..0e3411dd661b7350a593289f4bbf0a634cce3c1c 100644 (file)
@@ -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)
                })
        }
 }
index 40dd2e0c4644b32bcf357690ae1e1cee4de95f03..0460bb73c771ed6046c95097742a188f9186d64d 100644 (file)
@@ -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)
                })
        }
 }
index 0b8bbe86f4adb1fbd1e34b76e98e11f39cf6ac3e..9775ed546590f26cef5b322f7c04e9fc6276a8d4 100644 (file)
 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)
                })
        }
 }
index c9af0f23596f8fd08a8fa4c9526cc1984ff2240c..1313fdfed18548947a6cda319b1db862d8de8dbb 100644 (file)
@@ -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)
                })
        }
 }
index cf6cd6e088cd48d3789fb2f618f903eb1e420dcc..9c0acda892585577510a26d3eab5640694fecfb3 100644 (file)
@@ -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)
                })
        }
 }
index 95f4e7531936084ba01548f5ae9aa2fdf026c49d..ecbc37e45314b7453864eb9d9d1cac22a57b4a53 100644 (file)
@@ -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)
                })
        }
 }
index ba1d505f9f019f314d7e02c80fa63790f298bf34..98b82b6ba877d0aaae02959384404a4f863815a6 100644 (file)
@@ -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)
                })
        }
 }
index 8097af7312f3891ea2626f435ff4a3933d4cbcef..36b53dc87cd3755572fea831ab4b742113afe235 100644 (file)
@@ -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)
                })
        }
 }
index 7de0322895ba3e0140cd83a5adacd76fc8f109b4..43c3a557882255e870a737ecd9c43ccc7b012368 100644 (file)
@@ -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)
                })
        }
 }
index dda48738063a1307efbb8260bb1151e557a6a2c3..e5efe1d26bb38b44bc5de84bacc517b9b3877062 100644 (file)
@@ -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)
                })
        }
 }
index e01083bb8172b668c95c108fd113232899eff17b..7ad13c9fd01084e6c54c9915cf418b9f9d57e663 100644 (file)
 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)
                })
        }
 }
index f3d046a23a0f4618525403fd1158239e8d149dca..b5e8ce242b01c5e793da5702c24fb942d8bb034a 100644 (file)
@@ -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)
                })
        }
 }
index f904711ccb06963a27ad8d38c6a3a524923c2681..4616f716a28d75f5967e3654b02f2ede81331c84 100644 (file)
@@ -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)
                })
        }
 }
index e5faf174c6e43f02603bfdb785154f07ff9eff33..479d7e8e197f7ab951331adec562c6e8671e21e2 100644 (file)
@@ -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 (file)
index 0000000..8d2e86b
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+
+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)
+       }
+}