2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
use std::collections::HashSet;
|
2020-03-20 09:46:26 -04:00
|
|
|
use std::env::current_dir;
|
2024-03-07 20:16:32 -05:00
|
|
|
use std::fs::FileType;
|
2020-03-20 09:46:26 -04:00
|
|
|
use std::fs::OpenOptions;
|
2022-10-28 16:19:55 -04:00
|
|
|
use std::io::Error;
|
|
|
|
use std::io::ErrorKind;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2023-03-08 10:13:13 -05:00
|
|
|
use std::sync::Arc;
|
2022-11-08 14:17:24 -05:00
|
|
|
use std::time::Duration;
|
2020-02-17 13:11:45 -05:00
|
|
|
use walkdir::WalkDir;
|
2018-07-26 17:54:22 -04:00
|
|
|
|
2024-01-15 19:15:39 -05:00
|
|
|
use deno_config::glob::FilePatterns;
|
|
|
|
use deno_config::glob::PathOrPattern;
|
|
|
|
use deno_config::glob::PathOrPatternSet;
|
|
|
|
use deno_core::anyhow::anyhow;
|
|
|
|
use deno_core::anyhow::Context;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
pub use deno_core::normalize_path;
|
|
|
|
use deno_core::unsync::spawn_blocking;
|
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_runtime::deno_fs::FileSystem;
|
|
|
|
use deno_runtime::deno_node::PathClean;
|
|
|
|
|
2024-03-07 20:16:32 -05:00
|
|
|
use crate::util::gitignore::DirGitIgnores;
|
|
|
|
use crate::util::gitignore::GitIgnoreTree;
|
2024-05-14 14:26:48 -04:00
|
|
|
use crate::util::path::get_atomic_file_path;
|
2023-03-08 10:13:13 -05:00
|
|
|
use crate::util::progress_bar::ProgressBar;
|
|
|
|
use crate::util::progress_bar::ProgressBarStyle;
|
|
|
|
use crate::util::progress_bar::ProgressMessagePrompt;
|
2023-01-07 15:22:09 -05:00
|
|
|
|
2024-05-31 23:25:08 -04:00
|
|
|
pub fn atomic_write_file_with_retries<T: AsRef<[u8]>>(
|
|
|
|
file_path: &Path,
|
|
|
|
data: T,
|
|
|
|
mode: u32,
|
|
|
|
) -> std::io::Result<()> {
|
|
|
|
let mut count = 0;
|
|
|
|
loop {
|
|
|
|
match atomic_write_file(file_path, data.as_ref(), mode) {
|
|
|
|
Ok(()) => return Ok(()),
|
|
|
|
Err(err) => {
|
|
|
|
if count >= 5 {
|
|
|
|
// too many retries, return the error
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
count += 1;
|
|
|
|
let sleep_ms = std::cmp::min(50, 10 * count);
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(sleep_ms));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
/// Writes the file to the file system at a temporary path, then
|
|
|
|
/// renames it to the destination in a single sys call in order
|
|
|
|
/// to never leave the file system in a corrupted state.
|
|
|
|
///
|
|
|
|
/// This also handles creating the directory if a NotFound error
|
|
|
|
/// occurs.
|
2020-12-18 13:30:49 -05:00
|
|
|
pub fn atomic_write_file<T: AsRef<[u8]>>(
|
2023-08-01 20:49:09 -04:00
|
|
|
file_path: &Path,
|
2020-12-18 13:30:49 -05:00
|
|
|
data: T,
|
|
|
|
mode: u32,
|
|
|
|
) -> std::io::Result<()> {
|
2023-08-01 20:49:09 -04:00
|
|
|
fn atomic_write_file_raw(
|
|
|
|
temp_file_path: &Path,
|
|
|
|
file_path: &Path,
|
|
|
|
data: &[u8],
|
|
|
|
mode: u32,
|
|
|
|
) -> std::io::Result<()> {
|
|
|
|
write_file(temp_file_path, data, mode)?;
|
2024-05-31 23:25:08 -04:00
|
|
|
std::fs::rename(temp_file_path, file_path).map_err(|err| {
|
|
|
|
// clean up the created temp file on error
|
|
|
|
let _ = std::fs::remove_file(temp_file_path);
|
|
|
|
err
|
|
|
|
})
|
2023-08-01 20:49:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn inner(file_path: &Path, data: &[u8], mode: u32) -> std::io::Result<()> {
|
2024-05-14 14:26:48 -04:00
|
|
|
let temp_file_path = get_atomic_file_path(file_path);
|
2023-08-01 20:49:09 -04:00
|
|
|
|
|
|
|
if let Err(write_err) =
|
|
|
|
atomic_write_file_raw(&temp_file_path, file_path, data, mode)
|
|
|
|
{
|
|
|
|
if write_err.kind() == ErrorKind::NotFound {
|
|
|
|
let parent_dir_path = file_path.parent().unwrap();
|
|
|
|
match std::fs::create_dir_all(parent_dir_path) {
|
|
|
|
Ok(()) => {
|
|
|
|
return atomic_write_file_raw(
|
|
|
|
&temp_file_path,
|
|
|
|
file_path,
|
|
|
|
data,
|
|
|
|
mode,
|
|
|
|
)
|
2023-11-01 15:59:51 -04:00
|
|
|
.map_err(|err| add_file_context_to_err(file_path, err));
|
2023-08-01 20:49:09 -04:00
|
|
|
}
|
|
|
|
Err(create_err) => {
|
|
|
|
if !parent_dir_path.exists() {
|
|
|
|
return Err(Error::new(
|
|
|
|
create_err.kind(),
|
|
|
|
format!(
|
|
|
|
"{:#} (for '{}')\nCheck the permission of the directory.",
|
|
|
|
create_err,
|
|
|
|
parent_dir_path.display()
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-01 15:59:51 -04:00
|
|
|
return Err(add_file_context_to_err(file_path, write_err));
|
2023-08-01 20:49:09 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
inner(file_path, data.as_ref(), mode)
|
2020-12-18 13:30:49 -05:00
|
|
|
}
|
|
|
|
|
2023-11-01 15:59:51 -04:00
|
|
|
/// Creates a std::fs::File handling if the parent does not exist.
|
|
|
|
pub fn create_file(file_path: &Path) -> std::io::Result<std::fs::File> {
|
|
|
|
match std::fs::File::create(file_path) {
|
|
|
|
Ok(file) => Ok(file),
|
|
|
|
Err(err) => {
|
|
|
|
if err.kind() == ErrorKind::NotFound {
|
|
|
|
let parent_dir_path = file_path.parent().unwrap();
|
|
|
|
match std::fs::create_dir_all(parent_dir_path) {
|
|
|
|
Ok(()) => {
|
|
|
|
return std::fs::File::create(file_path)
|
|
|
|
.map_err(|err| add_file_context_to_err(file_path, err));
|
|
|
|
}
|
|
|
|
Err(create_err) => {
|
|
|
|
if !parent_dir_path.exists() {
|
|
|
|
return Err(Error::new(
|
|
|
|
create_err.kind(),
|
|
|
|
format!(
|
|
|
|
"{:#} (for '{}')\nCheck the permission of the directory.",
|
|
|
|
create_err,
|
|
|
|
parent_dir_path.display()
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(add_file_context_to_err(file_path, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_file_context_to_err(file_path: &Path, err: Error) -> Error {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
|
|
|
format!("{:#} (for '{}')", err, file_path.display()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-12-11 08:36:34 -05:00
|
|
|
pub fn write_file<T: AsRef<[u8]>>(
|
2018-09-11 12:00:57 -04:00
|
|
|
filename: &Path,
|
2018-12-11 08:36:34 -05:00
|
|
|
data: T,
|
2020-03-07 22:29:12 -05:00
|
|
|
mode: u32,
|
2018-09-11 12:00:57 -04:00
|
|
|
) -> std::io::Result<()> {
|
2020-03-07 22:29:12 -05:00
|
|
|
write_file_2(filename, data, true, mode, true, false)
|
2019-02-02 14:26:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_file_2<T: AsRef<[u8]>>(
|
|
|
|
filename: &Path,
|
|
|
|
data: T,
|
2020-03-07 22:29:12 -05:00
|
|
|
update_mode: bool,
|
|
|
|
mode: u32,
|
2019-02-02 14:26:18 -05:00
|
|
|
is_create: bool,
|
|
|
|
is_append: bool,
|
|
|
|
) -> std::io::Result<()> {
|
2018-09-11 12:00:57 -04:00
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
.read(false)
|
|
|
|
.write(true)
|
|
|
|
.append(is_append)
|
|
|
|
.truncate(!is_append)
|
2019-02-02 14:26:18 -05:00
|
|
|
.create(is_create)
|
2018-09-11 12:00:57 -04:00
|
|
|
.open(filename)?;
|
|
|
|
|
2020-03-07 22:29:12 -05:00
|
|
|
if update_mode {
|
2020-03-20 09:46:26 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
let mode = mode & 0o777;
|
|
|
|
let permissions = PermissionsExt::from_mode(mode);
|
|
|
|
file.set_permissions(permissions)?;
|
2018-08-23 18:36:45 -04:00
|
|
|
}
|
2020-03-20 09:46:26 -04:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
let _ = mode;
|
2018-08-23 18:36:45 -04:00
|
|
|
}
|
2018-09-14 15:30:43 -04:00
|
|
|
|
2020-03-20 09:46:26 -04:00
|
|
|
file.write_all(data.as_ref())
|
2019-05-07 21:58:58 -04:00
|
|
|
}
|
2019-07-17 18:15:30 -04:00
|
|
|
|
2020-10-29 22:19:03 -04:00
|
|
|
/// Similar to `std::fs::canonicalize()` but strips UNC prefixes on Windows.
|
|
|
|
pub fn canonicalize_path(path: &Path) -> Result<PathBuf, Error> {
|
2023-05-01 20:35:33 -04:00
|
|
|
Ok(deno_core::strip_unc_prefix(path.canonicalize()?))
|
2021-10-25 11:54:36 -04:00
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
/// Canonicalizes a path which might be non-existent by going up the
|
|
|
|
/// ancestors until it finds a directory that exists, canonicalizes
|
|
|
|
/// that path, then adds back the remaining path components.
|
|
|
|
///
|
|
|
|
/// Note: When using this, you should be aware that a symlink may
|
|
|
|
/// subsequently be created along this path by some other code.
|
|
|
|
pub fn canonicalize_path_maybe_not_exists(
|
|
|
|
path: &Path,
|
2023-05-10 20:06:59 -04:00
|
|
|
) -> Result<PathBuf, Error> {
|
2023-11-29 09:32:23 -05:00
|
|
|
canonicalize_path_maybe_not_exists_with_custom_fn(path, canonicalize_path)
|
2023-05-10 20:06:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn canonicalize_path_maybe_not_exists_with_fs(
|
2023-11-29 09:32:23 -05:00
|
|
|
path: &Path,
|
|
|
|
fs: &dyn FileSystem,
|
|
|
|
) -> Result<PathBuf, Error> {
|
|
|
|
canonicalize_path_maybe_not_exists_with_custom_fn(path, |path| {
|
|
|
|
fs.realpath_sync(path).map_err(|err| err.into_io_error())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn canonicalize_path_maybe_not_exists_with_custom_fn(
|
2023-05-10 20:06:59 -04:00
|
|
|
path: &Path,
|
|
|
|
canonicalize: impl Fn(&Path) -> Result<PathBuf, Error>,
|
2022-09-22 11:17:02 -04:00
|
|
|
) -> Result<PathBuf, Error> {
|
|
|
|
let path = path.to_path_buf().clean();
|
|
|
|
let mut path = path.as_path();
|
|
|
|
let mut names_stack = Vec::new();
|
|
|
|
loop {
|
2023-05-10 20:06:59 -04:00
|
|
|
match canonicalize(path) {
|
2022-09-22 11:17:02 -04:00
|
|
|
Ok(mut canonicalized_path) => {
|
|
|
|
for name in names_stack.into_iter().rev() {
|
|
|
|
canonicalized_path = canonicalized_path.join(name);
|
|
|
|
}
|
|
|
|
return Ok(canonicalized_path);
|
|
|
|
}
|
|
|
|
Err(err) if err.kind() == ErrorKind::NotFound => {
|
2023-11-13 09:44:01 -05:00
|
|
|
names_stack.push(match path.file_name() {
|
|
|
|
Some(name) => name.to_owned(),
|
|
|
|
None => return Err(err),
|
|
|
|
});
|
|
|
|
path = match path.parent() {
|
|
|
|
Some(parent) => parent,
|
|
|
|
None => return Err(err),
|
|
|
|
};
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
pub fn resolve_from_cwd(path: &Path) -> Result<PathBuf, AnyError> {
|
2020-01-20 09:45:44 -05:00
|
|
|
let resolved_path = if path.is_absolute() {
|
|
|
|
path.to_owned()
|
2019-07-17 18:15:30 -04:00
|
|
|
} else {
|
2021-04-21 11:52:00 -04:00
|
|
|
let cwd =
|
|
|
|
current_dir().context("Failed to get current working directory")?;
|
2019-07-17 18:15:30 -04:00
|
|
|
cwd.join(path)
|
|
|
|
};
|
|
|
|
|
2022-12-17 17:20:15 -05:00
|
|
|
Ok(normalize_path(resolved_path))
|
2019-07-17 18:15:30 -04:00
|
|
|
}
|
2019-09-07 14:13:09 -04:00
|
|
|
|
2024-03-07 20:16:32 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct WalkEntry<'a> {
|
|
|
|
pub path: &'a Path,
|
|
|
|
pub file_type: &'a FileType,
|
|
|
|
pub patterns: &'a FilePatterns,
|
|
|
|
}
|
|
|
|
|
2020-11-14 07:05:26 -05:00
|
|
|
/// Collects file paths that satisfy the given predicate, by recursively walking `files`.
|
|
|
|
/// If the walker visits a path that is listed in `ignore`, it skips descending into the directory.
|
2024-03-07 20:16:32 -05:00
|
|
|
pub struct FileCollector<TFilter: Fn(WalkEntry) -> bool> {
|
2022-12-07 13:10:10 -05:00
|
|
|
file_filter: TFilter,
|
|
|
|
ignore_git_folder: bool,
|
|
|
|
ignore_node_modules: bool,
|
2024-03-09 20:40:53 -05:00
|
|
|
vendor_folder: Option<PathBuf>,
|
2024-03-07 20:16:32 -05:00
|
|
|
use_gitignore: bool,
|
2022-12-07 13:10:10 -05:00
|
|
|
}
|
2020-11-14 07:05:26 -05:00
|
|
|
|
2024-03-07 20:16:32 -05:00
|
|
|
impl<TFilter: Fn(WalkEntry) -> bool> FileCollector<TFilter> {
|
2022-12-07 13:10:10 -05:00
|
|
|
pub fn new(file_filter: TFilter) -> Self {
|
|
|
|
Self {
|
|
|
|
file_filter,
|
|
|
|
ignore_git_folder: false,
|
|
|
|
ignore_node_modules: false,
|
2024-03-09 20:40:53 -05:00
|
|
|
vendor_folder: None,
|
2024-03-07 20:16:32 -05:00
|
|
|
use_gitignore: false,
|
2020-11-14 07:05:26 -05:00
|
|
|
}
|
|
|
|
}
|
2023-01-07 15:22:09 -05:00
|
|
|
|
2022-12-07 13:10:10 -05:00
|
|
|
pub fn ignore_node_modules(mut self) -> Self {
|
|
|
|
self.ignore_node_modules = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-03-09 20:40:53 -05:00
|
|
|
pub fn set_vendor_folder(mut self, vendor_folder: Option<PathBuf>) -> Self {
|
|
|
|
self.vendor_folder = vendor_folder;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-12-07 13:10:10 -05:00
|
|
|
pub fn ignore_git_folder(mut self) -> Self {
|
|
|
|
self.ignore_git_folder = true;
|
|
|
|
self
|
|
|
|
}
|
2020-11-14 07:05:26 -05:00
|
|
|
|
2024-03-07 20:16:32 -05:00
|
|
|
pub fn use_gitignore(mut self) -> Self {
|
|
|
|
self.use_gitignore = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
pub fn collect_file_patterns(
|
2022-12-07 13:10:10 -05:00
|
|
|
&self,
|
2024-01-08 12:18:42 -05:00
|
|
|
file_patterns: FilePatterns,
|
2022-12-07 13:10:10 -05:00
|
|
|
) -> Result<Vec<PathBuf>, AnyError> {
|
2024-03-07 20:16:32 -05:00
|
|
|
fn is_pattern_matched(
|
|
|
|
maybe_git_ignore: Option<&DirGitIgnores>,
|
|
|
|
path: &Path,
|
|
|
|
is_dir: bool,
|
|
|
|
file_patterns: &FilePatterns,
|
|
|
|
) -> bool {
|
|
|
|
use deno_config::glob::FilePatternsMatch;
|
|
|
|
|
|
|
|
let path_kind = match is_dir {
|
|
|
|
true => deno_config::glob::PathKind::Directory,
|
|
|
|
false => deno_config::glob::PathKind::File,
|
|
|
|
};
|
|
|
|
match file_patterns.matches_path_detail(path, path_kind) {
|
|
|
|
FilePatternsMatch::Passed => {
|
|
|
|
// check gitignore
|
|
|
|
let is_gitignored = maybe_git_ignore
|
|
|
|
.as_ref()
|
|
|
|
.map(|git_ignore| git_ignore.is_ignored(path, is_dir))
|
|
|
|
.unwrap_or(false);
|
|
|
|
!is_gitignored
|
|
|
|
}
|
|
|
|
FilePatternsMatch::PassedOptedOutExclude => true,
|
|
|
|
FilePatternsMatch::Excluded => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut maybe_git_ignores = if self.use_gitignore {
|
2024-03-08 00:46:06 -05:00
|
|
|
// Override explicitly specified include paths in the
|
|
|
|
// .gitignore file. This does not apply to globs because
|
|
|
|
// that is way too complicated to reason about.
|
|
|
|
let include_paths = file_patterns
|
|
|
|
.include
|
|
|
|
.as_ref()
|
|
|
|
.map(|include| {
|
|
|
|
include
|
|
|
|
.inner()
|
|
|
|
.iter()
|
|
|
|
.filter_map(|path_or_pattern| {
|
|
|
|
if let PathOrPattern::Path(p) = path_or_pattern {
|
|
|
|
Some(p.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
|
|
|
Some(GitIgnoreTree::new(
|
|
|
|
Arc::new(deno_runtime::deno_fs::RealFs),
|
|
|
|
include_paths,
|
|
|
|
))
|
2024-03-07 20:16:32 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2022-12-07 13:10:10 -05:00
|
|
|
let mut target_files = Vec::new();
|
2024-01-08 12:18:42 -05:00
|
|
|
let mut visited_paths = HashSet::new();
|
|
|
|
let file_patterns_by_base = file_patterns.split_by_base();
|
2024-01-18 15:57:30 -05:00
|
|
|
for file_patterns in file_patterns_by_base {
|
|
|
|
let file = normalize_path(&file_patterns.base);
|
2024-01-08 12:18:42 -05:00
|
|
|
// use an iterator in order to minimize the number of file system operations
|
|
|
|
let mut iterator = WalkDir::new(&file)
|
|
|
|
.follow_links(false) // the default, but be explicit
|
|
|
|
.into_iter();
|
|
|
|
loop {
|
|
|
|
let e = match iterator.next() {
|
|
|
|
None => break,
|
|
|
|
Some(Err(_)) => continue,
|
|
|
|
Some(Ok(entry)) => entry,
|
|
|
|
};
|
|
|
|
let file_type = e.file_type();
|
|
|
|
let is_dir = file_type.is_dir();
|
2024-03-07 20:16:32 -05:00
|
|
|
let path = e.path().to_path_buf();
|
|
|
|
let maybe_gitignore =
|
|
|
|
maybe_git_ignores.as_mut().and_then(|git_ignores| {
|
2024-03-08 14:25:22 -05:00
|
|
|
if is_dir {
|
|
|
|
git_ignores.get_resolved_git_ignore_for_dir(&path)
|
|
|
|
} else {
|
|
|
|
git_ignores.get_resolved_git_ignore_for_file(&path)
|
|
|
|
}
|
2024-03-07 20:16:32 -05:00
|
|
|
});
|
|
|
|
if !is_pattern_matched(
|
|
|
|
maybe_gitignore.as_deref(),
|
|
|
|
&path,
|
|
|
|
is_dir,
|
|
|
|
&file_patterns,
|
|
|
|
) {
|
2024-01-08 12:18:42 -05:00
|
|
|
if is_dir {
|
2022-12-07 13:10:10 -05:00
|
|
|
iterator.skip_current_dir();
|
|
|
|
}
|
2024-01-08 12:18:42 -05:00
|
|
|
} else if is_dir {
|
2024-03-09 20:40:53 -05:00
|
|
|
// allow the user to opt out of ignoring by explicitly specifying the dir
|
|
|
|
let opt_out_ignore = file == path;
|
|
|
|
let should_ignore_dir = !opt_out_ignore && self.is_ignored_dir(&path);
|
|
|
|
if should_ignore_dir || !visited_paths.insert(path.clone()) {
|
2024-01-08 12:18:42 -05:00
|
|
|
iterator.skip_current_dir();
|
|
|
|
}
|
2024-03-07 20:16:32 -05:00
|
|
|
} else if (self.file_filter)(WalkEntry {
|
|
|
|
path: &path,
|
|
|
|
file_type: &file_type,
|
|
|
|
patterns: &file_patterns,
|
|
|
|
}) && visited_paths.insert(path.clone())
|
2024-01-08 12:18:42 -05:00
|
|
|
{
|
2024-03-07 20:16:32 -05:00
|
|
|
target_files.push(path);
|
2022-12-07 13:10:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(target_files)
|
|
|
|
}
|
2024-03-09 20:40:53 -05:00
|
|
|
|
|
|
|
fn is_ignored_dir(&self, path: &Path) -> bool {
|
|
|
|
path
|
|
|
|
.file_name()
|
|
|
|
.map(|dir_name| {
|
|
|
|
let dir_name = dir_name.to_string_lossy().to_lowercase();
|
|
|
|
let is_ignored_file = match dir_name.as_str() {
|
|
|
|
"node_modules" => self.ignore_node_modules,
|
|
|
|
".git" => self.ignore_git_folder,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
is_ignored_file
|
|
|
|
})
|
|
|
|
.unwrap_or(false)
|
|
|
|
|| self.is_vendor_folder(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_vendor_folder(&self, path: &Path) -> bool {
|
|
|
|
self
|
|
|
|
.vendor_folder
|
|
|
|
.as_ref()
|
|
|
|
.map(|vendor_folder| path == *vendor_folder)
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
2020-11-14 07:05:26 -05:00
|
|
|
}
|
|
|
|
|
2021-08-13 06:06:49 -04:00
|
|
|
/// Collects module specifiers that satisfy the given predicate as a file path, by recursively walking `include`.
|
|
|
|
/// Specifiers that start with http and https are left intact.
|
2022-12-07 13:10:10 -05:00
|
|
|
/// Note: This ignores all .git and node_modules folders.
|
|
|
|
pub fn collect_specifiers(
|
2024-01-08 12:18:42 -05:00
|
|
|
mut files: FilePatterns,
|
2024-03-27 14:25:39 -04:00
|
|
|
vendor_folder: Option<PathBuf>,
|
2024-03-07 20:16:32 -05:00
|
|
|
predicate: impl Fn(WalkEntry) -> bool,
|
2022-12-07 13:10:10 -05:00
|
|
|
) -> Result<Vec<ModuleSpecifier>, AnyError> {
|
2021-08-13 06:06:49 -04:00
|
|
|
let mut prepared = vec![];
|
2021-08-14 04:16:24 -04:00
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
// break out the remote specifiers
|
|
|
|
if let Some(include_mut) = &mut files.include {
|
|
|
|
let includes = std::mem::take(include_mut);
|
|
|
|
let path_or_patterns = includes.into_path_or_patterns();
|
|
|
|
let mut result = Vec::with_capacity(path_or_patterns.len());
|
|
|
|
for path_or_pattern in path_or_patterns {
|
|
|
|
match path_or_pattern {
|
|
|
|
PathOrPattern::Path(path) => {
|
2024-01-15 19:15:39 -05:00
|
|
|
if path.is_dir() {
|
2024-01-08 12:18:42 -05:00
|
|
|
result.push(PathOrPattern::Path(path));
|
|
|
|
} else if !files.exclude.matches_path(&path) {
|
2024-02-08 20:40:26 -05:00
|
|
|
let url = specifier_from_file_path(&path)?;
|
2024-01-08 12:18:42 -05:00
|
|
|
prepared.push(url);
|
|
|
|
}
|
|
|
|
}
|
2024-03-07 20:16:32 -05:00
|
|
|
PathOrPattern::NegatedPath(path) => {
|
|
|
|
// add it back
|
|
|
|
result.push(PathOrPattern::NegatedPath(path));
|
|
|
|
}
|
2024-01-15 19:15:39 -05:00
|
|
|
PathOrPattern::RemoteUrl(remote_url) => {
|
|
|
|
prepared.push(remote_url);
|
|
|
|
}
|
2024-01-08 12:18:42 -05:00
|
|
|
PathOrPattern::Pattern(pattern) => {
|
|
|
|
// add it back
|
|
|
|
result.push(PathOrPattern::Pattern(pattern));
|
|
|
|
}
|
|
|
|
}
|
2021-08-13 06:06:49 -04:00
|
|
|
}
|
2024-01-08 12:18:42 -05:00
|
|
|
*include_mut = PathOrPatternSet::new(result);
|
2021-08-13 06:06:49 -04:00
|
|
|
}
|
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
let collected_files = FileCollector::new(predicate)
|
|
|
|
.ignore_git_folder()
|
|
|
|
.ignore_node_modules()
|
2024-03-27 14:25:39 -04:00
|
|
|
.set_vendor_folder(vendor_folder)
|
2024-01-08 12:18:42 -05:00
|
|
|
.collect_file_patterns(files)?;
|
|
|
|
let mut collected_files_as_urls = collected_files
|
|
|
|
.iter()
|
2024-02-08 20:40:26 -05:00
|
|
|
.map(|f| specifier_from_file_path(f).unwrap())
|
2024-01-08 12:18:42 -05:00
|
|
|
.collect::<Vec<ModuleSpecifier>>();
|
|
|
|
|
|
|
|
collected_files_as_urls.sort();
|
|
|
|
prepared.extend(collected_files_as_urls);
|
|
|
|
|
2021-08-13 06:06:49 -04:00
|
|
|
Ok(prepared)
|
|
|
|
}
|
|
|
|
|
2021-11-23 10:38:11 -05:00
|
|
|
/// Asynchronously removes a directory and all its descendants, but does not error
|
|
|
|
/// when the directory does not exist.
|
2021-06-25 21:44:27 -04:00
|
|
|
pub async fn remove_dir_all_if_exists(path: &Path) -> std::io::Result<()> {
|
|
|
|
let result = tokio::fs::remove_dir_all(path).await;
|
|
|
|
match result {
|
|
|
|
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
|
|
|
|
_ => result,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-28 14:59:17 -04:00
|
|
|
mod clone_dir_imp {
|
|
|
|
|
|
|
|
#[cfg(target_vendor = "apple")]
|
|
|
|
mod apple {
|
|
|
|
use super::super::copy_dir_recursive;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
|
|
|
use std::path::Path;
|
|
|
|
fn clonefile(from: &Path, to: &Path) -> std::io::Result<()> {
|
|
|
|
let from = std::ffi::CString::new(from.as_os_str().as_bytes())?;
|
|
|
|
let to = std::ffi::CString::new(to.as_os_str().as_bytes())?;
|
|
|
|
// SAFETY: `from` and `to` are valid C strings.
|
|
|
|
let ret = unsafe { libc::clonefile(from.as_ptr(), to.as_ptr(), 0) };
|
|
|
|
if ret != 0 {
|
|
|
|
return Err(std::io::Error::last_os_error());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clone_dir_recursive(from: &Path, to: &Path) -> Result<(), AnyError> {
|
|
|
|
if let Some(parent) = to.parent() {
|
|
|
|
std::fs::create_dir_all(parent)?;
|
|
|
|
}
|
|
|
|
// Try to clone the whole directory
|
|
|
|
if let Err(err) = clonefile(from, to) {
|
|
|
|
if err.kind() != std::io::ErrorKind::AlreadyExists {
|
|
|
|
log::warn!(
|
|
|
|
"Failed to clone dir {:?} to {:?} via clonefile: {}",
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
err
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// clonefile won't overwrite existing files, so if the dir exists
|
|
|
|
// we need to handle it recursively.
|
|
|
|
copy_dir_recursive(from, to)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_vendor = "apple")]
|
|
|
|
pub(super) use apple::clone_dir_recursive;
|
|
|
|
|
|
|
|
#[cfg(not(target_vendor = "apple"))]
|
|
|
|
pub(super) fn clone_dir_recursive(
|
|
|
|
from: &std::path::Path,
|
|
|
|
to: &std::path::Path,
|
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
if let Err(e) = super::hard_link_dir_recursive(from, to) {
|
|
|
|
log::debug!("Failed to hard link dir {:?} to {:?}: {}", from, to, e);
|
|
|
|
super::copy_dir_recursive(from, to)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clones a directory to another directory. The exact method
|
|
|
|
/// is not guaranteed - it may be a hardlink, copy, or other platform-specific
|
|
|
|
/// operation.
|
|
|
|
///
|
|
|
|
/// Note: Does not handle symlinks.
|
|
|
|
pub fn clone_dir_recursive(from: &Path, to: &Path) -> Result<(), AnyError> {
|
|
|
|
clone_dir_imp::clone_dir_recursive(from, to)
|
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
/// Copies a directory to another directory.
|
|
|
|
///
|
|
|
|
/// Note: Does not handle symlinks.
|
|
|
|
pub fn copy_dir_recursive(from: &Path, to: &Path) -> Result<(), AnyError> {
|
2022-11-17 20:59:10 -05:00
|
|
|
std::fs::create_dir_all(to)
|
2022-09-22 11:17:02 -04:00
|
|
|
.with_context(|| format!("Creating {}", to.display()))?;
|
2022-11-17 20:59:10 -05:00
|
|
|
let read_dir = std::fs::read_dir(from)
|
2022-09-22 11:17:02 -04:00
|
|
|
.with_context(|| format!("Reading {}", from.display()))?;
|
|
|
|
|
|
|
|
for entry in read_dir {
|
|
|
|
let entry = entry?;
|
|
|
|
let file_type = entry.file_type()?;
|
|
|
|
let new_from = from.join(entry.file_name());
|
|
|
|
let new_to = to.join(entry.file_name());
|
|
|
|
|
|
|
|
if file_type.is_dir() {
|
|
|
|
copy_dir_recursive(&new_from, &new_to).with_context(|| {
|
|
|
|
format!("Dir {} to {}", new_from.display(), new_to.display())
|
|
|
|
})?;
|
|
|
|
} else if file_type.is_file() {
|
|
|
|
std::fs::copy(&new_from, &new_to).with_context(|| {
|
|
|
|
format!("Copying {} to {}", new_from.display(), new_to.display())
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
/// Hardlinks the files in one directory to another directory.
|
|
|
|
///
|
|
|
|
/// Note: Does not handle symlinks.
|
|
|
|
pub fn hard_link_dir_recursive(from: &Path, to: &Path) -> Result<(), AnyError> {
|
2022-11-17 20:59:10 -05:00
|
|
|
std::fs::create_dir_all(to)
|
2022-11-08 14:17:24 -05:00
|
|
|
.with_context(|| format!("Creating {}", to.display()))?;
|
2022-11-17 20:59:10 -05:00
|
|
|
let read_dir = std::fs::read_dir(from)
|
2022-11-08 14:17:24 -05:00
|
|
|
.with_context(|| format!("Reading {}", from.display()))?;
|
|
|
|
|
|
|
|
for entry in read_dir {
|
|
|
|
let entry = entry?;
|
|
|
|
let file_type = entry.file_type()?;
|
|
|
|
let new_from = from.join(entry.file_name());
|
|
|
|
let new_to = to.join(entry.file_name());
|
|
|
|
|
|
|
|
if file_type.is_dir() {
|
|
|
|
hard_link_dir_recursive(&new_from, &new_to).with_context(|| {
|
|
|
|
format!("Dir {} to {}", new_from.display(), new_to.display())
|
|
|
|
})?;
|
|
|
|
} else if file_type.is_file() {
|
|
|
|
// note: chance for race conditions here between attempting to create,
|
|
|
|
// then removing, then attempting to create. There doesn't seem to be
|
|
|
|
// a way to hard link with overwriting in Rust, but maybe there is some
|
|
|
|
// way with platform specific code. The workaround here is to handle
|
|
|
|
// scenarios where something else might create or remove files.
|
|
|
|
if let Err(err) = std::fs::hard_link(&new_from, &new_to) {
|
|
|
|
if err.kind() == ErrorKind::AlreadyExists {
|
|
|
|
if let Err(err) = std::fs::remove_file(&new_to) {
|
|
|
|
if err.kind() == ErrorKind::NotFound {
|
|
|
|
// Assume another process/thread created this hard link to the file we are wanting
|
|
|
|
// to remove then sleep a little bit to let the other process/thread move ahead
|
|
|
|
// faster to reduce contention.
|
|
|
|
std::thread::sleep(Duration::from_millis(10));
|
|
|
|
} else {
|
|
|
|
return Err(err).with_context(|| {
|
|
|
|
format!(
|
|
|
|
"Removing file to hard link {} to {}",
|
|
|
|
new_from.display(),
|
|
|
|
new_to.display()
|
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always attempt to recreate the hardlink. In contention scenarios, the other process
|
|
|
|
// might have been killed or exited after removing the file, but before creating the hardlink
|
|
|
|
if let Err(err) = std::fs::hard_link(&new_from, &new_to) {
|
|
|
|
// Assume another process/thread created this hard link to the file we are wanting
|
|
|
|
// to now create then sleep a little bit to let the other process/thread move ahead
|
|
|
|
// faster to reduce contention.
|
|
|
|
if err.kind() == ErrorKind::AlreadyExists {
|
|
|
|
std::thread::sleep(Duration::from_millis(10));
|
|
|
|
} else {
|
|
|
|
return Err(err).with_context(|| {
|
|
|
|
format!(
|
|
|
|
"Hard linking {} to {}",
|
|
|
|
new_from.display(),
|
|
|
|
new_to.display()
|
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(err).with_context(|| {
|
|
|
|
format!(
|
|
|
|
"Hard linking {} to {}",
|
|
|
|
new_from.display(),
|
|
|
|
new_to.display()
|
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
pub fn symlink_dir(oldpath: &Path, newpath: &Path) -> Result<(), AnyError> {
|
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
|
|
|
format!(
|
|
|
|
"{}, symlink '{}' -> '{}'",
|
|
|
|
err,
|
|
|
|
oldpath.display(),
|
|
|
|
newpath.display()
|
|
|
|
),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::symlink;
|
2022-11-17 20:59:10 -05:00
|
|
|
symlink(oldpath, newpath).map_err(err_mapper)?;
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
use std::os::windows::fs::symlink_dir;
|
2022-11-18 12:54:01 -05:00
|
|
|
symlink_dir(oldpath, newpath).map_err(err_mapper)?;
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
/// Gets the total size (in bytes) of a directory.
|
|
|
|
pub fn dir_size(path: &Path) -> std::io::Result<u64> {
|
|
|
|
let entries = std::fs::read_dir(path)?;
|
|
|
|
let mut total = 0;
|
|
|
|
for entry in entries {
|
|
|
|
let entry = entry?;
|
|
|
|
total += match entry.metadata()? {
|
|
|
|
data if data.is_dir() => dir_size(&entry.path())?,
|
|
|
|
data => data.len(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(total)
|
|
|
|
}
|
|
|
|
|
2023-03-08 10:13:13 -05:00
|
|
|
struct LaxSingleProcessFsFlagInner {
|
|
|
|
file_path: PathBuf,
|
|
|
|
fs_file: std::fs::File,
|
|
|
|
finished_token: Arc<tokio_util::sync::CancellationToken>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for LaxSingleProcessFsFlagInner {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
use fs3::FileExt;
|
|
|
|
// kill the poll thread
|
|
|
|
self.finished_token.cancel();
|
|
|
|
// release the file lock
|
|
|
|
if let Err(err) = self.fs_file.unlock() {
|
|
|
|
log::debug!(
|
|
|
|
"Failed releasing lock for {}. {:#}",
|
|
|
|
self.file_path.display(),
|
|
|
|
err
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A file system based flag that will attempt to synchronize multiple
|
|
|
|
/// processes so they go one after the other. In scenarios where
|
|
|
|
/// synchronization cannot be achieved, it will allow the current process
|
|
|
|
/// to proceed.
|
|
|
|
///
|
|
|
|
/// This should only be used in places where it's ideal for multiple
|
|
|
|
/// processes to not update something on the file system at the same time,
|
|
|
|
/// but it's not that big of a deal.
|
2024-04-10 18:08:23 -04:00
|
|
|
pub struct LaxSingleProcessFsFlag(
|
|
|
|
#[allow(dead_code)] Option<LaxSingleProcessFsFlagInner>,
|
|
|
|
);
|
2023-03-08 10:13:13 -05:00
|
|
|
|
|
|
|
impl LaxSingleProcessFsFlag {
|
|
|
|
pub async fn lock(file_path: PathBuf, long_wait_message: &str) -> Self {
|
|
|
|
log::debug!("Acquiring file lock at {}", file_path.display());
|
|
|
|
use fs3::FileExt;
|
|
|
|
let last_updated_path = file_path.with_extension("lock.poll");
|
|
|
|
let start_instant = std::time::Instant::now();
|
|
|
|
let open_result = std::fs::OpenOptions::new()
|
|
|
|
.read(true)
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
2024-04-10 18:08:23 -04:00
|
|
|
.truncate(false)
|
2023-03-08 10:13:13 -05:00
|
|
|
.open(&file_path);
|
|
|
|
|
|
|
|
match open_result {
|
|
|
|
Ok(fs_file) => {
|
|
|
|
let mut pb_update_guard = None;
|
|
|
|
let mut error_count = 0;
|
|
|
|
while error_count < 10 {
|
|
|
|
let lock_result = fs_file.try_lock_exclusive();
|
|
|
|
let poll_file_update_ms = 100;
|
|
|
|
match lock_result {
|
|
|
|
Ok(_) => {
|
|
|
|
log::debug!("Acquired file lock at {}", file_path.display());
|
|
|
|
let _ignore = std::fs::write(&last_updated_path, "");
|
|
|
|
let token = Arc::new(tokio_util::sync::CancellationToken::new());
|
|
|
|
|
|
|
|
// Spawn a blocking task that will continually update a file
|
|
|
|
// signalling the lock is alive. This is a fail safe for when
|
|
|
|
// a file lock is never released. For example, on some operating
|
|
|
|
// systems, if a process does not release the lock (say it's
|
|
|
|
// killed), then the OS may release it at an indeterminate time
|
|
|
|
//
|
|
|
|
// This uses a blocking task because we use a single threaded
|
|
|
|
// runtime and this is time sensitive so we don't want it to update
|
|
|
|
// at the whims of of whatever is occurring on the runtime thread.
|
2023-05-14 17:40:01 -04:00
|
|
|
spawn_blocking({
|
2023-03-08 10:13:13 -05:00
|
|
|
let token = token.clone();
|
|
|
|
let last_updated_path = last_updated_path.clone();
|
|
|
|
move || {
|
|
|
|
let mut i = 0;
|
|
|
|
while !token.is_cancelled() {
|
|
|
|
i += 1;
|
|
|
|
let _ignore =
|
|
|
|
std::fs::write(&last_updated_path, i.to_string());
|
|
|
|
std::thread::sleep(Duration::from_millis(
|
|
|
|
poll_file_update_ms,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return Self(Some(LaxSingleProcessFsFlagInner {
|
|
|
|
file_path,
|
|
|
|
fs_file,
|
|
|
|
finished_token: token,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
// show a message if it's been a while
|
|
|
|
if pb_update_guard.is_none()
|
|
|
|
&& start_instant.elapsed().as_millis() > 1_000
|
|
|
|
{
|
|
|
|
let pb = ProgressBar::new(ProgressBarStyle::TextOnly);
|
|
|
|
let guard = pb.update_with_prompt(
|
|
|
|
ProgressMessagePrompt::Blocking,
|
|
|
|
long_wait_message,
|
|
|
|
);
|
|
|
|
pb_update_guard = Some((guard, pb));
|
|
|
|
}
|
|
|
|
|
|
|
|
// sleep for a little bit
|
|
|
|
tokio::time::sleep(Duration::from_millis(20)).await;
|
|
|
|
|
|
|
|
// Poll the last updated path to check if it's stopped updating,
|
|
|
|
// which is an indication that the file lock is claimed, but
|
|
|
|
// was never properly released.
|
|
|
|
match std::fs::metadata(&last_updated_path)
|
|
|
|
.and_then(|p| p.modified())
|
|
|
|
{
|
|
|
|
Ok(last_updated_time) => {
|
|
|
|
let current_time = std::time::SystemTime::now();
|
|
|
|
match current_time.duration_since(last_updated_time) {
|
|
|
|
Ok(duration) => {
|
|
|
|
if duration.as_millis()
|
|
|
|
> (poll_file_update_ms * 2) as u128
|
|
|
|
{
|
|
|
|
// the other process hasn't updated this file in a long time
|
|
|
|
// so maybe it was killed and the operating system hasn't
|
|
|
|
// released the file lock yet
|
|
|
|
return Self(None);
|
|
|
|
} else {
|
|
|
|
error_count = 0; // reset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
error_count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
error_count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(pb_update_guard); // explicit for clarity
|
|
|
|
Self(None)
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
log::debug!(
|
|
|
|
"Failed to open file lock at {}. {:#}",
|
|
|
|
file_path.display(),
|
|
|
|
err
|
|
|
|
);
|
|
|
|
Self(None) // let the process through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 20:40:26 -05:00
|
|
|
pub fn specifier_from_file_path(
|
|
|
|
path: &Path,
|
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
|
|
|
ModuleSpecifier::from_file_path(path)
|
|
|
|
.map_err(|_| anyhow!("Invalid file path '{}'", path.display()))
|
|
|
|
}
|
|
|
|
|
2019-09-07 14:13:09 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2023-03-08 10:13:13 -05:00
|
|
|
use deno_core::futures;
|
|
|
|
use deno_core::parking_lot::Mutex;
|
2022-12-07 13:10:10 -05:00
|
|
|
use pretty_assertions::assert_eq;
|
2023-06-10 11:09:45 -04:00
|
|
|
use test_util::PathRef;
|
2022-04-01 11:15:37 -04:00
|
|
|
use test_util::TempDir;
|
2023-03-08 10:13:13 -05:00
|
|
|
use tokio::sync::Notify;
|
2019-09-07 14:13:09 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_child() {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = current_dir().unwrap();
|
2020-01-20 09:45:44 -05:00
|
|
|
assert_eq!(resolve_from_cwd(Path::new("a")).unwrap(), cwd.join("a"));
|
2019-09-07 14:13:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_dot() {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = current_dir().unwrap();
|
2020-01-20 09:45:44 -05:00
|
|
|
assert_eq!(resolve_from_cwd(Path::new(".")).unwrap(), cwd);
|
2019-09-07 14:13:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_parent() {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = current_dir().unwrap();
|
2020-01-20 09:45:44 -05:00
|
|
|
assert_eq!(resolve_from_cwd(Path::new("a/..")).unwrap(), cwd);
|
2019-09-07 14:13:09 -04:00
|
|
|
}
|
|
|
|
|
2020-02-26 16:11:52 -05:00
|
|
|
#[test]
|
|
|
|
fn test_normalize_path() {
|
|
|
|
assert_eq!(normalize_path(Path::new("a/../b")), PathBuf::from("b"));
|
|
|
|
assert_eq!(normalize_path(Path::new("a/./b/")), PathBuf::from("a/b/"));
|
|
|
|
assert_eq!(
|
|
|
|
normalize_path(Path::new("a/./b/../c")),
|
|
|
|
PathBuf::from("a/c")
|
|
|
|
);
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
assert_eq!(
|
|
|
|
normalize_path(Path::new("C:\\a\\.\\b\\..\\c")),
|
|
|
|
PathBuf::from("C:\\a\\c")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 14:13:09 -04:00
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_absolute() {
|
2023-04-12 19:45:53 -04:00
|
|
|
let expected = Path::new("a");
|
|
|
|
let cwd = current_dir().unwrap();
|
|
|
|
let absolute_expected = cwd.join(expected);
|
|
|
|
assert_eq!(resolve_from_cwd(expected).unwrap(), absolute_expected);
|
2019-09-07 14:13:09 -04:00
|
|
|
}
|
2020-02-17 13:11:45 -05:00
|
|
|
|
2020-11-14 07:05:26 -05:00
|
|
|
#[test]
|
|
|
|
fn test_collect_files() {
|
2023-06-10 11:09:45 -04:00
|
|
|
fn create_files(dir_path: &PathRef, files: &[&str]) {
|
|
|
|
dir_path.create_dir_all();
|
2020-11-14 07:05:26 -05:00
|
|
|
for f in files {
|
2023-06-10 11:09:45 -04:00
|
|
|
dir_path.join(f).write("");
|
2020-11-14 07:05:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dir.ts
|
|
|
|
// ├── a.ts
|
|
|
|
// ├── b.js
|
|
|
|
// ├── child
|
2022-12-07 13:10:10 -05:00
|
|
|
// | ├── git
|
|
|
|
// | | └── git.js
|
2023-08-01 20:49:09 -04:00
|
|
|
// | ├── node_modules
|
|
|
|
// | | └── node_modules.js
|
2023-08-06 21:56:56 -04:00
|
|
|
// | ├── vendor
|
|
|
|
// | | └── vendor.js
|
2020-11-14 07:05:26 -05:00
|
|
|
// │ ├── e.mjs
|
|
|
|
// │ ├── f.mjsx
|
|
|
|
// │ ├── .foo.TS
|
|
|
|
// │ └── README.md
|
|
|
|
// ├── c.tsx
|
|
|
|
// ├── d.jsx
|
|
|
|
// └── ignore
|
|
|
|
// ├── g.d.ts
|
|
|
|
// └── .gitignore
|
|
|
|
|
2022-04-01 11:15:37 -04:00
|
|
|
let t = TempDir::new();
|
2020-11-14 07:05:26 -05:00
|
|
|
|
|
|
|
let root_dir_path = t.path().join("dir.ts");
|
|
|
|
let root_dir_files = ["a.ts", "b.js", "c.tsx", "d.jsx"];
|
|
|
|
create_files(&root_dir_path, &root_dir_files);
|
|
|
|
|
|
|
|
let child_dir_path = root_dir_path.join("child");
|
|
|
|
let child_dir_files = ["e.mjs", "f.mjsx", ".foo.TS", "README.md"];
|
|
|
|
create_files(&child_dir_path, &child_dir_files);
|
|
|
|
|
2022-12-07 13:10:10 -05:00
|
|
|
t.create_dir_all("dir.ts/child/node_modules");
|
|
|
|
t.write("dir.ts/child/node_modules/node_modules.js", "");
|
|
|
|
t.create_dir_all("dir.ts/child/.git");
|
|
|
|
t.write("dir.ts/child/.git/git.js", "");
|
2023-08-06 21:56:56 -04:00
|
|
|
t.create_dir_all("dir.ts/child/vendor");
|
|
|
|
t.write("dir.ts/child/vendor/vendor.js", "");
|
2022-12-07 13:10:10 -05:00
|
|
|
|
2020-11-14 07:05:26 -05:00
|
|
|
let ignore_dir_path = root_dir_path.join("ignore");
|
|
|
|
let ignore_dir_files = ["g.d.ts", ".gitignore"];
|
|
|
|
create_files(&ignore_dir_path, &ignore_dir_files);
|
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
let file_patterns = FilePatterns {
|
2024-01-18 15:57:30 -05:00
|
|
|
base: root_dir_path.to_path_buf(),
|
|
|
|
include: None,
|
2024-01-15 19:15:39 -05:00
|
|
|
exclude: PathOrPatternSet::new(vec![PathOrPattern::Path(
|
|
|
|
ignore_dir_path.to_path_buf(),
|
|
|
|
)]),
|
2024-01-08 12:18:42 -05:00
|
|
|
};
|
2024-03-07 20:16:32 -05:00
|
|
|
let file_collector = FileCollector::new(|e| {
|
2020-11-22 15:45:44 -05:00
|
|
|
// exclude dotfiles
|
2024-03-07 20:16:32 -05:00
|
|
|
e.path
|
2020-11-22 15:45:44 -05:00
|
|
|
.file_name()
|
|
|
|
.and_then(|f| f.to_str())
|
2023-03-15 17:46:36 -04:00
|
|
|
.map(|f| !f.starts_with('.'))
|
|
|
|
.unwrap_or(false)
|
2024-01-08 12:18:42 -05:00
|
|
|
});
|
2022-12-07 13:10:10 -05:00
|
|
|
|
|
|
|
let result = file_collector
|
2024-01-08 12:18:42 -05:00
|
|
|
.collect_file_patterns(file_patterns.clone())
|
2022-12-07 13:10:10 -05:00
|
|
|
.unwrap();
|
|
|
|
let expected = [
|
|
|
|
"README.md",
|
|
|
|
"a.ts",
|
|
|
|
"b.js",
|
|
|
|
"c.tsx",
|
|
|
|
"d.jsx",
|
|
|
|
"e.mjs",
|
|
|
|
"f.mjsx",
|
|
|
|
"git.js",
|
|
|
|
"node_modules.js",
|
2023-08-06 21:56:56 -04:00
|
|
|
"vendor.js",
|
2022-12-07 13:10:10 -05:00
|
|
|
];
|
|
|
|
let mut file_names = result
|
|
|
|
.into_iter()
|
|
|
|
.map(|r| r.file_name().unwrap().to_string_lossy().to_string())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
file_names.sort();
|
|
|
|
assert_eq!(file_names, expected);
|
|
|
|
|
|
|
|
// test ignoring the .git and node_modules folder
|
2023-08-01 20:49:09 -04:00
|
|
|
let file_collector = file_collector
|
|
|
|
.ignore_git_folder()
|
|
|
|
.ignore_node_modules()
|
2024-03-27 14:25:39 -04:00
|
|
|
.set_vendor_folder(Some(child_dir_path.join("vendor").to_path_buf()));
|
2022-12-07 13:10:10 -05:00
|
|
|
let result = file_collector
|
2024-01-08 12:18:42 -05:00
|
|
|
.collect_file_patterns(file_patterns.clone())
|
2022-12-07 13:10:10 -05:00
|
|
|
.unwrap();
|
2020-11-14 07:05:26 -05:00
|
|
|
let expected = [
|
2022-12-07 13:10:10 -05:00
|
|
|
"README.md",
|
2020-11-14 07:05:26 -05:00
|
|
|
"a.ts",
|
|
|
|
"b.js",
|
2022-12-07 13:10:10 -05:00
|
|
|
"c.tsx",
|
|
|
|
"d.jsx",
|
2020-11-14 07:05:26 -05:00
|
|
|
"e.mjs",
|
|
|
|
"f.mjsx",
|
2022-12-07 13:10:10 -05:00
|
|
|
];
|
|
|
|
let mut file_names = result
|
|
|
|
.into_iter()
|
|
|
|
.map(|r| r.file_name().unwrap().to_string_lossy().to_string())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
file_names.sort();
|
|
|
|
assert_eq!(file_names, expected);
|
|
|
|
|
|
|
|
// test opting out of ignoring by specifying the dir
|
2024-01-08 12:18:42 -05:00
|
|
|
let file_patterns = FilePatterns {
|
2024-01-18 15:57:30 -05:00
|
|
|
base: root_dir_path.to_path_buf(),
|
2024-01-15 19:15:39 -05:00
|
|
|
include: Some(PathOrPatternSet::new(vec![
|
|
|
|
PathOrPattern::Path(root_dir_path.to_path_buf()),
|
|
|
|
PathOrPattern::Path(
|
2024-01-08 12:18:42 -05:00
|
|
|
root_dir_path.to_path_buf().join("child/node_modules/"),
|
2024-01-15 19:15:39 -05:00
|
|
|
),
|
|
|
|
])),
|
|
|
|
exclude: PathOrPatternSet::new(vec![PathOrPattern::Path(
|
|
|
|
ignore_dir_path.to_path_buf(),
|
|
|
|
)]),
|
2024-01-08 12:18:42 -05:00
|
|
|
};
|
|
|
|
let result = file_collector.collect_file_patterns(file_patterns).unwrap();
|
2022-12-07 13:10:10 -05:00
|
|
|
let expected = [
|
2020-11-14 07:05:26 -05:00
|
|
|
"README.md",
|
2022-12-07 13:10:10 -05:00
|
|
|
"a.ts",
|
|
|
|
"b.js",
|
2020-11-14 07:05:26 -05:00
|
|
|
"c.tsx",
|
|
|
|
"d.jsx",
|
2022-12-07 13:10:10 -05:00
|
|
|
"e.mjs",
|
|
|
|
"f.mjsx",
|
|
|
|
"node_modules.js",
|
2020-11-14 07:05:26 -05:00
|
|
|
];
|
2022-12-07 13:10:10 -05:00
|
|
|
let mut file_names = result
|
|
|
|
.into_iter()
|
|
|
|
.map(|r| r.file_name().unwrap().to_string_lossy().to_string())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
file_names.sort();
|
|
|
|
assert_eq!(file_names, expected);
|
2020-11-14 07:05:26 -05:00
|
|
|
}
|
2021-08-13 06:06:49 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_collect_specifiers() {
|
2023-06-10 11:09:45 -04:00
|
|
|
fn create_files(dir_path: &PathRef, files: &[&str]) {
|
|
|
|
dir_path.create_dir_all();
|
2021-08-13 06:06:49 -04:00
|
|
|
for f in files {
|
2023-06-10 11:09:45 -04:00
|
|
|
dir_path.join(f).write("");
|
2021-08-13 06:06:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dir.ts
|
|
|
|
// ├── a.ts
|
|
|
|
// ├── b.js
|
|
|
|
// ├── child
|
|
|
|
// │ ├── e.mjs
|
|
|
|
// │ ├── f.mjsx
|
|
|
|
// │ ├── .foo.TS
|
|
|
|
// │ └── README.md
|
|
|
|
// ├── c.tsx
|
|
|
|
// ├── d.jsx
|
|
|
|
// └── ignore
|
|
|
|
// ├── g.d.ts
|
|
|
|
// └── .gitignore
|
|
|
|
|
2022-04-01 11:15:37 -04:00
|
|
|
let t = TempDir::new();
|
2021-08-13 06:06:49 -04:00
|
|
|
|
|
|
|
let root_dir_path = t.path().join("dir.ts");
|
|
|
|
let root_dir_files = ["a.ts", "b.js", "c.tsx", "d.jsx"];
|
|
|
|
create_files(&root_dir_path, &root_dir_files);
|
|
|
|
|
|
|
|
let child_dir_path = root_dir_path.join("child");
|
|
|
|
let child_dir_files = ["e.mjs", "f.mjsx", ".foo.TS", "README.md"];
|
|
|
|
create_files(&child_dir_path, &child_dir_files);
|
|
|
|
|
|
|
|
let ignore_dir_path = root_dir_path.join("ignore");
|
|
|
|
let ignore_dir_files = ["g.d.ts", ".gitignore"];
|
|
|
|
create_files(&ignore_dir_path, &ignore_dir_files);
|
|
|
|
|
2024-03-07 20:16:32 -05:00
|
|
|
let predicate = |e: WalkEntry| {
|
2022-07-04 10:46:10 -04:00
|
|
|
// exclude dotfiles
|
2024-03-07 20:16:32 -05:00
|
|
|
e.path
|
2022-07-04 10:46:10 -04:00
|
|
|
.file_name()
|
|
|
|
.and_then(|f| f.to_str())
|
2023-03-15 17:46:36 -04:00
|
|
|
.map(|f| !f.starts_with('.'))
|
|
|
|
.unwrap_or(false)
|
2022-07-04 10:46:10 -04:00
|
|
|
};
|
|
|
|
|
2021-08-13 06:06:49 -04:00
|
|
|
let result = collect_specifiers(
|
2024-01-08 12:18:42 -05:00
|
|
|
FilePatterns {
|
2024-01-18 15:57:30 -05:00
|
|
|
base: root_dir_path.to_path_buf(),
|
2024-01-08 12:18:42 -05:00
|
|
|
include: Some(
|
2024-03-07 20:16:32 -05:00
|
|
|
PathOrPatternSet::from_include_relative_path_or_patterns(
|
2024-01-15 19:15:39 -05:00
|
|
|
root_dir_path.as_path(),
|
|
|
|
&[
|
|
|
|
"http://localhost:8080".to_string(),
|
|
|
|
"./".to_string(),
|
|
|
|
"https://localhost:8080".to_string(),
|
|
|
|
],
|
|
|
|
)
|
2024-01-08 12:18:42 -05:00
|
|
|
.unwrap(),
|
|
|
|
),
|
2024-01-15 19:15:39 -05:00
|
|
|
exclude: PathOrPatternSet::new(vec![PathOrPattern::Path(
|
|
|
|
ignore_dir_path.to_path_buf(),
|
|
|
|
)]),
|
2023-01-07 15:22:09 -05:00
|
|
|
},
|
2024-03-27 14:25:39 -04:00
|
|
|
None,
|
2022-07-04 10:46:10 -04:00
|
|
|
predicate,
|
2021-08-13 06:06:49 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
let root_dir_url = ModuleSpecifier::from_file_path(&root_dir_path)
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
|
|
|
let expected = vec![
|
|
|
|
"http://localhost:8080/".to_string(),
|
|
|
|
"https://localhost:8080/".to_string(),
|
|
|
|
format!("{root_dir_url}/a.ts"),
|
|
|
|
format!("{root_dir_url}/b.js"),
|
|
|
|
format!("{root_dir_url}/c.tsx"),
|
|
|
|
format!("{root_dir_url}/child/README.md"),
|
|
|
|
format!("{root_dir_url}/child/e.mjs"),
|
|
|
|
format!("{root_dir_url}/child/f.mjsx"),
|
|
|
|
format!("{root_dir_url}/d.jsx"),
|
|
|
|
];
|
2022-07-04 10:46:10 -04:00
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
assert_eq!(
|
|
|
|
result
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| s.to_string())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
expected
|
|
|
|
);
|
2022-07-04 10:46:10 -04:00
|
|
|
|
|
|
|
let scheme = if cfg!(target_os = "windows") {
|
|
|
|
"file:///"
|
|
|
|
} else {
|
|
|
|
"file://"
|
|
|
|
};
|
|
|
|
let result = collect_specifiers(
|
2024-01-08 12:18:42 -05:00
|
|
|
FilePatterns {
|
2024-01-18 15:57:30 -05:00
|
|
|
base: root_dir_path.to_path_buf(),
|
2024-01-15 19:15:39 -05:00
|
|
|
include: Some(PathOrPatternSet::new(vec![PathOrPattern::new(
|
|
|
|
&format!(
|
2024-01-08 12:18:42 -05:00
|
|
|
"{}{}",
|
|
|
|
scheme,
|
|
|
|
root_dir_path.join("child").to_string().replace('\\', "/")
|
2024-01-15 19:15:39 -05:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.unwrap()])),
|
2024-01-08 12:18:42 -05:00
|
|
|
exclude: Default::default(),
|
2023-01-07 15:22:09 -05:00
|
|
|
},
|
2024-03-27 14:25:39 -04:00
|
|
|
None,
|
2022-07-04 10:46:10 -04:00
|
|
|
predicate,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
let expected = vec![
|
|
|
|
format!("{root_dir_url}/child/README.md"),
|
|
|
|
format!("{root_dir_url}/child/e.mjs"),
|
|
|
|
format!("{root_dir_url}/child/f.mjsx"),
|
|
|
|
];
|
2021-08-13 06:06:49 -04:00
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
assert_eq!(
|
|
|
|
result
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| s.to_string())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
expected
|
|
|
|
);
|
2021-08-13 06:06:49 -04:00
|
|
|
}
|
2021-10-25 11:54:36 -04:00
|
|
|
|
2023-03-08 10:13:13 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn lax_fs_lock() {
|
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let lock_path = temp_dir.path().join("file.lock");
|
|
|
|
let signal1 = Arc::new(Notify::new());
|
|
|
|
let signal2 = Arc::new(Notify::new());
|
|
|
|
let signal3 = Arc::new(Notify::new());
|
|
|
|
let signal4 = Arc::new(Notify::new());
|
|
|
|
tokio::spawn({
|
|
|
|
let lock_path = lock_path.clone();
|
|
|
|
let signal1 = signal1.clone();
|
|
|
|
let signal2 = signal2.clone();
|
|
|
|
let signal3 = signal3.clone();
|
|
|
|
let signal4 = signal4.clone();
|
|
|
|
let temp_dir = temp_dir.clone();
|
|
|
|
async move {
|
|
|
|
let flag =
|
2023-06-10 11:09:45 -04:00
|
|
|
LaxSingleProcessFsFlag::lock(lock_path.to_path_buf(), "waiting")
|
|
|
|
.await;
|
2023-03-08 10:13:13 -05:00
|
|
|
signal1.notify_one();
|
|
|
|
signal2.notified().await;
|
|
|
|
tokio::time::sleep(Duration::from_millis(10)).await; // give the other thread time to acquire the lock
|
|
|
|
temp_dir.write("file.txt", "update1");
|
|
|
|
signal3.notify_one();
|
|
|
|
signal4.notified().await;
|
|
|
|
drop(flag);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let signal5 = Arc::new(Notify::new());
|
|
|
|
tokio::spawn({
|
|
|
|
let temp_dir = temp_dir.clone();
|
|
|
|
let signal5 = signal5.clone();
|
|
|
|
async move {
|
|
|
|
signal1.notified().await;
|
|
|
|
signal2.notify_one();
|
2023-06-10 11:09:45 -04:00
|
|
|
let flag =
|
|
|
|
LaxSingleProcessFsFlag::lock(lock_path.to_path_buf(), "waiting")
|
|
|
|
.await;
|
2023-03-08 10:13:13 -05:00
|
|
|
temp_dir.write("file.txt", "update2");
|
|
|
|
signal5.notify_one();
|
|
|
|
drop(flag);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
signal3.notified().await;
|
|
|
|
assert_eq!(temp_dir.read_to_string("file.txt"), "update1");
|
|
|
|
signal4.notify_one();
|
|
|
|
signal5.notified().await;
|
|
|
|
assert_eq!(temp_dir.read_to_string("file.txt"), "update2");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn lax_fs_lock_ordered() {
|
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let lock_path = temp_dir.path().join("file.lock");
|
|
|
|
let output_path = temp_dir.path().join("output");
|
|
|
|
let expected_order = Arc::new(Mutex::new(Vec::new()));
|
|
|
|
let count = 10;
|
|
|
|
let mut tasks = Vec::with_capacity(count);
|
|
|
|
|
|
|
|
std::fs::write(&output_path, "").unwrap();
|
|
|
|
|
|
|
|
for i in 0..count {
|
|
|
|
let lock_path = lock_path.clone();
|
|
|
|
let output_path = output_path.clone();
|
|
|
|
let expected_order = expected_order.clone();
|
|
|
|
tasks.push(tokio::spawn(async move {
|
|
|
|
let flag =
|
2023-06-10 11:09:45 -04:00
|
|
|
LaxSingleProcessFsFlag::lock(lock_path.to_path_buf(), "waiting")
|
|
|
|
.await;
|
2023-03-08 10:13:13 -05:00
|
|
|
expected_order.lock().push(i.to_string());
|
|
|
|
// be extremely racy
|
|
|
|
let mut output = std::fs::read_to_string(&output_path).unwrap();
|
|
|
|
if !output.is_empty() {
|
|
|
|
output.push('\n');
|
|
|
|
}
|
|
|
|
output.push_str(&i.to_string());
|
|
|
|
std::fs::write(&output_path, output).unwrap();
|
|
|
|
drop(flag);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
futures::future::join_all(tasks).await;
|
|
|
|
let expected_output = expected_order.lock().join("\n");
|
|
|
|
assert_eq!(
|
|
|
|
std::fs::read_to_string(output_path).unwrap(),
|
|
|
|
expected_output
|
|
|
|
);
|
|
|
|
}
|
2020-02-17 13:11:45 -05:00
|
|
|
}
|