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
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use std::borrow::Cow;
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::sync::Arc;
use anyhow::Context;
use lsp_types::Url;
use serde::de::DeserializeOwned;
use serde::Serialize;
use crate::assertions::assert_wildcard_match;
/// Represents a path on the file system, which can be used
/// to perform specific actions.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
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 uri(&self) -> Url {
Url::from_directory_path(self.as_path()).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_json<TValue: DeserializeOwned>(&self) -> TValue {
serde_json::from_str(&self.read_to_string()).unwrap()
}
pub fn rename(&self, to: impl AsRef<Path>) {
fs::rename(self, self.join(to)).unwrap();
}
pub fn write(&self, text: impl AsRef<str>) {
fs::write(self, text.as_ref()).unwrap();
}
pub fn write_json<TValue: Serialize>(&self, value: &TValue) {
let text = serde_json::to_string_pretty(value).unwrap();
self.write(text);
}
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();
}
}
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();
}
}
pub fn read_dir(&self) -> fs::ReadDir {
fs::read_dir(self.as_path())
.with_context(|| format!("Reading {}", self.as_path().display()))
.unwrap()
}
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) {
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() {
new_from.copy(&new_to);
}
}
}
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();
}
}
pub fn assert_matches_file(&self, wildcard_file: impl AsRef<Path>) -> &Self {
let wildcard_file = PathRef::new(wildcard_file);
println!("output path {}", wildcard_file);
let expected_text = wildcard_file.read_to_string();
self.assert_matches_text(&expected_text)
}
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
}
}
#[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 uri(&self) -> Url {
Url::from_directory_path(self.path()).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<str>) {
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)
}
}
|