Skip to content

Commit

Permalink
chore: various improvements to tests (denoland#21222)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Nov 17, 2023
1 parent b7d14d9 commit 29011d5
Show file tree
Hide file tree
Showing 15 changed files with 546 additions and 657 deletions.
119 changes: 54 additions & 65 deletions cli/tests/integration/cert_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ use lsp_types::Url;
use std::io::BufReader;
use std::io::Cursor;
use std::io::Read;
use std::process::Command;
use std::sync::Arc;
use test_util as util;
use test_util::TempDir;
use util::TestContext;

itest_flaky!(cafile_url_imports {
Expand Down Expand Up @@ -125,91 +123,82 @@ fn cafile_compile() {

#[flaky_test::flaky_test]
fn cafile_install_remote_module() {
let _g = util::http_server();
let temp_dir = TempDir::new();
let context = TestContext::with_http_server();
let temp_dir = context.temp_dir();
let bin_dir = temp_dir.path().join("bin");
std::fs::create_dir(&bin_dir).unwrap();
let deno_dir = TempDir::new();
bin_dir.create_dir_all();
let cafile = util::testdata_path().join("tls/RootCA.pem");

let install_output = Command::new(util::deno_exe_path())
.env("DENO_DIR", deno_dir.path())
.current_dir(util::testdata_path())
.arg("install")
.arg("--cert")
.arg(cafile)
.arg("--root")
.arg(temp_dir.path())
.arg("-n")
.arg("echo_test")
.arg("https://localhost:5545/echo.ts")
.output()
.expect("Failed to spawn script");
println!("{}", std::str::from_utf8(&install_output.stdout).unwrap());
eprintln!("{}", std::str::from_utf8(&install_output.stderr).unwrap());
assert!(install_output.status.success());
let install_output = context
.new_command()
.args_vec([
"install",
"--cert",
&cafile.to_string_lossy(),
"--root",
&temp_dir.path().to_string_lossy(),
"-n",
"echo_test",
"https://localhost:5545/echo.ts",
])
.split_output()
.run();
println!("{}", install_output.stdout());
eprintln!("{}", install_output.stderr());
install_output.assert_exit_code(0);

let mut echo_test_path = bin_dir.join("echo_test");
if cfg!(windows) {
echo_test_path = echo_test_path.with_extension("cmd");
}
assert!(echo_test_path.exists());

let output = Command::new(echo_test_path)
.current_dir(temp_dir.path())
.arg("foo")
let output = context
.new_command()
.name(echo_test_path)
.args("foo")
.env("PATH", util::target_dir())
.output()
.expect("failed to spawn script");
let stdout = std::str::from_utf8(&output.stdout).unwrap().trim();
assert!(stdout.ends_with("foo"));
.run();
output.assert_matches_text("[WILDCARD]foo");
}

#[flaky_test::flaky_test]
fn cafile_bundle_remote_exports() {
let _g = util::http_server();
let context = TestContext::with_http_server();

// First we have to generate a bundle of some remote module that has exports.
let mod1 = "https://localhost:5545/subdir/mod1.ts";
let cafile = util::testdata_path().join("tls/RootCA.pem");
let t = TempDir::new();
let t = context.temp_dir();
let bundle = t.path().join("mod1.bundle.js");
let mut deno = util::deno_cmd()
.current_dir(util::testdata_path())
.arg("bundle")
.arg("--cert")
.arg(cafile)
.arg(mod1)
.arg(&bundle)
.spawn()
.expect("failed to spawn script");
let status = deno.wait().expect("failed to wait for the child process");
assert!(status.success());
context
.new_command()
.args_vec([
"bundle",
"--cert",
&cafile.to_string_lossy(),
mod1,
&bundle.to_string_lossy(),
])
.run()
.skip_output_check()
.assert_exit_code(0);

assert!(bundle.is_file());

// Now we try to use that bundle from another module.
let test = t.path().join("test.js");
std::fs::write(
&test,
"
import { printHello3 } from \"./mod1.bundle.js\";
printHello3(); ",
)
.expect("error writing file");

let output = util::deno_cmd()
.current_dir(util::testdata_path())
.arg("run")
.arg("--check")
.arg(&test)
.output()
.expect("failed to spawn script");
// check the output of the test.ts program.
assert!(std::str::from_utf8(&output.stdout)
.unwrap()
.trim()
.ends_with("Hello"));
assert_eq!(output.stderr, b"");
test.write(
"import { printHello3 } from \"./mod1.bundle.js\";
printHello3();",
);

context
.new_command()
.args_vec(["run", "--quiet", "--check", &test.to_string_lossy()])
.run()
.assert_matches_text("[WILDCARD]Hello\n")
.assert_exit_code(0);
}

#[tokio::test]
Expand All @@ -223,7 +212,7 @@ async fn listen_tls_alpn() {
.arg("--allow-read")
.arg("./cert/listen_tls_alpn.ts")
.arg("4504")
.stdout(std::process::Stdio::piped())
.stdout_piped()
.spawn()
.unwrap();
let stdout = child.stdout.as_mut().unwrap();
Expand Down Expand Up @@ -272,7 +261,7 @@ async fn listen_tls_alpn_fail() {
.arg("--allow-read")
.arg("./cert/listen_tls_alpn_fail.ts")
.arg("4505")
.stdout(std::process::Stdio::piped())
.stdout_piped()
.spawn()
.unwrap();
let stdout = child.stdout.as_mut().unwrap();
Expand Down
7 changes: 2 additions & 5 deletions cli/tests/integration/compile_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use std::fs::File;
use test_util as util;
use util::assert_contains;
use util::assert_not_contains;
Expand Down Expand Up @@ -219,7 +218,7 @@ fn compile_with_file_exists_error() {
dir.path().join("args/")
};
let file_path = dir.path().join("args");
File::create(&file_path).unwrap();
file_path.write("");
context
.new_command()
.args_vec([
Expand Down Expand Up @@ -294,9 +293,7 @@ fn compile_with_conflict_file_exists_error() {
exe
))
.assert_exit_code(1);
assert!(std::fs::read(&exe)
.unwrap()
.eq(b"SHOULD NOT BE OVERWRITTEN"));
exe.assert_matches_text("SHOULD NOT BE OVERWRITTEN");
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/integration/eval_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn eval_p() {
.arg("eval")
.arg("-p")
.arg("1+2")
.stdout(std::process::Stdio::piped())
.stdout_piped()
.spawn()
.unwrap()
.wait_with_output()
Expand All @@ -28,7 +28,7 @@ fn eval_randomness() {
.arg("eval")
.arg("-p")
.arg("Math.random()")
.stdout(std::process::Stdio::piped())
.stdout_piped()
.spawn()
.unwrap()
.wait_with_output()
Expand Down
13 changes: 4 additions & 9 deletions cli/tests/integration/flags_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use std::process::Stdio;
use test_util as util;
use util::assert_contains;

#[test]
fn help_flag() {
Expand All @@ -20,14 +20,9 @@ fn help_output() {
let output = util::deno_cmd()
.current_dir(util::testdata_path())
.arg("--help")
.stdout(Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
.run();

assert!(output.status.success());
let stdout = std::str::from_utf8(&output.stdout).unwrap();
let stdout = output.combined_output();
let subcommand_descriptions = vec![
"Run a JavaScript or TypeScript program",
"Run benchmarks",
Expand Down Expand Up @@ -56,7 +51,7 @@ fn help_output() {
];

for description in subcommand_descriptions {
assert!(stdout.contains(description));
assert_contains!(stdout, description);
}
}

Expand Down
29 changes: 10 additions & 19 deletions cli/tests/integration/fmt_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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::PathRef;
use util::TestContext;
Expand Down Expand Up @@ -91,25 +90,17 @@ fn fmt_test() {
}

#[test]
fn fmt_stdin_error() {
use std::io::Write;
let mut deno = util::deno_cmd()
fn fmt_stdin_syntax_error() {
let output = util::deno_cmd()
.current_dir(util::testdata_path())
.arg("fmt")
.arg("-")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap();
let stdin = deno.stdin.as_mut().unwrap();
let invalid_js = b"import { example }";
stdin.write_all(invalid_js).unwrap();
let output = deno.wait_with_output().unwrap();
// Error message might change. Just check stdout empty, stderr not.
assert!(output.stdout.is_empty());
assert!(!output.stderr.is_empty());
assert!(!output.status.success());
.set_stdin_text("import { example }")
.split_output()
.run();
assert!(output.stdout().is_empty());
assert!(!output.stderr().is_empty());
output.assert_exit_code(1);
}

#[test]
Expand All @@ -132,7 +123,8 @@ fn fmt_auto_ignore_git_and_node_modules() {
bad_json_path.write("bad json\n");
}

let temp_dir = TempDir::new();
let context = TestContext::default();
let temp_dir = context.temp_dir();
let t = temp_dir.path().join("target");
let nest_git = t.join("nest").join(".git");
let git_dir = t.join(".git");
Expand All @@ -147,7 +139,6 @@ fn fmt_auto_ignore_git_and_node_modules() {
create_bad_json(nest_node_modules);
create_bad_json(node_modules_dir);

let context = TestContext::default();
let output = context
.new_command()
.cwd(t)
Expand Down
Loading

0 comments on commit 29011d5

Please sign in to comment.