just playing with tangled
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
15use jj_lib::matchers::{EverythingMatcher, FilesMatcher};
16use jj_lib::repo_path::RepoPath;
17use jj_lib::tree::DiffSummary;
18use test_case::test_case;
19use testutils::TestRepo;
20
21#[test_case(false ; "local backend")]
22#[test_case(true ; "git backend")]
23fn test_types(use_git: bool) {
24 let test_repo = TestRepo::init(use_git);
25 let repo = &test_repo.repo;
26
27 let clean_path = RepoPath::from_internal_string("clean");
28 let modified_path = RepoPath::from_internal_string("modified");
29 let added_path = RepoPath::from_internal_string("added");
30 let removed_path = RepoPath::from_internal_string("removed");
31
32 let tree1 = testutils::create_tree(
33 repo,
34 &[
35 (&clean_path, "clean"),
36 (&modified_path, "contents before"),
37 (&removed_path, "removed contents"),
38 ],
39 );
40
41 let tree2 = testutils::create_tree(
42 repo,
43 &[
44 (&clean_path, "clean"),
45 (&modified_path, "contents after"),
46 (&added_path, "added contents"),
47 ],
48 );
49
50 assert_eq!(
51 tree1.diff_summary(&tree2, &EverythingMatcher),
52 DiffSummary {
53 modified: vec![modified_path],
54 added: vec![added_path],
55 removed: vec![removed_path]
56 }
57 );
58}
59
60#[test_case(false ; "local backend")]
61#[test_case(true ; "git backend")]
62fn test_tree_file_transition(use_git: bool) {
63 let test_repo = TestRepo::init(use_git);
64 let repo = &test_repo.repo;
65
66 let dir_file_path = RepoPath::from_internal_string("dir/file");
67 let dir_path = RepoPath::from_internal_string("dir");
68
69 let tree1 = testutils::create_tree(repo, &[(&dir_file_path, "contents")]);
70 let tree2 = testutils::create_tree(repo, &[(&dir_path, "contents")]);
71
72 assert_eq!(
73 tree1.diff_summary(&tree2, &EverythingMatcher),
74 DiffSummary {
75 modified: vec![],
76 added: vec![dir_path.clone()],
77 removed: vec![dir_file_path.clone()]
78 }
79 );
80 assert_eq!(
81 tree2.diff_summary(&tree1, &EverythingMatcher),
82 DiffSummary {
83 modified: vec![],
84 added: vec![dir_file_path],
85 removed: vec![dir_path]
86 }
87 );
88}
89
90#[test_case(false ; "local backend")]
91#[test_case(true ; "git backend")]
92fn test_sorting(use_git: bool) {
93 let test_repo = TestRepo::init(use_git);
94 let repo = &test_repo.repo;
95
96 let a_path = RepoPath::from_internal_string("a");
97 let b_path = RepoPath::from_internal_string("b");
98 let f_a_path = RepoPath::from_internal_string("f/a");
99 let f_b_path = RepoPath::from_internal_string("f/b");
100 let f_f_a_path = RepoPath::from_internal_string("f/f/a");
101 let f_f_b_path = RepoPath::from_internal_string("f/f/b");
102 let n_path = RepoPath::from_internal_string("n");
103 let s_b_path = RepoPath::from_internal_string("s/b");
104 let z_path = RepoPath::from_internal_string("z");
105
106 let tree1 = testutils::create_tree(
107 repo,
108 &[
109 (&a_path, "before"),
110 (&f_a_path, "before"),
111 (&f_f_a_path, "before"),
112 ],
113 );
114
115 let tree2 = testutils::create_tree(
116 repo,
117 &[
118 (&a_path, "after"),
119 (&b_path, "after"),
120 (&f_a_path, "after"),
121 (&f_b_path, "after"),
122 (&f_f_a_path, "after"),
123 (&f_f_b_path, "after"),
124 (&n_path, "after"),
125 (&s_b_path, "after"),
126 (&z_path, "after"),
127 ],
128 );
129
130 assert_eq!(
131 tree1.diff_summary(&tree2, &EverythingMatcher),
132 DiffSummary {
133 modified: vec![a_path.clone(), f_a_path.clone(), f_f_a_path.clone()],
134 added: vec![
135 b_path.clone(),
136 f_b_path.clone(),
137 f_f_b_path.clone(),
138 n_path.clone(),
139 s_b_path.clone(),
140 z_path.clone(),
141 ],
142 removed: vec![]
143 }
144 );
145 assert_eq!(
146 tree2.diff_summary(&tree1, &EverythingMatcher),
147 DiffSummary {
148 modified: vec![a_path, f_a_path, f_f_a_path],
149 added: vec![],
150 removed: vec![b_path, f_b_path, f_f_b_path, n_path, s_b_path, z_path]
151 }
152 );
153}
154
155#[test_case(false ; "local backend")]
156#[test_case(true ; "git backend")]
157fn test_matcher_dir_file_transition(use_git: bool) {
158 let test_repo = TestRepo::init(use_git);
159 let repo = &test_repo.repo;
160
161 let a_path = RepoPath::from_internal_string("a");
162 let a_a_path = RepoPath::from_internal_string("a/a");
163
164 let tree1 = testutils::create_tree(repo, &[(&a_path, "before")]);
165 let tree2 = testutils::create_tree(repo, &[(&a_a_path, "after")]);
166
167 let matcher = FilesMatcher::new(&[a_path.clone()]);
168 assert_eq!(
169 tree1.diff_summary(&tree2, &matcher),
170 DiffSummary {
171 modified: vec![],
172 added: vec![],
173 removed: vec![a_path.clone()]
174 }
175 );
176 assert_eq!(
177 tree2.diff_summary(&tree1, &matcher),
178 DiffSummary {
179 modified: vec![],
180 added: vec![a_path.clone()],
181 removed: vec![]
182 }
183 );
184
185 let matcher = FilesMatcher::new(&[a_a_path.clone()]);
186 assert_eq!(
187 tree1.diff_summary(&tree2, &matcher),
188 DiffSummary {
189 modified: vec![],
190 added: vec![a_a_path.clone()],
191 removed: vec![]
192 }
193 );
194 assert_eq!(
195 tree2.diff_summary(&tree1, &matcher),
196 DiffSummary {
197 modified: vec![],
198 added: vec![],
199 removed: vec![a_a_path.clone()]
200 }
201 );
202
203 let matcher = FilesMatcher::new(&[a_path.clone(), a_a_path.clone()]);
204 assert_eq!(
205 tree1.diff_summary(&tree2, &matcher),
206 DiffSummary {
207 modified: vec![],
208 added: vec![a_a_path.clone()],
209 removed: vec![a_path.clone()]
210 }
211 );
212 assert_eq!(
213 tree2.diff_summary(&tree1, &matcher),
214 DiffSummary {
215 modified: vec![],
216 added: vec![a_path],
217 removed: vec![a_a_path]
218 }
219 );
220}
221
222#[test_case(false ; "local backend")]
223#[test_case(true ; "git backend")]
224fn test_matcher_normal_cases(use_git: bool) {
225 let test_repo = TestRepo::init(use_git);
226 let repo = &test_repo.repo;
227
228 let a_path = RepoPath::from_internal_string("a");
229 let dir1_a_path = RepoPath::from_internal_string("dir1/a");
230 let dir2_b_path = RepoPath::from_internal_string("dir2/b");
231 let z_path = RepoPath::from_internal_string("z");
232
233 let tree1 = testutils::create_tree(repo, &[(&a_path, "before"), (&dir1_a_path, "before")]);
234 // File "a" gets modified
235 // File "dir1/a" gets modified
236 // File "dir2/b" gets created
237 // File "z" gets created
238 let tree2 = testutils::create_tree(
239 repo,
240 &[
241 (&a_path, "after"),
242 (&dir1_a_path, "after"),
243 (&dir2_b_path, "after"),
244 (&z_path, "after"),
245 ],
246 );
247
248 let matcher = FilesMatcher::new(&[a_path.clone(), z_path.clone()]);
249 assert_eq!(
250 tree1.diff_summary(&tree2, &matcher),
251 DiffSummary {
252 modified: vec![a_path.clone()],
253 added: vec![z_path.clone()],
254 removed: vec![]
255 }
256 );
257 assert_eq!(
258 tree2.diff_summary(&tree1, &matcher),
259 DiffSummary {
260 modified: vec![a_path],
261 added: vec![],
262 removed: vec![z_path]
263 }
264 );
265
266 let matcher = FilesMatcher::new(&[dir1_a_path.clone(), dir2_b_path.clone()]);
267 assert_eq!(
268 tree1.diff_summary(&tree2, &matcher),
269 DiffSummary {
270 modified: vec![dir1_a_path.clone()],
271 added: vec![dir2_b_path.clone()],
272 removed: vec![]
273 }
274 );
275 assert_eq!(
276 tree2.diff_summary(&tree1, &matcher),
277 DiffSummary {
278 modified: vec![dir1_a_path],
279 added: vec![],
280 removed: vec![dir2_b_path]
281 }
282 );
283}