1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 04:52:26 -05:00

reduce dependency on outer CliOptions

This commit is contained in:
Nayeem Rahman 2024-12-16 11:57:08 +00:00
parent 5d76fc9d3e
commit 5807b53dae
4 changed files with 78 additions and 158 deletions

View file

@ -78,7 +78,6 @@ use std::io::Cursor;
use std::io::Read;
use std::io::Seek;
use std::net::SocketAddr;
use std::num::NonZeroUsize;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
@ -264,22 +263,6 @@ impl CacheSetting {
}
}
pub struct WorkspaceBenchOptions {
pub filter: Option<String>,
pub json: bool,
pub no_run: bool,
}
impl WorkspaceBenchOptions {
pub fn resolve(bench_flags: &BenchFlags) -> Self {
Self {
filter: bench_flags.filter.clone(),
json: bench_flags.json,
no_run: bench_flags.no_run,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BenchOptions {
pub files: FilePatterns,
@ -375,41 +358,6 @@ fn resolve_fmt_options(
options
}
#[derive(Clone, Debug)]
pub struct WorkspaceTestOptions {
pub doc: bool,
pub no_run: bool,
pub fail_fast: Option<NonZeroUsize>,
pub permit_no_files: bool,
pub filter: Option<String>,
pub shuffle: Option<u64>,
pub concurrent_jobs: NonZeroUsize,
pub trace_leaks: bool,
pub reporter: TestReporterConfig,
pub junit_path: Option<String>,
pub hide_stacktraces: bool,
}
impl WorkspaceTestOptions {
pub fn resolve(test_flags: &TestFlags) -> Self {
Self {
permit_no_files: test_flags.permit_no_files,
concurrent_jobs: test_flags
.concurrent_jobs
.unwrap_or_else(|| NonZeroUsize::new(1).unwrap()),
doc: test_flags.doc,
fail_fast: test_flags.fail_fast,
filter: test_flags.filter.clone(),
no_run: test_flags.no_run,
shuffle: test_flags.shuffle,
trace_leaks: test_flags.trace_leaks,
reporter: test_flags.reporter,
junit_path: test_flags.junit_path.clone(),
hide_stacktraces: test_flags.hide_stacktraces,
}
}
}
#[derive(Debug, Clone)]
pub struct TestOptions {
pub files: FilePatterns,
@ -1501,13 +1449,6 @@ impl CliOptions {
})
}
pub fn resolve_workspace_test_options(
&self,
test_flags: &TestFlags,
) -> WorkspaceTestOptions {
WorkspaceTestOptions::resolve(test_flags)
}
pub fn resolve_test_options_for_members(
&self,
test_flags: &TestFlags,
@ -1533,13 +1474,6 @@ impl CliOptions {
Ok(member_options)
}
pub fn resolve_workspace_bench_options(
&self,
bench_flags: &BenchFlags,
) -> WorkspaceBenchOptions {
WorkspaceBenchOptions::resolve(bench_flags)
}
pub fn resolve_bench_options_for_members(
&self,
bench_flags: &BenchFlags,

View file

@ -1163,6 +1163,7 @@ impl WorkspaceDirFilesFactory {
pub struct WorkspaceFilesFactory {
dirs: Vec<WorkspaceDirFilesFactory>,
initial_cwd: PathBuf,
}
impl WorkspaceFilesFactory {
@ -1186,6 +1187,10 @@ impl WorkspaceFilesFactory {
cli_options: &CliOptions,
watcher_communicator: Option<&Arc<WatcherCommunicator>>,
) -> Result<Self, AnyError> {
let initial_cwd = cli_options.initial_cwd().to_path_buf();
if let Some(watcher_communicator) = watcher_communicator {
let _ = watcher_communicator.watch_paths(cli_options.watch_paths());
}
workspace_dirs_with_files.sort_by_cached_key(|(d, _)| d.dir_url().clone());
let all_scopes = Arc::new(
workspace_dirs_with_files
@ -1197,6 +1202,15 @@ impl WorkspaceFilesFactory {
let dir_count = workspace_dirs_with_files.len();
let mut dirs = Vec::with_capacity(dir_count);
for (workspace_dir, files) in workspace_dirs_with_files {
if let Some(watcher_communicator) = watcher_communicator {
let _ = watcher_communicator.watch_paths(
files
.include
.iter()
.flat_map(|set| set.base_paths())
.collect(),
);
}
let scope_options = (dir_count > 1).then(|| ScopeOptions {
scope: workspace_dir
.has_deno_or_pkg_json()
@ -1237,7 +1251,19 @@ impl WorkspaceFilesFactory {
permissions_options: Default::default(),
});
}
Ok(Self { dirs })
Ok(Self { dirs, initial_cwd })
}
pub fn dirs(&self) -> &Vec<WorkspaceDirFilesFactory> {
&self.dirs
}
pub fn initial_cwd(&self) -> &PathBuf {
&self.initial_cwd
}
pub fn found_specifiers(&self) -> bool {
self.dirs.iter().any(|e| !e.specifiers.is_empty())
}
pub async fn check(&self) -> Result<(), AnyError> {
@ -1282,12 +1308,4 @@ impl WorkspaceFilesFactory {
}
Ok(())
}
pub fn dirs(&self) -> &Vec<WorkspaceDirFilesFactory> {
&self.dirs
}
pub fn found_specifiers(&self) -> bool {
self.dirs.iter().any(|e| !e.specifiers.is_empty())
}
}

View file

@ -432,11 +432,8 @@ pub async fn run_benchmarks(
flags: Arc<Flags>,
bench_flags: BenchFlags,
) -> Result<(), AnyError> {
let log_level = flags.log_level;
let cli_options = CliOptions::from_flags(flags)?;
let workspace_bench_options =
cli_options.resolve_workspace_bench_options(&bench_flags);
let log_level = cli_options.log_level();
let workspace_dirs_with_files = cli_options
.resolve_bench_options_for_members(&bench_flags)?
.into_iter()
@ -472,7 +469,7 @@ pub async fn run_benchmarks(
workspace_files_factory.check().await?;
if workspace_bench_options.no_run {
if bench_flags.no_run {
return Ok(());
}
@ -480,8 +477,8 @@ pub async fn run_benchmarks(
&workspace_files_factory,
None,
BenchSpecifierOptions {
filter: TestFilter::from_flag(&workspace_bench_options.filter),
json: workspace_bench_options.json,
filter: TestFilter::from_flag(&bench_flags.filter),
json: bench_flags.json,
log_level,
},
)
@ -508,25 +505,13 @@ pub async fn run_benchmarks_with_watch(
let bench_flags = bench_flags.clone();
watcher_communicator.show_path_changed(changed_paths.clone());
Ok(async move {
let log_level: Option<Level> = flags.log_level;
let cli_options = CliOptions::from_flags(flags)?;
let workspace_bench_options =
cli_options.resolve_workspace_bench_options(&bench_flags);
let log_level = cli_options.log_level();
let _ = watcher_communicator.watch_paths(cli_options.watch_paths());
let workspace_dirs_with_files = cli_options
.resolve_bench_options_for_members(&bench_flags)?
.into_iter()
.map(|(d, o)| (d, o.files))
.collect::<Vec<_>>();
let watch_paths = workspace_dirs_with_files
.iter()
.filter_map(|(_, files)| {
files.include.as_ref().map(|set| set.base_paths())
})
.flatten()
.collect::<Vec<_>>();
let _ = watcher_communicator.watch_paths(watch_paths);
let workspace_files_factory =
WorkspaceFilesFactory::from_workspace_dirs_with_files(
workspace_dirs_with_files,
@ -554,7 +539,7 @@ pub async fn run_benchmarks_with_watch(
workspace_files_factory.check().await?;
if workspace_bench_options.no_run {
if bench_flags.no_run {
return Ok(());
}
@ -562,8 +547,8 @@ pub async fn run_benchmarks_with_watch(
&workspace_files_factory,
changed_paths.map(|p| p.into_iter().collect()).as_ref(),
BenchSpecifierOptions {
filter: TestFilter::from_flag(&workspace_bench_options.filter),
json: workspace_bench_options.json,
filter: TestFilter::from_flag(&bench_flags.filter),
json: bench_flags.json,
log_level,
},
)

View file

@ -1497,14 +1497,8 @@ pub async fn run_tests(
flags: Arc<Flags>,
test_flags: TestFlags,
) -> Result<(), AnyError> {
let log_level = flags.log_level;
let cli_options = CliOptions::from_flags(flags)?;
let workspace_test_options =
cli_options.resolve_workspace_test_options(&test_flags);
// Various test files should not share the same permissions in terms of
// `PermissionsContainer` - otherwise granting/revoking permissions in one
// file would have impact on other files, which is undesirable.
let log_level = cli_options.log_level();
let workspace_dirs_with_files = cli_options
.resolve_test_options_for_members(&test_flags)?
.into_iter()
@ -1517,47 +1511,47 @@ pub async fn run_tests(
collect_specifiers_for_tests(patterns, cli_options, file_fetcher, doc)
.boxed_local()
},
workspace_test_options.doc,
test_flags.doc,
Some(extract_doc_tests),
&cli_options,
None,
)
.await?;
if !workspace_test_options.permit_no_files
&& !workspace_files_factory.found_specifiers()
if !test_flags.permit_no_files && !workspace_files_factory.found_specifiers()
{
return Err(generic_error("No test modules found"));
}
workspace_files_factory.check().await?;
if workspace_test_options.no_run {
if test_flags.no_run {
return Ok(());
}
let initial_cwd = workspace_files_factory.initial_cwd();
test_specifiers(
&workspace_files_factory,
None,
TestSpecifiersOptions {
cwd: Url::from_directory_path(cli_options.initial_cwd()).map_err(
|_| {
generic_error(format!(
"Unable to construct URL from the path of cwd: {}",
cli_options.initial_cwd().to_string_lossy(),
))
},
)?,
concurrent_jobs: workspace_test_options.concurrent_jobs,
fail_fast: workspace_test_options.fail_fast,
cwd: Url::from_directory_path(initial_cwd).map_err(|_| {
generic_error(format!(
"Unable to construct URL from the path of cwd: {}",
initial_cwd.to_string_lossy(),
))
})?,
concurrent_jobs: test_flags
.concurrent_jobs
.unwrap_or_else(|| NonZeroUsize::new(1).unwrap()),
fail_fast: test_flags.fail_fast,
log_level,
filter: workspace_test_options.filter.is_some(),
reporter: workspace_test_options.reporter,
junit_path: workspace_test_options.junit_path,
hide_stacktraces: workspace_test_options.hide_stacktraces,
filter: test_flags.filter.is_some(),
reporter: test_flags.reporter,
junit_path: test_flags.junit_path,
hide_stacktraces: test_flags.hide_stacktraces,
specifier: TestSpecifierOptions {
filter: TestFilter::from_flag(&workspace_test_options.filter),
shuffle: workspace_test_options.shuffle,
trace_leaks: workspace_test_options.trace_leaks,
filter: TestFilter::from_flag(&test_flags.filter),
shuffle: test_flags.shuffle,
trace_leaks: test_flags.trace_leaks,
},
},
)
@ -1598,25 +1592,13 @@ pub async fn run_tests_with_watch(
let test_flags = test_flags.clone();
watcher_communicator.show_path_changed(changed_paths.clone());
Ok(async move {
let log_level = flags.log_level;
let cli_options = CliOptions::from_flags(flags)?;
let workspace_test_options =
cli_options.resolve_workspace_test_options(&test_flags);
let log_level = cli_options.log_level();
let _ = watcher_communicator.watch_paths(cli_options.watch_paths());
let workspace_dirs_with_files = cli_options
.resolve_test_options_for_members(&test_flags)?
.into_iter()
.map(|(d, o)| (d, o.files))
.collect::<Vec<_>>();
let watch_paths = workspace_dirs_with_files
.iter()
.filter_map(|(_, files)| {
files.include.as_ref().map(|set| set.base_paths())
})
.flatten()
.collect::<Vec<_>>();
let _ = watcher_communicator.watch_paths(watch_paths);
let workspace_files_factory =
WorkspaceFilesFactory::from_workspace_dirs_with_files(
workspace_dirs_with_files,
@ -1629,7 +1611,7 @@ pub async fn run_tests_with_watch(
)
.boxed_local()
},
workspace_test_options.doc,
test_flags.doc,
Some(extract_doc_tests),
&cli_options,
Some(&watcher_communicator),
@ -1638,33 +1620,34 @@ pub async fn run_tests_with_watch(
workspace_files_factory.check().await?;
if workspace_test_options.no_run {
if test_flags.no_run {
return Ok(());
}
let initial_cwd = workspace_files_factory.initial_cwd();
test_specifiers(
&workspace_files_factory,
changed_paths.map(|p| p.into_iter().collect()).as_ref(),
TestSpecifiersOptions {
cwd: Url::from_directory_path(cli_options.initial_cwd()).map_err(
|_| {
generic_error(format!(
"Unable to construct URL from the path of cwd: {}",
cli_options.initial_cwd().to_string_lossy(),
))
},
)?,
concurrent_jobs: workspace_test_options.concurrent_jobs,
fail_fast: workspace_test_options.fail_fast,
cwd: Url::from_directory_path(initial_cwd).map_err(|_| {
generic_error(format!(
"Unable to construct URL from the path of cwd: {}",
initial_cwd.to_string_lossy(),
))
})?,
concurrent_jobs: test_flags
.concurrent_jobs
.unwrap_or_else(|| NonZeroUsize::new(1).unwrap()),
fail_fast: test_flags.fail_fast,
log_level,
filter: workspace_test_options.filter.is_some(),
reporter: workspace_test_options.reporter,
junit_path: workspace_test_options.junit_path,
hide_stacktraces: workspace_test_options.hide_stacktraces,
filter: test_flags.filter.is_some(),
reporter: test_flags.reporter,
junit_path: test_flags.junit_path,
hide_stacktraces: test_flags.hide_stacktraces,
specifier: TestSpecifierOptions {
filter: TestFilter::from_flag(&workspace_test_options.filter),
shuffle: workspace_test_options.shuffle,
trace_leaks: workspace_test_options.trace_leaks,
filter: TestFilter::from_flag(&test_flags.filter),
shuffle: test_flags.shuffle,
trace_leaks: test_flags.trace_leaks,
},
},
)