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

fix(cli): normalize test command errors (#11375)

This commit is contained in:
Casper Beyer 2021-07-22 19:34:29 +08:00 committed by GitHub
parent 78fc9a4c60
commit 4861b13aab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 27 deletions

View file

@ -1234,7 +1234,7 @@ async fn test_command(
tools::test_runner::is_supported, tools::test_runner::is_supported,
)?; )?;
let failed = test_runner::run_tests( test_runner::run_tests(
program_state.clone(), program_state.clone(),
permissions, permissions,
lib, lib,
@ -1249,10 +1249,6 @@ async fn test_command(
concurrent_jobs, concurrent_jobs,
) )
.await?; .await?;
if failed {
std::process::exit(1);
}
} }
Ok(()) Ok(())

View file

@ -32,3 +32,4 @@ failures:
test result: FAILED. 0 passed; 3 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD]) test result: FAILED. 0 passed; 3 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD])
error: Test failed

View file

@ -78,3 +78,4 @@ failures:
test result: FAILED. 0 passed; 10 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD]) test result: FAILED. 0 passed; 10 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD])
error: Test failed

View file

@ -15,3 +15,4 @@ failures:
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD]) test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD])
error: Test failed

View file

@ -16,3 +16,4 @@ failures:
test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD]) test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD])
error: Test failed

View file

@ -4,5 +4,4 @@ test only ... ok ([WILDCARD])
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out ([WILDCARD]) test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out ([WILDCARD])
FAILED because the "only" option was used error: Test failed because the "only" option was used

View file

@ -11,6 +11,7 @@ use crate::module_graph;
use crate::program_state::ProgramState; use crate::program_state::ProgramState;
use crate::tokio_util; use crate::tokio_util;
use crate::tools::coverage::CoverageCollector; use crate::tools::coverage::CoverageCollector;
use deno_core::error::generic_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::futures::future; use deno_core::futures::future;
use deno_core::futures::stream; use deno_core::futures::stream;
@ -320,7 +321,6 @@ pub async fn run_test_file(
/// Runs tests. /// Runs tests.
/// ///
/// Returns a boolean indicating whether the tests failed.
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub async fn run_tests( pub async fn run_tests(
program_state: Arc<ProgramState>, program_state: Arc<ProgramState>,
@ -335,7 +335,11 @@ pub async fn run_tests(
filter: Option<String>, filter: Option<String>,
shuffle: Option<u64>, shuffle: Option<u64>,
concurrent_jobs: usize, concurrent_jobs: usize,
) -> Result<bool, AnyError> { ) -> Result<(), AnyError> {
if !allow_none && doc_modules.is_empty() && test_modules.is_empty() {
return Err(generic_error("No test modules found"));
}
let test_modules = if let Some(seed) = shuffle { let test_modules = if let Some(seed) = shuffle {
let mut rng = SmallRng::seed_from_u64(seed); let mut rng = SmallRng::seed_from_u64(seed);
let mut test_modules = test_modules.clone(); let mut test_modules = test_modules.clone();
@ -423,13 +427,6 @@ pub async fn run_tests(
program_state.maybe_import_map.clone(), program_state.maybe_import_map.clone(),
) )
.await?; .await?;
} else if test_modules.is_empty() {
println!("No matching test modules found");
if !allow_none {
std::process::exit(1);
}
return Ok(false);
} }
program_state program_state
@ -443,7 +440,7 @@ pub async fn run_tests(
.await?; .await?;
if no_run { if no_run {
return Ok(false); return Ok(());
} }
// Because scripts, and therefore worker.execute cannot detect unresolved promises at the moment // Because scripts, and therefore worker.execute cannot detect unresolved promises at the moment
@ -493,7 +490,7 @@ pub async fn run_tests(
}) })
}); });
let join_futures = stream::iter(join_handles) let join_stream = stream::iter(join_handles)
.buffer_unordered(concurrent_jobs) .buffer_unordered(concurrent_jobs)
.collect::<Vec<Result<Result<(), AnyError>, tokio::task::JoinError>>>(); .collect::<Vec<Result<Result<(), AnyError>, tokio::task::JoinError>>>();
@ -552,17 +549,20 @@ pub async fn run_tests(
reporter.report_summary(&summary, &elapsed); reporter.report_summary(&summary, &elapsed);
if used_only { if used_only {
println!( return Err(generic_error(
"{} because the \"only\" option was used\n", "Test failed because the \"only\" option was used",
colors::red("FAILED") ));
);
} }
used_only || summary.failed > 0 if summary.failed > 0 {
return Err(generic_error("Test failed"));
}
Ok(())
}) })
}; };
let (result, join_results) = future::join(handler, join_futures).await; let (join_results, result) = future::join(join_stream, handler).await;
let mut join_errors = join_results.into_iter().filter_map(|join_result| { let mut join_errors = join_results.into_iter().filter_map(|join_result| {
join_result join_result
@ -572,10 +572,22 @@ pub async fn run_tests(
}); });
if let Some(e) = join_errors.next() { if let Some(e) = join_errors.next() {
Err(e) return Err(e);
} else {
Ok(result.unwrap_or(false))
} }
match result {
Ok(result) => {
if let Some(err) = result.err() {
return Err(err);
}
}
Err(err) => {
return Err(err.into());
}
}
Ok(())
} }
#[cfg(test)] #[cfg(test)]