just playing with tangled
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
15use std::path::Path;
16
17use crate::common::TestEnvironment;
18
19#[test]
20fn test_commit_with_description_from_cli() {
21 let test_env = TestEnvironment::default();
22 test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
23 let workspace_path = test_env.env_root().join("repo");
24
25 // Description applies to the current working-copy (not the new one)
26 test_env.jj_cmd_ok(&workspace_path, &["commit", "-m=first"]);
27 insta::assert_snapshot!(get_log_output(&test_env, &workspace_path), @r###"
28 @ b88fb4e51bdd
29 ◉ 69542c1984c1 first
30 ◉ 000000000000
31 "###);
32}
33
34#[test]
35fn test_commit_with_editor() {
36 let mut test_env = TestEnvironment::default();
37 test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
38 let workspace_path = test_env.env_root().join("repo");
39
40 // Check that the text file gets initialized with the current description and
41 // set a new one
42 test_env.jj_cmd_ok(&workspace_path, &["describe", "-m=initial"]);
43 let edit_script = test_env.set_up_fake_editor();
44 std::fs::write(&edit_script, ["dump editor0", "write\nmodified"].join("\0")).unwrap();
45 test_env.jj_cmd_ok(&workspace_path, &["commit"]);
46 insta::assert_snapshot!(get_log_output(&test_env, &workspace_path), @r###"
47 @ 3df78bc2b9b5
48 ◉ 30a8c2b3d6eb modified
49 ◉ 000000000000
50 "###);
51 insta::assert_snapshot!(
52 std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r###"
53 initial
54
55 JJ: Lines starting with "JJ: " (like this one) will be removed.
56 "###);
57
58 // Check that the editor content includes diff summary
59 std::fs::write(workspace_path.join("file1"), "foo\n").unwrap();
60 std::fs::write(workspace_path.join("file2"), "foo\n").unwrap();
61 test_env.jj_cmd_ok(&workspace_path, &["describe", "-m=add files"]);
62 std::fs::write(&edit_script, "dump editor1").unwrap();
63 test_env.jj_cmd_ok(&workspace_path, &["commit"]);
64 insta::assert_snapshot!(
65 std::fs::read_to_string(test_env.env_root().join("editor1")).unwrap(), @r###"
66 add files
67
68 JJ: This commit contains the following changes:
69 JJ: A file1
70 JJ: A file2
71
72 JJ: Lines starting with "JJ: " (like this one) will be removed.
73 "###);
74}
75
76#[test]
77fn test_commit_interactive() {
78 let mut test_env = TestEnvironment::default();
79 test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
80 let workspace_path = test_env.env_root().join("repo");
81
82 std::fs::write(workspace_path.join("file1"), "foo\n").unwrap();
83 std::fs::write(workspace_path.join("file2"), "bar\n").unwrap();
84 test_env.jj_cmd_ok(&workspace_path, &["describe", "-m=add files"]);
85 let edit_script = test_env.set_up_fake_editor();
86 std::fs::write(edit_script, ["dump editor"].join("\0")).unwrap();
87
88 let diff_editor = test_env.set_up_fake_diff_editor();
89 std::fs::write(diff_editor, "rm file2").unwrap();
90
91 // Create a commit interactively and select only file1
92 test_env.jj_cmd_ok(&workspace_path, &["commit", "-i"]);
93
94 insta::assert_snapshot!(
95 std::fs::read_to_string(test_env.env_root().join("editor")).unwrap(), @r###"
96 add files
97
98 JJ: This commit contains the following changes:
99 JJ: A file1
100
101 JJ: Lines starting with "JJ: " (like this one) will be removed.
102 "###);
103
104 // Try again with --tool=<name>, which implies --interactive
105 test_env.jj_cmd_ok(&workspace_path, &["undo"]);
106 test_env.jj_cmd_ok(
107 &workspace_path,
108 &[
109 "commit",
110 "--config-toml=ui.diff-editor='false'",
111 "--tool=fake-diff-editor",
112 ],
113 );
114
115 insta::assert_snapshot!(
116 std::fs::read_to_string(test_env.env_root().join("editor")).unwrap(), @r###"
117 add files
118
119 JJ: This commit contains the following changes:
120 JJ: A file1
121
122 JJ: Lines starting with "JJ: " (like this one) will be removed.
123 "###);
124}
125
126#[test]
127fn test_commit_with_default_description() {
128 let mut test_env = TestEnvironment::default();
129 test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
130 test_env.add_config(r#"ui.default-description = "\n\nTESTED=TODO""#);
131 let workspace_path = test_env.env_root().join("repo");
132
133 std::fs::write(workspace_path.join("file1"), "foo\n").unwrap();
134 std::fs::write(workspace_path.join("file2"), "bar\n").unwrap();
135 let edit_script = test_env.set_up_fake_editor();
136 std::fs::write(edit_script, ["dump editor"].join("\0")).unwrap();
137 test_env.jj_cmd_ok(&workspace_path, &["commit"]);
138
139 insta::assert_snapshot!(get_log_output(&test_env, &workspace_path), @r#"
140 @ 8dc0591d00f7
141 ◉ 7e780ba80aeb TESTED=TODO
142 ◉ 000000000000
143 "#);
144 assert_eq!(
145 std::fs::read_to_string(test_env.env_root().join("editor")).unwrap(),
146 r#"
147
148TESTED=TODO
149JJ: This commit contains the following changes:
150JJ: A file1
151JJ: A file2
152
153JJ: Lines starting with "JJ: " (like this one) will be removed.
154"#
155 );
156}
157
158#[test]
159fn test_commit_without_working_copy() {
160 let test_env = TestEnvironment::default();
161 test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
162 let workspace_path = test_env.env_root().join("repo");
163
164 test_env.jj_cmd_ok(&workspace_path, &["workspace", "forget"]);
165 let stderr = test_env.jj_cmd_failure(&workspace_path, &["commit", "-m=first"]);
166 insta::assert_snapshot!(stderr, @r###"
167 Error: This command requires a working copy
168 "###);
169}
170
171#[test]
172fn test_commit_paths() {
173 let test_env = TestEnvironment::default();
174 test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
175 let workspace_path = test_env.env_root().join("repo");
176
177 std::fs::write(workspace_path.join("file1"), "foo\n").unwrap();
178 std::fs::write(workspace_path.join("file2"), "bar\n").unwrap();
179
180 test_env.jj_cmd_ok(&workspace_path, &["commit", "-m=first", "file1"]);
181 let stdout = test_env.jj_cmd_success(&workspace_path, &["diff", "-r", "@-"]);
182 insta::assert_snapshot!(stdout, @r###"
183 Added regular file file1:
184 1: foo
185 "###);
186
187 let stdout = test_env.jj_cmd_success(&workspace_path, &["diff"]);
188 insta::assert_snapshot!(stdout, @"
189 Added regular file file2:
190 1: bar
191 ");
192}
193
194#[test]
195fn test_commit_paths_warning() {
196 let test_env = TestEnvironment::default();
197 test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
198 let workspace_path = test_env.env_root().join("repo");
199
200 std::fs::write(workspace_path.join("file1"), "foo\n").unwrap();
201 std::fs::write(workspace_path.join("file2"), "bar\n").unwrap();
202
203 let (stdout, stderr) = test_env.jj_cmd_ok(&workspace_path, &["commit", "-m=first", "file3"]);
204 insta::assert_snapshot!(stderr, @r###"
205 Warning: The given paths do not match any file: file3
206 Working copy now at: rlvkpnrz 67872820 (no description set)
207 Parent commit : qpvuntsm 69542c19 (empty) first
208 "###);
209 insta::assert_snapshot!(stdout, @"");
210
211 let stdout = test_env.jj_cmd_success(&workspace_path, &["diff"]);
212 insta::assert_snapshot!(stdout, @r###"
213 Added regular file file1:
214 1: foo
215 Added regular file file2:
216 1: bar
217 "###);
218}
219
220fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
221 let template = r#"commit_id.short() ++ " " ++ description"#;
222 test_env.jj_cmd_success(cwd, &["log", "-T", template])
223}