Debian Codegraph @

cli/tests/test_sign_unsign_commands.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 crate::common::TestEnvironment;
16
17 #[test]
18 fn test_sign() {
19 let test_env = TestEnvironment::default();
20
21 test_env.add_config(
22 r#"
23 [ui]
24 show-cryptographic-signatures = true
25
26 [signing]
27 behavior = "keep"
28 backend = "test"
29 "#,
30 );
31
32 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
33 let work_dir = test_env.work_dir("repo");
34 work_dir.run_jj(["commit", "-m", "one"]).success();
35 work_dir.run_jj(["commit", "-m", "two"]).success();
36 work_dir.run_jj(["commit", "-m", "three"]).success();
37
38 let output = work_dir.run_jj(["log", "-r", "all()"]);
39 insta::assert_snapshot!(output, @"
40 @ zsuskuln [email protected] 2001-02-03 08:05:10 fbef508b
41 │ (empty) (no description set)
42 ○ kkmpptxz [email protected] 2001-02-03 08:05:10 8c63f712
43 │ (empty) three
44 ○ rlvkpnrz [email protected] 2001-02-03 08:05:09 26a2c4cb
45 │ (empty) two
46 ○ qpvuntsm [email protected] 2001-02-03 08:05:08 401ea16f
47 │ (empty) one
48 ◆ zzzzzzzz root() 00000000
49 [EOF]
50 ");
51
52 let output = work_dir.run_jj(["sign", "-r", "..@"]);
53 insta::assert_snapshot!(output, @"
54 ------- stderr -------
55 Signed 4 commits:
56 qpvuntsm 7fb98da0 (empty) one
57 rlvkpnrz 062a3c5a (empty) two
58 kkmpptxz d2174a79 (empty) three
59 zsuskuln 8d7bc037 (empty) (no description set)
60 Working copy (@) now at: zsuskuln 8d7bc037 (empty) (no description set)
61 Parent commit (@-) : kkmpptxz d2174a79 (empty) three
62 [EOF]
63 ");
64
65 let output = work_dir.run_jj(["log", "-r", "all()"]);
66 insta::assert_snapshot!(output, @"
67 @ zsuskuln [email protected] 2001-02-03 08:05:12 8d7bc037 [✓︎]
68 │ (empty) (no description set)
69 ○ kkmpptxz [email protected] 2001-02-03 08:05:12 d2174a79 [✓︎]
70 │ (empty) three
71 ○ rlvkpnrz [email protected] 2001-02-03 08:05:12 062a3c5a [✓︎]
72 │ (empty) two
73 ○ qpvuntsm [email protected] 2001-02-03 08:05:12 7fb98da0 [✓︎]
74 │ (empty) one
75 ◆ zzzzzzzz root() 00000000
76 [EOF]
77 ");
78
79 // Commits already always signed, even if they are already signed by me.
80 // https://github.com/jj-vcs/jj/issues/5786
81 let output = work_dir.run_jj(["sign", "-r", "..@"]);
82 insta::assert_snapshot!(output, @"
83 ------- stderr -------
84 Signed 4 commits:
85 qpvuntsm a57217b4 (empty) one
86 rlvkpnrz e0c0e7ad (empty) two
87 kkmpptxz fc827eb8 (empty) three
88 zsuskuln 66574289 (empty) (no description set)
89 Working copy (@) now at: zsuskuln 66574289 (empty) (no description set)
90 Parent commit (@-) : kkmpptxz fc827eb8 (empty) three
91 [EOF]
92 ");
93
94 // Signing nothing is a valid no-op.
95 let output = work_dir.run_jj(["sign", "-r", "none()"]);
96 insta::assert_snapshot!(output, @"
97 ------- stderr -------
98 Nothing changed.
99 [EOF]
100 ");
101 }
102
103 #[test]
104 fn test_sign_default_revset() {
105 let test_env = TestEnvironment::default();
106
107 test_env.add_config(
108 r#"
109 [ui]
110 show-cryptographic-signatures = true
111
112 [signing]
113 behavior = "keep"
114 backend = "test"
115 "#,
116 );
117
118 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
119 let work_dir = test_env.work_dir("repo");
120 work_dir.run_jj(["commit", "-m", "one"]).success();
121
122 test_env.add_config("revsets.sign = '@'");
123
124 work_dir.run_jj(["sign"]).success();
125
126 let output = work_dir.run_jj(["log", "-r", "all()"]);
127 insta::assert_snapshot!(output, @"
128 @ rlvkpnrz [email protected] 2001-02-03 08:05:09 72a53d81 [✓︎]
129 │ (empty) (no description set)
130 ○ qpvuntsm [email protected] 2001-02-03 08:05:08 401ea16f
131 │ (empty) one
132 ◆ zzzzzzzz root() 00000000
133 [EOF]
134 ");
135 }
136
137 #[test]
138 fn test_sign_with_key() {
139 let test_env = TestEnvironment::default();
140
141 test_env.add_config(
142 r#"
143 [ui]
144 show-cryptographic-signatures = true
145
146 [signing]
147 behavior = "keep"
148 backend = "test"
149 key = "some-key"
150 "#,
151 );
152
153 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
154 let work_dir = test_env.work_dir("repo");
155 work_dir.run_jj(["commit", "-m", "one"]).success();
156 work_dir.run_jj(["commit", "-m", "two"]).success();
157
158 work_dir.run_jj(["sign", "-r", "@-"]).success();
159 work_dir
160 .run_jj(["sign", "-r", "@--", "--key", "another-key"])
161 .success();
162
163 let output = work_dir.run_jj(["log", "-r", "@-|@--", "-Tbuiltin_log_detailed"]);
164 insta::assert_snapshot!(output, @"
165 ○ Commit ID: 810ff318afe002ce54260260e4d4f7071eb476ed
166 │ Change ID: rlvkpnrzqnoowoytxnquwvuryrwnrmlp
167 │ Author : Test User <[email protected]> (2001-02-03 08:05:09)
168 │ Committer: Test User <[email protected]> (2001-02-03 08:05:11)
169 │ Signature: good signature by test-display some-key
170 │
171 │ two
172 │
173 ○ Commit ID: eec44cafe0dc853b67cc7e14ca4fe3b80d3687f1
174 │ Change ID: qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu
175 ~ Author : Test User <[email protected]> (2001-02-03 08:05:08)
176 Committer: Test User <[email protected]> (2001-02-03 08:05:11)
177 Signature: good signature by test-display another-key
178
179 one
180
181 [EOF]
182 ");
183 }
184
185 #[test]
186 fn test_warn_about_signing_commits_not_authored_by_me() {
187 let test_env = TestEnvironment::default();
188
189 test_env.add_config(
190 r#"
191 [ui]
192 show-cryptographic-signatures = true
193
194 [signing]
195 behavior = "keep"
196 backend = "test"
197 "#,
198 );
199
200 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
201 let work_dir = test_env.work_dir("repo");
202 work_dir.run_jj(["commit", "-m", "one"]).success();
203 work_dir.run_jj(["commit", "-m", "two"]).success();
204 work_dir.run_jj(["commit", "-m", "three"]).success();
205
206 work_dir
207 .run_jj(&[
208 "metaedit",
209 "--author=Someone Else <[email protected]>",
210 "..@-",
211 ])
212 .success();
213 let output = work_dir.run_jj(["sign", "-r", "..@-"]);
214 insta::assert_snapshot!(output, @r"
215 ------- stderr -------
216 Signed 3 commits:
217 qpvuntsm 2c0b7924 (empty) one
218 rlvkpnrz 0e054ee0 (empty) two
219 kkmpptxz ed55e398 (empty) three
220 Warning: 3 of these commits are not authored by you.
221 Rebased 1 descendant commits.
222 Working copy (@) now at: zsuskuln 1b3596cb (empty) (no description set)
223 Parent commit (@-) : kkmpptxz ed55e398 (empty) three
224 [EOF]
225 ");
226
227 let output = work_dir.run_jj(["log", "-r", "all()"]);
228 insta::assert_snapshot!(output, @"
229 @ zsuskuln [email protected] 2001-02-03 08:05:12 1b3596cb
230 │ (empty) (no description set)
231 ○ kkmpptxz [email protected] 2001-02-03 08:05:12 ed55e398 [✓︎]
232 │ (empty) three
233 ○ rlvkpnrz [email protected] 2001-02-03 08:05:12 0e054ee0 [✓︎]
234 │ (empty) two
235 ○ qpvuntsm [email protected] 2001-02-03 08:05:12 2c0b7924 [✓︎]
236 │ (empty) one
237 ◆ zzzzzzzz root() 00000000
238 [EOF]
239 ");
240 }
241
242 #[test]
243 fn test_keep_signatures_in_rebased_descendants() {
244 let test_env = TestEnvironment::default();
245
246 test_env.add_config(
247 r#"
248 [ui]
249 show-cryptographic-signatures = true
250
251 [signing]
252 behavior = "drop"
253 backend = "test"
254 "#,
255 );
256
257 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
258 let work_dir = test_env.work_dir("repo");
259 work_dir.run_jj(["commit", "-m", "A"]).success();
260 work_dir.run_jj(["commit", "-m", "B"]).success();
261 work_dir.run_jj(["desc", "-m", "C"]).success();
262
263 let output = work_dir.run_jj(["sign", "-r", "@|@--"]);
264 insta::assert_snapshot!(output, @r"
265 ------- stderr -------
266 Signed 2 commits:
267 qpvuntsm 0e149d92 (empty) A
268 kkmpptxz ab7e21e9 (empty) C
269 Rebased 1 descendant commits.
270 Working copy (@) now at: kkmpptxz ab7e21e9 (empty) C
271 Parent commit (@-) : rlvkpnrz 3981b3e4 (empty) B
272 [EOF]
273 ");
274
275 let output = work_dir.run_jj(["log", "-r", "all()"]);
276 insta::assert_snapshot!(output, @"
277 @ kkmpptxz [email protected] 2001-02-03 08:05:11 ab7e21e9 [✓︎]
278 │ (empty) C
279 ○ rlvkpnrz [email protected] 2001-02-03 08:05:11 3981b3e4
280 │ (empty) B
281 ○ qpvuntsm [email protected] 2001-02-03 08:05:11 0e149d92 [✓︎]
282 │ (empty) A
283 ◆ zzzzzzzz root() 00000000
284 [EOF]
285 ");
286 }
287
288 #[test]
289 fn test_abort_with_error_if_no_signing_backend_is_configured() {
290 let test_env = TestEnvironment::default();
291
292 test_env.add_config(
293 r#"
294 [signing]
295 backend = "none"
296 "#,
297 );
298
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(["sign", "-r", "@-"]);
303
304 insta::assert_snapshot!(output, @"
305 ------- stderr -------
306 Error: No signing backend configured
307 Hint: For configuring a signing backend, see https://docs.jj-vcs.dev/latest/config/#commit-signing
308 [EOF]
309 [exit status: 1]
310 ");
311 }
312
313 #[test]
314 fn test_unsign() {
315 let test_env = TestEnvironment::default();
316
317 test_env.add_config(
318 r#"
319 [ui]
320 show-cryptographic-signatures = true
321
322 [signing]
323 behavior = "keep"
324 backend = "test"
325 "#,
326 );
327
328 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
329 let work_dir = test_env.work_dir("repo");
330 work_dir.run_jj(["commit", "-m", "one"]).success();
331 work_dir.run_jj(["commit", "-m", "two"]).success();
332 work_dir.run_jj(["commit", "-m", "three"]).success();
333
334 work_dir.run_jj(["sign", "-r", "..@"]).success();
335
336 let output = work_dir.run_jj(["log", "-r", "all()"]);
337 insta::assert_snapshot!(output, @"
338 @ zsuskuln [email protected] 2001-02-03 08:05:11 be4609e2 [✓︎]
339 │ (empty) (no description set)
340 ○ kkmpptxz [email protected] 2001-02-03 08:05:11 7b6ad8e6 [✓︎]
341 │ (empty) three
342 ○ rlvkpnrz [email protected] 2001-02-03 08:05:11 8dc06170 [✓︎]
343 │ (empty) two
344 ○ qpvuntsm [email protected] 2001-02-03 08:05:11 fbef1f02 [✓︎]
345 │ (empty) one
346 ◆ zzzzzzzz root() 00000000
347 [EOF]
348 ");
349
350 // Unsign a single commit, resigning the descendant
351 let output = work_dir.run_jj(["unsign", "-r", "@-"]);
352 insta::assert_snapshot!(output, @r"
353 ------- stderr -------
354 Unsigned 1 commits:
355 kkmpptxz 9efcace6 (empty) three
356 Rebased 1 descendant commits.
357 Working copy (@) now at: zsuskuln 654e12b9 (empty) (no description set)
358 Parent commit (@-) : kkmpptxz 9efcace6 (empty) three
359 [EOF]
360 ");
361
362 let output = work_dir.run_jj(["log", "-r", "all()"]);
363 insta::assert_snapshot!(output, @"
364 @ zsuskuln [email protected] 2001-02-03 08:05:13 654e12b9 [✓︎]
365 │ (empty) (no description set)
366 ○ kkmpptxz [email protected] 2001-02-03 08:05:13 9efcace6
367 │ (empty) three
368 ○ rlvkpnrz [email protected] 2001-02-03 08:05:11 8dc06170 [✓︎]
369 │ (empty) two
370 ○ qpvuntsm [email protected] 2001-02-03 08:05:11 fbef1f02 [✓︎]
371 │ (empty) one
372 ◆ zzzzzzzz root() 00000000
373 [EOF]
374 ");
375
376 // Unsign multiple commits, with both signed and unsigned descendants
377 let output = work_dir.run_jj(["unsign", "-r", "..@--"]);
378 insta::assert_snapshot!(output, @r"
379 ------- stderr -------
380 Unsigned 2 commits:
381 qpvuntsm d265d645 (empty) one
382 rlvkpnrz 6e8ced57 (empty) two
383 Rebased 2 descendant commits.
384 Working copy (@) now at: zsuskuln ad24007a (empty) (no description set)
385 Parent commit (@-) : kkmpptxz df10e25c (empty) three
386 [EOF]
387 ");
388
389 let output = work_dir.run_jj(["log", "-r", "all()"]);
390 insta::assert_snapshot!(output, @"
391 @ zsuskuln [email protected] 2001-02-03 08:05:15 ad24007a [✓︎]
392 │ (empty) (no description set)
393 ○ kkmpptxz [email protected] 2001-02-03 08:05:15 df10e25c
394 │ (empty) three
395 ○ rlvkpnrz [email protected] 2001-02-03 08:05:15 6e8ced57
396 │ (empty) two
397 ○ qpvuntsm [email protected] 2001-02-03 08:05:15 d265d645
398 │ (empty) one
399 ◆ zzzzzzzz root() 00000000
400 [EOF]
401 ");
402
403 // Unsigning nothing is a valid no-op.
404 let output = work_dir.run_jj(["unsign", "-r", "none()"]);
405 insta::assert_snapshot!(output, @"
406 ------- stderr -------
407 Nothing changed.
408 [EOF]
409 ");
410 }
411
412 #[test]
413 fn test_warn_about_unsigning_commits_not_authored_by_me() {
414 let test_env = TestEnvironment::default();
415
416 test_env.add_config(
417 r#"
418 [ui]
419 show-cryptographic-signatures = true
420
421 [signing]
422 behavior = "keep"
423 backend = "test"
424 "#,
425 );
426
427 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
428 let work_dir = test_env.work_dir("repo");
429 work_dir.run_jj(["commit", "-m", "one"]).success();
430 work_dir.run_jj(["commit", "-m", "two"]).success();
431 work_dir.run_jj(["commit", "-m", "three"]).success();
432
433 work_dir.run_jj(["sign", "-r", "..@"]).success();
434
435 let run_jj_as_someone_else = |args: &[&str]| {
436 let output = work_dir.run_jj_with(|cmd| {
437 cmd.env("JJ_USER", "Someone Else")
438 .env("JJ_EMAIL", "[email protected]")
439 .args(args)
440 });
441 (output.stdout, output.stderr)
442 };
443
444 let (_, stderr) = run_jj_as_someone_else(&["unsign", "-r", "..@"]);
445 insta::assert_snapshot!(stderr, @r"
446 Unsigned 4 commits:
447 qpvuntsm 4430b844 (empty) one
448 rlvkpnrz 65d9cdf7 (empty) two
449 kkmpptxz f6eb4a7e (empty) three
450 zsuskuln 0fda7ce2 (empty) (no description set)
451 Warning: 4 of these commits are not authored by you.
452 Working copy (@) now at: zsuskuln 0fda7ce2 (empty) (no description set)
453 Parent commit (@-) : kkmpptxz f6eb4a7e (empty) three
454 [EOF]
455 ");
456
457 let output = work_dir.run_jj(["log", "-r", "all()"]);
458 insta::assert_snapshot!(output, @"
459 @ zsuskuln [email protected] 2001-02-03 08:05:12 0fda7ce2
460 │ (empty) (no description set)
461 ○ kkmpptxz [email protected] 2001-02-03 08:05:12 f6eb4a7e
462 │ (empty) three
463 ○ rlvkpnrz [email protected] 2001-02-03 08:05:12 65d9cdf7
464 │ (empty) two
465 ○ qpvuntsm [email protected] 2001-02-03 08:05:12 4430b844
466 │ (empty) one
467 ◆ zzzzzzzz root() 00000000
468 [EOF]
469 ");
470 }