summaryrefslogtreecommitdiff
path: root/cli/tools/test.rs
diff options
context:
space:
mode:
authorGeert-Jan Zwiers <geertjanzwiers@protonmail.com>2023-01-07 21:22:09 +0100
committerGitHub <noreply@github.com>2023-01-07 15:22:09 -0500
commit84ef26ac9b5f0e1199d77837cd97cb203baa8729 (patch)
treeeb8f3422d397724d004dba0e9667b3f6c2b49a57 /cli/tools/test.rs
parentfac64478157ee563b185edb5734688e4523df3a1 (diff)
refactor(cli/tools): move flag and config logic to CliOptions (#17008)
Co-authored-by: David Sherret <dsherret@gmail.com>
Diffstat (limited to 'cli/tools/test.rs')
-rw-r--r--cli/tools/test.rs120
1 files changed, 37 insertions, 83 deletions
diff --git a/cli/tools/test.rs b/cli/tools/test.rs
index 9d1774fff..4cac8a404 100644
--- a/cli/tools/test.rs
+++ b/cli/tools/test.rs
@@ -1,7 +1,8 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
-use crate::args::Flags;
-use crate::args::TestFlags;
+use crate::args::CliOptions;
+use crate::args::FilesConfig;
+use crate::args::TestOptions;
use crate::args::TypeCheckMode;
use crate::colors;
use crate::display;
@@ -16,7 +17,6 @@ use crate::util::file_watcher::ResolutionResult;
use crate::util::fs::collect_specifiers;
use crate::util::path::get_extension;
use crate::util::path::is_supported_ext;
-use crate::util::path::specifier_to_file_path;
use crate::worker::create_main_worker_for_test_or_bench;
use deno_ast::swc::common::comments::CommentKind;
@@ -1237,15 +1237,13 @@ fn is_supported_test_ext(path: &Path) -> bool {
/// - Specifiers matching the `is_supported_test_path` are marked as `TestMode::Executable`.
/// - Specifiers matching both predicates are marked as `TestMode::Both`
fn collect_specifiers_with_test_mode(
- include: Vec<String>,
- ignore: Vec<PathBuf>,
+ files: FilesConfig,
include_inline: bool,
) -> Result<Vec<(ModuleSpecifier, TestMode)>, AnyError> {
- let module_specifiers =
- collect_specifiers(include.clone(), &ignore, is_supported_test_path)?;
+ let module_specifiers = collect_specifiers(&files, is_supported_test_path)?;
if include_inline {
- return collect_specifiers(include, &ignore, is_supported_test_ext).map(
+ return collect_specifiers(&files, is_supported_test_ext).map(
|specifiers| {
specifiers
.into_iter()
@@ -1281,44 +1279,10 @@ fn collect_specifiers_with_test_mode(
/// as well.
async fn fetch_specifiers_with_test_mode(
ps: &ProcState,
- include: Vec<String>,
- ignore: Vec<PathBuf>,
- include_inline: bool,
+ files: FilesConfig,
+ doc: bool,
) -> Result<Vec<(ModuleSpecifier, TestMode)>, AnyError> {
- let maybe_test_config = ps.options.to_test_config()?;
-
- let mut include_files = include.clone();
- let mut exclude_files = ignore.clone();
-
- if let Some(test_config) = maybe_test_config.as_ref() {
- if include_files.is_empty() {
- include_files = test_config
- .files
- .include
- .iter()
- .map(|s| s.to_string())
- .collect::<Vec<_>>();
- }
-
- if exclude_files.is_empty() {
- exclude_files = test_config
- .files
- .exclude
- .iter()
- .filter_map(|s| specifier_to_file_path(s).ok())
- .collect::<Vec<_>>();
- }
- }
-
- if include_files.is_empty() {
- include_files.push(".".to_string());
- }
-
- let mut specifiers_with_mode = collect_specifiers_with_test_mode(
- include_files,
- exclude_files,
- include_inline,
- )?;
+ let mut specifiers_with_mode = collect_specifiers_with_test_mode(files, doc)?;
for (specifier, mode) in &mut specifiers_with_mode {
let file = ps
.file_fetcher
@@ -1336,31 +1300,28 @@ async fn fetch_specifiers_with_test_mode(
}
pub async fn run_tests(
- flags: Flags,
- test_flags: TestFlags,
+ cli_options: CliOptions,
+ test_options: TestOptions,
) -> Result<(), AnyError> {
- let ps = ProcState::build(flags).await?;
+ let ps = ProcState::from_options(Arc::new(cli_options)).await?;
// 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 permissions =
Permissions::from_options(&ps.options.permissions_options())?;
- let specifiers_with_mode = fetch_specifiers_with_test_mode(
- &ps,
- test_flags.include,
- test_flags.ignore.clone(),
- test_flags.doc,
- )
- .await?;
- if !test_flags.allow_none && specifiers_with_mode.is_empty() {
+ let specifiers_with_mode =
+ fetch_specifiers_with_test_mode(&ps, test_options.files, test_options.doc)
+ .await?;
+
+ if !test_options.allow_none && specifiers_with_mode.is_empty() {
return Err(generic_error("No test modules found"));
}
check_specifiers(&ps, permissions.clone(), specifiers_with_mode.clone())
.await?;
- if test_flags.no_run {
+ if test_options.no_run {
return Ok(());
}
@@ -1369,9 +1330,9 @@ pub async fn run_tests(
permissions,
specifiers_with_mode,
TestSpecifierOptions {
- concurrent_jobs: test_flags.concurrent_jobs,
- fail_fast: test_flags.fail_fast,
- filter: TestFilter::from_flag(&test_flags.filter),
+ concurrent_jobs: test_options.concurrent_jobs,
+ fail_fast: test_options.fail_fast,
+ filter: TestFilter::from_flag(&test_options.filter),
},
)
.await?;
@@ -1380,35 +1341,32 @@ pub async fn run_tests(
}
pub async fn run_tests_with_watch(
- flags: Flags,
- test_flags: TestFlags,
+ cli_options: CliOptions,
+ test_options: TestOptions,
) -> Result<(), AnyError> {
- let ps = ProcState::build(flags).await?;
+ let ps = ProcState::from_options(Arc::new(cli_options)).await?;
// 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 permissions =
Permissions::from_options(&ps.options.permissions_options())?;
- let include = test_flags.include;
- let ignore = test_flags.ignore.clone();
- let paths_to_watch: Vec<_> = include.iter().map(PathBuf::from).collect();
+ let paths_to_watch: Vec<_> = test_options.files.include.clone();
let no_check = ps.options.type_check_mode() == TypeCheckMode::None;
+ let test_options = &test_options;
let resolver = |changed: Option<Vec<PathBuf>>| {
let paths_to_watch = paths_to_watch.clone();
let paths_to_watch_clone = paths_to_watch.clone();
let files_changed = changed.is_some();
- let include = include.clone();
- let ignore = ignore.clone();
let ps = ps.clone();
async move {
- let test_modules = if test_flags.doc {
- collect_specifiers(include.clone(), &ignore, is_supported_test_ext)
+ let test_modules = if test_options.doc {
+ collect_specifiers(&test_options.files, is_supported_test_ext)
} else {
- collect_specifiers(include.clone(), &ignore, is_supported_test_path)
+ collect_specifiers(&test_options.files, is_supported_test_path)
}?;
let mut paths_to_watch = paths_to_watch_clone;
@@ -1517,20 +1475,16 @@ pub async fn run_tests_with_watch(
})
};
- let cli_options = ps.options.clone();
let operation = |modules_to_reload: Vec<(ModuleSpecifier, ModuleKind)>| {
- let filter = test_flags.filter.clone();
- let include = include.clone();
- let ignore = ignore.clone();
let permissions = permissions.clone();
let ps = ps.clone();
+ let test_options = test_options.clone();
async move {
let specifiers_with_mode = fetch_specifiers_with_test_mode(
&ps,
- include.clone(),
- ignore.clone(),
- test_flags.doc,
+ test_options.files,
+ test_options.doc,
)
.await?
.iter()
@@ -1543,7 +1497,7 @@ pub async fn run_tests_with_watch(
check_specifiers(&ps, permissions.clone(), specifiers_with_mode.clone())
.await?;
- if test_flags.no_run {
+ if test_options.no_run {
return Ok(());
}
@@ -1552,9 +1506,9 @@ pub async fn run_tests_with_watch(
permissions.clone(),
specifiers_with_mode,
TestSpecifierOptions {
- concurrent_jobs: test_flags.concurrent_jobs,
- fail_fast: test_flags.fail_fast,
- filter: TestFilter::from_flag(&filter),
+ concurrent_jobs: test_options.concurrent_jobs,
+ fail_fast: test_options.fail_fast,
+ filter: TestFilter::from_flag(&test_options.filter),
},
)
.await?;
@@ -1568,7 +1522,7 @@ pub async fn run_tests_with_watch(
operation,
file_watcher::PrintConfig {
job_name: "Test".to_string(),
- clear_screen: !cli_options.no_clear_screen(),
+ clear_screen: !ps.options.no_clear_screen(),
},
)
.await?;