Debian Codegraph @

cli/tests/test_file_track_untrack_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_track_untrack() {
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 work_dir.write_file("file1", "initial");
24 work_dir.write_file("file1.bak", "initial");
25 work_dir.write_file("file2.bak", "initial");
26 let target_dir = work_dir.create_dir("target");
27 target_dir.write_file("file2", "initial");
28 target_dir.write_file("file3", "initial");
29
30 // Run a command so all the files get tracked, then add "*.bak" to the ignore
31 // patterns
32 work_dir.run_jj(["st"]).success();
33 work_dir.write_file(".gitignore", "*.bak\n");
34 let files_before = work_dir.run_jj(["file", "list"]).success();
35
36 // Errors out when not run at the head operation
37 let output = work_dir.run_jj(["file", "untrack", "file1", "--at-op", "@-"]);
38 insta::assert_snapshot!(output, @"
39 ------- stderr -------
40 Error: This command must be able to update the working copy.
41 Hint: Don't use --at-op.
42 [EOF]
43 [exit status: 1]
44 ");
45 // Errors out when no path is specified
46 let output = work_dir.run_jj(["file", "untrack"]);
47 insta::assert_snapshot!(output, @"
48 ------- stderr -------
49 error: the following required arguments were not provided:
50 <FILESETS>...
51
52 Usage: jj file untrack <FILESETS>...
53
54 For more information, try '--help'.
55 [EOF]
56 [exit status: 2]
57 ");
58 // Errors out when a specified file is not ignored
59 let output = work_dir.run_jj(["file", "untrack", "file1", "file1.bak"]);
60 insta::assert_snapshot!(output, @"
61 ------- stderr -------
62 Error: 'file1' is not ignored.
63 Hint: Files that are not ignored will be added back by the next command.
64 Make sure they're ignored, then try again.
65 [EOF]
66 [exit status: 1]
67 ");
68 let files_after = work_dir.run_jj(["file", "list"]).success();
69 // There should be no changes to the state when there was an error
70 assert_eq!(files_after, files_before);
71
72 // Can untrack a single file
73 assert!(files_before.stdout.raw().contains("file1.bak\n"));
74 let output = work_dir.run_jj(["file", "untrack", "file1.bak"]);
75 insta::assert_snapshot!(output, @"");
76 let files_after = work_dir.run_jj(["file", "list"]).success();
77 // The file is no longer tracked
78 assert!(!files_after.stdout.raw().contains("file1.bak"));
79 // Other files that match the ignore pattern are not untracked
80 assert!(files_after.stdout.raw().contains("file2.bak"));
81 // The files still exist on disk
82 assert!(work_dir.root().join("file1.bak").exists());
83 assert!(work_dir.root().join("file2.bak").exists());
84
85 // Warns if path doesn't exist
86 let output = work_dir.run_jj(["file", "untrack", "nonexistent"]);
87 insta::assert_snapshot!(output.normalize_backslash(), @"
88 ------- stderr -------
89 Warning: No matching entries for paths: nonexistent
90 [EOF]
91 ");
92
93 // Errors out when multiple specified files are not ignored
94 let output = work_dir.run_jj(["file", "untrack", "target"]);
95 insta::assert_snapshot!(output.normalize_backslash(), @"
96 ------- stderr -------
97 Error: 'target/file2' and 1 other files are not ignored.
98 Hint: Files that are not ignored will be added back by the next command.
99 Make sure they're ignored, then try again.
100 [EOF]
101 [exit status: 1]
102 ");
103
104 // Can untrack after adding to ignore patterns
105 work_dir.write_file(".gitignore", "*.bak\ntarget/\n");
106 let output = work_dir.run_jj(["file", "untrack", "target"]);
107 insta::assert_snapshot!(output, @"");
108 let files_after = work_dir.run_jj(["file", "list"]).success();
109 assert!(!files_after.stdout.raw().contains("target"));
110 }
111
112 #[test]
113 fn test_track_untrack_sparse() {
114 let test_env = TestEnvironment::default();
115 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
116 let work_dir = test_env.work_dir("repo");
117
118 work_dir.write_file("file1", "contents");
119 work_dir.write_file("file2", "contents");
120
121 // When untracking a file that's not included in the sparse working copy, it
122 // doesn't need to be ignored (because it won't be automatically added
123 // back).
124 let output = work_dir.run_jj(["file", "list"]);
125 insta::assert_snapshot!(output, @"
126 file1
127 file2
128 [EOF]
129 ");
130 work_dir
131 .run_jj(["sparse", "set", "--clear", "--add", "file1"])
132 .success();
133 let output = work_dir.run_jj(["file", "untrack", "file2"]);
134 insta::assert_snapshot!(output, @"");
135 let output = work_dir.run_jj(["file", "list"]);
136 insta::assert_snapshot!(output, @"
137 file1
138 [EOF]
139 ");
140 // Trying to manually track a file that's not included in the sparse working has
141 // no effect. TODO: At least a warning would be useful
142 let output = work_dir.run_jj(["file", "track", "file2"]);
143 insta::assert_snapshot!(output, @"");
144 let output = work_dir.run_jj(["file", "list"]);
145 insta::assert_snapshot!(output, @"
146 file1
147 [EOF]
148 ");
149 }
150
151 #[test]
152 fn test_auto_track() {
153 let test_env = TestEnvironment::default();
154 test_env.add_config(r#"snapshot.auto-track = 'glob:*.rs'"#);
155 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
156 let work_dir = test_env.work_dir("repo");
157
158 work_dir.write_file("file1.rs", "initial");
159 work_dir.write_file("file2.md", "initial");
160 work_dir.write_file("file3.md", "initial");
161
162 // Only configured paths get auto-tracked
163 let output = work_dir.run_jj(["file", "list"]);
164 insta::assert_snapshot!(output, @"
165 file1.rs
166 [EOF]
167 ");
168
169 // Can manually track paths
170 let output = work_dir.run_jj(["file", "track", "file3.md"]);
171 insta::assert_snapshot!(output, @"");
172 let output = work_dir.run_jj(["file", "list"]);
173 insta::assert_snapshot!(output, @"
174 file1.rs
175 file3.md
176 [EOF]
177 ");
178
179 // Can manually untrack paths
180 let output = work_dir.run_jj(["file", "untrack", "file3.md"]);
181 insta::assert_snapshot!(output, @"");
182 let output = work_dir.run_jj(["file", "list"]);
183 insta::assert_snapshot!(output, @"
184 file1.rs
185 [EOF]
186 ");
187
188 // CWD-relative paths in `snapshot.auto-track` are evaluated from the repo root
189 let sub_dir = work_dir.create_dir("sub");
190 sub_dir.write_file("file1.rs", "initial");
191 let output = sub_dir.run_jj(["file", "list"]);
192 insta::assert_snapshot!(output.normalize_backslash(), @"
193 ../file1.rs
194 [EOF]
195 ");
196
197 // But `jj file track` wants CWD-relative paths
198 let output = sub_dir.run_jj(["file", "track", "file1.rs"]);
199 insta::assert_snapshot!(output, @"");
200 let output = sub_dir.run_jj(["file", "list"]);
201 insta::assert_snapshot!(output.normalize_backslash(), @"
202 ../file1.rs
203 file1.rs
204 [EOF]
205 ");
206 }
207
208 #[test]
209 fn test_track_ignored() {
210 let test_env = TestEnvironment::default();
211 test_env.add_config(r#"snapshot.auto-track = 'none()'"#);
212 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
213 let work_dir = test_env.work_dir("repo");
214
215 // Trackable files exist but auto-tracking is not enabled
216 work_dir.write_file(".gitignore", "*.bak\n");
217 work_dir.write_file("file1", "initial");
218 work_dir.write_file("file1.bak", "initial");
219 let output = work_dir.run_jj(["file", "list"]);
220 insta::assert_snapshot!(output, @"");
221
222 // Create a descendant, updates will be seen as rebases
223 work_dir.run_jj(["new", "--no-edit"]).success();
224
225 // Track an unignored path
226 // TODO: fix GH #8298 so tracking is immediate
227 let output = work_dir.run_jj(["file", "track", "file1"]);
228 insta::assert_snapshot!(output, @"");
229 let output = work_dir.run_jj(["file", "list"]);
230 insta::assert_snapshot!(output, @r"
231 file1
232 [EOF]
233 ------- stderr -------
234 Rebased 1 descendant commits onto updated working copy.
235 [EOF]
236 ");
237
238 // Track another unignored path
239 work_dir.write_file("file2", "initial");
240 let output = work_dir.run_jj(["file", "track", "file2"]);
241 insta::assert_snapshot!(output, @"");
242 let output = work_dir.run_jj(["file", "list"]);
243 insta::assert_snapshot!(output, @r"
244 file1
245 file2
246 [EOF]
247 ------- stderr -------
248 Rebased 1 descendant commits onto updated working copy.
249 [EOF]
250 ");
251
252 // Now untrack the file; eviction is immediate and affects descendants
253 let output = work_dir.run_jj(["file", "untrack", "file2"]);
254 insta::assert_snapshot!(output, @r"
255 ------- stderr -------
256 Rebased 1 descendant commits.
257 [EOF]
258 ");
259
260 // Track an ignored path without --include-ignored (should not work)
261 let output = work_dir.run_jj(["file", "track", "file1.bak"]);
262 insta::assert_snapshot!(output, @"");
263 let output = work_dir.run_jj(["file", "list"]);
264 insta::assert_snapshot!(output, @"
265 file1
266 [EOF]
267 ");
268 }
269
270 #[test]
271 fn test_track_ignored_with_flag() {
272 let test_env = TestEnvironment::default();
273 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
274 let work_dir = test_env.work_dir("repo");
275
276 work_dir.write_file(".gitignore", "*.ignored\n");
277 work_dir.write_file("file1.txt", "content");
278 work_dir.write_file("file2.ignored", "ignored content");
279
280 // Test the setup
281 let output = work_dir.run_jj(["file", "list"]);
282 insta::assert_snapshot!(output, @"
283 .gitignore
284 file1.txt
285 [EOF]
286 ");
287
288 // Track ignored file with --include-ignored
289 let output = work_dir.run_jj(["file", "track", "--include-ignored", "file2.ignored"]);
290 insta::assert_snapshot!(output, @"");
291 let output = work_dir.run_jj(["file", "list"]);
292 insta::assert_snapshot!(output, @"
293 .gitignore
294 file1.txt
295 file2.ignored
296 [EOF]
297 ");
298 }
299
300 #[test]
301 fn test_track_large_file_with_flag() {
302 let test_env = TestEnvironment::default();
303 test_env.add_config(r#"snapshot.max-new-file-size = "10""#);
304 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
305 let work_dir = test_env.work_dir("repo");
306
307 work_dir.write_file("small.txt", "small");
308 work_dir.write_file("large1.txt", "a".repeat(20).as_str());
309 work_dir.write_file("large2.txt", "b".repeat(20).as_str());
310
311 // Test the setup - both large files warned
312 let output = work_dir.run_jj(["file", "list"]);
313 insta::assert_snapshot!(output, @"
314 small.txt
315 [EOF]
316 ------- stderr -------
317 Warning: Refused to snapshot some files:
318 large1.txt: 20.0B (20 bytes); the maximum size allowed is 10.0B (10 bytes)
319 large2.txt: 20.0B (20 bytes); the maximum size allowed is 10.0B (10 bytes)
320 Hint: This is to prevent large files from being added by accident. To fix this:
321 * Add the file(s) to `.gitignore`
322 * Run `jj config set --repo snapshot.max-new-file-size 20`
323 This will increase the maximum file size allowed for new files, in this repository only.
324 * Run `jj --config snapshot.max-new-file-size=20 status`
325 This will increase the maximum file size allowed for new files, for this command only.
326 [EOF]
327 ");
328
329 // Track only large1.txt - large2.txt should still be warned about
330 let output = work_dir.run_jj(["file", "track", "--include-ignored", "large1.txt"]);
331 insta::assert_snapshot!(output, @"
332 ------- stderr -------
333 Warning: Refused to snapshot some files:
334 large2.txt: 20.0B (20 bytes); the maximum size allowed is 10.0B (10 bytes)
335 Hint: This is to prevent large files from being added by accident. To fix this:
336 * Add the file(s) to `.gitignore`
337 * Run `jj config set --repo snapshot.max-new-file-size 20`
338 This will increase the maximum file size allowed for new files, in this repository only.
339 * Run `jj --config snapshot.max-new-file-size=20 file track large2.txt`
340 This will increase the maximum file size allowed for new files, for this command only.
341 * Run `jj file track --include-ignored large2.txt`
342 This will track the file(s) regardless of size.
343 [EOF]
344 ");
345 let output = work_dir.run_jj(["file", "list"]);
346 insta::assert_snapshot!(output, @"
347 large1.txt
348 small.txt
349 [EOF]
350 ------- stderr -------
351 Warning: Refused to snapshot some files:
352 large2.txt: 20.0B (20 bytes); the maximum size allowed is 10.0B (10 bytes)
353 Hint: This is to prevent large files from being added by accident. To fix this:
354 * Add the file(s) to `.gitignore`
355 * Run `jj config set --repo snapshot.max-new-file-size 20`
356 This will increase the maximum file size allowed for new files, in this repository only.
357 * Run `jj --config snapshot.max-new-file-size=20 status`
358 This will increase the maximum file size allowed for new files, for this command only.
359 [EOF]
360 ");
361 }
362
363 #[test]
364 fn test_track_ignored_directory() {
365 let test_env = TestEnvironment::default();
366 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
367 let work_dir = test_env.work_dir("repo");
368
369 work_dir.write_file(".gitignore", "ignored_dir/\n");
370 let ignored_dir = work_dir.create_dir("ignored_dir");
371 ignored_dir.write_file("file1.txt", "content1");
372 ignored_dir.write_file("file2.txt", "content2");
373
374 // Test the setup
375 let output = work_dir.run_jj(["file", "list"]);
376 insta::assert_snapshot!(output, @"
377 .gitignore
378 [EOF]
379 ");
380
381 // Track ignored directory with --include-ignored
382 let output = work_dir.run_jj(["file", "track", "--include-ignored", "ignored_dir"]);
383 insta::assert_snapshot!(output, @"");
384 let output = work_dir.run_jj(["file", "list"]);
385 insta::assert_snapshot!(output.normalize_backslash(), @"
386 .gitignore
387 ignored_dir/file1.txt
388 ignored_dir/file2.txt
389 [EOF]
390 ");
391 }