Debian Codegraph @

cli/tests/test_undo_redo_commands.rs

raw
1 // Copyright 2022 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use crate::common::TestEnvironment;
16
17 #[test]
18 fn test_undo_root_operation() {
19 let test_env = TestEnvironment::default();
20 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
21 let work_dir = test_env.work_dir("repo");
22
23 let output = work_dir.run_jj(["undo"]);
24 insta::assert_snapshot!(output, @"
25 ------- stderr -------
26 Undid operation: f63ee16f9553 (2001-02-03 08:05:07) add workspace 'default'
27 Restored to operation: 000000000000 root()
28 Warning: The current workspace 'default' no longer exists after this operation. The working copy was left untouched.
29 Hint: Restore to an operation that contains the workspace (e.g. `jj undo` or `jj redo`).
30 [EOF]
31 ");
32
33 let output = work_dir.run_jj(["undo"]);
34 insta::assert_snapshot!(output, @"
35 ------- stderr -------
36 Error: Cannot undo root operation
37 [EOF]
38 [exit status: 1]
39 ");
40 }
41
42 #[test]
43 fn test_undo_merge_operation() {
44 let test_env = TestEnvironment::default();
45 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
46 let work_dir = test_env.work_dir("repo");
47
48 work_dir.run_jj(["new"]).success();
49 work_dir.run_jj(["new", "--at-op=@-"]).success();
50 let output = work_dir.run_jj(["undo"]);
51 insta::assert_snapshot!(output, @"
52 ------- stderr -------
53 Concurrent modification detected, resolving automatically.
54 Error: Cannot undo a merge operation
55 Hint: Consider using `jj op restore` instead
56 [EOF]
57 [exit status: 1]
58 ");
59 }
60
61 #[test]
62 fn test_undo_push_operation() {
63 let test_env = TestEnvironment::default();
64
65 test_env
66 .run_jj_in(".", ["git", "init", "--colocate", "origin"])
67 .success();
68 test_env
69 .run_jj_in(".", ["git", "clone", "origin", "repo"])
70 .success();
71 let work_dir = test_env.work_dir("repo");
72
73 work_dir.write_file("foo", "foo");
74 work_dir.run_jj(["commit", "-mfoo"]).success();
75 work_dir.run_jj(["git", "push", "-c@-"]).success();
76 let output = work_dir.run_jj(["undo"]);
77 insta::assert_snapshot!(output, @"
78 ------- stderr -------
79 Warning: Undoing a push operation often leads to conflicted bookmarks.
80 Hint: To avoid this, run `jj redo` now.
81 Undid operation: 841f09db81a1 (2001-02-03 08:05:10) push bookmark push-rlvkpnrzqnoo to git remote origin
82 Restored to operation: 27cf57bcf92a (2001-02-03 08:05:09) commit 3850397cf31988d0657948307ad5bbe873d76a38
83 [EOF]
84 ");
85 }
86
87 #[test]
88 fn test_jump_over_old_undo_stack() {
89 let test_env = TestEnvironment::default();
90 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
91 let work_dir = test_env.work_dir("repo");
92
93 // create a few normal operations
94 for state in 'A'..='D' {
95 work_dir.write_file("state", state.to_string());
96 work_dir.run_jj(["debug", "snapshot"]).success();
97 }
98 assert_eq!(work_dir.read_file("state"), "D");
99
100 // undo operations D and C, restoring the state of B
101 work_dir.run_jj(["undo"]).success();
102 assert_eq!(work_dir.read_file("state"), "C");
103 work_dir.run_jj(["undo"]).success();
104 assert_eq!(work_dir.read_file("state"), "B");
105
106 // create operations E and F
107 work_dir.write_file("state", "E");
108 work_dir.run_jj(["debug", "snapshot"]).success();
109 work_dir.write_file("state", "F");
110 work_dir.run_jj(["debug", "snapshot"]).success();
111 assert_eq!(work_dir.read_file("state"), "F");
112
113 // undo operations F, E and B, restoring the state of A while skipping the
114 // undo-stack of C and D in the op log
115 work_dir.run_jj(["undo"]).success();
116 assert_eq!(work_dir.read_file("state"), "E");
117 work_dir.run_jj(["undo"]).success();
118 assert_eq!(work_dir.read_file("state"), "B");
119 work_dir.run_jj(["undo"]).success();
120 assert_eq!(work_dir.read_file("state"), "A");
121 }
122
123 #[test]
124 fn test_undo_ignores_op_revert() {
125 let test_env = TestEnvironment::default();
126 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
127 let work_dir = test_env.work_dir("repo");
128
129 // create a few normal operations
130 work_dir.write_file("state", "A");
131 work_dir.run_jj(["debug", "snapshot"]).success();
132 work_dir.write_file("state", "B");
133 work_dir.run_jj(["debug", "snapshot"]).success();
134 assert_eq!(work_dir.read_file("state"), "B");
135
136 // `op revert` works the same way as `undo` initially, but running `undo`
137 // afterwards will result in a no-op. `undo` does not recognize operations
138 // created by `op revert` as undo-operations on which an undo-stack can
139 // be grown.
140 work_dir.run_jj(["op", "revert"]).success();
141 assert_eq!(work_dir.read_file("state"), "A");
142 work_dir.run_jj(["undo"]).success();
143 assert_eq!(work_dir.read_file("state"), "B");
144 }
145
146 #[test]
147 fn test_redo_non_undo_operation() {
148 let test_env = TestEnvironment::default();
149 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
150 let work_dir = test_env.work_dir("repo");
151
152 work_dir.run_jj(["new", "-m", "a"]).success();
153 let output = work_dir.run_jj(["redo"]);
154 insta::assert_snapshot!(output, @"
155 ------- stderr -------
156 Error: Nothing to redo
157 [EOF]
158 [exit status: 1]
159 ");
160 }
161
162 #[test]
163 fn test_jump_over_old_redo_stack() {
164 let test_env = TestEnvironment::default();
165 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
166 let work_dir = test_env.work_dir("repo");
167
168 // create a few normal operations
169 for state in 'A'..='D' {
170 work_dir.write_file("state", state.to_string());
171 work_dir.run_jj(["debug", "snapshot"]).success();
172 }
173 assert_eq!(work_dir.read_file("state"), "D");
174
175 insta::assert_snapshot!(work_dir.run_jj(["undo", "--quiet"]), @"");
176 assert_eq!(work_dir.read_file("state"), "C");
177 work_dir.run_jj(["undo"]).success();
178 assert_eq!(work_dir.read_file("state"), "B");
179 work_dir.run_jj(["undo"]).success();
180 assert_eq!(work_dir.read_file("state"), "A");
181
182 // create two adjacent redo-stacks
183 insta::assert_snapshot!(work_dir.run_jj(["redo", "--quiet"]), @"");
184 assert_eq!(work_dir.read_file("state"), "B");
185 work_dir.run_jj(["redo"]).success();
186 assert_eq!(work_dir.read_file("state"), "C");
187 work_dir.run_jj(["undo"]).success();
188 assert_eq!(work_dir.read_file("state"), "B");
189 work_dir.run_jj(["redo"]).success();
190 assert_eq!(work_dir.read_file("state"), "C");
191
192 // jump over two adjacent redo-stacks
193 work_dir.run_jj(["redo"]).success();
194 assert_eq!(work_dir.read_file("state"), "D");
195
196 // nothing left to redo
197 insta::assert_snapshot!(work_dir.run_jj(["redo"]), @"
198 ------- stderr -------
199 Error: Nothing to redo
200 [EOF]
201 [exit status: 1]
202 ");
203 }