0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00

chore(cli/tests): use test builder in more integration tests (#18031)

This commit is contained in:
Geert-Jan Zwiers 2023-03-13 14:40:46 +01:00 committed by GitHub
parent 224cff7a2f
commit 983447e860
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 442 additions and 513 deletions

View file

@ -1,11 +1,10 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use std::process::Command; use std::process::Command;
use std::process::Stdio;
use test_util as util; use test_util as util;
use util::env_vars_for_npm_tests; use util::env_vars_for_npm_tests;
use util::env_vars_for_npm_tests_no_sync_download; use util::env_vars_for_npm_tests_no_sync_download;
use util::TempDir; use util::TestContext;
itest!(_095_check_with_bare_import { itest!(_095_check_with_bare_import {
args: "check cache/095_cache_with_bare_import.ts", args: "check cache/095_cache_with_bare_import.ts",
@ -87,31 +86,34 @@ itest!(check_no_error_truncation {
#[test] #[test]
fn cache_switching_config_then_no_config() { fn cache_switching_config_then_no_config() {
let deno_dir = util::new_deno_dir(); let context = TestContext::default();
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));
// should now not do type checking even when it changes // should now not do type checking even when it changes
// configs because it previously did // configs because it previously did
assert!(!does_type_checking(&deno_dir, true)); assert!(!does_type_checking(&context, true));
assert!(!does_type_checking(&deno_dir, false)); assert!(!does_type_checking(&context, false));
fn does_type_checking(deno_dir: &util::TempDir, with_config: bool) -> bool { fn does_type_checking(context: &TestContext, with_config: bool) -> bool {
let mut cmd = util::deno_cmd_with_deno_dir(deno_dir); let mut args = vec![
cmd "check".to_string(),
.current_dir(util::testdata_path()) "check/cache_config_on_off/main.ts".to_string(),
.stderr(Stdio::piped()) ];
.arg("check")
.arg("check/cache_config_on_off/main.ts");
if with_config { if with_config {
cmd let mut slice = vec![
.arg("--config") "--config".to_string(),
.arg("check/cache_config_on_off/deno.json"); "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") stderr.contains("Check")
} }
} }
@ -119,61 +121,72 @@ fn cache_switching_config_then_no_config() {
#[test] #[test]
fn reload_flag() { fn reload_flag() {
// should do type checking whenever someone specifies --reload // should do type checking whenever someone specifies --reload
let deno_dir = util::new_deno_dir(); let context = TestContext::default();
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));
fn does_type_checking(deno_dir: &util::TempDir, reload: bool) -> bool { assert!(does_type_checking(&context, false));
let mut cmd = util::deno_cmd_with_deno_dir(deno_dir); assert!(!does_type_checking(&context, false));
cmd assert!(does_type_checking(&context, true));
.current_dir(util::testdata_path()) assert!(does_type_checking(&context, true));
.stderr(Stdio::piped()) assert!(!does_type_checking(&context, false));
.arg("check")
.arg("check/cache_config_on_off/main.ts"); 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 { 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(); let output = context.new_command().args_vec(args).split_output().run();
assert!(output.status.success()); output.assert_exit_code(0);
let stderr = std::str::from_utf8(&output.stderr).unwrap(); let stderr = output.stderr();
stderr.contains("Check") stderr.contains("Check")
} }
} }
#[test] #[test]
fn typecheck_declarations_ns() { fn typecheck_declarations_ns() {
let output = util::deno_cmd() let context = TestContext::default();
.arg("test") let args = vec![
.arg("--doc") "test".to_string(),
.arg(util::root_path().join("cli/tsc/dts/lib.deno.ns.d.ts")) "--doc".to_string(),
.output() util::root_path()
.unwrap(); .join("cli/tsc/dts/lib.deno.ns.d.ts")
println!("stdout: {}", String::from_utf8(output.stdout).unwrap()); .to_string_lossy()
println!("stderr: {}", String::from_utf8(output.stderr).unwrap()); .into_owned(),
assert!(output.status.success()); ];
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] #[test]
fn typecheck_declarations_unstable() { fn typecheck_declarations_unstable() {
let output = util::deno_cmd() let context = TestContext::default();
.arg("test") let args = vec![
.arg("--doc") "test".to_string(),
.arg("--unstable") "--doc".to_string(),
.arg(util::root_path().join("cli/tsc/dts/lib.deno.unstable.d.ts")) "--unstable".to_string(),
.output() util::root_path()
.unwrap(); .join("cli/tsc/dts/lib.deno.unstable.d.ts")
println!("stdout: {}", String::from_utf8(output.stdout).unwrap()); .to_string_lossy()
println!("stderr: {}", String::from_utf8(output.stderr).unwrap()); .into_owned(),
assert!(output.status.success()); ];
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] #[test]
fn typecheck_core() { 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"); let test_file = deno_dir.path().join("test_deno_core_types.ts");
std::fs::write( std::fs::write(
&test_file, &test_file,
@ -189,18 +202,18 @@ fn typecheck_core() {
), ),
) )
.unwrap(); .unwrap();
let output = util::deno_cmd_with_deno_dir(&deno_dir)
.arg("run") let args = vec!["run".to_string(), test_file.to_string_lossy().into_owned()];
.arg(test_file.to_str().unwrap()) let output = context.new_command().args_vec(args).split_output().run();
.output()
.unwrap(); println!("stdout: {}", output.stdout());
println!("stdout: {}", String::from_utf8(output.stdout).unwrap()); println!("stderr: {}", output.stderr());
println!("stderr: {}", String::from_utf8(output.stderr).unwrap()); output.assert_exit_code(0);
assert!(output.status.success());
} }
#[test] #[test]
fn ts_no_recheck_on_redirect() { fn ts_no_recheck_on_redirect() {
// TODO: port to test builder
let deno_dir = util::new_deno_dir(); let deno_dir = util::new_deno_dir();
let e = util::deno_exe_path(); let e = util::deno_exe_path();

View file

@ -3,6 +3,8 @@
use std::fs; use std::fs;
use test_util as util; use test_util as util;
use test_util::TempDir; use test_util::TempDir;
use util::TestContext;
use util::TestContextBuilder;
#[test] #[test]
fn branch() { fn branch() {
@ -26,7 +28,8 @@ fn no_snaps() {
#[test] #[test]
fn error_if_invalid_cache() { 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 deno_dir_path = deno_dir.path();
let tempdir = TempDir::new(); let tempdir = TempDir::new();
let tempdir = tempdir.path().join("cov"); 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(); std::fs::copy(mod_test_path, mod_test_temp_path).unwrap();
// Generate coverage // Generate coverage
let status = util::deno_cmd_with_deno_dir(&deno_dir) let output = context
.current_dir(deno_dir_path) .new_command()
.arg("test") .args_vec(vec![
.arg("--quiet") "test".to_string(),
.arg(format!("--coverage={}", tempdir.to_str().unwrap())) "--quiet".to_string(),
.stdout(std::process::Stdio::piped()) format!("--coverage={}", tempdir.to_str().unwrap()),
.stderr(std::process::Stdio::inherit()) ])
.status() .run();
.unwrap();
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 // Modify the file between deno test and deno coverage, thus invalidating the cache
std::fs::copy(mod_after_path, mod_temp_path).unwrap(); std::fs::copy(mod_after_path, mod_temp_path).unwrap();
let output = util::deno_cmd_with_deno_dir(&deno_dir) let output = context
.current_dir(deno_dir_path) .new_command()
.arg("coverage") .args_vec(vec![
.arg(format!("{}/", tempdir.to_str().unwrap())) "coverage".to_string(),
.stdout(std::process::Stdio::piped()) format!("{}/", tempdir.to_str().unwrap()),
.stderr(std::process::Stdio::piped()) ])
.output() .run();
.unwrap();
assert!(output.stdout.is_empty()); output.assert_exit_code(1);
let out = output.combined_output();
// Expect error // Expect error
let error = let error = util::strip_ansi_codes(out).to_string();
util::strip_ansi_codes(std::str::from_utf8(&output.stderr).unwrap())
.to_string();
assert!(error.contains("error: Missing transpiled source code")); assert!(error.contains("error: Missing transpiled source code"));
assert!(error.contains("Before generating coverage report, run `deno test --coverage` to ensure consistent state.")); assert!(error.contains("Before generating coverage report, run `deno test --coverage` to ensure consistent state."));
} }
fn run_coverage_text(test_name: &str, extension: &str) { fn run_coverage_text(test_name: &str, extension: &str) {
let deno_dir = TempDir::new(); let context = TestContext::default();
let tempdir = TempDir::new(); let tempdir = context.deno_dir();
let tempdir = tempdir.path().join("cov"); let tempdir = tempdir.path().join("cov");
let status = util::deno_cmd_with_deno_dir(&deno_dir) let output = context
.current_dir(util::testdata_path()) .new_command()
.arg("test") .args_vec(vec![
.arg("-A") "test".to_string(),
.arg("--quiet") "-A".to_string(),
.arg("--unstable") "--quiet".to_string(),
.arg(format!("--coverage={}", tempdir.to_str().unwrap())) format!("--coverage={}", tempdir.to_str().unwrap()),
.arg(format!("coverage/{test_name}_test.{extension}")) format!("coverage/{test_name}_test.{extension}"),
.stdout(std::process::Stdio::piped()) ])
.stderr(std::process::Stdio::inherit()) .run();
.status()
.unwrap();
assert!(status.success()); output.assert_exit_code(0);
output.skip_output_check();
let output = util::deno_cmd_with_deno_dir(&deno_dir) let output = context
.current_dir(util::testdata_path()) .new_command()
.arg("coverage") .args_vec(vec![
.arg("--unstable") "coverage".to_string(),
.arg(format!("{}/", tempdir.to_str().unwrap())) format!("{}/", tempdir.to_str().unwrap()),
.stdout(std::process::Stdio::piped()) ])
.stderr(std::process::Stdio::piped()) .split_output()
.output() .run();
.unwrap();
// Verify there's no "Check" being printed // Verify there's no "Check" being printed
assert!(output.stderr.is_empty()); assert!(output.stderr().is_empty());
let actual = let actual = util::strip_ansi_codes(output.stdout()).to_string();
util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap())
.to_string();
let expected = fs::read_to_string( let expected = fs::read_to_string(
util::testdata_path().join(format!("coverage/{test_name}_expected.out")), 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"); panic!("pattern match failed");
} }
assert!(output.status.success()); output.assert_exit_code(0);
let output = util::deno_cmd_with_deno_dir(&deno_dir) let output = context
.current_dir(util::testdata_path()) .new_command()
.arg("coverage") .args_vec(vec![
.arg("--quiet") "coverage".to_string(),
.arg("--unstable") "--quiet".to_string(),
.arg("--lcov") "--lcov".to_string(),
.arg(format!("{}/", tempdir.to_str().unwrap())) format!("{}/", tempdir.to_str().unwrap()),
.stdout(std::process::Stdio::piped()) ])
.stderr(std::process::Stdio::inherit()) .run();
.output()
.unwrap();
let actual = let actual = util::strip_ansi_codes(output.combined_output()).to_string();
util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap())
.to_string();
let expected = fs::read_to_string( let expected = fs::read_to_string(
util::testdata_path().join(format!("coverage/{test_name}_expected.lcov")), 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"); panic!("pattern match failed");
} }
assert!(output.status.success()); output.assert_exit_code(0);
} }
#[test] #[test]
fn multifile_coverage() { fn multifile_coverage() {
let deno_dir = TempDir::new(); let context = TestContext::default();
let tempdir = TempDir::new(); let tempdir = context.deno_dir();
let tempdir = tempdir.path().join("cov"); let tempdir = tempdir.path().join("cov");
let status = util::deno_cmd_with_deno_dir(&deno_dir) let output = context
.current_dir(util::testdata_path()) .new_command()
.arg("test") .args_vec(vec![
.arg("--quiet") "test".to_string(),
.arg("--unstable") "--quiet".to_string(),
.arg(format!("--coverage={}", tempdir.to_str().unwrap())) format!("--coverage={}", tempdir.to_str().unwrap()),
.arg("coverage/multifile/") format!("coverage/multifile/"),
.stdout(std::process::Stdio::piped()) ])
.stderr(std::process::Stdio::inherit()) .run();
.status()
.unwrap();
assert!(status.success()); output.assert_exit_code(0);
output.skip_output_check();
let output = util::deno_cmd_with_deno_dir(&deno_dir) let output = context
.current_dir(util::testdata_path()) .new_command()
.arg("coverage") .args_vec(vec![
.arg("--unstable") "coverage".to_string(),
.arg(format!("{}/", tempdir.to_str().unwrap())) format!("{}/", tempdir.to_str().unwrap()),
.stdout(std::process::Stdio::piped()) ])
.stderr(std::process::Stdio::piped()) .split_output()
.output() .run();
.unwrap();
// Verify there's no "Check" being printed // Verify there's no "Check" being printed
assert!(output.stderr.is_empty()); assert!(output.stderr().is_empty());
let actual = let actual = util::strip_ansi_codes(output.stdout()).to_string();
util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap())
.to_string();
let expected = fs::read_to_string( let expected = fs::read_to_string(
util::testdata_path().join("coverage/multifile/expected.out"), util::testdata_path().join("coverage/multifile/expected.out"),
@ -212,24 +201,19 @@ fn multifile_coverage() {
println!("EXPECTED\n{expected}\nEXPECTED"); println!("EXPECTED\n{expected}\nEXPECTED");
panic!("pattern match failed"); 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) let actual = util::strip_ansi_codes(output.combined_output()).to_string();
.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 expected = fs::read_to_string( let expected = fs::read_to_string(
util::testdata_path().join("coverage/multifile/expected.lcov"), util::testdata_path().join("coverage/multifile/expected.lcov"),
@ -242,48 +226,42 @@ fn multifile_coverage() {
panic!("pattern match failed"); panic!("pattern match failed");
} }
assert!(output.status.success()); output.assert_exit_code(0);
} }
fn no_snaps_included(test_name: &str, extension: &str) { fn no_snaps_included(test_name: &str, extension: &str) {
let deno_dir = TempDir::new(); let context = TestContext::default();
let tempdir = TempDir::new(); let tempdir = context.deno_dir();
let tempdir = tempdir.path().join("cov"); let tempdir = tempdir.path().join("cov");
let status = util::deno_cmd_with_deno_dir(&deno_dir) let output = context
.current_dir(util::testdata_path()) .new_command()
.arg("test") .args_vec(vec![
.arg("--quiet") "test".to_string(),
.arg("--unstable") "--quiet".to_string(),
.arg("--allow-read") "--allow-read".to_string(),
.arg(format!("--coverage={}", tempdir.to_str().unwrap())) format!("--coverage={}", tempdir.to_str().unwrap()),
.arg(format!( format!("coverage/no_snaps_included/{test_name}_test.{extension}"),
"coverage/no_snaps_included/{test_name}_test.{extension}" ])
)) .run();
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.status()
.unwrap();
assert!(status.success()); output.assert_exit_code(0);
output.skip_output_check();
let output = util::deno_cmd_with_deno_dir(&deno_dir) let output = context
.current_dir(util::testdata_path()) .new_command()
.arg("coverage") .args_vec(vec![
.arg("--unstable") "coverage".to_string(),
.arg("--include=no_snaps_included.ts") "--include=no_snaps_included.ts".to_string(),
.arg(format!("{}/", tempdir.to_str().unwrap())) format!("{}/", tempdir.to_str().unwrap()),
.stdout(std::process::Stdio::piped()) ])
.stderr(std::process::Stdio::piped()) .split_output()
.output() .run();
.unwrap();
// Verify there's no "Check" being printed // Verify there's no "Check" being printed
assert!(output.stderr.is_empty()); assert!(output.stderr().is_empty());
let actual = let actual = util::strip_ansi_codes(output.stdout()).to_string();
util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap())
.to_string();
let expected = fs::read_to_string( let expected = fs::read_to_string(
util::testdata_path().join("coverage/no_snaps_included/expected.out"), 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"); panic!("pattern match failed");
} }
assert!(output.status.success()); output.assert_exit_code(0);
} }
#[test] #[test]
fn no_transpiled_lines() { fn no_transpiled_lines() {
let deno_dir = TempDir::new(); let context = TestContext::default();
let tempdir = TempDir::new(); let tempdir = context.deno_dir();
let tempdir = tempdir.path().join("cov"); let tempdir = tempdir.path().join("cov");
let status = util::deno_cmd_with_deno_dir(&deno_dir) let output = context
.current_dir(util::testdata_path()) .new_command()
.arg("test") .args_vec(vec![
.arg("--quiet") "test".to_string(),
.arg(format!("--coverage={}", tempdir.to_str().unwrap())) "--quiet".to_string(),
.arg("coverage/no_transpiled_lines/") format!("--coverage={}", tempdir.to_str().unwrap()),
.stdout(std::process::Stdio::piped()) "coverage/no_transpiled_lines/".to_string(),
.stderr(std::process::Stdio::inherit()) ])
.status() .run();
.unwrap();
assert!(status.success()); output.assert_exit_code(0);
output.skip_output_check();
let output = util::deno_cmd_with_deno_dir(&deno_dir) let output = context
.current_dir(util::testdata_path()) .new_command()
.arg("coverage") .args_vec(vec![
.arg("--include=no_transpiled_lines/index.ts") "coverage".to_string(),
.arg(format!("{}/", tempdir.to_str().unwrap())) "--include=no_transpiled_lines/index.ts".to_string(),
.stdout(std::process::Stdio::piped()) format!("{}/", tempdir.to_str().unwrap()),
.stderr(std::process::Stdio::piped()) ])
.output() .run();
.unwrap();
let actual = let actual = util::strip_ansi_codes(output.combined_output()).to_string();
util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap())
.to_string();
let expected = fs::read_to_string( let expected = fs::read_to_string(
util::testdata_path().join("coverage/no_transpiled_lines/expected.out"), util::testdata_path().join("coverage/no_transpiled_lines/expected.out"),
@ -343,22 +318,19 @@ fn no_transpiled_lines() {
panic!("pattern match failed"); panic!("pattern match failed");
} }
assert!(output.status.success()); output.assert_exit_code(0);
let output = util::deno_cmd_with_deno_dir(&deno_dir) let output = context
.current_dir(util::testdata_path()) .new_command()
.arg("coverage") .args_vec(vec![
.arg("--lcov") "coverage".to_string(),
.arg("--include=no_transpiled_lines/index.ts") "--lcov".to_string(),
.arg(format!("{}/", tempdir.to_str().unwrap())) "--include=no_transpiled_lines/index.ts".to_string(),
.stdout(std::process::Stdio::piped()) format!("{}/", tempdir.to_str().unwrap()),
.stderr(std::process::Stdio::inherit()) ])
.output() .run();
.unwrap();
let actual = let actual = util::strip_ansi_codes(output.combined_output()).to_string();
util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap())
.to_string();
let expected = fs::read_to_string( let expected = fs::read_to_string(
util::testdata_path().join("coverage/no_transpiled_lines/expected.lcov"), util::testdata_path().join("coverage/no_transpiled_lines/expected.lcov"),
@ -371,5 +343,5 @@ fn no_transpiled_lines() {
panic!("pattern match failed"); panic!("pattern match failed");
} }
assert!(output.status.success()); output.assert_exit_code(0);
} }

View file

@ -1,8 +1,8 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use test_util as util; use test_util as util;
use test_util::TempDir;
use util::assert_contains; use util::assert_contains;
use util::TestContext;
itest!(deno_doc_builtin { itest!(deno_doc_builtin {
args: "doc", args: "doc",
@ -11,24 +11,18 @@ itest!(deno_doc_builtin {
#[test] #[test]
fn deno_doc() { fn deno_doc() {
let dir = TempDir::new(); let context = TestContext::default();
// try this twice to ensure it works with the cache // try this twice to ensure it works with the cache
for _ in 0..2 { for _ in 0..2 {
let output = util::deno_cmd_with_deno_dir(&dir) let output = context
.current_dir(util::testdata_path()) .new_command()
.arg("doc")
.arg("doc/deno_doc.ts")
.env("NO_COLOR", "1") .env("NO_COLOR", "1")
.stdout(std::process::Stdio::piped()) .args("doc doc/deno_doc.ts")
.spawn() .split_output()
.unwrap() .run();
.wait_with_output()
.unwrap(); output.assert_exit_code(0);
assert!(output.status.success()); assert_contains!(output.stdout(), "function foo");
assert_contains!(
std::str::from_utf8(&output.stdout).unwrap(),
"function foo"
);
} }
} }

View file

@ -2,10 +2,12 @@
use test_util as util; use test_util as util;
use test_util::TempDir; use test_util::TempDir;
use util::TestContext;
#[test] #[test]
fn fmt_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 testdata_fmt_dir = util::testdata_path().join("fmt");
let fixed_js = testdata_fmt_dir.join("badly_formatted_fixed.js"); let fixed_js = testdata_fmt_dir.join("badly_formatted_fixed.js");
let badly_formatted_original_js = let badly_formatted_original_js =
@ -27,49 +29,57 @@ fn fmt_test() {
let badly_formatted_json_str = badly_formatted_json.to_str().unwrap(); let badly_formatted_json_str = badly_formatted_json.to_str().unwrap();
std::fs::copy(badly_formatted_original_json, &badly_formatted_json).unwrap(); std::fs::copy(badly_formatted_original_json, &badly_formatted_json).unwrap();
// First, check formatting by ignoring the badly formatted file. // First, check formatting by ignoring the badly formatted file.
let status = util::deno_cmd() let s = testdata_fmt_dir.as_os_str().to_str().unwrap();
.current_dir(&testdata_fmt_dir)
.arg("fmt") let output = context
.arg(format!( .new_command()
"--ignore={badly_formatted_js_str},{badly_formatted_md_str},{badly_formatted_json_str}" .cwd(s)
)) .args_vec(vec![
.arg("--check") "fmt".to_string(),
.arg(badly_formatted_js_str) format!(
.arg(badly_formatted_md_str) "--ignore={badly_formatted_js_str},{badly_formatted_md_str},{badly_formatted_json_str}",
.arg(badly_formatted_json_str) ),
.spawn() format!(
.unwrap() "--check {badly_formatted_js_str} {badly_formatted_md_str} {badly_formatted_json_str}",
.wait() ),
.unwrap(); ])
.run();
// No target files found // No target files found
assert!(!status.success()); output.assert_exit_code(1);
output.skip_output_check();
// Check without ignore. // Check without ignore.
let status = util::deno_cmd() let output = context
.current_dir(&testdata_fmt_dir) .new_command()
.arg("fmt") .cwd(s)
.arg("--check") .args_vec(vec![
.arg(badly_formatted_js_str) "fmt".to_string(),
.arg(badly_formatted_md_str) "--check".to_string(),
.arg(badly_formatted_json_str) badly_formatted_js_str.to_string(),
.spawn() badly_formatted_md_str.to_string(),
.unwrap() badly_formatted_json_str.to_string(),
.wait() ])
.unwrap(); .run();
assert!(!status.success());
output.assert_exit_code(1);
output.skip_output_check();
// Format the source file. // Format the source file.
let status = util::deno_cmd() let output = context
.current_dir(&testdata_fmt_dir) .new_command()
.arg("fmt") .cwd(s)
.arg(badly_formatted_js_str) .args_vec(vec![
.arg(badly_formatted_md_str) "fmt".to_string(),
.arg(badly_formatted_json_str) badly_formatted_js_str.to_string(),
.spawn() badly_formatted_md_str.to_string(),
.unwrap() badly_formatted_json_str.to_string(),
.wait() ])
.unwrap(); .run();
assert!(status.success());
output.assert_exit_code(0);
output.skip_output_check();
let expected_js = std::fs::read_to_string(fixed_js).unwrap(); let expected_js = std::fs::read_to_string(fixed_js).unwrap();
let expected_md = std::fs::read_to_string(fixed_md).unwrap(); let expected_md = std::fs::read_to_string(fixed_md).unwrap();
let expected_json = std::fs::read_to_string(fixed_json).unwrap(); let expected_json = std::fs::read_to_string(fixed_json).unwrap();
@ -105,22 +115,15 @@ fn fmt_stdin_error() {
#[test] #[test]
fn fmt_ignore_unexplicit_files() { fn fmt_ignore_unexplicit_files() {
let output = util::deno_cmd() let context = TestContext::default();
.current_dir(util::testdata_path()) let output = context
.new_command()
.env("NO_COLOR", "1") .env("NO_COLOR", "1")
.arg("fmt") .args("fmt --check --ignore=./")
.arg("--check") .run();
.arg("--ignore=./")
.stderr(std::process::Stdio::piped()) output.assert_exit_code(1);
.spawn() assert_eq!(output.combined_output(), "error: No target files found.\n");
.unwrap()
.wait_with_output()
.unwrap();
assert!(!output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stderr),
"error: No target files found.\n"
);
} }
#[test] #[test]
@ -148,20 +151,17 @@ fn fmt_auto_ignore_git_and_node_modules() {
create_bad_json(git_dir); create_bad_json(git_dir);
create_bad_json(nest_node_modules); create_bad_json(nest_node_modules);
create_bad_json(node_modules_dir); 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") .env("NO_COLOR", "1")
.arg("fmt") .args("fmt")
.stderr(std::process::Stdio::piped()) .run();
.spawn()
.unwrap() output.assert_exit_code(1);
.wait_with_output() assert_eq!(output.combined_output(), "error: No target files found.\n");
.unwrap();
assert!(!output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stderr),
"error: No target files found.\n"
);
} }
itest!(fmt_quiet_check_fmt_dir { itest!(fmt_quiet_check_fmt_dir {

View file

@ -1,27 +1,21 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use std::process::Stdio;
use test_util as util; use test_util as util;
use test_util::TempDir;
use util::assert_contains; use util::assert_contains;
use util::TestContextBuilder;
#[test] #[test]
fn init_subcommand_without_dir() { fn init_subcommand_without_dir() {
let temp_dir = TempDir::new(); let context = TestContextBuilder::new().use_temp_cwd().build();
let cwd = temp_dir.path(); let deno_dir = context.deno_dir();
let deno_dir = util::new_deno_dir();
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); let cwd = deno_dir.path();
let output = deno_cmd
.current_dir(cwd) let output = context.new_command().args("init").split_output().run();
.arg("init")
.stderr(Stdio::piped()) output.assert_exit_code(0);
.spawn()
.unwrap() let stderr = output.stderr();
.wait_with_output()
.unwrap();
assert!(output.status.success());
let stderr = String::from_utf8(output.stderr).unwrap();
assert_contains!(stderr, "Project initialized"); assert_contains!(stderr, "Project initialized");
assert!(!stderr.contains("cd")); assert!(!stderr.contains("cd"));
assert_contains!(stderr, "deno run main.ts"); assert_contains!(stderr, "deno run main.ts");
@ -31,65 +25,52 @@ fn init_subcommand_without_dir() {
assert!(cwd.join("deno.jsonc").exists()); assert!(cwd.join("deno.jsonc").exists());
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); let output = context
let output = deno_cmd .new_command()
.current_dir(cwd)
.env("NO_COLOR", "1") .env("NO_COLOR", "1")
.arg("run") .args("run main.ts")
.arg("main.ts") .split_output()
.stdout(Stdio::piped()) .run();
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Add 2 + 3 = 5\n");
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); output.assert_exit_code(0);
let output = deno_cmd assert_eq!(output.stdout().as_bytes(), b"Add 2 + 3 = 5\n");
.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");
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); let output = context
let output = deno_cmd .new_command()
.current_dir(cwd)
.env("NO_COLOR", "1") .env("NO_COLOR", "1")
.arg("bench") .args("test")
.stdout(Stdio::piped()) .split_output()
.spawn() .run();
.unwrap()
.wait_with_output() output.assert_exit_code(0);
.unwrap(); assert_contains!(output.stdout(), "1 passed");
assert!(output.status.success()); 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] #[test]
fn init_subcommand_with_dir_arg() { fn init_subcommand_with_dir_arg() {
let temp_dir = TempDir::new(); let context = TestContextBuilder::new().use_temp_cwd().build();
let cwd = temp_dir.path(); let deno_dir = context.deno_dir();
let deno_dir = util::new_deno_dir(); let cwd = deno_dir.path();
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); let output = context
let output = deno_cmd .new_command()
.current_dir(cwd) .args("init my_dir")
.arg("init") .split_output()
.arg("my_dir") .run();
.stderr(Stdio::piped())
.spawn() output.assert_exit_code(0);
.unwrap()
.wait_with_output() let stderr = output.stderr();
.unwrap();
assert!(output.status.success());
let stderr = String::from_utf8(output.stderr).unwrap();
assert_contains!(stderr, "Project initialized"); assert_contains!(stderr, "Project initialized");
assert_contains!(stderr, "cd my_dir"); assert_contains!(stderr, "cd my_dir");
assert_contains!(stderr, "deno run main.ts"); 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()); assert!(cwd.join("my_dir/deno.jsonc").exists());
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); let output = context
let output = deno_cmd .new_command()
.current_dir(cwd)
.env("NO_COLOR", "1") .env("NO_COLOR", "1")
.arg("run") .args("run my_dir/main.ts")
.arg("my_dir/main.ts") .split_output()
.stdout(Stdio::piped()) .run();
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Add 2 + 3 = 5\n");
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); output.assert_exit_code(0);
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");
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); assert_eq!(output.stdout().as_bytes(), b"Add 2 + 3 = 5\n");
let output = deno_cmd output.skip_output_check();
.current_dir(cwd)
let output = context
.new_command()
.env("NO_COLOR", "1") .env("NO_COLOR", "1")
.arg("bench") .args("test my_dir/main_test.ts")
.arg("my_dir/main_bench.ts") .split_output()
.stdout(Stdio::piped()) .run();
.spawn()
.unwrap() output.assert_exit_code(0);
.wait_with_output() assert_contains!(output.stdout(), "1 passed");
.unwrap(); output.skip_output_check();
assert!(output.status.success());
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] #[test]
fn init_subcommand_with_quiet_arg() { fn init_subcommand_with_quiet_arg() {
let temp_dir = TempDir::new(); let context = TestContextBuilder::new().use_temp_cwd().build();
let cwd = temp_dir.path(); let deno_dir = context.deno_dir();
let deno_dir = util::new_deno_dir(); let cwd = deno_dir.path();
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); let output = context
let output = deno_cmd .new_command()
.current_dir(cwd) .args("init --quiet")
.arg("init") .split_output()
.arg("--quiet") .run();
.stdout(Stdio::piped())
.spawn() output.assert_exit_code(0);
.unwrap()
.wait_with_output() assert_eq!(output.stdout(), "");
.unwrap();
assert!(output.status.success());
let stdout = String::from_utf8(output.stdout).unwrap();
assert_eq!(stdout, "");
assert!(cwd.join("deno.jsonc").exists()); assert!(cwd.join("deno.jsonc").exists());
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); let output = context
let output = deno_cmd .new_command()
.current_dir(cwd)
.env("NO_COLOR", "1") .env("NO_COLOR", "1")
.arg("run") .args("run main.ts")
.arg("main.ts") .split_output()
.stdout(Stdio::piped()) .run();
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Add 2 + 3 = 5\n");
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); output.assert_exit_code(0);
let output = deno_cmd assert_eq!(output.stdout().as_bytes(), b"Add 2 + 3 = 5\n");
.current_dir(cwd) output.skip_output_check();
.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");
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir); let output = context
let output = deno_cmd .new_command()
.current_dir(cwd)
.env("NO_COLOR", "1") .env("NO_COLOR", "1")
.arg("bench") .args("test")
.stdout(Stdio::piped()) .split_output()
.spawn() .run();
.unwrap()
.wait_with_output() output.assert_exit_code(0);
.unwrap(); assert_contains!(output.stdout(), "1 passed");
assert!(output.status.success()); 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();
} }

View file

@ -2,6 +2,7 @@
use deno_core::url::Url; use deno_core::url::Url;
use test_util as util; use test_util as util;
use util::assert_contains;
use util::env_vars_for_npm_tests; use util::env_vars_for_npm_tests;
use util::TestContext; use util::TestContext;
@ -355,24 +356,17 @@ itest!(test_with_custom_jsx {
#[test] #[test]
fn captured_output() { fn captured_output() {
let output = util::deno_cmd() let context = TestContext::default();
.current_dir(util::testdata_path()) let output = context
.arg("test") .new_command()
.arg("--allow-run") .args("test --allow-run --allow-read --unstable test/captured_output.ts")
.arg("--allow-read")
.arg("--unstable")
.arg("test/captured_output.ts")
.env("NO_COLOR", "1") .env("NO_COLOR", "1")
.stdout(std::process::Stdio::piped()) .run();
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
let output_start = "------- output -------"; let output_start = "------- output -------";
let output_end = "----- output end -----"; let output_end = "----- output end -----";
assert!(output.status.success()); output.assert_exit_code(0);
let output_text = String::from_utf8(output.stdout).unwrap(); let output_text = output.combined_output();
let start = output_text.find(output_start).unwrap() + output_start.len(); let start = output_text.find(output_start).unwrap() + output_start.len();
let end = output_text.find(output_end).unwrap(); let end = output_text.find(output_end).unwrap();
// replace zero width space that may appear in test output due // replace zero width space that may appear in test output due
@ -392,20 +386,16 @@ fn captured_output() {
#[test] #[test]
fn recursive_permissions_pledge() { fn recursive_permissions_pledge() {
let output = util::deno_cmd() let context = TestContext::default();
.current_dir(util::testdata_path()) let output = context
.arg("test") .new_command()
.arg("test/recursive_permissions_pledge.js") .args("test test/recursive_permissions_pledge.js")
.stderr(std::process::Stdio::piped()) .run();
.stdout(std::process::Stdio::piped()) output.assert_exit_code(1);
.spawn() assert_contains!(
.unwrap() output.combined_output(),
.wait_with_output()
.unwrap();
assert!(!output.status.success());
assert!(String::from_utf8(output.stderr).unwrap().contains(
"pledge test permissions called before restoring previous pledge" "pledge test permissions called before restoring previous pledge"
)); );
} }
#[test] #[test]
@ -418,7 +408,7 @@ fn file_protocol() {
let context = TestContext::default(); let context = TestContext::default();
context context
.new_command() .new_command()
.args(format!("test {file_url}")) .args_vec(vec!["test".to_string(), file_url])
.run() .run()
.assert_matches_file("test/file_protocol.out"); .assert_matches_file("test/file_protocol.out");
} }