X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=cmd%2Fsafcm%2Fsync_changes_test.go;h=6c7ba392aac7b36b01399eb7e1d8c6fba4af9b8c;hb=64a43bef32b144ce9894815a92f7083306f7ef1e;hp=c6dd0f8eba432803ed4812c09c4891e07bd2e653;hpb=81f2ac475e93815f079a28d9c61b4690ca8adef4;p=safcm%2Fsafcm.git diff --git a/cmd/safcm/sync_changes_test.go b/cmd/safcm/sync_changes_test.go index c6dd0f8..6c7ba39 100644 --- a/cmd/safcm/sync_changes_test.go +++ b/cmd/safcm/sync_changes_test.go @@ -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{}, + "", + }, + + { + "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", + }, + }, + }, + "\nexecuted 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", + }, + }, + }, + "executed 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 @@ -1044,12 +1191,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" + `executed 5 command(s) (dry-run) `, },