Debian Codegraph @

cli/tests/test_util_command.rs

raw
1 // Copyright 2023 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 std::fs;
16
17 use insta::assert_snapshot;
18 use test_case::test_case;
19 use testutils::TestRepoBackend;
20 use testutils::TestResult;
21 use testutils::TestWorkspace;
22
23 use crate::common::TestEnvironment;
24
25 #[test_case(TestRepoBackend::Simple, "Simple" ; "simple backend")]
26 #[test_case(TestRepoBackend::Git, "git" ; "git backend")]
27 fn test_util_backend_name(backend: TestRepoBackend, expected_name: &str) {
28 let test_env = TestEnvironment::default();
29 let test_workspace = TestWorkspace::init_with_backend(backend);
30 let root = test_workspace.workspace.workspace_root();
31 let output = test_env
32 .run_jj_in(&root, ["util", "backend", "name"])
33 .success();
34 assert_eq!(output.stdout.raw(), &[expected_name, "\n"].concat());
35 }
36
37 #[test]
38 fn test_util_config_schema() {
39 let test_env = TestEnvironment::default();
40 let output = test_env.run_jj_in(".", ["util", "config-schema"]);
41 // Validate partial snapshot, redacting any lines nested 2+ indent levels.
42 insta::with_settings!({filters => vec![(r"(?m)(^ .*$\r?\n)+", " [...]\n")]}, {
43 assert_snapshot!(output, @r#"
44 {
45 "$schema": "http://json-schema.org/draft-04/schema",
46 "$comment": "`taplo` and the corresponding VS Code plugins only support version draft-04 of JSON Schema, see <https://taplo.tamasfe.dev/configuration/developing-schemas.html>. draft-07 is mostly compatible with it, newer versions may not be.",
47 "title": "Jujutsu config",
48 "type": "object",
49 "description": "User configuration for Jujutsu VCS. See https://docs.jj-vcs.dev/latest/config/ for details",
50 "properties": {
51 [...]
52 }
53 }
54 [EOF]
55 "#);
56 });
57 }
58
59 #[test]
60 fn test_gc_args() {
61 let test_env = TestEnvironment::default();
62 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
63 let work_dir = test_env.work_dir("repo");
64
65 let output = work_dir.run_jj(["util", "gc"]);
66 insta::assert_snapshot!(output, @"");
67
68 let output = work_dir.run_jj(["util", "gc", "--at-op=@-"]);
69 insta::assert_snapshot!(output, @"
70 ------- stderr -------
71 Error: Cannot garbage collect from a non-head operation
72 [EOF]
73 [exit status: 1]
74 ");
75
76 let output = work_dir.run_jj(["util", "gc", "--expire=foobar"]);
77 insta::assert_snapshot!(output, @"
78 ------- stderr -------
79 Error: --expire only accepts 'now'
80 [EOF]
81 [exit status: 1]
82 ");
83 }
84
85 #[test]
86 fn test_gc_operation_log() {
87 let test_env = TestEnvironment::default();
88 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
89 let work_dir = test_env.work_dir("repo");
90
91 // Create an operation.
92 work_dir.write_file("file", "a change\n");
93 work_dir.run_jj(["commit", "-m", "a change"]).success();
94 let op_to_remove = work_dir.current_operation_id();
95
96 // Make another operation the head.
97 work_dir.write_file("file", "another change\n");
98 work_dir
99 .run_jj(["commit", "-m", "another change"])
100 .success();
101
102 // This works before the operation is removed.
103 work_dir
104 .run_jj(["debug", "object", "operation", &op_to_remove])
105 .success();
106
107 // Remove some operations.
108 work_dir.run_jj(["operation", "abandon", "..@-"]).success();
109 work_dir.run_jj(["util", "gc", "--expire=now"]).success();
110
111 // Now this doesn't work.
112 let output = work_dir.run_jj(["debug", "object", "operation", &op_to_remove]);
113 insta::assert_snapshot!(output.strip_stderr_last_line(), @"
114 ------- stderr -------
115 Internal error: Failed to load an operation
116 Caused by:
117 1: Object 2bf28acab8bf15817f17f696d4483584440e13d155280691c7dd5abfa2e7e9b67258f06022cfdd5b86295cbfcfd42e9132740b8d3a75d18c2b0718b45f69d8d9 of type operation not found
118 2: Cannot access $TEST_ENV/repo/.jj/repo/op_store/operations/2bf28acab8bf15817f17f696d4483584440e13d155280691c7dd5abfa2e7e9b67258f06022cfdd5b86295cbfcfd42e9132740b8d3a75d18c2b0718b45f69d8d9
119 [EOF]
120 [exit status: 255]
121 ");
122 }
123
124 #[test]
125 fn test_shell_completions() {
126 #[track_caller]
127 fn test(shell: &str) {
128 let test_env = TestEnvironment::default();
129 // Use the local backend because GitBackend::gc() depends on the git CLI.
130 let output = test_env
131 .run_jj_in(".", ["util", "completion", shell])
132 .success();
133 // Ensures only stdout contains text
134 assert!(
135 !output.stdout.is_empty() && output.stderr.is_empty(),
136 "{output}"
137 );
138 }
139
140 test("bash");
141 test("elvish");
142 test("fish");
143 test("nushell");
144 test("power-shell");
145 test("zsh");
146 }
147
148 #[test]
149 fn test_util_exec() {
150 let test_env = TestEnvironment::default();
151 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
152 let output = test_env.run_jj_in(
153 ".",
154 [
155 "util",
156 "exec",
157 "--",
158 formatter_path.to_str().unwrap(),
159 "--append",
160 "hello",
161 ],
162 );
163 // Ensures only stdout contains text
164 insta::assert_snapshot!(output, @"hello[EOF]");
165 }
166
167 #[test]
168 fn test_util_exec_fail() {
169 let test_env = TestEnvironment::default();
170 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
171 let output = test_env.run_jj_in(
172 ".",
173 [
174 "util",
175 "exec",
176 "--",
177 formatter_path.to_str().unwrap(),
178 "--badopt",
179 ],
180 );
181 // Ensures only stdout contains text
182 insta::assert_snapshot!(output.normalize_stderr_with(|s| s.replace(".exe", "")), @"
183 ------- stderr -------
184 error: unexpected argument '--badopt' found
185
186 tip: a similar argument exists: '--abort'
187
188 Usage: fake-formatter --abort
189
190 For more information, try '--help'.
191 [EOF]
192 [exit status: 2]
193 ");
194 }
195
196 #[test]
197 fn test_util_exec_not_found() {
198 let test_env = TestEnvironment::default();
199 let output = test_env.run_jj_in(".", ["util", "exec", "--", "jj-test-missing-program"]);
200 insta::assert_snapshot!(output.strip_stderr_last_line(), @"
201 ------- stderr -------
202 Error: Failed to execute external command 'jj-test-missing-program'
203 [EOF]
204 [exit status: 1]
205 ");
206 }
207
208 #[test]
209 fn test_util_exec_crash() {
210 let test_env = TestEnvironment::default();
211 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
212 let output = test_env.run_jj_in(
213 ".",
214 [
215 "util",
216 "exec",
217 "--",
218 formatter_path.to_str().unwrap(),
219 "--abort",
220 ],
221 );
222
223 if cfg!(unix) {
224 insta::assert_snapshot!(output, @"
225 ------- stderr -------
226 Error: External command was terminated by signal: 15 (SIGTERM)
227 [EOF]
228 [exit status: 1]
229 ");
230 } else if cfg!(windows) {
231 // abort produces STATUS_STACK_BUFFER_OVERRUN (0xc0000409)
232 insta::assert_snapshot!(output, @r"
233 [exit status: -1073740791]
234 ");
235 }
236 }
237
238 #[cfg(unix)]
239 #[test]
240 fn test_util_exec_sets_env() {
241 let test_env = TestEnvironment::default();
242 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
243 let output = test_env.run_jj_in(
244 ".",
245 [
246 "-R",
247 "repo",
248 "util",
249 "exec",
250 "--",
251 "/bin/sh",
252 "-c",
253 r#"echo "$JJ_WORKSPACE_ROOT""#,
254 ],
255 );
256 insta::assert_snapshot!(output, @"
257 $TEST_ENV/repo
258 [EOF]
259 ");
260 }
261
262 #[test]
263 fn test_install_man_pages() -> TestResult {
264 let test_env = TestEnvironment::default();
265
266 // no man pages present
267 let man_dir = test_env.env_root().join("man1");
268 assert!(!man_dir.exists());
269
270 // install man pages
271 let output = test_env.run_jj_in(".", ["util", "install-man-pages", "."]);
272 insta::assert_snapshot!(output, @"");
273
274 // confirm something is now present
275 assert!(man_dir.is_dir());
276 assert!(fs::read_dir(man_dir)?.next().is_some());
277 Ok(())
278 }
279
280 #[test]
281 fn test_util_snapshot() {
282 let test_env = TestEnvironment::default();
283 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
284 let work_dir = test_env.work_dir("repo");
285
286 work_dir.write_file("foo", "foo");
287
288 let output = work_dir.run_jj(["util", "snapshot"]);
289 insta::assert_snapshot!(output, @"
290 ------- stderr -------
291 Snapshot complete.
292 [EOF]
293 ");
294 }
295
296 #[test]
297 fn test_util_snapshot_nothing_changed() {
298 let test_env = TestEnvironment::default();
299 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
300 let work_dir = test_env.work_dir("repo");
301
302 let output = work_dir.run_jj(["util", "snapshot"]);
303 insta::assert_snapshot!(output, @"
304 ------- stderr -------
305 No snapshot needed.
306 [EOF]
307 ");
308 }