From: Simon Ruderich Date: Mon, 5 Apr 2021 08:36:02 +0000 (+0200) Subject: tests: use subtests X-Git-Url: https://ruderich.org/simon/gitweb/?p=safcm%2Fsafcm.git;a=commitdiff_plain;h=992eaae7fec45c2d58fff89d1bc0ae920a899296 tests: use subtests This removes the need to print the name of the test in each t.Errorf() call which reduces unnecessary code. It also permits skipping tests which will be used in the future. --- diff --git a/cmd/safcm-remote/ainsl/ainsl_test.go b/cmd/safcm-remote/ainsl/ainsl_test.go index 25258ff..e4d6b46 100644 --- a/cmd/safcm-remote/ainsl/ainsl_test.go +++ b/cmd/safcm-remote/ainsl/ainsl_test.go @@ -293,6 +293,7 @@ func TestHandle(t *testing.T) { } for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { // Create separate test directory for each test case path := filepath.Join(cwd, "testdata", tc.name) err = os.Mkdir(path, 0700) @@ -310,13 +311,13 @@ func TestHandle(t *testing.T) { changes, err := handle(tc.path, tc.line, tc.create) if !reflect.DeepEqual(tc.expChanges, changes) { - t.Errorf("%s: changes: %s", tc.name, + 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("%s: err = %#v, want %#v", - tc.name, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } files, err := ft.WalkDir(path) @@ -324,9 +325,10 @@ func TestHandle(t *testing.T) { t.Fatal(err) } if !reflect.DeepEqual(tc.expFiles, files) { - t.Errorf("%s: files: %s", tc.name, + t.Errorf("files: %s", cmp.Diff(tc.expFiles, files)) } + }) } if !t.Failed() { diff --git a/cmd/safcm-remote/sync/commands_test.go b/cmd/safcm-remote/sync/commands_test.go index 1b1adf7..eb0cc0b 100644 --- a/cmd/safcm-remote/sync/commands_test.go +++ b/cmd/safcm-remote/sync/commands_test.go @@ -497,9 +497,9 @@ func TestSyncCommands(t *testing.T) { } for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { s, res := prepareSync(tc.req, &testRunner{ t: t, - name: tc.name, expCmds: tc.expCmds, resStdout: tc.stdout, resStderr: tc.stderr, @@ -510,18 +510,19 @@ func TestSyncCommands(t *testing.T) { 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("%s: err = %#v, want %#v", - tc.name, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } dbg := res.Wait() if !reflect.DeepEqual(tc.expResp, s.resp) { - t.Errorf("%s: resp: %s", tc.name, + t.Errorf("resp: %s", cmp.Diff(tc.expResp, s.resp)) } if !reflect.DeepEqual(tc.expDbg, dbg) { - t.Errorf("%s: dbg: %s", tc.name, + t.Errorf("dbg: %s", cmp.Diff(tc.expDbg, dbg)) } + }) } } diff --git a/cmd/safcm-remote/sync/files_test.go b/cmd/safcm-remote/sync/files_test.go index c5c7216..32130e9 100644 --- a/cmd/safcm-remote/sync/files_test.go +++ b/cmd/safcm-remote/sync/files_test.go @@ -841,6 +841,7 @@ func TestSyncFiles(t *testing.T) { } for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { // Create separate test directory for each test case path := filepath.Join(cwd, "testdata", "files-"+tc.name) err = os.Mkdir(path, 0700) @@ -858,15 +859,14 @@ func TestSyncFiles(t *testing.T) { s, res := prepareSync(tc.req, &testRunner{ t: t, - name: tc.name, }) 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("%s: err = %#v, want %#v", - tc.name, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } dbg := res.Wait() // Remove random file names from result @@ -874,7 +874,7 @@ func TestSyncFiles(t *testing.T) { dbg[i] = randFilesRegexp.ReplaceAllString(x, `RND"`) } if !reflect.DeepEqual(tc.expDbg, dbg) { - t.Errorf("%s: dbg: %s", tc.name, + t.Errorf("dbg: %s", cmp.Diff(tc.expDbg, dbg)) } @@ -883,18 +883,19 @@ func TestSyncFiles(t *testing.T) { t.Fatal(err) } if !reflect.DeepEqual(tc.expFiles, files) { - t.Errorf("%s: files: %s", tc.name, + t.Errorf("files: %s", cmp.Diff(tc.expFiles, files)) } if !reflect.DeepEqual(tc.expResp, s.resp) { - t.Errorf("%s: resp: %s", tc.name, + t.Errorf("resp: %s", cmp.Diff(tc.expResp, s.resp)) } if !reflect.DeepEqual(tc.triggers, s.triggers) { - t.Errorf("%s: triggers: %s", tc.name, + t.Errorf("triggers: %s", cmp.Diff(tc.triggers, s.triggers)) } + }) } os.Remove(tmpTestFilePath) @@ -2395,6 +2396,7 @@ file } for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { // Create separate test directory for each test case path := filepath.Join(cwd, "testdata", "file-"+tc.name) err = os.Mkdir(path, 0700) @@ -2412,7 +2414,6 @@ file s, res := prepareSync(tc.req, &testRunner{ t: t, - name: tc.name, }) s.setDefaults() @@ -2423,8 +2424,8 @@ file 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("%s: err = %#v, want %#v", - tc.name, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } dbg := res.Wait() // Remove random file names from result @@ -2432,7 +2433,7 @@ file dbg[i] = randFilesRegexp.ReplaceAllString(x, `RND"`) } if !reflect.DeepEqual(tc.expDbg, dbg) { - t.Errorf("%s: dbg: %s", tc.name, + t.Errorf("dbg: %s", cmp.Diff(tc.expDbg, dbg)) } @@ -2441,18 +2442,19 @@ file t.Fatal(err) } if !reflect.DeepEqual(tc.expFiles, files) { - t.Errorf("%s: files: %s", tc.name, + t.Errorf("files: %s", cmp.Diff(tc.expFiles, files)) } if tc.expChanged != changed { - t.Errorf("%s: changed = %#v, want %#v", - tc.name, changed, tc.expChanged) + t.Errorf("changed = %#v, want %#v", + changed, tc.expChanged) } if !reflect.DeepEqual(tc.expResp, s.resp) { - t.Errorf("%s: resp: %s", tc.name, + t.Errorf("resp: %s", cmp.Diff(tc.expResp, s.resp)) } + }) } if !t.Failed() { diff --git a/cmd/safcm-remote/sync/packages_debian_test.go b/cmd/safcm-remote/sync/packages_debian_test.go index f67dda9..51b6b74 100644 --- a/cmd/safcm-remote/sync/packages_debian_test.go +++ b/cmd/safcm-remote/sync/packages_debian_test.go @@ -300,9 +300,9 @@ install ok installed golang-1.16-src } for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { s, res := prepareSync(tc.req, &testRunner{ t: t, - name: tc.name, expCmds: tc.expCmds, resStdout: tc.stdout, resStderr: tc.stderr, @@ -312,18 +312,19 @@ 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("%s: err = %#v, want %#v", - tc.name, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } dbg := res.Wait() if !reflect.DeepEqual(tc.expResp, s.resp) { - t.Errorf("%s: resp: %s", tc.name, + t.Errorf("resp: %s", cmp.Diff(tc.expResp, s.resp)) } if !reflect.DeepEqual(tc.expDbg, dbg) { - t.Errorf("%s: dbg: %s", tc.name, + t.Errorf("dbg: %s", cmp.Diff(tc.expDbg, dbg)) } + }) } } diff --git a/cmd/safcm-remote/sync/services_systemd_test.go b/cmd/safcm-remote/sync/services_systemd_test.go index 8ba0bfa..df226fc 100644 --- a/cmd/safcm-remote/sync/services_systemd_test.go +++ b/cmd/safcm-remote/sync/services_systemd_test.go @@ -507,9 +507,9 @@ LoadError= } for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { s, res := prepareSync(tc.req, &testRunner{ t: t, - name: tc.name, expCmds: tc.expCmds, resStdout: tc.stdout, resStderr: tc.stderr, @@ -519,18 +519,19 @@ 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("%s: err = %#v, want %#v", - tc.name, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } dbg := res.Wait() if !reflect.DeepEqual(tc.expResp, s.resp) { - t.Errorf("%s: resp: %s", tc.name, + t.Errorf("resp: %s", cmp.Diff(tc.expResp, s.resp)) } if !reflect.DeepEqual(tc.expDbg, dbg) { - t.Errorf("%s: dbg: %s", tc.name, + t.Errorf("dbg: %s", cmp.Diff(tc.expDbg, dbg)) } + }) } } diff --git a/cmd/safcm-remote/sync/sync_test.go b/cmd/safcm-remote/sync/sync_test.go index 44e2be2..4c0f08d 100644 --- a/cmd/safcm-remote/sync/sync_test.go +++ b/cmd/safcm-remote/sync/sync_test.go @@ -33,7 +33,6 @@ import ( type testRunner struct { t *testing.T - name string expCmds []*exec.Cmd resStdout [][]byte resStderr [][]byte @@ -56,8 +55,7 @@ func (r *testRunner) CombinedOutput(cmd *exec.Cmd) ([]byte, error) { stdout, stderr, err := r.check("combinedOutput", cmd) if stderr != nil { // stdout also contains stderr - r.t.Fatalf("%s: CombinedOutput: stderr != nil, but %v", - r.name, stderr) + r.t.Fatalf("CombinedOutput: stderr != nil, but %v", stderr) } return stdout, err } @@ -65,22 +63,22 @@ func (r *testRunner) check(method string, cmd *exec.Cmd) ( []byte, []byte, error) { if len(r.expCmds) == 0 { - r.t.Fatalf("%s: %s: empty expCmds", r.name, method) + r.t.Fatalf("%s: empty expCmds", method) } if len(r.resStdout) == 0 { - r.t.Fatalf("%s: %s: empty resStdout", r.name, method) + r.t.Fatalf("%s: empty resStdout", method) } if len(r.resStderr) == 0 { - r.t.Fatalf("%s: %s: empty resStderr", r.name, method) + r.t.Fatalf("%s: empty resStderr", method) } if len(r.resError) == 0 { - r.t.Fatalf("%s: %s: empty resError", r.name, method) + r.t.Fatalf("%s: empty resError", method) } exp := r.expCmds[0] r.expCmds = r.expCmds[1:] if !reflect.DeepEqual(exp, cmd) { - r.t.Errorf("%s: %s: %s", r.name, method, + r.t.Errorf("%s: %s", method, cmp.Diff(exp, cmd, cmpopts.IgnoreUnexported( exec.Cmd{}, bytes.Buffer{}))) @@ -140,20 +138,16 @@ func (s *syncTestResult) Wait() []string { // All expected commands must have been executed if len(s.runner.expCmds) != 0 { - s.runner.t.Errorf("%s: expCmds left: %v", - s.runner.name, s.runner.expCmds) + s.runner.t.Errorf("expCmds left: %v", s.runner.expCmds) } if len(s.runner.resStdout) != 0 { - s.runner.t.Errorf("%s: resStdout left: %v", - s.runner.name, s.runner.resStdout) + s.runner.t.Errorf("resStdout left: %v", s.runner.resStdout) } if len(s.runner.resStderr) != 0 { - s.runner.t.Errorf("%s: resStderr left: %v", - s.runner.name, s.runner.resStderr) + s.runner.t.Errorf("resStderr left: %v", s.runner.resStderr) } if len(s.runner.resError) != 0 { - s.runner.t.Errorf("%s: resError left: %v", - s.runner.name, s.runner.resError) + s.runner.t.Errorf("resError left: %v", s.runner.resError) } return s.dbg diff --git a/cmd/safcm-remote/sync/triggers_test.go b/cmd/safcm-remote/sync/triggers_test.go index 27a0d02..cc41886 100644 --- a/cmd/safcm-remote/sync/triggers_test.go +++ b/cmd/safcm-remote/sync/triggers_test.go @@ -55,10 +55,12 @@ 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("%s: res: %s", tc.name, + t.Errorf("res: %s", cmp.Diff(tc.exp, res)) } + }) } } diff --git a/cmd/safcm/config/files_test.go b/cmd/safcm/config/files_test.go index 398394c..ef31591 100644 --- a/cmd/safcm/config/files_test.go +++ b/cmd/safcm/config/files_test.go @@ -188,16 +188,18 @@ 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("%s: res: %s", tc.group, + 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("%s: err = %#v, want %#v", - tc.group, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } + }) } } diff --git a/cmd/safcm/config/groups_test.go b/cmd/safcm/config/groups_test.go index ccf0968..68f18ba 100644 --- a/cmd/safcm/config/groups_test.go +++ b/cmd/safcm/config/groups_test.go @@ -185,6 +185,7 @@ func TestLoadGroups(t *testing.T) { } for _, tc := range tests { + t.Run(tc.path, func(t *testing.T) { err := os.Chdir(tc.path) if err != nil { t.Fatal(err) @@ -193,19 +194,20 @@ func TestLoadGroups(t *testing.T) { res, err := LoadGroups(tc.cfg, tc.hosts) if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("%s: res: %s", tc.path, + 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("%s: err = %#v, want %#v", - tc.path, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } err = os.Chdir(cwd) if err != nil { t.Fatal(err) } + }) } } @@ -315,15 +317,17 @@ func TestResolveHostGroups(t *testing.T) { } for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { res, err := ResolveHostGroups(tc.host, allGroups, tc.detected) if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("%s: res: %s", tc.name, + 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("%s: err = %#v, want %#v", - tc.name, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } + }) } } diff --git a/cmd/safcm/config/hosts_test.go b/cmd/safcm/config/hosts_test.go index 49f4d56..36a8c37 100644 --- a/cmd/safcm/config/hosts_test.go +++ b/cmd/safcm/config/hosts_test.go @@ -83,6 +83,7 @@ func TestLoadHosts(t *testing.T) { } for _, tc := range tests { + t.Run(tc.path, func(t *testing.T) { err := os.Chdir(filepath.Join(cwd, tc.path)) if err != nil { t.Fatal(err) @@ -91,13 +92,14 @@ func TestLoadHosts(t *testing.T) { res, err := LoadHosts() if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("%s: res: %s", tc.path, + 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("%s: err = %#v, want %#v", - tc.path, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } + }) } } diff --git a/cmd/safcm/config/permissions_test.go b/cmd/safcm/config/permissions_test.go index cec72a6..4efefaf 100644 --- a/cmd/safcm/config/permissions_test.go +++ b/cmd/safcm/config/permissions_test.go @@ -226,23 +226,24 @@ This is FreeBSD host } for _, tc := range tests { + t.Run(tc.group, func(t *testing.T) { // Use LoadFiles() so we work on real data and don't make any // mistakes generating it files, err := LoadFiles(tc.group) if err != nil { - t.Fatalf("%s: err = %#v, want nil", - tc.group, err) + t.Fatalf("err = %#v, want nil", err) } err = LoadPermissions(tc.group, files) if !reflect.DeepEqual(tc.exp, files) { - t.Errorf("%s: res: %s", tc.group, + 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("%s: err = %#v, want %#v", - tc.group, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } + }) } } diff --git a/cmd/safcm/config/templates_test.go b/cmd/safcm/config/templates_test.go index 6a0182c..bd65c09 100644 --- a/cmd/safcm/config/templates_test.go +++ b/cmd/safcm/config/templates_test.go @@ -242,24 +242,25 @@ This is GNU/Linux host } for _, tc := range tests { + t.Run(tc.group, func(t *testing.T) { // Use LoadFiles() so we work on real data and don't make any // mistakes generating it files, err := LoadFiles(tc.group) if err != nil { - t.Fatalf("%s: err = %#v, want nil", - tc.group, err) + t.Fatalf("err = %#v, want nil", err) } err = LoadTemplates(tc.group, files, host, groups, allHosts, allGroups) if !reflect.DeepEqual(tc.exp, files) { - t.Errorf("%s: res: %s", tc.group, + 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("%s: err = %#v, want %#v", - tc.group, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } + }) } } diff --git a/cmd/safcm/config/triggers_test.go b/cmd/safcm/config/triggers_test.go index a27ec6a..96e52d7 100644 --- a/cmd/safcm/config/triggers_test.go +++ b/cmd/safcm/config/triggers_test.go @@ -133,23 +133,24 @@ This is FreeBSD host } for _, tc := range tests { + t.Run(tc.group, func(t *testing.T) { // Use LoadFiles() so we work on real data and don't make any // mistakes generating it files, err := LoadFiles(tc.group) if err != nil { - t.Fatalf("%s: err = %#v, want nil", - tc.group, err) + t.Fatalf("err = %#v, want nil", err) } err = LoadTriggers(tc.group, files) if !reflect.DeepEqual(tc.exp, files) { - t.Errorf("%s: res: %s", tc.group, + 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("%s: err = %#v, want %#v", - tc.group, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } + }) } } diff --git a/cmd/safcm/sync_changes_test.go b/cmd/safcm/sync_changes_test.go index f48d441..767564f 100644 --- a/cmd/safcm/sync_changes_test.go +++ b/cmd/safcm/sync_changes_test.go @@ -283,6 +283,7 @@ func TestFormatFileChanges(t *testing.T) { } for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { s := &Sync{ config: &config.Config{ DryRun: tc.dryRun, @@ -291,9 +292,10 @@ func TestFormatFileChanges(t *testing.T) { res := s.formatFileChanges(tc.changes) if tc.exp != res { - t.Errorf("%s: res: %s", tc.name, + t.Errorf("res: %s", cmp.Diff(tc.exp, res)) } + }) } } @@ -354,6 +356,7 @@ func TestFormatPackageChanges(t *testing.T) { } for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { s := &Sync{ config: &config.Config{ DryRun: tc.dryRun, @@ -362,9 +365,10 @@ func TestFormatPackageChanges(t *testing.T) { res := s.formatPackageChanges(tc.changes) if tc.exp != res { - t.Errorf("%s: res: %s", tc.name, + t.Errorf("res: %s", cmp.Diff(tc.exp, res)) } + }) } } @@ -447,6 +451,7 @@ func TestFormatServiceChanges(t *testing.T) { } for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { s := &Sync{ config: &config.Config{ DryRun: tc.dryRun, @@ -455,9 +460,10 @@ func TestFormatServiceChanges(t *testing.T) { res := s.formatServiceChanges(tc.changes) if tc.exp != res { - t.Errorf("%s: res: %s", tc.name, + t.Errorf("res: %s", cmp.Diff(tc.exp, res)) } + }) } } @@ -657,6 +663,7 @@ func TestFormatCommandChanges(t *testing.T) { } for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { s := &Sync{ config: &config.Config{ DryRun: tc.dryRun, @@ -666,8 +673,9 @@ func TestFormatCommandChanges(t *testing.T) { res := s.formatCommandChanges(tc.changes) if tc.exp != res { - t.Errorf("%s: res: %s", tc.name, + t.Errorf("res: %s", cmp.Diff(tc.exp, res)) } + }) } } diff --git a/cmd/safcm/sync_info_test.go b/cmd/safcm/sync_info_test.go index b883bb7..f234d9b 100644 --- a/cmd/safcm/sync_info_test.go +++ b/cmd/safcm/sync_info_test.go @@ -68,10 +68,12 @@ 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("%s: res: %s", tc.name, + t.Errorf("res: %s", cmp.Diff(tc.exp, res)) } + }) } } diff --git a/cmd/safcm/sync_sync_test.go b/cmd/safcm/sync_sync_test.go index f6aafd5..65a490a 100644 --- a/cmd/safcm/sync_sync_test.go +++ b/cmd/safcm/sync_sync_test.go @@ -419,6 +419,7 @@ func TestHostSyncReq(t *testing.T) { } for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { err = os.Chdir(filepath.Join(cwd, "testdata", tc.project)) if err != nil { t.Fatal(err) @@ -469,21 +470,21 @@ func TestHostSyncReq(t *testing.T) { res, err := s.hostSyncReq(tc.detected) if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("%s: res: %s", tc.name, + 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("%s: err = %#v, want %#v", - tc.name, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } close(ch) <-done if !reflect.DeepEqual(tc.expEvents, events) { - t.Errorf("%s: events: %s", tc.name, + t.Errorf("events: %s", cmp.Diff(tc.expEvents, events)) } - + }) } } diff --git a/cmd/safcm/sync_test.go b/cmd/safcm/sync_test.go index dba1d7e..753f942 100644 --- a/cmd/safcm/sync_test.go +++ b/cmd/safcm/sync_test.go @@ -166,15 +166,17 @@ 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("%s: res: %s", tc.name, + 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("%s: err = %#v, want %#v", - tc.name, err, tc.expErr) + t.Errorf("err = %#v, want %#v", + err, tc.expErr) } + }) } } diff --git a/cmd/safcm/term_test.go b/cmd/safcm/term_test.go index 63ad3c9..f433eb5 100644 --- a/cmd/safcm/term_test.go +++ b/cmd/safcm/term_test.go @@ -82,10 +82,12 @@ 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("%s: res: %s", tc.name, + t.Errorf("res: %s", cmp.Diff(tc.exp, res)) } + }) } }