From: Simon Ruderich Date: Mon, 5 Apr 2021 08:44:52 +0000 (+0200) Subject: tests: go fmt and rewrap X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=4299c5d0946ecfa034e9eefe6b16e50bab94a820;p=safcm%2Fsafcm.git tests: go fmt and rewrap --- diff --git a/cmd/safcm-remote/ainsl/ainsl_test.go b/cmd/safcm-remote/ainsl/ainsl_test.go index e4d6b46..564bc37 100644 --- a/cmd/safcm-remote/ainsl/ainsl_test.go +++ b/cmd/safcm-remote/ainsl/ainsl_test.go @@ -294,40 +294,40 @@ 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) - if err != nil { - t.Fatal(err) - } - err = os.Chdir(path) - if err != nil { - t.Fatal(err) - } + // Create separate test directory for each test case + path := filepath.Join(cwd, "testdata", tc.name) + err = os.Mkdir(path, 0700) + if err != nil { + t.Fatal(err) + } + err = os.Chdir(path) + if err != nil { + t.Fatal(err) + } - if tc.prepare != nil { - tc.prepare() - } + if tc.prepare != nil { + tc.prepare() + } - 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) - } + 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) + } - 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)) - } + 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)) + } }) } diff --git a/cmd/safcm-remote/sync/commands_test.go b/cmd/safcm-remote/sync/commands_test.go index eb0cc0b..3c47c17 100644 --- a/cmd/safcm-remote/sync/commands_test.go +++ b/cmd/safcm-remote/sync/commands_test.go @@ -498,31 +498,31 @@ 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, - expCmds: tc.expCmds, - resStdout: tc.stdout, - resStderr: tc.stderr, - resError: tc.errors, - }) - s.triggers = tc.triggers + s, res := prepareSync(tc.req, &testRunner{ + t: t, + expCmds: tc.expCmds, + resStdout: tc.stdout, + resStderr: tc.stderr, + resError: tc.errors, + }) + 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) - } - dbg := res.Wait() + 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) + } + 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)) - } + 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)) + } }) } } diff --git a/cmd/safcm-remote/sync/files_test.go b/cmd/safcm-remote/sync/files_test.go index 32130e9..e57401e 100644 --- a/cmd/safcm-remote/sync/files_test.go +++ b/cmd/safcm-remote/sync/files_test.go @@ -842,59 +842,59 @@ 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) - if err != nil { - t.Fatal(err) - } - err = os.Chdir(path) - if err != nil { - t.Fatal(err) - } - - if tc.prepare != nil { - tc.prepare() - } - - s, res := prepareSync(tc.req, &testRunner{ - t: 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) - } - 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)) - } - - 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)) - } - - 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)) - } + // Create separate test directory for each test case + path := filepath.Join(cwd, "testdata", "files-"+tc.name) + err = os.Mkdir(path, 0700) + if err != nil { + t.Fatal(err) + } + err = os.Chdir(path) + if err != nil { + t.Fatal(err) + } + + if tc.prepare != nil { + tc.prepare() + } + + s, res := prepareSync(tc.req, &testRunner{ + t: 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) + } + 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)) + } + + 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)) + } + + 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)) + } }) } @@ -2397,63 +2397,63 @@ 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) - if err != nil { - t.Fatal(err) - } - err = os.Chdir(path) - if err != nil { - t.Fatal(err) - } - - if tc.prepare != nil { - tc.prepare() - } - - s, res := prepareSync(tc.req, &testRunner{ - t: t, - }) - s.setDefaults() - - // Deterministic temporary symlink names - rand.Seed(0) - - 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) - } - 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)) - } - - 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)) - } - - 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)) - } + // Create separate test directory for each test case + path := filepath.Join(cwd, "testdata", "file-"+tc.name) + err = os.Mkdir(path, 0700) + if err != nil { + t.Fatal(err) + } + err = os.Chdir(path) + if err != nil { + t.Fatal(err) + } + + if tc.prepare != nil { + tc.prepare() + } + + s, res := prepareSync(tc.req, &testRunner{ + t: t, + }) + s.setDefaults() + + // Deterministic temporary symlink names + rand.Seed(0) + + 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) + } + 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)) + } + + 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)) + } + + 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)) + } }) } diff --git a/cmd/safcm-remote/sync/packages_debian_test.go b/cmd/safcm-remote/sync/packages_debian_test.go index 51b6b74..ff0e066 100644 --- a/cmd/safcm-remote/sync/packages_debian_test.go +++ b/cmd/safcm-remote/sync/packages_debian_test.go @@ -301,30 +301,30 @@ 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, - expCmds: tc.expCmds, - resStdout: tc.stdout, - resStderr: tc.stderr, - resError: tc.errors, - }) + s, res := prepareSync(tc.req, &testRunner{ + t: t, + expCmds: tc.expCmds, + resStdout: tc.stdout, + resStderr: tc.stderr, + resError: tc.errors, + }) - 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) - } - dbg := res.Wait() + 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) + } + 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)) - } + 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)) + } }) } } diff --git a/cmd/safcm-remote/sync/services_systemd_test.go b/cmd/safcm-remote/sync/services_systemd_test.go index df226fc..40dd2e0 100644 --- a/cmd/safcm-remote/sync/services_systemd_test.go +++ b/cmd/safcm-remote/sync/services_systemd_test.go @@ -508,30 +508,30 @@ LoadError= for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - s, res := prepareSync(tc.req, &testRunner{ - t: t, - expCmds: tc.expCmds, - resStdout: tc.stdout, - resStderr: tc.stderr, - resError: tc.errors, - }) - - 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) - } - 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)) - } + s, res := prepareSync(tc.req, &testRunner{ + t: t, + expCmds: tc.expCmds, + resStdout: tc.stdout, + resStderr: tc.stderr, + resError: tc.errors, + }) + + 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) + } + 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)) + } }) } } diff --git a/cmd/safcm-remote/sync/triggers_test.go b/cmd/safcm-remote/sync/triggers_test.go index cc41886..0b8bbe8 100644 --- a/cmd/safcm-remote/sync/triggers_test.go +++ b/cmd/safcm-remote/sync/triggers_test.go @@ -56,11 +56,10 @@ 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)) - } + res := triggerPaths(tc.path) + if !reflect.DeepEqual(tc.exp, res) { + 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 ef31591..c9af0f2 100644 --- a/cmd/safcm/config/files_test.go +++ b/cmd/safcm/config/files_test.go @@ -189,17 +189,16 @@ 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) - } + 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) + } }) } } diff --git a/cmd/safcm/config/groups_test.go b/cmd/safcm/config/groups_test.go index 68f18ba..b2bf5ec 100644 --- a/cmd/safcm/config/groups_test.go +++ b/cmd/safcm/config/groups_test.go @@ -186,27 +186,26 @@ 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) - } + err := os.Chdir(tc.path) + if err != nil { + t.Fatal(err) + } - res, err := LoadGroups(tc.cfg, tc.hosts) + 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) - } + 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) + } - err = os.Chdir(cwd) - if err != nil { - t.Fatal(err) - } + err = os.Chdir(cwd) + if err != nil { + t.Fatal(err) + } }) } } @@ -318,16 +317,16 @@ 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("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) - } + 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) + } }) } } diff --git a/cmd/safcm/config/hosts_test.go b/cmd/safcm/config/hosts_test.go index 36a8c37..95f4e75 100644 --- a/cmd/safcm/config/hosts_test.go +++ b/cmd/safcm/config/hosts_test.go @@ -84,22 +84,21 @@ 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) - } + err := os.Chdir(filepath.Join(cwd, tc.path)) + if err != nil { + t.Fatal(err) + } - res, err := LoadHosts() + 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) - } + 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) + } }) } } diff --git a/cmd/safcm/config/permissions_test.go b/cmd/safcm/config/permissions_test.go index 4efefaf..ba1d505 100644 --- a/cmd/safcm/config/permissions_test.go +++ b/cmd/safcm/config/permissions_test.go @@ -227,23 +227,22 @@ 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("err = %#v, want nil", err) - } - err = LoadPermissions(tc.group, files) + // 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("err = %#v, want nil", err) + } + 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) - } + 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) + } }) } } diff --git a/cmd/safcm/config/templates_test.go b/cmd/safcm/config/templates_test.go index bd65c09..8097af7 100644 --- a/cmd/safcm/config/templates_test.go +++ b/cmd/safcm/config/templates_test.go @@ -243,24 +243,23 @@ 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("err = %#v, want nil", err) - } - err = LoadTemplates(tc.group, files, - host, groups, allHosts, allGroups) + // 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("err = %#v, want nil", err) + } + 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) - } + 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) + } }) } } diff --git a/cmd/safcm/config/triggers_test.go b/cmd/safcm/config/triggers_test.go index 96e52d7..7de0322 100644 --- a/cmd/safcm/config/triggers_test.go +++ b/cmd/safcm/config/triggers_test.go @@ -134,23 +134,22 @@ 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("err = %#v, want nil", err) - } - err = LoadTriggers(tc.group, files) + // 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("err = %#v, want nil", err) + } + 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) - } + 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) + } }) } } diff --git a/cmd/safcm/sync_changes_test.go b/cmd/safcm/sync_changes_test.go index 767564f..dda4873 100644 --- a/cmd/safcm/sync_changes_test.go +++ b/cmd/safcm/sync_changes_test.go @@ -284,17 +284,16 @@ 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, - }, - } + s := &Sync{ + config: &config.Config{ + DryRun: tc.dryRun, + }, + } - res := s.formatFileChanges(tc.changes) - if tc.exp != res { - t.Errorf("res: %s", - cmp.Diff(tc.exp, res)) - } + res := s.formatFileChanges(tc.changes) + if tc.exp != res { + t.Errorf("res: %s", cmp.Diff(tc.exp, res)) + } }) } } @@ -357,17 +356,16 @@ 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, - }, - } + s := &Sync{ + config: &config.Config{ + DryRun: tc.dryRun, + }, + } - res := s.formatPackageChanges(tc.changes) - if tc.exp != res { - t.Errorf("res: %s", - cmp.Diff(tc.exp, res)) - } + res := s.formatPackageChanges(tc.changes) + if tc.exp != res { + t.Errorf("res: %s", cmp.Diff(tc.exp, res)) + } }) } } @@ -452,17 +450,16 @@ 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, - }, - } + s := &Sync{ + config: &config.Config{ + DryRun: tc.dryRun, + }, + } - res := s.formatServiceChanges(tc.changes) - if tc.exp != res { - t.Errorf("res: %s", - cmp.Diff(tc.exp, res)) - } + res := s.formatServiceChanges(tc.changes) + if tc.exp != res { + t.Errorf("res: %s", cmp.Diff(tc.exp, res)) + } }) } } @@ -664,18 +661,17 @@ 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, - Quiet: tc.quiet, - }, - } + s := &Sync{ + config: &config.Config{ + DryRun: tc.dryRun, + Quiet: tc.quiet, + }, + } - res := s.formatCommandChanges(tc.changes) - if tc.exp != res { - t.Errorf("res: %s", - cmp.Diff(tc.exp, res)) - } + res := s.formatCommandChanges(tc.changes) + if tc.exp != res { + 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 f234d9b..e01083b 100644 --- a/cmd/safcm/sync_info_test.go +++ b/cmd/safcm/sync_info_test.go @@ -69,11 +69,10 @@ 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)) - } + res := hostInfoRespToGroups(tc.resp) + if !reflect.DeepEqual(tc.exp, res) { + 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 65a490a..f3d046a 100644 --- a/cmd/safcm/sync_sync_test.go +++ b/cmd/safcm/sync_sync_test.go @@ -420,71 +420,71 @@ 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) - } + err = os.Chdir(filepath.Join(cwd, + "testdata", tc.project)) + if err != nil { + t.Fatal(err) + } - // `safcm fixperms` in case user has strict umask - log.SetOutput(io.Discard) - err := MainFixperms() - if err != nil { - t.Fatal(err) - } - log.SetOutput(os.Stderr) + // `safcm fixperms` in case user has strict umask + log.SetOutput(io.Discard) + err := MainFixperms() + if err != nil { + t.Fatal(err) + } + log.SetOutput(os.Stderr) - cfg, allHosts, allGroups, err := LoadBaseFiles() - if err != nil { - t.Fatal(err) - } - cfg.LogLevel = tc.level + cfg, allHosts, allGroups, err := LoadBaseFiles() + if err != nil { + t.Fatal(err) + } + cfg.LogLevel = tc.level - var events []string - ch := make(chan Event) - done := make(chan struct{}) - go func() { - for { - x, ok := <-ch - if !ok { - break + var events []string + ch := make(chan Event) + done := make(chan struct{}) + go func() { + for { + x, ok := <-ch + if !ok { + break + } + if x.ConnEvent.Type != 0 { + panic("unexpected ConnEvent") + } + events = append(events, + fmt.Sprintf("%s: %v %d %s", + x.Host.Name, + x.Error, x.Log.Level, + x.Log.Text)) } - if x.ConnEvent.Type != 0 { - panic("unexpected ConnEvent") - } - events = append(events, - fmt.Sprintf("%s: %v %d %s", - x.Host.Name, - x.Error, x.Log.Level, - x.Log.Text)) - } - done <- struct{}{} - }() + done <- struct{}{} + }() - s := &Sync{ - host: allHosts.Map[tc.host], - config: cfg, - allHosts: allHosts, - allGroups: allGroups, - events: ch, - } + s := &Sync{ + host: allHosts.Map[tc.host], + config: cfg, + allHosts: allHosts, + allGroups: allGroups, + events: ch, + } - 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) - } + 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) + } - close(ch) - <-done - if !reflect.DeepEqual(tc.expEvents, events) { - t.Errorf("events: %s", - cmp.Diff(tc.expEvents, events)) - } + close(ch) + <-done + if !reflect.DeepEqual(tc.expEvents, events) { + t.Errorf("events: %s", + cmp.Diff(tc.expEvents, events)) + } }) } } diff --git a/cmd/safcm/sync_test.go b/cmd/safcm/sync_test.go index 753f942..f904711 100644 --- a/cmd/safcm/sync_test.go +++ b/cmd/safcm/sync_test.go @@ -167,16 +167,15 @@ 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) - } + 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) + } }) } } diff --git a/cmd/safcm/term_test.go b/cmd/safcm/term_test.go index f433eb5..e5faf17 100644 --- a/cmd/safcm/term_test.go +++ b/cmd/safcm/term_test.go @@ -83,11 +83,10 @@ 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)) - } + res := EscapeControlCharacters(tc.isTTY, tc.x) + if tc.exp != res { + t.Errorf("res: %s", cmp.Diff(tc.exp, res)) + } }) } }