-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest_remove_overlapping_paths.rs
More file actions
263 lines (242 loc) · 7.33 KB
/
test_remove_overlapping_paths.rs
File metadata and controls
263 lines (242 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use super::{Api, remove_overlapping_paths};
use normalize_path::NormalizePath;
use pipe_trait::Pipe;
use pretty_assertions::assert_eq;
use std::{convert::Infallible, path::PathBuf};
const MOCKED_CURRENT_DIR: &str = "/home/user/current-dir";
const MOCKED_SYMLINKS: &[(&str, &str)] = &[
("/home/user/current-dir/link-to-current-dir", "."),
("/home/user/current-dir/link-to-parent-dir", ".."),
("/home/user/current-dir/link-to-root", "/"),
("/home/user/current-dir/link-to-bin", "/usr/bin"),
("/home/user/current-dir/link-to-foo", "foo"),
("/home/user/current-dir/link-to-bar", "bar"),
("/home/user/current-dir/link-to-012", "0/1/2"),
];
fn resolve_symlink(absolute_path: PathBuf) -> PathBuf {
assert!(
absolute_path.is_absolute(),
"absolute_path should be absolute: {absolute_path:?}",
);
for &(link_path, link_target) in MOCKED_SYMLINKS {
let link_path = PathBuf::from(link_path);
assert!(
link_path.is_absolute(),
"link_path should be absolute: {link_path:?}",
);
let Some(parent) = link_path.parent() else {
panic!("Cannot get parent of {link_path:?}");
};
if let Ok(suffix) = absolute_path.strip_prefix(&link_path) {
return parent
.join(link_target)
.join(suffix)
.normalize()
.pipe(resolve_symlink);
}
}
absolute_path
}
/// Mocked implementation of [`Api`] for testing purposes.
struct MockedApi;
impl Api for MockedApi {
type Argument = &'static str;
type RealPath = PathBuf;
type RealPathError = Infallible;
fn canonicalize(path: &Self::Argument) -> Result<Self::RealPath, Self::RealPathError> {
MOCKED_CURRENT_DIR
.pipe(PathBuf::from)
.join(path)
.normalize()
.pipe(resolve_symlink)
.pipe(Ok)
}
fn is_real_dir(path: &Self::Argument) -> bool {
let path = MOCKED_CURRENT_DIR.pipe(PathBuf::from).join(path);
MOCKED_SYMLINKS
.iter()
.all(|(link, _)| PathBuf::from(link).normalize() != path)
}
fn starts_with(path: &Self::RealPath, prefix: &Self::RealPath) -> bool {
path.starts_with(prefix)
}
}
#[test]
#[cfg_attr(not(unix), ignore = "only one path separator style is tested")]
fn remove_nothing() {
let original = vec!["foo", "bar", "abc/def", "0/1/2"];
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = original;
assert_eq!(actual, expected);
}
#[test]
#[cfg_attr(not(unix), ignore = "only one path separator style is tested")]
fn remove_duplicated_arguments() {
let original = dbg!(vec![
"foo",
"bar",
"abc/def",
"foo",
"0/1/2",
"./bar",
"./abc/./def",
]);
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = vec!["foo", "bar", "abc/def", "0/1/2"];
assert_eq!(actual, expected);
let original = dbg!(vec![
"foo",
"./bar",
"bar",
"./abc/./def",
"abc/def",
"foo",
"0/1/2",
]);
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = vec!["foo", "./bar", "./abc/./def", "0/1/2"];
assert_eq!(actual, expected);
}
#[test]
#[cfg_attr(not(unix), ignore = "only one path separator style is tested")]
fn remove_overlapping_sub_paths() {
let original = vec![
"foo/child",
"foo",
"bar",
"abc/def",
"0/1/2",
"bar/child",
"0/1/2/3",
];
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = vec!["foo", "bar", "abc/def", "0/1/2"];
assert_eq!(actual, expected);
}
#[test]
#[cfg_attr(not(unix), ignore = "only one path separator style is tested")]
fn remove_all_except_current_dir() {
let original = dbg!(vec!["foo", "bar", ".", "abc/def", "0/1/2"]);
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = vec!["."];
assert_eq!(actual, expected);
let original = dbg!(vec![
"foo",
"bar",
".",
"abc/def",
"0/1/2",
MOCKED_CURRENT_DIR,
]);
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = vec!["."];
assert_eq!(actual, expected);
let original = dbg!(vec![
"foo",
"bar",
MOCKED_CURRENT_DIR,
".",
"abc/def",
"0/1/2",
]);
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = vec![MOCKED_CURRENT_DIR];
assert_eq!(actual, expected);
}
#[test]
#[cfg_attr(not(unix), ignore = "only one path separator style is tested")]
fn remove_all_except_parent_dir() {
let original = dbg!(vec!["foo", "bar", "..", "abc/def", ".", "0/1/2"]);
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = vec![".."];
assert_eq!(actual, expected);
let original = dbg!(vec![
"foo",
"/home/user",
"bar",
"..",
"abc/def",
".",
"0/1/2",
]);
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = vec!["/home/user"];
assert_eq!(actual, expected);
}
#[test]
#[cfg_attr(not(unix), ignore = "only one path separator style is tested")]
fn remove_overlapping_real_paths() {
let original = dbg!(vec![
"foo",
"bar",
"abc/def",
"link-to-foo/child",
"link-to-bar/a/b/c",
"0/1/2",
]);
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = vec!["foo", "bar", "abc/def", "0/1/2"];
assert_eq!(actual, expected);
let original = dbg!(vec![
"link-to-foo/child",
"link-to-bar/a/b/c",
"foo",
"bar",
"abc/def",
"0/1/2",
]);
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = vec!["foo", "bar", "abc/def", "0/1/2"];
assert_eq!(actual, expected);
let original = dbg!(vec![
"link-to-current-dir/foo",
"foo",
"bar",
"abc/def",
"link-to-current-dir/bar",
"0/1/2",
]);
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = vec!["link-to-current-dir/foo", "bar", "abc/def", "0/1/2"];
assert_eq!(actual, expected);
}
#[test]
#[cfg_attr(not(unix), ignore = "only one path separator style is tested")]
fn do_not_remove_symlinks() {
let original = dbg!(vec![
"foo",
"bar",
"abc/def",
"link-to-foo",
"link-to-bar",
"0/1/2",
]);
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = original;
assert_eq!(actual, expected);
let original = dbg!(vec![
"foo/child",
"bar",
"abc/def",
"link-to-foo",
"link-to-bar",
"0/1/2",
]);
let mut actual = original.clone();
remove_overlapping_paths::<MockedApi>(&mut actual);
let expected = original;
assert_eq!(actual, expected);
}