]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
changes: refactor into separate function and add basic tests
authorSimon Ruderich <simon@ruderich.org>
Sat, 10 Apr 2021 17:53:05 +0000 (19:53 +0200)
committerSimon Ruderich <simon@ruderich.org>
Sat, 10 Apr 2021 17:53:05 +0000 (19:53 +0200)
cmd/safcm/sync_changes.go
cmd/safcm/sync_changes_test.go
cmd/safcm/sync_sync.go

index 7ef0541fcb4809b17669c2acda6a1e0f8c357bc0..1ec3cb60b435632fedac3e95b975239d1f12160e 100644 (file)
@@ -30,6 +30,30 @@ import (
 // the remote helper is untrusted and must be either escaped with %q or by
 // calling EscapeControlCharacters().
 
+func (s *Sync) formatChanges(resp safcm.MsgSyncResp) string {
+       var changes []string
+       if len(resp.FileChanges) > 0 {
+               changes = append(changes,
+                       s.formatFileChanges(resp.FileChanges))
+       }
+       if len(resp.PackageChanges) > 0 {
+               changes = append(changes,
+                       s.formatPackageChanges(resp.PackageChanges))
+       }
+       if len(resp.ServiceChanges) > 0 {
+               changes = append(changes,
+                       s.formatServiceChanges(resp.ServiceChanges))
+       }
+       if len(resp.CommandChanges) > 0 {
+               changes = append(changes,
+                       s.formatCommandChanges(resp.CommandChanges))
+       }
+       if len(changes) == 0 {
+               return ""
+       }
+       return "\n" + strings.Join(changes, "\n")
+}
+
 func (s *Sync) formatFileChanges(changes []safcm.FileChange) string {
        var buf strings.Builder
        fmt.Fprintf(&buf, "changed %d file(s):", len(changes))
index c6dd0f8eba432803ed4812c09c4891e07bd2e653..9326dbbf3e6638120d9cdbcfc5237a5e241265e0 100644 (file)
@@ -24,6 +24,96 @@ import (
        "ruderich.org/simon/safcm/testutil"
 )
 
+func TestFormatChanges(t *testing.T) {
+       tests := []struct {
+               name   string
+               dryRun 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,
+                       safcm.MsgSyncResp{},
+                       "",
+               },
+
+               {
+                       "changes",
+                       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",
+               },
+       }
+
+       for _, tc := range tests {
+               t.Run(tc.name, func(t *testing.T) {
+                       s := &Sync{
+                               config: &config.Config{
+                                       DryRun: tc.dryRun,
+                               },
+                               isTTY: tc.isTTY,
+                       }
+
+                       res := s.formatChanges(tc.resp)
+                       testutil.AssertEqual(t, "res", res, tc.exp)
+               })
+       }
+}
+
 func TestFormatFileChanges(t *testing.T) {
        tests := []struct {
                name    string
index e84b7f416c07b84df027f017c826d8b381324d4a..f8aeea2517031c69f214f04d8b13268f1f90ab3c 100644 (file)
@@ -44,26 +44,9 @@ func (s *Sync) hostSync(conn *rpc.Conn, detectedGroups []string) error {
        }
 
        // Display changes
-       var changes []string
-       if len(resp.FileChanges) > 0 {
-               changes = append(changes,
-                       s.formatFileChanges(resp.FileChanges))
-       }
-       if len(resp.PackageChanges) > 0 {
-               changes = append(changes,
-                       s.formatPackageChanges(resp.PackageChanges))
-       }
-       if len(resp.ServiceChanges) > 0 {
-               changes = append(changes,
-                       s.formatServiceChanges(resp.ServiceChanges))
-       }
-       if len(resp.CommandChanges) > 0 {
-               changes = append(changes,
-                       s.formatCommandChanges(resp.CommandChanges))
-       }
-       if len(changes) > 0 {
-               s.logf(safcm.LogInfo, true, "%s",
-                       "\n"+strings.Join(changes, "\n"))
+       changes := s.formatChanges(resp)
+       if changes != "" {
+               s.logf(safcm.LogInfo, true, "%s", changes)
        }
 
        if resp.Error != "" {