]> ruderich.org/simon Gitweb - safcm/safcm.git/blobdiff - cmd/safcm/sync_changes_test.go
changes: add "(hidden)" to commands with no output and quiet mode
[safcm/safcm.git] / cmd / safcm / sync_changes_test.go
index c6dd0f8eba432803ed4812c09c4891e07bd2e653..219373c9522e4edfba458d3dd410b306c7ccdb06 100644 (file)
@@ -24,6 +24,153 @@ import (
        "ruderich.org/simon/safcm/testutil"
 )
 
+func TestFormatChanges(t *testing.T) {
+       tests := []struct {
+               name   string
+               dryRun bool
+               quiet  bool
+               isTTY  bool
+               resp   safcm.MsgSyncResp
+               exp    string
+       }{
+
+               // Just a few basic tests and border cases; see the other
+               // tests for more detailed tests of each format function
+
+               {
+                       "no changes",
+                       false,
+                       false,
+                       false,
+                       safcm.MsgSyncResp{},
+                       "no changes",
+               },
+
+               {
+                       "changes",
+                       false,
+                       false,
+                       false,
+                       safcm.MsgSyncResp{
+                               FileChanges: []safcm.FileChange{
+                                       {
+                                               Path:    "created",
+                                               Created: true,
+                                               New: safcm.FileChangeInfo{
+                                                       Mode:  0644,
+                                                       User:  "user",
+                                                       Uid:   1000,
+                                                       Group: "group",
+                                                       Gid:   2000,
+                                               },
+                                       },
+                               },
+                               PackageChanges: []safcm.PackageChange{
+                                       {
+                                               Name: "package-one",
+                                       },
+                                       {
+                                               Name: "package-two",
+                                       },
+                               },
+                               ServiceChanges: []safcm.ServiceChange{
+                                       {
+                                               Name:    "service-one",
+                                               Started: true,
+                                       },
+                                       {
+                                               Name:    "service-two",
+                                               Enabled: true,
+                                       },
+                                       {
+                                               Name:    "service-three",
+                                               Started: true,
+                                               Enabled: true,
+                                       },
+                               },
+                               CommandChanges: []safcm.CommandChange{
+                                       {
+                                               Command: "fake command",
+                                               Output:  "fake output",
+                                       },
+                                       {
+                                               Command: "fake command with no output",
+                                       },
+                               },
+                       },
+                       "\nchanged 1 file(s):\n\"created\": created, file, user(1000) group(2000), 0644\n\ninstalled 2 package(s):\n\"package-one\"\n\"package-two\"\n\nmodified 3 service(s):\n\"service-one\": started\n\"service-two\": enabled\n\"service-three\": started, enabled\n\nexecuted 2 command(s):\n\"fake command\":\n   > fake output\n   > \\ No newline at end of file\n\"fake command with no output\"\n",
+               },
+
+               {
+                       "command changes only, dry-run",
+                       true,
+                       false,
+                       false,
+                       safcm.MsgSyncResp{
+                               CommandChanges: []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",
+                                       },
+                               },
+                       },
+                       "\nwill execute 5 command(s): (dry-run)\n\"fake command\"\n\"fake command with no output\"\n\"fake command with newline\"\n\"fake command with more output\"\n\"fake failed command\"\n",
+               },
+               {
+                       "command changes only, quiet & dry-run",
+                       true,
+                       true,
+                       false,
+                       safcm.MsgSyncResp{
+                               CommandChanges: []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",
+                                       },
+                               },
+                       },
+                       "will execute 5 command(s) (dry-run)\n",
+               },
+       }
+
+       for _, tc := range tests {
+               t.Run(tc.name, func(t *testing.T) {
+                       s := &Sync{
+                               config: &config.Config{
+                                       DryRun: tc.dryRun,
+                                       Quiet:  tc.quiet,
+                               },
+                               isTTY: tc.isTTY,
+                       }
+
+                       res := s.formatChanges(tc.resp)
+                       testutil.AssertEqual(t, "res", res, tc.exp)
+               })
+       }
+}
+
 func TestFormatFileChanges(t *testing.T) {
        tests := []struct {
                name    string
@@ -396,7 +543,7 @@ func TestFormatFileChanges(t *testing.T) {
                                        },
                                },
                        },
-                       `changed 1 file(s): (dry-run)
+                       `will change 1 file(s): (dry-run)
 "file": created, file, user(1000) group(2000), 0644
 `,
                },
@@ -418,7 +565,7 @@ func TestFormatFileChanges(t *testing.T) {
                                        },
                                },
                        },
-                       "changed 1 file(s): (dry-run)\n\x1B[36m\"file\"\x1B[0m: \x1B[32mcreated\x1B[0m, file, user(1000) group(2000), 0644\n",
+                       "will change 1 file(s): (dry-run)\n\x1B[36m\"file\"\x1B[0m: \x1B[32mcreated\x1B[0m, file, user(1000) group(2000), 0644\n",
                },
 
                {
@@ -576,7 +723,7 @@ func TestFormatPackageChanges(t *testing.T) {
                                        Name: "package-two",
                                },
                        },
-                       `installed 2 package(s): (dry-run)
+                       `will install 2 package(s): (dry-run)
 "package-one"
 "package-two"
 `,
@@ -594,7 +741,7 @@ func TestFormatPackageChanges(t *testing.T) {
                                        Name: "package-two",
                                },
                        },
-                       "installed 2 package(s): (dry-run)\n\x1b[36m\"package-one\"\x1b[0m\n\x1b[36m\"package-two\"\x1b[0m\n",
+                       "will install 2 package(s): (dry-run)\n\x1b[36m\"package-one\"\x1b[0m\n\x1b[36m\"package-two\"\x1b[0m\n",
                },
 
                {
@@ -715,7 +862,7 @@ func TestFormatServiceChanges(t *testing.T) {
                                        Enabled: true,
                                },
                        },
-                       `modified 3 service(s): (dry-run)
+                       `will modify 3 service(s): (dry-run)
 "service-one": started
 "service-two": enabled
 "service-three": started, enabled
@@ -741,7 +888,7 @@ func TestFormatServiceChanges(t *testing.T) {
                                        Enabled: true,
                                },
                        },
-                       "modified 3 service(s): (dry-run)\n\x1b[36m\"service-one\"\x1b[0m: started\n\x1b[36m\"service-two\"\x1b[0m: enabled\n\x1b[36m\"service-three\"\x1b[0m: started, enabled\n",
+                       "will modify 3 service(s): (dry-run)\n\x1b[36m\"service-one\"\x1b[0m: started\n\x1b[36m\"service-two\"\x1b[0m: enabled\n\x1b[36m\"service-three\"\x1b[0m: started, enabled\n",
                },
 
                {
@@ -889,13 +1036,10 @@ func TestFormatCommandChanges(t *testing.T) {
                        []safcm.CommandChange{
                                {
                                        Command: "fake command",
-                                       Output:  "fake output",
                                },
                        },
-                       `executed 1 command(s): (dry-run)
-"fake command":
-   > fake output
-   > \ No newline at end of file
+                       `will execute 1 command(s): (dry-run)
+"fake command"
 `,
                },
 
@@ -907,10 +1051,9 @@ func TestFormatCommandChanges(t *testing.T) {
                        []safcm.CommandChange{
                                {
                                        Command: "fake command",
-                                       Output:  "fake output",
                                },
                        },
-                       "executed 1 command(s): (dry-run)\n\x1b[36m\"fake command\"\x1b[0m:\n   > fake output\n   > \\ No newline at end of file\n",
+                       "will execute 1 command(s): (dry-run)\n\x1b[36m\"fake command\"\x1b[0m\n",
                },
 
                {
@@ -940,7 +1083,7 @@ func TestFormatCommandChanges(t *testing.T) {
                                        Error:   "fake error",
                                },
                        },
-                       `executed 5 command(s), 1 with no output:
+                       `executed 5 command(s), 1 with no output (hidden):
 "fake command":
    > fake output
    > \ No newline at end of file
@@ -983,7 +1126,7 @@ func TestFormatCommandChanges(t *testing.T) {
                                        Error:   "fake error",
                                },
                        },
-                       "executed 5 command(s), 1 with no output:\n\x1b[36m\"fake command\"\x1b[0m:\n   > fake output\n   > \\ No newline at end of file\n\x1b[36m\"fake command with newline\"\x1b[0m:\n   > fake output\n\x1b[36m\"fake command with more output\"\x1b[0m:\n   > fake out\n   > fake put\n   > fake\n\x1b[36m\"fake failed command\"\x1b[0m, failed: \"fake error\":\n   > fake output\n   > \\ No newline at end of file\n",
+                       "executed 5 command(s), 1 with no output (hidden):\n\x1b[36m\"fake command\"\x1b[0m:\n   > fake output\n   > \\ No newline at end of file\n\x1b[36m\"fake command with newline\"\x1b[0m:\n   > fake output\n\x1b[36m\"fake command with more output\"\x1b[0m:\n   > fake out\n   > fake put\n   > fake\n\x1b[36m\"fake failed command\"\x1b[0m, failed: \"fake error\":\n   > fake output\n   > \\ No newline at end of file\n",
                },
 
                {
@@ -999,7 +1142,7 @@ func TestFormatCommandChanges(t *testing.T) {
                                        Command: "fake command with no output",
                                },
                        },
-                       `executed 2 command(s), 2 with no output
+                       `executed 2 command(s), 2 with no output (hidden)
 `,
                },
 
@@ -1017,7 +1160,7 @@ func TestFormatCommandChanges(t *testing.T) {
                                        Command: "fake command with no output",
                                },
                        },
-                       `executed 2 command(s), 1 with no output:
+                       `executed 2 command(s), 1 with no output (hidden):
 "fake command with no output but error", failed: "fake error"
 `,
                },
@@ -1044,12 +1187,7 @@ func TestFormatCommandChanges(t *testing.T) {
                                        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"
+                       `will execute 5 command(s) (dry-run)
 `,
                },