-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
posixfs_tests.rs
473 lines (434 loc) · 12.9 KB
/
posixfs_tests.rs
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).
use std::path::{Path, PathBuf};
use std::sync::Arc;
use hashing::EMPTY_DIGEST;
use testutil::make_file;
use crate::{
DigestTrie, Dir, DirectoryListing, File, GitignoreStyleExcludes, GlobExpansionConjunction,
GlobMatching, Link, PathGlobs, PathStat, PathStatGetter, PosixFS, Stat, StrictGlobMatching,
SymlinkBehavior, TypedPath, Vfs,
};
#[tokio::test]
async fn is_executable_false() {
let dir = tempfile::TempDir::new().unwrap();
make_file(&dir.path().join("marmosets"), &[], 0o611);
assert_only_file_is_executable(dir.path(), false).await;
}
#[tokio::test]
async fn is_executable_true() {
let dir = tempfile::TempDir::new().unwrap();
make_file(&dir.path().join("photograph_marmosets"), &[], 0o700);
assert_only_file_is_executable(dir.path(), true).await;
}
#[tokio::test]
async fn file_path() {
let dir = tempfile::TempDir::new().unwrap();
let path = PathBuf::from("marmosets");
let fs = new_posixfs(&dir.path());
let expected_path = std::fs::canonicalize(dir.path()).unwrap().join(&path);
let actual_path = fs.file_path(&File {
path: path.clone(),
is_executable: false,
});
assert_eq!(actual_path, expected_path);
}
#[tokio::test]
async fn stat_executable_file() {
let dir = tempfile::TempDir::new().unwrap();
let posix_fs = new_posixfs(&dir.path());
let path = PathBuf::from("photograph_marmosets");
make_file(&dir.path().join(&path), &[], 0o700);
assert_eq!(
posix_fs.stat_sync(path.clone()).unwrap().unwrap(),
super::Stat::File(File {
path: path,
is_executable: true,
})
)
}
#[tokio::test]
async fn stat_nonexecutable_file() {
let dir = tempfile::TempDir::new().unwrap();
let posix_fs = new_posixfs(&dir.path());
let path = PathBuf::from("marmosets");
make_file(&dir.path().join(&path), &[], 0o600);
assert_eq!(
posix_fs.stat_sync(path.clone()).unwrap().unwrap(),
super::Stat::File(File {
path: path,
is_executable: false,
})
)
}
#[tokio::test]
async fn stat_dir() {
let dir = tempfile::TempDir::new().unwrap();
let posix_fs = new_posixfs(&dir.path());
let path = PathBuf::from("enclosure");
std::fs::create_dir(dir.path().join(&path)).unwrap();
assert_eq!(
posix_fs.stat_sync(path.clone()).unwrap().unwrap(),
super::Stat::Dir(Dir(path))
)
}
#[tokio::test]
async fn stat_symlink() {
let dir = tempfile::TempDir::new().unwrap();
let posix_fs = new_posixfs(&dir.path());
let path = PathBuf::from("marmosets");
make_file(&dir.path().join(&path), &[], 0o600);
let link_path = PathBuf::from("remarkably_similar_marmoset");
std::os::unix::fs::symlink(&dir.path().join(path.clone()), dir.path().join(&link_path)).unwrap();
assert_eq!(
posix_fs.stat_sync(link_path.clone()).unwrap().unwrap(),
super::Stat::Link(Link {
path: link_path,
target: dir.path().join(path)
})
)
}
#[tokio::test]
async fn stat_symlink_oblivious() {
let dir = tempfile::TempDir::new().unwrap();
let posix_fs = new_posixfs_symlink_oblivious(&dir.path());
let path = PathBuf::from("marmosets");
make_file(&dir.path().join(&path), &[], 0o600);
let link_path = PathBuf::from("remarkably_similar_marmoset");
std::os::unix::fs::symlink(&dir.path().join(path), dir.path().join(&link_path)).unwrap();
// Symlink oblivious stat will give us the destination type.
assert_eq!(
posix_fs.stat_sync(link_path.clone()).unwrap().unwrap(),
super::Stat::File(File {
path: link_path,
is_executable: false,
})
)
}
#[tokio::test]
async fn stat_other() {
assert_eq!(
new_posixfs("/dev")
.stat_sync(PathBuf::from("null"))
.unwrap(),
None,
);
}
#[tokio::test]
async fn stat_missing() {
let dir = tempfile::TempDir::new().unwrap();
let posix_fs = new_posixfs(&dir.path());
assert_eq!(
posix_fs.stat_sync(PathBuf::from("no_marmosets")).unwrap(),
None,
);
}
#[tokio::test]
async fn scandir_empty() {
let dir = tempfile::TempDir::new().unwrap();
let posix_fs = new_posixfs(&dir.path());
let path = PathBuf::from("empty_enclosure");
std::fs::create_dir(dir.path().join(&path)).unwrap();
assert_eq!(
posix_fs.scandir(Dir(path)).await.unwrap(),
DirectoryListing(vec![])
);
}
#[tokio::test]
async fn scandir() {
let dir = tempfile::TempDir::new().unwrap();
let path = PathBuf::from("enclosure");
std::fs::create_dir(dir.path().join(&path)).unwrap();
let a_marmoset = path.join("a_marmoset");
let feed = path.join("feed");
let hammock = path.join("hammock");
let remarkably_similar_marmoset = path.join("remarkably_similar_marmoset");
let sneaky_marmoset = path.join("sneaky_marmoset");
make_file(&dir.path().join(&feed), &[], 0o700);
make_file(&dir.path().join(&a_marmoset), &[], 0o600);
make_file(&dir.path().join(&sneaky_marmoset), &[], 0o600);
std::os::unix::fs::symlink(
&dir.path().join(&a_marmoset),
dir
.path()
.join(&dir.path().join(&remarkably_similar_marmoset)),
)
.unwrap();
std::fs::create_dir(dir.path().join(&hammock)).unwrap();
make_file(
&dir.path().join(&hammock).join("napping_marmoset"),
&[],
0o600,
);
// Symlink aware.
assert_eq!(
new_posixfs(&dir.path())
.scandir(Dir(path.clone()))
.await
.unwrap(),
DirectoryListing(vec![
Stat::File(File {
path: a_marmoset.clone(),
is_executable: false,
}),
Stat::File(File {
path: feed.clone(),
is_executable: true,
}),
Stat::Dir(Dir(hammock.clone())),
Stat::Link(Link {
path: remarkably_similar_marmoset.clone(),
target: dir.path().join(&a_marmoset)
}),
Stat::File(File {
path: sneaky_marmoset.clone(),
is_executable: false,
}),
])
);
// Symlink oblivious.
assert_eq!(
new_posixfs_symlink_oblivious(&dir.path())
.scandir(Dir(path))
.await
.unwrap(),
DirectoryListing(vec![
Stat::File(File {
path: a_marmoset,
is_executable: false,
}),
Stat::File(File {
path: feed,
is_executable: true,
}),
Stat::Dir(Dir(hammock)),
Stat::File(File {
path: remarkably_similar_marmoset,
is_executable: false,
}),
Stat::File(File {
path: sneaky_marmoset,
is_executable: false,
}),
])
);
}
#[tokio::test]
async fn scandir_missing() {
let dir = tempfile::TempDir::new().unwrap();
let posix_fs = new_posixfs(&dir.path());
posix_fs
.scandir(Dir(PathBuf::from("no_marmosets_here")))
.await
.expect_err("Want error");
}
#[tokio::test]
async fn path_stats_for_paths() {
let dir = tempfile::TempDir::new().unwrap();
let root_path = dir.path();
// File tree:
// dir
// dir/recursive_symlink -> ../symlink -> executable_file
// dir_symlink -> dir
// executable_file
// regular_file
// symlink -> executable_file
// symlink_to_nothing -> doesnotexist
make_file(&root_path.join("executable_file"), &[], 0o700);
make_file(&root_path.join("regular_file"), &[], 0o600);
std::fs::create_dir(&root_path.join("dir")).unwrap();
std::os::unix::fs::symlink("executable_file", &root_path.join("symlink")).unwrap();
std::os::unix::fs::symlink(
"../symlink",
&root_path.join("dir").join("recursive_symlink"),
)
.unwrap();
std::os::unix::fs::symlink("dir", &root_path.join("dir_symlink")).unwrap();
std::os::unix::fs::symlink("doesnotexist", &root_path.join("symlink_to_nothing")).unwrap();
let posix_fs = Arc::new(new_posixfs(&root_path));
let path_stats = posix_fs
.path_stats(vec![
PathBuf::from("executable_file"),
PathBuf::from("regular_file"),
PathBuf::from("dir"),
PathBuf::from("symlink"),
PathBuf::from("dir").join("recursive_symlink"),
PathBuf::from("dir_symlink"),
PathBuf::from("symlink_to_nothing"),
PathBuf::from("doesnotexist"),
])
.await
.unwrap();
let v: Vec<Option<PathStat>> = vec![
Some(PathStat::file(
PathBuf::from("executable_file"),
File {
path: PathBuf::from("executable_file"),
is_executable: true,
},
)),
Some(PathStat::file(
PathBuf::from("regular_file"),
File {
path: PathBuf::from("regular_file"),
is_executable: false,
},
)),
Some(PathStat::dir(
PathBuf::from("dir"),
Dir(PathBuf::from("dir")),
)),
Some(PathStat::file(
PathBuf::from("symlink"),
File {
path: PathBuf::from("executable_file"),
is_executable: true,
},
)),
Some(PathStat::file(
PathBuf::from("dir").join("recursive_symlink"),
File {
path: PathBuf::from("executable_file"),
is_executable: true,
},
)),
Some(PathStat::dir(
PathBuf::from("dir_symlink"),
Dir(PathBuf::from("dir")),
)),
None,
None,
];
assert_eq!(v, path_stats);
}
#[tokio::test]
async fn memfs_expand_basic() {
// Create two files, with the effect that there is a nested directory for the longer path.
let p1 = PathBuf::from("some/file");
let p2 = PathBuf::from("some/other");
let p3 = p2.join("file");
let fs = DigestTrie::from_unique_paths(
vec![
TypedPath::File {
path: &p1,
is_executable: false,
},
TypedPath::File {
path: &p3,
is_executable: false,
},
],
&vec![(p1.clone(), EMPTY_DIGEST), (p3.clone(), EMPTY_DIGEST)]
.into_iter()
.collect(),
)
.unwrap();
let globs = PathGlobs::new(
vec!["some/*".into()],
StrictGlobMatching::Ignore,
GlobExpansionConjunction::AnyMatch,
)
.parse()
.unwrap();
assert_eq!(
fs.expand_globs(globs, SymlinkBehavior::Oblivious, None)
.await
.unwrap(),
vec![
PathStat::file(
p1.clone(),
File {
path: p1,
is_executable: false,
},
),
PathStat::dir(p2.clone(), Dir(p2)),
],
);
}
async fn assert_only_file_is_executable(path: &Path, want_is_executable: bool) {
let fs = new_posixfs(path);
let stats = fs.scandir(Dir(PathBuf::from("."))).await.unwrap();
assert_eq!(stats.0.len(), 1);
match stats.0.get(0).unwrap() {
&super::Stat::File(File {
is_executable: got, ..
}) => assert_eq!(want_is_executable, got),
other => panic!("Expected file, got {:?}", other),
}
}
fn new_posixfs<P: AsRef<Path>>(dir: P) -> PosixFS {
PosixFS::new(
dir.as_ref(),
GitignoreStyleExcludes::empty(),
task_executor::Executor::new(),
)
.unwrap()
}
fn new_posixfs_symlink_oblivious<P: AsRef<Path>>(dir: P) -> PosixFS {
PosixFS::new_with_symlink_behavior(
dir.as_ref(),
GitignoreStyleExcludes::empty(),
task_executor::Executor::new(),
SymlinkBehavior::Oblivious,
)
.unwrap()
}
async fn read_mock_files(input: Vec<PathBuf>, posix_fs: &Arc<PosixFS>) -> Vec<Stat> {
let path_stats = posix_fs.path_stats(input).await.unwrap();
path_stats
.into_iter()
.map(|item| {
let path_stat: PathStat = item.unwrap();
match path_stat {
PathStat::Dir { stat, .. } => Stat::Dir(stat),
PathStat::Link { stat, .. } => Stat::Link(stat),
PathStat::File { stat, .. } => Stat::File(stat),
}
})
.collect()
}
#[tokio::test]
async fn test_basic_gitignore_functionality() {
let root = tempfile::TempDir::new().unwrap();
let root_path = root.path();
let bytes = "content".as_bytes();
make_file(&root_path.join("non-ignored"), bytes, 0o700);
make_file(&root_path.join("ignored-file.tmp"), bytes, 0o700);
make_file(&root_path.join("important.x"), bytes, 0o700);
make_file(&root_path.join("unimportant.x"), bytes, 0o700);
let gitignore_content = "*.tmp\n!*.x";
let gitignore_path = root_path.join(".gitignore");
make_file(&gitignore_path, gitignore_content.as_bytes(), 0o700);
let executor = task_executor::Executor::new();
let ignorer =
GitignoreStyleExcludes::create_with_gitignore_file(vec![], Some(gitignore_path.clone()))
.unwrap();
let posix_fs = Arc::new(PosixFS::new(root.as_ref(), ignorer, executor).unwrap());
let stats = read_mock_files(
vec![
PathBuf::from("non-ignored"),
PathBuf::from("ignored-file.tmp"),
PathBuf::from("important.x"),
PathBuf::from("unimportant.x"),
],
&posix_fs,
)
.await;
assert!(!posix_fs.is_ignored(&stats[0]));
assert!(posix_fs.is_ignored(&stats[1]));
assert!(!posix_fs.is_ignored(&stats[2]));
assert!(!posix_fs.is_ignored(&stats[3]));
// Test that .gitignore files work in tandem with explicit ignores.
let executor = task_executor::Executor::new();
let ignorer = GitignoreStyleExcludes::create_with_gitignore_file(
vec!["unimportant.x".to_string()],
Some(gitignore_path),
)
.unwrap();
let posix_fs_2 = Arc::new(PosixFS::new(root.as_ref(), ignorer, executor).unwrap());
assert!(!posix_fs_2.is_ignored(&stats[0]));
assert!(posix_fs_2.is_ignored(&stats[1]));
assert!(!posix_fs_2.is_ignored(&stats[2]));
assert!(posix_fs_2.is_ignored(&stats[3]));
}