diff --git a/cli/tests/integration/check_tests.rs b/cli/tests/integration/check_tests.rs index d1148b2444..7f9ff3ff54 100644 --- a/cli/tests/integration/check_tests.rs +++ b/cli/tests/integration/check_tests.rs @@ -1,11 +1,10 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use std::process::Command; -use std::process::Stdio; use test_util as util; use util::env_vars_for_npm_tests; use util::env_vars_for_npm_tests_no_sync_download; -use util::TempDir; +use util::TestContext; itest!(_095_check_with_bare_import { args: "check cache/095_cache_with_bare_import.ts", @@ -87,31 +86,34 @@ itest!(check_no_error_truncation { #[test] fn cache_switching_config_then_no_config() { - let deno_dir = util::new_deno_dir(); - assert!(does_type_checking(&deno_dir, true)); - assert!(does_type_checking(&deno_dir, false)); + let context = TestContext::default(); + + assert!(does_type_checking(&context, true)); + assert!(does_type_checking(&context, false)); // should now not do type checking even when it changes // configs because it previously did - assert!(!does_type_checking(&deno_dir, true)); - assert!(!does_type_checking(&deno_dir, false)); + assert!(!does_type_checking(&context, true)); + assert!(!does_type_checking(&context, false)); - fn does_type_checking(deno_dir: &util::TempDir, with_config: bool) -> bool { - let mut cmd = util::deno_cmd_with_deno_dir(deno_dir); - cmd - .current_dir(util::testdata_path()) - .stderr(Stdio::piped()) - .arg("check") - .arg("check/cache_config_on_off/main.ts"); + fn does_type_checking(context: &TestContext, with_config: bool) -> bool { + let mut args = vec![ + "check".to_string(), + "check/cache_config_on_off/main.ts".to_string(), + ]; if with_config { - cmd - .arg("--config") - .arg("check/cache_config_on_off/deno.json"); + let mut slice = vec![ + "--config".to_string(), + "check/cache_config_on_off/deno.json".to_string(), + ]; + args.append(&mut slice); } - let output = cmd.spawn().unwrap().wait_with_output().unwrap(); - assert!(output.status.success()); - let stderr = std::str::from_utf8(&output.stderr).unwrap(); + let output = context.new_command().args_vec(args).split_output().run(); + + output.assert_exit_code(0); + + let stderr = output.stderr(); stderr.contains("Check") } } @@ -119,61 +121,72 @@ fn cache_switching_config_then_no_config() { #[test] fn reload_flag() { // should do type checking whenever someone specifies --reload - let deno_dir = util::new_deno_dir(); - assert!(does_type_checking(&deno_dir, false)); - assert!(!does_type_checking(&deno_dir, false)); - assert!(does_type_checking(&deno_dir, true)); - assert!(does_type_checking(&deno_dir, true)); - assert!(!does_type_checking(&deno_dir, false)); + let context = TestContext::default(); - fn does_type_checking(deno_dir: &util::TempDir, reload: bool) -> bool { - let mut cmd = util::deno_cmd_with_deno_dir(deno_dir); - cmd - .current_dir(util::testdata_path()) - .stderr(Stdio::piped()) - .arg("check") - .arg("check/cache_config_on_off/main.ts"); + assert!(does_type_checking(&context, false)); + assert!(!does_type_checking(&context, false)); + assert!(does_type_checking(&context, true)); + assert!(does_type_checking(&context, true)); + assert!(!does_type_checking(&context, false)); + + fn does_type_checking(context: &TestContext, reload: bool) -> bool { + let mut args = vec![ + "check".to_string(), + "check/cache_config_on_off/main.ts".to_string(), + ]; if reload { - cmd.arg("--reload"); + let mut slice = vec!["--reload".to_string()]; + args.append(&mut slice); } - let output = cmd.spawn().unwrap().wait_with_output().unwrap(); - assert!(output.status.success()); + let output = context.new_command().args_vec(args).split_output().run(); + output.assert_exit_code(0); - let stderr = std::str::from_utf8(&output.stderr).unwrap(); + let stderr = output.stderr(); stderr.contains("Check") } } #[test] fn typecheck_declarations_ns() { - let output = util::deno_cmd() - .arg("test") - .arg("--doc") - .arg(util::root_path().join("cli/tsc/dts/lib.deno.ns.d.ts")) - .output() - .unwrap(); - println!("stdout: {}", String::from_utf8(output.stdout).unwrap()); - println!("stderr: {}", String::from_utf8(output.stderr).unwrap()); - assert!(output.status.success()); + let context = TestContext::default(); + let args = vec![ + "test".to_string(), + "--doc".to_string(), + util::root_path() + .join("cli/tsc/dts/lib.deno.ns.d.ts") + .to_string_lossy() + .into_owned(), + ]; + let output = context.new_command().args_vec(args).split_output().run(); + + println!("stdout: {}", output.stdout()); + println!("stderr: {}", output.stderr()); + output.assert_exit_code(0); } #[test] fn typecheck_declarations_unstable() { - let output = util::deno_cmd() - .arg("test") - .arg("--doc") - .arg("--unstable") - .arg(util::root_path().join("cli/tsc/dts/lib.deno.unstable.d.ts")) - .output() - .unwrap(); - println!("stdout: {}", String::from_utf8(output.stdout).unwrap()); - println!("stderr: {}", String::from_utf8(output.stderr).unwrap()); - assert!(output.status.success()); + let context = TestContext::default(); + let args = vec![ + "test".to_string(), + "--doc".to_string(), + "--unstable".to_string(), + util::root_path() + .join("cli/tsc/dts/lib.deno.unstable.d.ts") + .to_string_lossy() + .into_owned(), + ]; + let output = context.new_command().args_vec(args).split_output().run(); + + println!("stdout: {}", output.stdout()); + println!("stderr: {}", output.stderr()); + output.assert_exit_code(0); } #[test] fn typecheck_core() { - let deno_dir = TempDir::new(); + let context = TestContext::default(); + let deno_dir = context.deno_dir(); let test_file = deno_dir.path().join("test_deno_core_types.ts"); std::fs::write( &test_file, @@ -189,18 +202,18 @@ fn typecheck_core() { ), ) .unwrap(); - let output = util::deno_cmd_with_deno_dir(&deno_dir) - .arg("run") - .arg(test_file.to_str().unwrap()) - .output() - .unwrap(); - println!("stdout: {}", String::from_utf8(output.stdout).unwrap()); - println!("stderr: {}", String::from_utf8(output.stderr).unwrap()); - assert!(output.status.success()); + + let args = vec!["run".to_string(), test_file.to_string_lossy().into_owned()]; + let output = context.new_command().args_vec(args).split_output().run(); + + println!("stdout: {}", output.stdout()); + println!("stderr: {}", output.stderr()); + output.assert_exit_code(0); } #[test] fn ts_no_recheck_on_redirect() { + // TODO: port to test builder let deno_dir = util::new_deno_dir(); let e = util::deno_exe_path(); diff --git a/cli/tests/integration/coverage_tests.rs b/cli/tests/integration/coverage_tests.rs index 27e0724184..022f216d87 100644 --- a/cli/tests/integration/coverage_tests.rs +++ b/cli/tests/integration/coverage_tests.rs @@ -3,6 +3,8 @@ use std::fs; use test_util as util; use test_util::TempDir; +use util::TestContext; +use util::TestContextBuilder; #[test] fn branch() { @@ -26,7 +28,8 @@ fn no_snaps() { #[test] fn error_if_invalid_cache() { - let deno_dir = TempDir::new(); + let context = TestContextBuilder::new().use_temp_cwd().build(); + let deno_dir = context.deno_dir(); let deno_dir_path = deno_dir.path(); let tempdir = TempDir::new(); let tempdir = tempdir.path().join("cov"); @@ -51,76 +54,70 @@ fn error_if_invalid_cache() { std::fs::copy(mod_test_path, mod_test_temp_path).unwrap(); // Generate coverage - let status = util::deno_cmd_with_deno_dir(&deno_dir) - .current_dir(deno_dir_path) - .arg("test") - .arg("--quiet") - .arg(format!("--coverage={}", tempdir.to_str().unwrap())) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::inherit()) - .status() - .unwrap(); + let output = context + .new_command() + .args_vec(vec![ + "test".to_string(), + "--quiet".to_string(), + format!("--coverage={}", tempdir.to_str().unwrap()), + ]) + .run(); - assert!(status.success()); + output.assert_exit_code(0); + output.skip_output_check(); // Modify the file between deno test and deno coverage, thus invalidating the cache std::fs::copy(mod_after_path, mod_temp_path).unwrap(); - let output = util::deno_cmd_with_deno_dir(&deno_dir) - .current_dir(deno_dir_path) - .arg("coverage") - .arg(format!("{}/", tempdir.to_str().unwrap())) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::piped()) - .output() - .unwrap(); + let output = context + .new_command() + .args_vec(vec![ + "coverage".to_string(), + format!("{}/", tempdir.to_str().unwrap()), + ]) + .run(); - assert!(output.stdout.is_empty()); + output.assert_exit_code(1); + let out = output.combined_output(); // Expect error - let error = - util::strip_ansi_codes(std::str::from_utf8(&output.stderr).unwrap()) - .to_string(); + let error = util::strip_ansi_codes(out).to_string(); assert!(error.contains("error: Missing transpiled source code")); assert!(error.contains("Before generating coverage report, run `deno test --coverage` to ensure consistent state.")); } fn run_coverage_text(test_name: &str, extension: &str) { - let deno_dir = TempDir::new(); - let tempdir = TempDir::new(); + let context = TestContext::default(); + let tempdir = context.deno_dir(); let tempdir = tempdir.path().join("cov"); - let status = util::deno_cmd_with_deno_dir(&deno_dir) - .current_dir(util::testdata_path()) - .arg("test") - .arg("-A") - .arg("--quiet") - .arg("--unstable") - .arg(format!("--coverage={}", tempdir.to_str().unwrap())) - .arg(format!("coverage/{test_name}_test.{extension}")) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::inherit()) - .status() - .unwrap(); + let output = context + .new_command() + .args_vec(vec![ + "test".to_string(), + "-A".to_string(), + "--quiet".to_string(), + format!("--coverage={}", tempdir.to_str().unwrap()), + format!("coverage/{test_name}_test.{extension}"), + ]) + .run(); - assert!(status.success()); + output.assert_exit_code(0); + output.skip_output_check(); - let output = util::deno_cmd_with_deno_dir(&deno_dir) - .current_dir(util::testdata_path()) - .arg("coverage") - .arg("--unstable") - .arg(format!("{}/", tempdir.to_str().unwrap())) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::piped()) - .output() - .unwrap(); + let output = context + .new_command() + .args_vec(vec![ + "coverage".to_string(), + format!("{}/", tempdir.to_str().unwrap()), + ]) + .split_output() + .run(); // Verify there's no "Check" being printed - assert!(output.stderr.is_empty()); + assert!(output.stderr().is_empty()); - let actual = - util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap()) - .to_string(); + let actual = util::strip_ansi_codes(output.stdout()).to_string(); let expected = fs::read_to_string( util::testdata_path().join(format!("coverage/{test_name}_expected.out")), @@ -133,23 +130,19 @@ fn run_coverage_text(test_name: &str, extension: &str) { panic!("pattern match failed"); } - assert!(output.status.success()); + output.assert_exit_code(0); - let output = util::deno_cmd_with_deno_dir(&deno_dir) - .current_dir(util::testdata_path()) - .arg("coverage") - .arg("--quiet") - .arg("--unstable") - .arg("--lcov") - .arg(format!("{}/", tempdir.to_str().unwrap())) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::inherit()) - .output() - .unwrap(); + let output = context + .new_command() + .args_vec(vec![ + "coverage".to_string(), + "--quiet".to_string(), + "--lcov".to_string(), + format!("{}/", tempdir.to_str().unwrap()), + ]) + .run(); - let actual = - util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap()) - .to_string(); + let actual = util::strip_ansi_codes(output.combined_output()).to_string(); let expected = fs::read_to_string( util::testdata_path().join(format!("coverage/{test_name}_expected.lcov")), @@ -162,45 +155,41 @@ fn run_coverage_text(test_name: &str, extension: &str) { panic!("pattern match failed"); } - assert!(output.status.success()); + output.assert_exit_code(0); } #[test] fn multifile_coverage() { - let deno_dir = TempDir::new(); - let tempdir = TempDir::new(); + let context = TestContext::default(); + let tempdir = context.deno_dir(); let tempdir = tempdir.path().join("cov"); - let status = util::deno_cmd_with_deno_dir(&deno_dir) - .current_dir(util::testdata_path()) - .arg("test") - .arg("--quiet") - .arg("--unstable") - .arg(format!("--coverage={}", tempdir.to_str().unwrap())) - .arg("coverage/multifile/") - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::inherit()) - .status() - .unwrap(); + let output = context + .new_command() + .args_vec(vec![ + "test".to_string(), + "--quiet".to_string(), + format!("--coverage={}", tempdir.to_str().unwrap()), + format!("coverage/multifile/"), + ]) + .run(); - assert!(status.success()); + output.assert_exit_code(0); + output.skip_output_check(); - let output = util::deno_cmd_with_deno_dir(&deno_dir) - .current_dir(util::testdata_path()) - .arg("coverage") - .arg("--unstable") - .arg(format!("{}/", tempdir.to_str().unwrap())) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::piped()) - .output() - .unwrap(); + let output = context + .new_command() + .args_vec(vec![ + "coverage".to_string(), + format!("{}/", tempdir.to_str().unwrap()), + ]) + .split_output() + .run(); // Verify there's no "Check" being printed - assert!(output.stderr.is_empty()); + assert!(output.stderr().is_empty()); - let actual = - util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap()) - .to_string(); + let actual = util::strip_ansi_codes(output.stdout()).to_string(); let expected = fs::read_to_string( util::testdata_path().join("coverage/multifile/expected.out"), @@ -212,24 +201,19 @@ fn multifile_coverage() { println!("EXPECTED\n{expected}\nEXPECTED"); panic!("pattern match failed"); } + output.assert_exit_code(0); - assert!(output.status.success()); + let output = context + .new_command() + .args_vec(vec![ + "coverage".to_string(), + "--quiet".to_string(), + "--lcov".to_string(), + format!("{}/", tempdir.to_str().unwrap()), + ]) + .run(); - let output = util::deno_cmd_with_deno_dir(&deno_dir) - .current_dir(util::testdata_path()) - .arg("coverage") - .arg("--quiet") - .arg("--unstable") - .arg("--lcov") - .arg(format!("{}/", tempdir.to_str().unwrap())) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::inherit()) - .output() - .unwrap(); - - let actual = - util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap()) - .to_string(); + let actual = util::strip_ansi_codes(output.combined_output()).to_string(); let expected = fs::read_to_string( util::testdata_path().join("coverage/multifile/expected.lcov"), @@ -242,48 +226,42 @@ fn multifile_coverage() { panic!("pattern match failed"); } - assert!(output.status.success()); + output.assert_exit_code(0); } fn no_snaps_included(test_name: &str, extension: &str) { - let deno_dir = TempDir::new(); - let tempdir = TempDir::new(); + let context = TestContext::default(); + let tempdir = context.deno_dir(); let tempdir = tempdir.path().join("cov"); - let status = util::deno_cmd_with_deno_dir(&deno_dir) - .current_dir(util::testdata_path()) - .arg("test") - .arg("--quiet") - .arg("--unstable") - .arg("--allow-read") - .arg(format!("--coverage={}", tempdir.to_str().unwrap())) - .arg(format!( - "coverage/no_snaps_included/{test_name}_test.{extension}" - )) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::piped()) - .status() - .unwrap(); + let output = context + .new_command() + .args_vec(vec![ + "test".to_string(), + "--quiet".to_string(), + "--allow-read".to_string(), + format!("--coverage={}", tempdir.to_str().unwrap()), + format!("coverage/no_snaps_included/{test_name}_test.{extension}"), + ]) + .run(); - assert!(status.success()); + output.assert_exit_code(0); + output.skip_output_check(); - let output = util::deno_cmd_with_deno_dir(&deno_dir) - .current_dir(util::testdata_path()) - .arg("coverage") - .arg("--unstable") - .arg("--include=no_snaps_included.ts") - .arg(format!("{}/", tempdir.to_str().unwrap())) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::piped()) - .output() - .unwrap(); + let output = context + .new_command() + .args_vec(vec![ + "coverage".to_string(), + "--include=no_snaps_included.ts".to_string(), + format!("{}/", tempdir.to_str().unwrap()), + ]) + .split_output() + .run(); // Verify there's no "Check" being printed - assert!(output.stderr.is_empty()); + assert!(output.stderr().is_empty()); - let actual = - util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap()) - .to_string(); + let actual = util::strip_ansi_codes(output.stdout()).to_string(); let expected = fs::read_to_string( util::testdata_path().join("coverage/no_snaps_included/expected.out"), @@ -296,41 +274,38 @@ fn no_snaps_included(test_name: &str, extension: &str) { panic!("pattern match failed"); } - assert!(output.status.success()); + output.assert_exit_code(0); } #[test] fn no_transpiled_lines() { - let deno_dir = TempDir::new(); - let tempdir = TempDir::new(); + let context = TestContext::default(); + let tempdir = context.deno_dir(); let tempdir = tempdir.path().join("cov"); - let status = util::deno_cmd_with_deno_dir(&deno_dir) - .current_dir(util::testdata_path()) - .arg("test") - .arg("--quiet") - .arg(format!("--coverage={}", tempdir.to_str().unwrap())) - .arg("coverage/no_transpiled_lines/") - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::inherit()) - .status() - .unwrap(); + let output = context + .new_command() + .args_vec(vec![ + "test".to_string(), + "--quiet".to_string(), + format!("--coverage={}", tempdir.to_str().unwrap()), + "coverage/no_transpiled_lines/".to_string(), + ]) + .run(); - assert!(status.success()); + output.assert_exit_code(0); + output.skip_output_check(); - let output = util::deno_cmd_with_deno_dir(&deno_dir) - .current_dir(util::testdata_path()) - .arg("coverage") - .arg("--include=no_transpiled_lines/index.ts") - .arg(format!("{}/", tempdir.to_str().unwrap())) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::piped()) - .output() - .unwrap(); + let output = context + .new_command() + .args_vec(vec![ + "coverage".to_string(), + "--include=no_transpiled_lines/index.ts".to_string(), + format!("{}/", tempdir.to_str().unwrap()), + ]) + .run(); - let actual = - util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap()) - .to_string(); + let actual = util::strip_ansi_codes(output.combined_output()).to_string(); let expected = fs::read_to_string( util::testdata_path().join("coverage/no_transpiled_lines/expected.out"), @@ -343,22 +318,19 @@ fn no_transpiled_lines() { panic!("pattern match failed"); } - assert!(output.status.success()); + output.assert_exit_code(0); - let output = util::deno_cmd_with_deno_dir(&deno_dir) - .current_dir(util::testdata_path()) - .arg("coverage") - .arg("--lcov") - .arg("--include=no_transpiled_lines/index.ts") - .arg(format!("{}/", tempdir.to_str().unwrap())) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::inherit()) - .output() - .unwrap(); + let output = context + .new_command() + .args_vec(vec![ + "coverage".to_string(), + "--lcov".to_string(), + "--include=no_transpiled_lines/index.ts".to_string(), + format!("{}/", tempdir.to_str().unwrap()), + ]) + .run(); - let actual = - util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap()) - .to_string(); + let actual = util::strip_ansi_codes(output.combined_output()).to_string(); let expected = fs::read_to_string( util::testdata_path().join("coverage/no_transpiled_lines/expected.lcov"), @@ -371,5 +343,5 @@ fn no_transpiled_lines() { panic!("pattern match failed"); } - assert!(output.status.success()); + output.assert_exit_code(0); } diff --git a/cli/tests/integration/doc_tests.rs b/cli/tests/integration/doc_tests.rs index 014a1d136a..de611d4056 100644 --- a/cli/tests/integration/doc_tests.rs +++ b/cli/tests/integration/doc_tests.rs @@ -1,8 +1,8 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use test_util as util; -use test_util::TempDir; use util::assert_contains; +use util::TestContext; itest!(deno_doc_builtin { args: "doc", @@ -11,24 +11,18 @@ itest!(deno_doc_builtin { #[test] fn deno_doc() { - let dir = TempDir::new(); + let context = TestContext::default(); // try this twice to ensure it works with the cache for _ in 0..2 { - let output = util::deno_cmd_with_deno_dir(&dir) - .current_dir(util::testdata_path()) - .arg("doc") - .arg("doc/deno_doc.ts") + let output = context + .new_command() .env("NO_COLOR", "1") - .stdout(std::process::Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(output.status.success()); - assert_contains!( - std::str::from_utf8(&output.stdout).unwrap(), - "function foo" - ); + .args("doc doc/deno_doc.ts") + .split_output() + .run(); + + output.assert_exit_code(0); + assert_contains!(output.stdout(), "function foo"); } } diff --git a/cli/tests/integration/fmt_tests.rs b/cli/tests/integration/fmt_tests.rs index 52aae2bd31..7812175a7b 100644 --- a/cli/tests/integration/fmt_tests.rs +++ b/cli/tests/integration/fmt_tests.rs @@ -2,10 +2,12 @@ use test_util as util; use test_util::TempDir; +use util::TestContext; #[test] fn fmt_test() { - let t = TempDir::new(); + let context = TestContext::default(); + let t = context.deno_dir(); let testdata_fmt_dir = util::testdata_path().join("fmt"); let fixed_js = testdata_fmt_dir.join("badly_formatted_fixed.js"); let badly_formatted_original_js = @@ -27,49 +29,57 @@ fn fmt_test() { let badly_formatted_json_str = badly_formatted_json.to_str().unwrap(); std::fs::copy(badly_formatted_original_json, &badly_formatted_json).unwrap(); // First, check formatting by ignoring the badly formatted file. - let status = util::deno_cmd() - .current_dir(&testdata_fmt_dir) - .arg("fmt") - .arg(format!( - "--ignore={badly_formatted_js_str},{badly_formatted_md_str},{badly_formatted_json_str}" - )) - .arg("--check") - .arg(badly_formatted_js_str) - .arg(badly_formatted_md_str) - .arg(badly_formatted_json_str) - .spawn() - .unwrap() - .wait() - .unwrap(); + let s = testdata_fmt_dir.as_os_str().to_str().unwrap(); + + let output = context + .new_command() + .cwd(s) + .args_vec(vec![ + "fmt".to_string(), + format!( + "--ignore={badly_formatted_js_str},{badly_formatted_md_str},{badly_formatted_json_str}", + ), + format!( + "--check {badly_formatted_js_str} {badly_formatted_md_str} {badly_formatted_json_str}", + ), + ]) + .run(); + // No target files found - assert!(!status.success()); + output.assert_exit_code(1); + output.skip_output_check(); // Check without ignore. - let status = util::deno_cmd() - .current_dir(&testdata_fmt_dir) - .arg("fmt") - .arg("--check") - .arg(badly_formatted_js_str) - .arg(badly_formatted_md_str) - .arg(badly_formatted_json_str) - .spawn() - .unwrap() - .wait() - .unwrap(); - assert!(!status.success()); + let output = context + .new_command() + .cwd(s) + .args_vec(vec![ + "fmt".to_string(), + "--check".to_string(), + badly_formatted_js_str.to_string(), + badly_formatted_md_str.to_string(), + badly_formatted_json_str.to_string(), + ]) + .run(); + + output.assert_exit_code(1); + output.skip_output_check(); // Format the source file. - let status = util::deno_cmd() - .current_dir(&testdata_fmt_dir) - .arg("fmt") - .arg(badly_formatted_js_str) - .arg(badly_formatted_md_str) - .arg(badly_formatted_json_str) - .spawn() - .unwrap() - .wait() - .unwrap(); - assert!(status.success()); + let output = context + .new_command() + .cwd(s) + .args_vec(vec![ + "fmt".to_string(), + badly_formatted_js_str.to_string(), + badly_formatted_md_str.to_string(), + badly_formatted_json_str.to_string(), + ]) + .run(); + + output.assert_exit_code(0); + output.skip_output_check(); + let expected_js = std::fs::read_to_string(fixed_js).unwrap(); let expected_md = std::fs::read_to_string(fixed_md).unwrap(); let expected_json = std::fs::read_to_string(fixed_json).unwrap(); @@ -105,22 +115,15 @@ fn fmt_stdin_error() { #[test] fn fmt_ignore_unexplicit_files() { - let output = util::deno_cmd() - .current_dir(util::testdata_path()) + let context = TestContext::default(); + let output = context + .new_command() .env("NO_COLOR", "1") - .arg("fmt") - .arg("--check") - .arg("--ignore=./") - .stderr(std::process::Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(!output.status.success()); - assert_eq!( - String::from_utf8_lossy(&output.stderr), - "error: No target files found.\n" - ); + .args("fmt --check --ignore=./") + .run(); + + output.assert_exit_code(1); + assert_eq!(output.combined_output(), "error: No target files found.\n"); } #[test] @@ -148,20 +151,17 @@ fn fmt_auto_ignore_git_and_node_modules() { create_bad_json(git_dir); create_bad_json(nest_node_modules); create_bad_json(node_modules_dir); - let output = util::deno_cmd() - .current_dir(t) + + let context = TestContext::default(); + let output = context + .new_command() + .cwd(t.as_os_str().to_str().unwrap()) .env("NO_COLOR", "1") - .arg("fmt") - .stderr(std::process::Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(!output.status.success()); - assert_eq!( - String::from_utf8_lossy(&output.stderr), - "error: No target files found.\n" - ); + .args("fmt") + .run(); + + output.assert_exit_code(1); + assert_eq!(output.combined_output(), "error: No target files found.\n"); } itest!(fmt_quiet_check_fmt_dir { diff --git a/cli/tests/integration/init_tests.rs b/cli/tests/integration/init_tests.rs index 5ba69b87f2..d92bfd371f 100644 --- a/cli/tests/integration/init_tests.rs +++ b/cli/tests/integration/init_tests.rs @@ -1,27 +1,21 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use std::process::Stdio; use test_util as util; -use test_util::TempDir; use util::assert_contains; +use util::TestContextBuilder; #[test] fn init_subcommand_without_dir() { - let temp_dir = TempDir::new(); - let cwd = temp_dir.path(); - let deno_dir = util::new_deno_dir(); + let context = TestContextBuilder::new().use_temp_cwd().build(); + let deno_dir = context.deno_dir(); - let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); - let output = deno_cmd - .current_dir(cwd) - .arg("init") - .stderr(Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(output.status.success()); - let stderr = String::from_utf8(output.stderr).unwrap(); + let cwd = deno_dir.path(); + + let output = context.new_command().args("init").split_output().run(); + + output.assert_exit_code(0); + + let stderr = output.stderr(); assert_contains!(stderr, "Project initialized"); assert!(!stderr.contains("cd")); assert_contains!(stderr, "deno run main.ts"); @@ -31,65 +25,52 @@ fn init_subcommand_without_dir() { assert!(cwd.join("deno.jsonc").exists()); - let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); - let output = deno_cmd - .current_dir(cwd) + let output = context + .new_command() .env("NO_COLOR", "1") - .arg("run") - .arg("main.ts") - .stdout(Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(output.status.success()); - assert_eq!(output.stdout, b"Add 2 + 3 = 5\n"); + .args("run main.ts") + .split_output() + .run(); - let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); - let output = deno_cmd - .current_dir(cwd) - .env("NO_COLOR", "1") - .arg("test") - .stdout(Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(output.status.success()); - let stdout = String::from_utf8(output.stdout).unwrap(); - assert_contains!(stdout, "1 passed"); + output.assert_exit_code(0); + assert_eq!(output.stdout().as_bytes(), b"Add 2 + 3 = 5\n"); - let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); - let output = deno_cmd - .current_dir(cwd) + let output = context + .new_command() .env("NO_COLOR", "1") - .arg("bench") - .stdout(Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(output.status.success()); + .args("test") + .split_output() + .run(); + + output.assert_exit_code(0); + assert_contains!(output.stdout(), "1 passed"); + output.skip_output_check(); + + let output = context + .new_command() + .env("NO_COLOR", "1") + .args("bench") + .run(); + + output.assert_exit_code(0); + output.skip_output_check(); } #[test] fn init_subcommand_with_dir_arg() { - let temp_dir = TempDir::new(); - let cwd = temp_dir.path(); - let deno_dir = util::new_deno_dir(); + let context = TestContextBuilder::new().use_temp_cwd().build(); + let deno_dir = context.deno_dir(); + let cwd = deno_dir.path(); - let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); - let output = deno_cmd - .current_dir(cwd) - .arg("init") - .arg("my_dir") - .stderr(Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(output.status.success()); - let stderr = String::from_utf8(output.stderr).unwrap(); + let output = context + .new_command() + .args("init my_dir") + .split_output() + .run(); + + output.assert_exit_code(0); + + let stderr = output.stderr(); assert_contains!(stderr, "Project initialized"); assert_contains!(stderr, "cd my_dir"); assert_contains!(stderr, "deno run main.ts"); @@ -99,107 +80,86 @@ fn init_subcommand_with_dir_arg() { assert!(cwd.join("my_dir/deno.jsonc").exists()); - let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); - let output = deno_cmd - .current_dir(cwd) + let output = context + .new_command() .env("NO_COLOR", "1") - .arg("run") - .arg("my_dir/main.ts") - .stdout(Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(output.status.success()); - assert_eq!(output.stdout, b"Add 2 + 3 = 5\n"); + .args("run my_dir/main.ts") + .split_output() + .run(); - let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); - let output = deno_cmd - .current_dir(cwd) - .env("NO_COLOR", "1") - .arg("test") - .arg("my_dir/main_test.ts") - .stdout(Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(output.status.success()); - let stdout = String::from_utf8(output.stdout).unwrap(); - assert_contains!(stdout, "1 passed"); + output.assert_exit_code(0); - let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); - let output = deno_cmd - .current_dir(cwd) + assert_eq!(output.stdout().as_bytes(), b"Add 2 + 3 = 5\n"); + output.skip_output_check(); + + let output = context + .new_command() .env("NO_COLOR", "1") - .arg("bench") - .arg("my_dir/main_bench.ts") - .stdout(Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(output.status.success()); + .args("test my_dir/main_test.ts") + .split_output() + .run(); + + output.assert_exit_code(0); + assert_contains!(output.stdout(), "1 passed"); + output.skip_output_check(); + + let output = context + .new_command() + .env("NO_COLOR", "1") + .args("bench my_dir/main_bench.ts") + .split_output() + .run(); + + output.assert_exit_code(0); + output.skip_output_check(); } #[test] fn init_subcommand_with_quiet_arg() { - let temp_dir = TempDir::new(); - let cwd = temp_dir.path(); - let deno_dir = util::new_deno_dir(); + let context = TestContextBuilder::new().use_temp_cwd().build(); + let deno_dir = context.deno_dir(); + let cwd = deno_dir.path(); - let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); - let output = deno_cmd - .current_dir(cwd) - .arg("init") - .arg("--quiet") - .stdout(Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(output.status.success()); - let stdout = String::from_utf8(output.stdout).unwrap(); - assert_eq!(stdout, ""); + let output = context + .new_command() + .args("init --quiet") + .split_output() + .run(); + + output.assert_exit_code(0); + + assert_eq!(output.stdout(), ""); assert!(cwd.join("deno.jsonc").exists()); - let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); - let output = deno_cmd - .current_dir(cwd) + let output = context + .new_command() .env("NO_COLOR", "1") - .arg("run") - .arg("main.ts") - .stdout(Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(output.status.success()); - assert_eq!(output.stdout, b"Add 2 + 3 = 5\n"); + .args("run main.ts") + .split_output() + .run(); - let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); - let output = deno_cmd - .current_dir(cwd) - .env("NO_COLOR", "1") - .arg("test") - .stdout(Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(output.status.success()); - let stdout = String::from_utf8(output.stdout).unwrap(); - assert_contains!(stdout, "1 passed"); + output.assert_exit_code(0); + assert_eq!(output.stdout().as_bytes(), b"Add 2 + 3 = 5\n"); + output.skip_output_check(); - let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); - let output = deno_cmd - .current_dir(cwd) + let output = context + .new_command() .env("NO_COLOR", "1") - .arg("bench") - .stdout(Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(output.status.success()); + .args("test") + .split_output() + .run(); + + output.assert_exit_code(0); + assert_contains!(output.stdout(), "1 passed"); + output.skip_output_check(); + + let output = context + .new_command() + .env("NO_COLOR", "1") + .args("bench") + .split_output() + .run(); + + output.assert_exit_code(0); + output.skip_output_check(); } diff --git a/cli/tests/integration/test_tests.rs b/cli/tests/integration/test_tests.rs index 712cd1c685..de7bc5fede 100644 --- a/cli/tests/integration/test_tests.rs +++ b/cli/tests/integration/test_tests.rs @@ -2,6 +2,7 @@ use deno_core::url::Url; use test_util as util; +use util::assert_contains; use util::env_vars_for_npm_tests; use util::TestContext; @@ -355,24 +356,17 @@ itest!(test_with_custom_jsx { #[test] fn captured_output() { - let output = util::deno_cmd() - .current_dir(util::testdata_path()) - .arg("test") - .arg("--allow-run") - .arg("--allow-read") - .arg("--unstable") - .arg("test/captured_output.ts") + let context = TestContext::default(); + let output = context + .new_command() + .args("test --allow-run --allow-read --unstable test/captured_output.ts") .env("NO_COLOR", "1") - .stdout(std::process::Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); + .run(); let output_start = "------- output -------"; let output_end = "----- output end -----"; - assert!(output.status.success()); - let output_text = String::from_utf8(output.stdout).unwrap(); + output.assert_exit_code(0); + let output_text = output.combined_output(); let start = output_text.find(output_start).unwrap() + output_start.len(); let end = output_text.find(output_end).unwrap(); // replace zero width space that may appear in test output due @@ -392,20 +386,16 @@ fn captured_output() { #[test] fn recursive_permissions_pledge() { - let output = util::deno_cmd() - .current_dir(util::testdata_path()) - .arg("test") - .arg("test/recursive_permissions_pledge.js") - .stderr(std::process::Stdio::piped()) - .stdout(std::process::Stdio::piped()) - .spawn() - .unwrap() - .wait_with_output() - .unwrap(); - assert!(!output.status.success()); - assert!(String::from_utf8(output.stderr).unwrap().contains( + let context = TestContext::default(); + let output = context + .new_command() + .args("test test/recursive_permissions_pledge.js") + .run(); + output.assert_exit_code(1); + assert_contains!( + output.combined_output(), "pledge test permissions called before restoring previous pledge" - )); + ); } #[test] @@ -418,7 +408,7 @@ fn file_protocol() { let context = TestContext::default(); context .new_command() - .args(format!("test {file_url}")) + .args_vec(vec!["test".to_string(), file_url]) .run() .assert_matches_file("test/file_protocol.out"); }