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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use lsp_types::Uri;
use pretty_assertions::assert_eq;
use std::borrow::Cow;
use std::collections::HashSet;
use std::ffi::OsStr;
use std::fs;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::str::FromStr;
use std::sync::Arc;
use anyhow::Context;
use serde::de::DeserializeOwned;
use serde::Serialize;
use url::Url;
use crate::assertions::assert_wildcard_match;
use crate::testdata_path;
/// Represents a path on the file system, which can be used
/// to perform specific actions.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct PathRef(PathBuf);
impl AsRef<Path> for PathRef {
fn as_ref(&self) -> &Path {
self.as_path()
}
}
impl AsRef<OsStr> for PathRef {
fn as_ref(&self) -> &OsStr {
self.as_path().as_ref()
}
}
impl std::fmt::Display for PathRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_path().display())
}
}
impl PathRef {
pub fn new(path: impl AsRef<Path>) -> Self {
Self(path.as_ref().to_path_buf())
}
pub fn parent(&self) -> PathRef {
PathRef(self.as_path().parent().unwrap().to_path_buf())
}
pub fn url_dir(&self) -> Url {
Url::from_directory_path(self.as_path()).unwrap()
}
pub fn url_file(&self) -> Url {
Url::from_file_path(self.as_path()).unwrap()
}
pub fn uri_dir(&self) -> Uri {
Uri::from_str(self.url_dir().as_str()).unwrap()
}
pub fn uri_file(&self) -> Uri {
Uri::from_str(self.url_file().as_str()).unwrap()
}
pub fn as_path(&self) -> &Path {
self.0.as_path()
}
pub fn to_path_buf(&self) -> PathBuf {
self.0.to_path_buf()
}
pub fn to_string_lossy(&self) -> Cow<str> {
self.0.to_string_lossy()
}
pub fn exists(&self) -> bool {
self.0.exists()
}
pub fn try_exists(&self) -> std::io::Result<bool> {
self.0.try_exists()
}
pub fn is_dir(&self) -> bool {
self.0.is_dir()
}
pub fn is_file(&self) -> bool {
self.0.is_file()
}
pub fn join(&self, path: impl AsRef<Path>) -> PathRef {
PathRef(self.as_path().join(path))
}
pub fn with_extension(&self, ext: impl AsRef<OsStr>) -> PathRef {
PathRef(self.as_path().with_extension(ext))
}
pub fn canonicalize(&self) -> PathRef {
PathRef(strip_unc_prefix(self.as_path().canonicalize().unwrap()))
}
pub fn create_dir_all(&self) {
fs::create_dir_all(self).unwrap();
}
pub fn remove_file(&self) {
fs::remove_file(self).unwrap();
}
pub fn remove_dir_all(&self) {
fs::remove_dir_all(self).unwrap();
}
pub fn read_to_string(&self) -> String {
self.read_to_string_if_exists().unwrap()
}
pub fn read_to_string_if_exists(&self) -> Result<String, anyhow::Error> {
fs::read_to_string(self)
.with_context(|| format!("Could not read file: {}", self))
}
pub fn read_to_bytes_if_exists(&self) -> Result<Vec<u8>, anyhow::Error> {
fs::read(self).with_context(|| format!("Could not read file: {}", self))
}
#[track_caller]
pub fn read_json<TValue: DeserializeOwned>(&self) -> TValue {
serde_json::from_str(&self.read_to_string())
.with_context(|| format!("Failed deserializing: {}", self))
.unwrap()
}
#[track_caller]
pub fn read_json_value(&self) -> serde_json::Value {
serde_json::from_str(&self.read_to_string())
.with_context(|| format!("Failed deserializing: {}", self))
.unwrap()
}
#[track_caller]
pub fn read_jsonc_value(&self) -> serde_json::Value {
jsonc_parser::parse_to_serde_value(
&self.read_to_string(),
&Default::default(),
)
.with_context(|| format!("Failed to parse {}", self))
.unwrap()
.unwrap_or_else(|| panic!("JSON file was empty for {}", self))
}
#[track_caller]
pub fn rename(&self, to: impl AsRef<Path>) {
fs::rename(self, self.join(to)).unwrap();
}
#[track_caller]
pub fn append(&self, text: impl AsRef<str>) {
let mut file = OpenOptions::new().append(true).open(self).unwrap();
file.write_all(text.as_ref().as_bytes()).unwrap();
}
#[track_caller]
pub fn write(&self, text: impl AsRef<[u8]>) {
fs::write(self, text).unwrap();
}
#[track_caller]
pub fn write_json<TValue: Serialize>(&self, value: &TValue) {
let text = serde_json::to_string_pretty(value).unwrap();
self.write(text);
}
#[track_caller]
pub fn symlink_dir(
&self,
oldpath: impl AsRef<Path>,
newpath: impl AsRef<Path>,
) {
#[cfg(unix)]
{
use std::os::unix::fs::symlink;
symlink(self.as_path().join(oldpath), self.as_path().join(newpath))
.unwrap();
}
#[cfg(not(unix))]
{
use std::os::windows::fs::symlink_dir;
symlink_dir(self.as_path().join(oldpath), self.as_path().join(newpath))
.unwrap();
}
}
#[track_caller]
pub fn symlink_file(
&self,
oldpath: impl AsRef<Path>,
newpath: impl AsRef<Path>,
) {
#[cfg(unix)]
{
use std::os::unix::fs::symlink;
symlink(self.as_path().join(oldpath), self.as_path().join(newpath))
.unwrap();
}
#[cfg(not(unix))]
{
use std::os::windows::fs::symlink_file;
symlink_file(self.as_path().join(oldpath), self.as_path().join(newpath))
.unwrap();
}
}
#[track_caller]
pub fn read_dir(&self) -> fs::ReadDir {
fs::read_dir(self.as_path())
.with_context(|| format!("Reading {}", self.as_path().display()))
.unwrap()
}
#[track_caller]
pub fn copy(&self, to: &impl AsRef<Path>) {
std::fs::copy(self.as_path(), to)
.with_context(|| format!("Copying {} to {}", self, to.as_ref().display()))
.unwrap();
}
/// Copies this directory to another directory.
///
/// Note: Does not handle symlinks.
pub fn copy_to_recursive(&self, to: &PathRef) {
self.copy_to_recursive_with_exclusions(to, &HashSet::new())
}
pub fn copy_to_recursive_with_exclusions(
&self,
to: &PathRef,
file_exclusions: &HashSet<PathRef>,
) {
to.create_dir_all();
let read_dir = self.read_dir();
for entry in read_dir {
let entry = entry.unwrap();
let file_type = entry.file_type().unwrap();
let new_from = self.join(entry.file_name());
let new_to = to.join(entry.file_name());
if file_type.is_dir() {
new_from.copy_to_recursive(&new_to);
} else if file_type.is_file() && !file_exclusions.contains(&new_from) {
new_from.copy(&new_to);
}
}
}
#[track_caller]
pub fn mark_executable(&self) {
if cfg!(unix) {
Command::new("chmod").arg("+x").arg(self).output().unwrap();
}
}
#[track_caller]
pub fn make_dir_readonly(&self) {
self.create_dir_all();
if cfg!(windows) {
Command::new("attrib").arg("+r").arg(self).output().unwrap();
} else if cfg!(unix) {
Command::new("chmod").arg("555").arg(self).output().unwrap();
}
}
#[track_caller]
pub fn assert_matches_file(&self, wildcard_file: impl AsRef<Path>) -> &Self {
let wildcard_file = testdata_path().join(wildcard_file);
#[allow(clippy::print_stdout)]
{
println!("output path {}", wildcard_file);
}
let expected_text = wildcard_file.read_to_string();
self.assert_matches_text(&expected_text)
}
#[track_caller]
pub fn assert_matches_text(&self, wildcard_text: impl AsRef<str>) -> &Self {
let actual = self.read_to_string();
assert_wildcard_match(&actual, wildcard_text.as_ref());
self
}
#[track_caller]
pub fn assert_matches_json(&self, expected: serde_json::Value) {
let actual_json = self.read_json_value();
if actual_json != expected {
let actual_text = serde_json::to_string_pretty(&actual_json).unwrap();
let expected_text = serde_json::to_string_pretty(&expected).unwrap();
assert_eq!(actual_text, expected_text);
}
}
}
#[cfg(not(windows))]
#[inline]
fn strip_unc_prefix(path: PathBuf) -> PathBuf {
path
}
/// Strips the unc prefix (ex. \\?\) from Windows paths.
///
/// Lifted from deno_core for use in the tests.
#[cfg(windows)]
fn strip_unc_prefix(path: PathBuf) -> PathBuf {
use std::path::Component;
use std::path::Prefix;
let mut components = path.components();
match components.next() {
Some(Component::Prefix(prefix)) => {
match prefix.kind() {
// \\?\device
Prefix::Verbatim(device) => {
let mut path = PathBuf::new();
path.push(format!(r"\\{}\", device.to_string_lossy()));
path.extend(components.filter(|c| !matches!(c, Component::RootDir)));
path
}
// \\?\c:\path
Prefix::VerbatimDisk(_) => {
let mut path = PathBuf::new();
path.push(prefix.as_os_str().to_string_lossy().replace(r"\\?\", ""));
path.extend(components);
path
}
// \\?\UNC\hostname\share_name\path
Prefix::VerbatimUNC(hostname, share_name) => {
let mut path = PathBuf::new();
path.push(format!(
r"\\{}\{}\",
hostname.to_string_lossy(),
share_name.to_string_lossy()
));
path.extend(components.filter(|c| !matches!(c, Component::RootDir)));
path
}
_ => path,
}
}
_ => path,
}
}
enum TempDirInner {
TempDir {
path_ref: PathRef,
// kept alive for the duration of the temp dir
_dir: tempfile::TempDir,
},
Path(PathRef),
Symlinked {
symlink: Arc<TempDirInner>,
target: Arc<TempDirInner>,
},
}
impl TempDirInner {
pub fn path(&self) -> &PathRef {
match self {
Self::Path(path_ref) => path_ref,
Self::TempDir { path_ref, .. } => path_ref,
Self::Symlinked { symlink, .. } => symlink.path(),
}
}
pub fn target_path(&self) -> &PathRef {
match self {
TempDirInner::Symlinked { target, .. } => target.target_path(),
_ => self.path(),
}
}
}
impl Drop for TempDirInner {
fn drop(&mut self) {
if let Self::Path(path) = self {
_ = fs::remove_dir_all(path);
}
}
}
/// For creating temporary directories in tests.
///
/// This was done because `tempfiles::TempDir` was very slow on Windows.
///
/// Note: Do not use this in actual code as this does not protect against
/// "insecure temporary file" security vulnerabilities.
#[derive(Clone)]
pub struct TempDir(Arc<TempDirInner>);
impl Default for TempDir {
fn default() -> Self {
Self::new()
}
}
impl TempDir {
pub fn new() -> Self {
Self::new_inner(&std::env::temp_dir(), None)
}
pub fn new_with_prefix(prefix: &str) -> Self {
Self::new_inner(&std::env::temp_dir(), Some(prefix))
}
pub fn new_in(parent_dir: &Path) -> Self {
Self::new_inner(parent_dir, None)
}
pub fn new_with_path(path: &Path) -> Self {
Self(Arc::new(TempDirInner::Path(PathRef(path.to_path_buf()))))
}
pub fn new_symlinked(target: TempDir) -> Self {
let target_path = target.path();
let path = target_path.parent().join(format!(
"{}_symlinked",
target_path.as_path().file_name().unwrap().to_str().unwrap()
));
target.symlink_dir(target.path(), &path);
TempDir(Arc::new(TempDirInner::Symlinked {
target: target.0,
symlink: Self::new_with_path(path.as_path()).0,
}))
}
/// Create a new temporary directory with the given prefix as part of its name, if specified.
fn new_inner(parent_dir: &Path, prefix: Option<&str>) -> Self {
let mut builder = tempfile::Builder::new();
builder.prefix(prefix.unwrap_or("deno-cli-test"));
let dir = builder
.tempdir_in(parent_dir)
.expect("Failed to create a temporary directory");
Self(Arc::new(TempDirInner::TempDir {
path_ref: PathRef(dir.path().to_path_buf()),
_dir: dir,
}))
}
pub fn url(&self) -> Url {
Url::from_directory_path(self.path()).unwrap()
}
pub fn uri(&self) -> Uri {
Uri::from_str(self.url().as_str()).unwrap()
}
pub fn path(&self) -> &PathRef {
self.0.path()
}
/// The resolved final target path if this is a symlink.
pub fn target_path(&self) -> &PathRef {
self.0.target_path()
}
pub fn create_dir_all(&self, path: impl AsRef<Path>) {
self.target_path().join(path).create_dir_all()
}
pub fn remove_file(&self, path: impl AsRef<Path>) {
self.target_path().join(path).remove_file()
}
pub fn remove_dir_all(&self, path: impl AsRef<Path>) {
self.target_path().join(path).remove_dir_all()
}
pub fn read_to_string(&self, path: impl AsRef<Path>) -> String {
self.target_path().join(path).read_to_string()
}
pub fn rename(&self, from: impl AsRef<Path>, to: impl AsRef<Path>) {
self.target_path().join(from).rename(to)
}
pub fn write(&self, path: impl AsRef<Path>, text: impl AsRef<[u8]>) {
self.target_path().join(path).write(text)
}
pub fn symlink_dir(
&self,
oldpath: impl AsRef<Path>,
newpath: impl AsRef<Path>,
) {
self.target_path().symlink_dir(oldpath, newpath)
}
pub fn symlink_file(
&self,
oldpath: impl AsRef<Path>,
newpath: impl AsRef<Path>,
) {
self.target_path().symlink_file(oldpath, newpath)
}
}
|