Debian Codegraph @

cli/tests/test_gitignores.rs

raw
1 // Copyright 2020 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::io::Write as _;
16
17 use testutils::TestResult;
18 use testutils::git;
19
20 use crate::common::TestEnvironment;
21
22 #[test]
23 fn test_gitignores() -> TestResult {
24 let test_env = TestEnvironment::default();
25 let work_dir = test_env.work_dir("repo");
26 git::init(work_dir.root());
27 work_dir
28 .run_jj(["git", "init", "--git-repo", "."])
29 .success();
30
31 // Say in core.excludesFiles that we don't want file1, file2, or file3
32 let mut file = std::fs::OpenOptions::new()
33 .append(true)
34 .open(work_dir.root().join(".git").join("config"))?;
35 // Put the file in "~/my-ignores" so we also test that "~" expands to "$HOME"
36 file.write_all(b"[core]\nexcludesFile=~/my-ignores\n")?;
37 drop(file);
38 std::fs::write(
39 test_env.home_dir().join("my-ignores"),
40 "file1\nfile2\nfile3",
41 )?;
42
43 // Say in .git/info/exclude that we actually do want file2 and file3
44 let mut file = std::fs::OpenOptions::new()
45 .append(true)
46 .open(work_dir.root().join(".git").join("info").join("exclude"))?;
47 file.write_all(b"!file2\n!file3")?;
48 drop(file);
49
50 // Say in .gitignore (in the working copy) that we actually do not want file2
51 // (again)
52 work_dir.write_file(".gitignore", "file2");
53
54 // Writes some files to the working copy
55 work_dir.write_file("file0", "contents");
56 work_dir.write_file("file1", "contents");
57 work_dir.write_file("file2", "contents");
58 work_dir.write_file("file3", "contents");
59
60 let output = work_dir.run_jj(["diff", "-s"]);
61 insta::assert_snapshot!(output, @"
62 A .gitignore
63 A file0
64 A file3
65 [EOF]
66 ");
67 Ok(())
68 }
69
70 #[test]
71 fn test_gitignores_relative_excludes_file_path() -> TestResult {
72 let test_env = TestEnvironment::default();
73 let work_dir = test_env.work_dir("repo");
74 test_env
75 .run_jj_in(".", ["git", "init", "--colocate", "repo"])
76 .success();
77
78 let mut file = std::fs::OpenOptions::new()
79 .append(true)
80 .open(work_dir.root().join(".git").join("config"))?;
81 file.write_all(b"[core]\nexcludesFile=../my-ignores\n")?;
82 drop(file);
83 std::fs::write(test_env.env_root().join("my-ignores"), "ignored\n")?;
84
85 work_dir.write_file("ignored", "");
86 work_dir.write_file("not-ignored", "");
87
88 // core.excludesFile should be resolved relative to the workspace root, not
89 // to the cwd.
90 let sub_dir = work_dir.create_dir("sub");
91 let output = sub_dir.run_jj(["diff", "-s"]);
92 insta::assert_snapshot!(output.normalize_backslash(), @"
93 A ../not-ignored
94 [EOF]
95 ");
96 let output = test_env.run_jj_in(".", ["-Rrepo", "diff", "-s"]);
97 insta::assert_snapshot!(output.normalize_backslash(), @"
98 A repo/not-ignored
99 [EOF]
100 ");
101 Ok(())
102 }
103
104 #[test]
105 fn test_gitignores_ignored_file_in_target_commit() {
106 let test_env = TestEnvironment::default();
107 let work_dir = test_env.work_dir("repo");
108 git::init(work_dir.root());
109 work_dir
110 .run_jj(["git", "init", "--git-repo", "."])
111 .success();
112
113 // Create a commit with file "ignored" in it
114 work_dir.write_file("ignored", "committed contents\n");
115 work_dir
116 .run_jj(["bookmark", "create", "-r@", "with-file"])
117 .success();
118 let target_commit_id = work_dir
119 .run_jj(["log", "--no-graph", "-T=commit_id", "-r=@"])
120 .success()
121 .stdout
122 .into_raw();
123
124 // Create another commit where we ignore that path
125 work_dir.run_jj(["new", "root()"]).success();
126 work_dir.write_file("ignored", "contents in working copy\n");
127 work_dir.write_file(".gitignore", ".gitignore\nignored\n");
128
129 // Update to the commit with the "ignored" file
130 let output = work_dir.run_jj(["edit", "with-file"]);
131 insta::assert_snapshot!(output, @"
132 ------- stderr -------
133 Working copy (@) now at: qpvuntsm 3cf51c1a with-file | (no description set)
134 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
135 Added 1 files, modified 0 files, removed 0 files
136 Warning: 1 of those updates were skipped because there were conflicting changes in the working copy.
137 Hint: Inspect the changes compared to the intended target with `jj diff --from 3cf51c1acf24`.
138 Discard the conflicting changes with `jj restore --from 3cf51c1acf24`.
139 [EOF]
140 ");
141 let output = work_dir.run_jj(["diff", "--git", "--from", &target_commit_id]);
142 insta::assert_snapshot!(output, @"
143 diff --git a/ignored b/ignored
144 index 8a69467466..4d9be5127b 100644
145 --- a/ignored
146 +++ b/ignored
147 @@ -1,1 +1,1 @@
148 -committed contents
149 +contents in working copy
150 [EOF]
151 ");
152 }