summaryrefslogtreecommitdiff
path: root/cmd/krino/history_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/krino/history_test.go')
-rw-r--r--cmd/krino/history_test.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/cmd/krino/history_test.go b/cmd/krino/history_test.go
index 2667b63..23b1e5f 100644
--- a/cmd/krino/history_test.go
+++ b/cmd/krino/history_test.go
@@ -315,3 +315,66 @@ func TestReviewUndoInvalidKeyReprompts(t *testing.T) {
t.Errorf("no mention of the rejected key:\n%s", out)
}
}
+
+// TestGlobalDryRunBeforeUndo: -n written before the subcommand, the way
+// krino.1 teaches flags, is a dry run of the undo. It shows the plan,
+// exits 0, and moves nothing back.
+func TestGlobalDryRunBeforeUndo(t *testing.T) {
+ h := matchingFixture(t)
+ if code, _, errOut := runCLI(t, "-y"); code != 0 {
+ t.Fatalf("apply: %d %s", code, errOut)
+ }
+ filed := filepath.Join(h, "dl", "Work", "Acme", "inv1.txt")
+
+ code, out, errOut := runCLI(t, "-n", "undo")
+ if code != 0 || !strings.Contains(out, "undo-move") {
+ t.Errorf("-n undo: %d %q\n%s", code, errOut, out)
+ }
+ if _, err := os.Stat(filed); err != nil {
+ t.Errorf("-n undo moved a file: %v", err)
+ }
+ if _, out, _ = runCLI(t, "log"); strings.Contains(out, "undone") {
+ t.Errorf("-n undo marked the run undone:\n%s", out)
+ }
+}
+
+// TestGlobalDryRunBeforeUndoConflictsWithYes: -n before the subcommand and
+// -y after it is the same conflict as both after it: exit 2, nothing
+// changed.
+func TestGlobalDryRunBeforeUndoConflictsWithYes(t *testing.T) {
+ h := matchingFixture(t)
+ if code, _, errOut := runCLI(t, "-y"); code != 0 {
+ t.Fatalf("apply: %d %s", code, errOut)
+ }
+ filed := filepath.Join(h, "dl", "Work", "Acme", "inv1.txt")
+
+ code, _, errOut := runCLI(t, "-n", "undo", "-y")
+ if code != 2 || !strings.Contains(errOut, "-y and -n cannot be used together") {
+ t.Errorf("-n undo -y: %d %q", code, errOut)
+ }
+ if _, err := os.Stat(filed); err != nil {
+ t.Errorf("-n undo -y moved a file: %v", err)
+ }
+ if _, out, _ := runCLI(t, "log"); strings.Contains(out, "undone") {
+ t.Errorf("-n undo -y marked the run undone:\n%s", out)
+ }
+}
+
+// TestGlobalYesBeforeUndo: -y before the subcommand applies the undo.
+func TestGlobalYesBeforeUndo(t *testing.T) {
+ h := matchingFixture(t)
+ if code, _, errOut := runCLI(t, "-y"); code != 0 {
+ t.Fatalf("apply: %d %s", code, errOut)
+ }
+ filed := filepath.Join(h, "dl", "Work", "Acme", "inv1.txt")
+
+ if code, _, errOut := runCLI(t, "-y", "undo"); code != 0 {
+ t.Fatalf("-y undo: %d %s", code, errOut)
+ }
+ if _, err := os.Stat(filepath.Join(h, "dl", "inv1.txt")); err != nil {
+ t.Errorf("-y undo did not put the file back: %v", err)
+ }
+ if _, err := os.Stat(filed); !os.IsNotExist(err) {
+ t.Error("the filed copy survived -y undo")
+ }
+}