]> ruderich.org/simon Gitweb - safcm/safcm.git/blobdiff - cmd/safcm/sync_changes_test.go
tests: add and use testutil package to reduce duplication
[safcm/safcm.git] / cmd / safcm / sync_changes_test.go
index 76a016834b87d8a09d68547bcea26720511b13a4..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) {
@@ -283,17 +282,16 @@ func TestFormatFileChanges(t *testing.T) {
        }
 
        for _, tc := range tests {
-               s := &Sync{
-                       config: &config.Config{
-                               DryRun: tc.dryRun,
-                       },
-               }
+               t.Run(tc.name, func(t *testing.T) {
+                       s := &Sync{
+                               config: &config.Config{
+                                       DryRun: tc.dryRun,
+                               },
+                       }
 
-               res := s.formatFileChanges(tc.changes)
-               if tc.exp != res {
-                       t.Errorf("%s: res: %s", tc.name,
-                               cmp.Diff(tc.exp, res))
-               }
+                       res := s.formatFileChanges(tc.changes)
+                       testutil.AssertEqual(t, "res", res, tc.exp)
+               })
        }
 }
 
@@ -354,17 +352,16 @@ func TestFormatPackageChanges(t *testing.T) {
        }
 
        for _, tc := range tests {
-               s := &Sync{
-                       config: &config.Config{
-                               DryRun: tc.dryRun,
-                       },
-               }
+               t.Run(tc.name, func(t *testing.T) {
+                       s := &Sync{
+                               config: &config.Config{
+                                       DryRun: tc.dryRun,
+                               },
+                       }
 
-               res := s.formatPackageChanges(tc.changes)
-               if tc.exp != res {
-                       t.Errorf("%s: res: %s", tc.name,
-                               cmp.Diff(tc.exp, res))
-               }
+                       res := s.formatPackageChanges(tc.changes)
+                       testutil.AssertEqual(t, "res", res, tc.exp)
+               })
        }
 }
 
@@ -447,17 +444,16 @@ func TestFormatServiceChanges(t *testing.T) {
        }
 
        for _, tc := range tests {
-               s := &Sync{
-                       config: &config.Config{
-                               DryRun: tc.dryRun,
-                       },
-               }
+               t.Run(tc.name, func(t *testing.T) {
+                       s := &Sync{
+                               config: &config.Config{
+                                       DryRun: tc.dryRun,
+                               },
+                       }
 
-               res := s.formatServiceChanges(tc.changes)
-               if tc.exp != res {
-                       t.Errorf("%s: res: %s", tc.name,
-                               cmp.Diff(tc.exp, res))
-               }
+                       res := s.formatServiceChanges(tc.changes)
+                       testutil.AssertEqual(t, "res", res, tc.exp)
+               })
        }
 }
 
@@ -465,6 +461,7 @@ func TestFormatCommandChanges(t *testing.T) {
        tests := []struct {
                name    string
                dryRun  bool
+               quiet   bool
                changes []safcm.CommandChange
                exp     string
        }{
@@ -472,6 +469,7 @@ func TestFormatCommandChanges(t *testing.T) {
                {
                        "regular",
                        false,
+                       false,
                        []safcm.CommandChange{
                                {
                                        Command: "fake command",
@@ -514,6 +512,7 @@ func TestFormatCommandChanges(t *testing.T) {
                {
                        "dry-run",
                        true,
+                       false,
                        []safcm.CommandChange{
                                {
                                        Command: "fake command",
@@ -527,9 +526,116 @@ func TestFormatCommandChanges(t *testing.T) {
 `,
                },
 
+               {
+                       "quiet",
+                       false,
+                       true,
+                       []safcm.CommandChange{
+                               {
+                                       Command: "fake command",
+                                       Output:  "fake output",
+                               },
+                               {
+                                       Command: "fake command with no output",
+                               },
+                               {
+                                       Command: "fake command with newline",
+                                       Output:  "fake output\n",
+                               },
+                               {
+                                       Command: "fake command with more output",
+                                       Output:  "fake out\nfake put\nfake\n",
+                               },
+                               {
+                                       Command: "fake failed command",
+                                       Output:  "fake output",
+                                       Error:   "fake error",
+                               },
+                       },
+                       `executed 5 command(s), 1 with no output:
+"fake command":
+   > fake output
+   > \ No newline at end of file
+"fake command with newline":
+   > fake output
+"fake command with more output":
+   > fake out
+   > fake put
+   > fake
+"fake failed command", failed: "fake error":
+   > fake output
+   > \ No newline at end of file
+`,
+               },
+
+               {
+                       "quiet (only quiet commands)",
+                       false,
+                       true,
+                       []safcm.CommandChange{
+                               {
+                                       Command: "fake command with no output",
+                               },
+                               {
+                                       Command: "fake command with no output",
+                               },
+                       },
+                       `executed 2 command(s), 2 with no output
+`,
+               },
+
+               {
+                       "quiet (quiet with errors)",
+                       false,
+                       true,
+                       []safcm.CommandChange{
+                               {
+                                       Command: "fake command with no output but error",
+                                       Error:   "fake error",
+                               },
+                               {
+                                       Command: "fake command with no output",
+                               },
+                       },
+                       `executed 2 command(s), 1 with no output:
+"fake command with no output but error", failed: "fake error"
+`,
+               },
+
+               {
+                       "quiet & dry-run",
+                       true,
+                       true,
+                       []safcm.CommandChange{
+                               {
+                                       Command: "fake command",
+                               },
+                               {
+                                       Command: "fake command with no output",
+                               },
+                               {
+                                       Command: "fake command with newline",
+                               },
+                               {
+                                       Command: "fake command with more output",
+                               },
+                               {
+                                       Command: "fake failed command",
+                               },
+                       },
+                       `executed 5 command(s): (dry-run)
+"fake command"
+"fake command with no output"
+"fake command with newline"
+"fake command with more output"
+"fake failed command"
+`,
+               },
+
                {
                        "escaping",
                        false,
+                       false,
                        []safcm.CommandChange{
                                {
                                        Command: "\x00",
@@ -547,16 +653,16 @@ func TestFormatCommandChanges(t *testing.T) {
        }
 
        for _, tc := range tests {
-               s := &Sync{
-                       config: &config.Config{
-                               DryRun: tc.dryRun,
-                       },
-               }
+               t.Run(tc.name, func(t *testing.T) {
+                       s := &Sync{
+                               config: &config.Config{
+                                       DryRun: tc.dryRun,
+                                       Quiet:  tc.quiet,
+                               },
+                       }
 
-               res := s.formatCommandChanges(tc.changes)
-               if tc.exp != res {
-                       t.Errorf("%s: res: %s", tc.name,
-                               cmp.Diff(tc.exp, res))
-               }
+                       res := s.formatCommandChanges(tc.changes)
+                       testutil.AssertEqual(t, "res", res, tc.exp)
+               })
        }
 }