2023-02-27 15:52:49 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::collections::HashMap;
|
2023-06-10 11:09:45 -04:00
|
|
|
use std::ffi::OsStr;
|
2023-02-27 15:52:49 -05:00
|
|
|
use std::io::Read;
|
|
|
|
use std::io::Write;
|
2023-02-28 14:10:12 -05:00
|
|
|
use std::path::Path;
|
2023-02-27 15:52:49 -05:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process::Command;
|
|
|
|
use std::process::Stdio;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use os_pipe::pipe;
|
|
|
|
|
2023-06-26 15:53:17 -04:00
|
|
|
use crate::assertions::assert_wildcard_match;
|
2023-02-27 15:52:49 -05:00
|
|
|
use crate::deno_exe_path;
|
2023-09-18 10:46:44 -04:00
|
|
|
use crate::env_vars_for_jsr_tests;
|
|
|
|
use crate::env_vars_for_npm_tests;
|
2023-06-10 11:09:45 -04:00
|
|
|
use crate::fs::PathRef;
|
2023-02-27 15:52:49 -05:00
|
|
|
use crate::http_server;
|
2023-03-08 18:15:20 -05:00
|
|
|
use crate::lsp::LspClientBuilder;
|
2023-08-29 15:02:54 -04:00
|
|
|
use crate::npm_registry_unset_url;
|
2023-03-28 17:49:00 -04:00
|
|
|
use crate::pty::Pty;
|
2023-02-27 15:52:49 -05:00
|
|
|
use crate::strip_ansi_codes;
|
|
|
|
use crate::testdata_path;
|
|
|
|
use crate::HttpServerGuard;
|
|
|
|
use crate::TempDir;
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct TestContextBuilder {
|
|
|
|
use_http_server: bool,
|
|
|
|
use_temp_cwd: bool,
|
2023-06-09 08:40:10 -04:00
|
|
|
use_symlinked_temp_dir: bool,
|
2023-02-27 15:52:49 -05:00
|
|
|
/// Copies the files at the specified directory in the "testdata" directory
|
|
|
|
/// to the temp folder and runs the test from there. This is useful when
|
|
|
|
/// the test creates files in the testdata directory (ex. a node_modules folder)
|
|
|
|
copy_temp_dir: Option<String>,
|
2023-07-21 09:12:26 -04:00
|
|
|
temp_dir_path: Option<PathBuf>,
|
2023-02-27 15:52:49 -05:00
|
|
|
cwd: Option<String>,
|
|
|
|
envs: HashMap<String, String>,
|
2023-06-10 11:09:45 -04:00
|
|
|
deno_exe: Option<PathRef>,
|
2023-02-27 15:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestContextBuilder {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
2023-03-03 17:27:05 -05:00
|
|
|
pub fn for_npm() -> Self {
|
2023-04-01 10:04:56 -04:00
|
|
|
Self::new().use_http_server().add_npm_env_vars()
|
2023-03-03 17:27:05 -05:00
|
|
|
}
|
|
|
|
|
2023-09-18 10:46:44 -04:00
|
|
|
pub fn for_jsr() -> Self {
|
|
|
|
Self::new().use_http_server().add_jsr_env_vars()
|
|
|
|
}
|
|
|
|
|
2023-07-21 09:12:26 -04:00
|
|
|
pub fn temp_dir_path(mut self, path: impl AsRef<Path>) -> Self {
|
|
|
|
self.temp_dir_path = Some(path.as_ref().to_path_buf());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-04-01 10:04:56 -04:00
|
|
|
pub fn use_http_server(mut self) -> Self {
|
2023-02-27 15:52:49 -05:00
|
|
|
self.use_http_server = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-04-01 10:04:56 -04:00
|
|
|
pub fn use_temp_cwd(mut self) -> Self {
|
2023-02-27 15:52:49 -05:00
|
|
|
self.use_temp_cwd = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-06-09 08:40:10 -04:00
|
|
|
/// Causes the temp directory to be symlinked to a target directory
|
|
|
|
/// which is useful for debugging issues that only show up on the CI.
|
|
|
|
///
|
|
|
|
/// Note: This method is not actually deprecated, it's just the CI
|
|
|
|
/// does this by default so there's no need to check in any code that
|
|
|
|
/// uses this into the repo. This is just for debugging purposes.
|
|
|
|
#[deprecated]
|
|
|
|
pub fn use_symlinked_temp_dir(mut self) -> Self {
|
|
|
|
self.use_symlinked_temp_dir = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-02-27 15:52:49 -05:00
|
|
|
/// Copies the files at the specified directory in the "testdata" directory
|
|
|
|
/// to the temp folder and runs the test from there. This is useful when
|
|
|
|
/// the test creates files in the testdata directory (ex. a node_modules folder)
|
2023-04-01 10:04:56 -04:00
|
|
|
pub fn use_copy_temp_dir(mut self, dir: impl AsRef<str>) -> Self {
|
2023-02-27 15:52:49 -05:00
|
|
|
self.copy_temp_dir = Some(dir.as_ref().to_string());
|
2023-03-03 17:27:05 -05:00
|
|
|
self
|
2023-02-27 15:52:49 -05:00
|
|
|
}
|
|
|
|
|
2023-04-01 10:04:56 -04:00
|
|
|
pub fn cwd(mut self, cwd: impl AsRef<str>) -> Self {
|
2023-02-27 15:52:49 -05:00
|
|
|
self.cwd = Some(cwd.as_ref().to_string());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-04-01 10:04:56 -04:00
|
|
|
pub fn env(mut self, key: impl AsRef<str>, value: impl AsRef<str>) -> Self {
|
2023-02-27 15:52:49 -05:00
|
|
|
self
|
|
|
|
.envs
|
|
|
|
.insert(key.as_ref().to_string(), value.as_ref().to_string());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-04-01 10:04:56 -04:00
|
|
|
pub fn add_npm_env_vars(mut self) -> Self {
|
2023-09-18 10:46:44 -04:00
|
|
|
for (key, value) in env_vars_for_npm_tests() {
|
2023-04-01 10:04:56 -04:00
|
|
|
self = self.env(key, value);
|
2023-03-03 17:27:05 -05:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-09-18 10:46:44 -04:00
|
|
|
pub fn add_jsr_env_vars(mut self) -> Self {
|
|
|
|
for (key, value) in env_vars_for_jsr_tests() {
|
|
|
|
self = self.env(key, value);
|
|
|
|
}
|
|
|
|
self
|
2023-03-03 17:27:05 -05:00
|
|
|
}
|
|
|
|
|
2023-02-27 15:52:49 -05:00
|
|
|
pub fn build(&self) -> TestContext {
|
2023-07-21 09:12:26 -04:00
|
|
|
let temp_dir_path = self
|
|
|
|
.temp_dir_path
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(std::env::temp_dir);
|
|
|
|
let deno_dir = TempDir::new_in(&temp_dir_path);
|
2023-07-26 18:52:31 -04:00
|
|
|
let temp_dir = TempDir::new_in(&temp_dir_path);
|
2023-06-09 08:40:10 -04:00
|
|
|
let temp_dir = if self.use_symlinked_temp_dir {
|
|
|
|
TempDir::new_symlinked(temp_dir)
|
|
|
|
} else {
|
|
|
|
temp_dir
|
|
|
|
};
|
2023-08-01 20:49:09 -04:00
|
|
|
let testdata_dir = testdata_path();
|
|
|
|
if let Some(temp_copy_dir) = &self.copy_temp_dir {
|
|
|
|
let test_data_path = testdata_dir.join(temp_copy_dir);
|
2023-03-13 14:18:29 -04:00
|
|
|
let temp_copy_dir = temp_dir.path().join(temp_copy_dir);
|
2023-06-10 11:09:45 -04:00
|
|
|
temp_copy_dir.create_dir_all();
|
|
|
|
test_data_path.copy_to_recursive(&temp_copy_dir);
|
2023-08-01 20:49:09 -04:00
|
|
|
}
|
2023-02-27 15:52:49 -05:00
|
|
|
|
2023-03-08 18:15:20 -05:00
|
|
|
let deno_exe = self.deno_exe.clone().unwrap_or_else(deno_exe_path);
|
2023-06-10 11:09:45 -04:00
|
|
|
println!("deno_exe path {}", deno_exe);
|
2023-02-27 15:52:49 -05:00
|
|
|
|
|
|
|
let http_server_guard = if self.use_http_server {
|
|
|
|
Some(Rc::new(http_server()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
TestContext {
|
|
|
|
cwd: self.cwd.clone(),
|
2023-03-08 18:15:20 -05:00
|
|
|
deno_exe,
|
2023-02-27 15:52:49 -05:00
|
|
|
envs: self.envs.clone(),
|
2023-08-01 20:49:09 -04:00
|
|
|
use_temp_cwd: self.use_temp_cwd || self.copy_temp_dir.is_some(),
|
2023-02-27 15:52:49 -05:00
|
|
|
_http_server_guard: http_server_guard,
|
|
|
|
deno_dir,
|
2023-03-13 14:18:29 -04:00
|
|
|
temp_dir,
|
2023-02-27 15:52:49 -05:00
|
|
|
testdata_dir,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct TestContext {
|
2023-06-10 11:09:45 -04:00
|
|
|
deno_exe: PathRef,
|
2023-02-27 15:52:49 -05:00
|
|
|
envs: HashMap<String, String>,
|
|
|
|
use_temp_cwd: bool,
|
|
|
|
cwd: Option<String>,
|
|
|
|
_http_server_guard: Option<Rc<HttpServerGuard>>,
|
|
|
|
deno_dir: TempDir,
|
2023-03-13 14:18:29 -04:00
|
|
|
temp_dir: TempDir,
|
2023-06-10 11:09:45 -04:00
|
|
|
testdata_dir: PathRef,
|
2023-02-27 15:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TestContext {
|
|
|
|
fn default() -> Self {
|
|
|
|
TestContextBuilder::default().build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestContext {
|
|
|
|
pub fn with_http_server() -> Self {
|
|
|
|
TestContextBuilder::default().use_http_server().build()
|
|
|
|
}
|
|
|
|
|
2023-06-10 11:09:45 -04:00
|
|
|
pub fn testdata_path(&self) -> &PathRef {
|
2023-02-27 15:52:49 -05:00
|
|
|
&self.testdata_dir
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deno_dir(&self) -> &TempDir {
|
|
|
|
&self.deno_dir
|
|
|
|
}
|
|
|
|
|
2023-03-13 14:18:29 -04:00
|
|
|
pub fn temp_dir(&self) -> &TempDir {
|
|
|
|
&self.temp_dir
|
|
|
|
}
|
|
|
|
|
2023-02-27 15:52:49 -05:00
|
|
|
pub fn new_command(&self) -> TestCommandBuilder {
|
|
|
|
TestCommandBuilder {
|
2023-06-10 11:09:45 -04:00
|
|
|
command_name: self.deno_exe.to_string(),
|
2023-02-27 15:52:49 -05:00
|
|
|
args: Default::default(),
|
|
|
|
args_vec: Default::default(),
|
|
|
|
stdin: Default::default(),
|
|
|
|
envs: Default::default(),
|
|
|
|
env_clear: Default::default(),
|
|
|
|
cwd: Default::default(),
|
2023-03-06 09:16:50 -05:00
|
|
|
split_output: false,
|
2023-02-27 15:52:49 -05:00
|
|
|
context: self.clone(),
|
|
|
|
}
|
|
|
|
}
|
2023-03-08 18:15:20 -05:00
|
|
|
|
|
|
|
pub fn new_lsp_command(&self) -> LspClientBuilder {
|
|
|
|
let mut builder = LspClientBuilder::new();
|
|
|
|
builder.deno_exe(&self.deno_exe).set_test_context(self);
|
|
|
|
builder
|
|
|
|
}
|
2023-02-27 15:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TestCommandBuilder {
|
2023-03-08 18:15:20 -05:00
|
|
|
command_name: String,
|
2023-02-27 15:52:49 -05:00
|
|
|
args: String,
|
|
|
|
args_vec: Vec<String>,
|
|
|
|
stdin: Option<String>,
|
|
|
|
envs: HashMap<String, String>,
|
|
|
|
env_clear: bool,
|
|
|
|
cwd: Option<String>,
|
2023-03-06 09:16:50 -05:00
|
|
|
split_output: bool,
|
2023-02-27 15:52:49 -05:00
|
|
|
context: TestContext,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestCommandBuilder {
|
2023-10-25 14:39:00 -04:00
|
|
|
pub fn name(mut self, name: impl AsRef<OsStr>) -> Self {
|
2023-06-10 11:09:45 -04:00
|
|
|
self.command_name = name.as_ref().to_string_lossy().to_string();
|
2023-02-27 15:52:49 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-04-01 10:04:56 -04:00
|
|
|
pub fn args(mut self, text: impl AsRef<str>) -> Self {
|
2023-02-27 15:52:49 -05:00
|
|
|
self.args = text.as_ref().to_string();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-03-30 17:47:53 -04:00
|
|
|
pub fn args_vec<T: AsRef<str>, I: IntoIterator<Item = T>>(
|
2023-04-01 10:04:56 -04:00
|
|
|
mut self,
|
2023-03-30 17:47:53 -04:00
|
|
|
args: I,
|
2023-04-01 10:04:56 -04:00
|
|
|
) -> Self {
|
2023-03-30 17:47:53 -04:00
|
|
|
self.args_vec = args.into_iter().map(|a| a.as_ref().to_string()).collect();
|
2023-02-27 15:52:49 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-04-01 10:04:56 -04:00
|
|
|
pub fn stdin(mut self, text: impl AsRef<str>) -> Self {
|
2023-02-27 15:52:49 -05:00
|
|
|
self.stdin = Some(text.as_ref().to_string());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-03-06 09:16:50 -05:00
|
|
|
/// Splits the output into stdout and stderr rather than having them combined.
|
2023-04-01 10:04:56 -04:00
|
|
|
pub fn split_output(mut self) -> Self {
|
2023-03-06 09:16:50 -05:00
|
|
|
// Note: it was previously attempted to capture stdout & stderr separately
|
|
|
|
// then forward the output to a combined pipe, but this was found to be
|
|
|
|
// too racy compared to providing the same combined pipe to both.
|
|
|
|
self.split_output = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-06-10 11:09:45 -04:00
|
|
|
pub fn env(
|
|
|
|
mut self,
|
|
|
|
key: impl AsRef<OsStr>,
|
|
|
|
value: impl AsRef<OsStr>,
|
|
|
|
) -> Self {
|
|
|
|
self.envs.insert(
|
|
|
|
key.as_ref().to_string_lossy().to_string(),
|
|
|
|
value.as_ref().to_string_lossy().to_string(),
|
|
|
|
);
|
2023-02-27 15:52:49 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-08-29 15:02:54 -04:00
|
|
|
pub fn envs<S: AsRef<OsStr>>(
|
|
|
|
self,
|
|
|
|
envs: impl IntoIterator<Item = (S, S)>,
|
|
|
|
) -> Self {
|
|
|
|
let mut this = self;
|
|
|
|
for (k, v) in envs {
|
|
|
|
this = this.env(k, v);
|
|
|
|
}
|
|
|
|
this
|
|
|
|
}
|
|
|
|
|
2023-04-01 10:04:56 -04:00
|
|
|
pub fn env_clear(mut self) -> Self {
|
2023-02-27 15:52:49 -05:00
|
|
|
self.env_clear = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-06-10 11:09:45 -04:00
|
|
|
pub fn cwd(mut self, cwd: impl AsRef<OsStr>) -> Self {
|
|
|
|
self.cwd = Some(cwd.as_ref().to_string_lossy().to_string());
|
2023-02-27 15:52:49 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-06-10 11:09:45 -04:00
|
|
|
fn build_cwd(&self) -> PathRef {
|
2023-08-01 20:49:09 -04:00
|
|
|
let root_dir = if self.context.use_temp_cwd {
|
2023-03-13 14:18:29 -04:00
|
|
|
self.context.temp_dir.path().to_owned()
|
2023-02-27 15:52:49 -05:00
|
|
|
} else {
|
|
|
|
self.context.testdata_dir.clone()
|
2023-08-01 20:49:09 -04:00
|
|
|
};
|
|
|
|
let specified_cwd = self.cwd.as_ref().or(self.context.cwd.as_ref());
|
|
|
|
match specified_cwd {
|
|
|
|
Some(cwd) => root_dir.join(cwd),
|
|
|
|
None => root_dir,
|
2023-03-28 17:49:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-10 11:09:45 -04:00
|
|
|
fn build_command_path(&self) -> PathRef {
|
2023-10-25 14:39:00 -04:00
|
|
|
let command_name = if cfg!(windows) && self.command_name == "npm" {
|
|
|
|
"npm.cmd"
|
|
|
|
} else {
|
|
|
|
&self.command_name
|
|
|
|
};
|
2023-03-28 17:49:00 -04:00
|
|
|
if command_name == "deno" {
|
|
|
|
deno_exe_path()
|
|
|
|
} else {
|
2023-06-10 11:09:45 -04:00
|
|
|
PathRef::new(PathBuf::from(command_name))
|
2023-03-28 17:49:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_args(&self) -> Vec<String> {
|
|
|
|
if self.args_vec.is_empty() {
|
2023-02-27 15:52:49 -05:00
|
|
|
std::borrow::Cow::Owned(
|
|
|
|
self
|
|
|
|
.args
|
|
|
|
.split_whitespace()
|
|
|
|
.map(|s| s.to_string())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
assert!(
|
|
|
|
self.args.is_empty(),
|
|
|
|
"Do not provide args when providing args_vec."
|
|
|
|
);
|
|
|
|
std::borrow::Cow::Borrowed(&self.args_vec)
|
|
|
|
}
|
|
|
|
.iter()
|
|
|
|
.map(|arg| {
|
2023-06-10 11:09:45 -04:00
|
|
|
arg.replace(
|
|
|
|
"$TESTDATA",
|
|
|
|
&self.context.testdata_dir.as_path().to_string_lossy(),
|
|
|
|
)
|
2023-02-27 15:52:49 -05:00
|
|
|
})
|
2023-03-28 17:49:00 -04:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:43:16 -04:00
|
|
|
fn build_envs(&self) -> HashMap<String, String> {
|
|
|
|
let mut envs = self.context.envs.clone();
|
|
|
|
for (key, value) in &self.envs {
|
|
|
|
envs.insert(key.to_string(), value.to_string());
|
|
|
|
}
|
|
|
|
envs
|
|
|
|
}
|
|
|
|
|
2023-03-28 17:49:00 -04:00
|
|
|
pub fn with_pty(&self, mut action: impl FnMut(Pty)) {
|
|
|
|
if !Pty::is_supported() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let args = self.build_args();
|
|
|
|
let args = args.iter().map(|s| s.as_str()).collect::<Vec<_>>();
|
2023-03-30 10:43:16 -04:00
|
|
|
let mut envs = self.build_envs();
|
2023-03-28 17:49:00 -04:00
|
|
|
if !envs.contains_key("NO_COLOR") {
|
|
|
|
// set this by default for pty tests
|
|
|
|
envs.insert("NO_COLOR".to_string(), "1".to_string());
|
|
|
|
}
|
2023-03-30 10:43:16 -04:00
|
|
|
|
|
|
|
// note(dsherret): for some reason I need to inject the current
|
|
|
|
// environment here for the pty tests or else I get dns errors
|
|
|
|
if !self.env_clear {
|
|
|
|
for (key, value) in std::env::vars() {
|
|
|
|
envs.entry(key).or_insert(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 17:49:00 -04:00
|
|
|
action(Pty::new(
|
2023-06-10 11:09:45 -04:00
|
|
|
self.build_command_path().as_path(),
|
2023-03-28 17:49:00 -04:00
|
|
|
&args,
|
2023-06-10 11:09:45 -04:00
|
|
|
self.build_cwd().as_path(),
|
2023-03-28 17:49:00 -04:00
|
|
|
Some(envs),
|
|
|
|
))
|
|
|
|
}
|
2023-02-27 15:52:49 -05:00
|
|
|
|
2023-05-10 20:06:59 -04:00
|
|
|
#[track_caller]
|
2023-03-28 17:49:00 -04:00
|
|
|
pub fn run(&self) -> TestCommandOutput {
|
|
|
|
fn read_pipe_to_string(mut pipe: os_pipe::PipeReader) -> String {
|
|
|
|
let mut output = String::new();
|
|
|
|
pipe.read_to_string(&mut output).unwrap();
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sanitize_output(text: String, args: &[String]) -> String {
|
|
|
|
let mut text = strip_ansi_codes(&text).to_string();
|
|
|
|
// deno test's output capturing flushes with a zero-width space in order to
|
2023-06-26 09:10:27 -04:00
|
|
|
// synchronize the output pipes. Occasionally this zero width space
|
2023-03-28 17:49:00 -04:00
|
|
|
// might end up in the output so strip it from the output comparison here.
|
|
|
|
if args.first().map(|s| s.as_str()) == Some("test") {
|
|
|
|
text = text.replace('\u{200B}', "");
|
|
|
|
}
|
|
|
|
text
|
|
|
|
}
|
|
|
|
|
|
|
|
let cwd = self.build_cwd();
|
|
|
|
let args = self.build_args();
|
|
|
|
let mut command = Command::new(self.build_command_path());
|
|
|
|
|
|
|
|
println!("command {} {}", self.command_name, args.join(" "));
|
2023-06-10 11:09:45 -04:00
|
|
|
println!("command cwd {}", cwd);
|
2023-02-27 15:52:49 -05:00
|
|
|
command.args(args.iter());
|
|
|
|
if self.env_clear {
|
|
|
|
command.env_clear();
|
|
|
|
}
|
2023-03-28 17:49:00 -04:00
|
|
|
command.env("DENO_DIR", self.context.deno_dir.path());
|
2023-10-25 14:39:00 -04:00
|
|
|
let mut envs = self.build_envs();
|
2023-08-29 15:02:54 -04:00
|
|
|
if !envs.contains_key("NPM_CONFIG_REGISTRY") {
|
2023-10-25 14:39:00 -04:00
|
|
|
envs.insert("NPM_CONFIG_REGISTRY".to_string(), npm_registry_unset_url());
|
2023-08-29 15:02:54 -04:00
|
|
|
}
|
2023-10-25 14:39:00 -04:00
|
|
|
command.envs(envs);
|
2023-02-27 15:52:49 -05:00
|
|
|
command.current_dir(cwd);
|
|
|
|
command.stdin(Stdio::piped());
|
2023-03-06 09:16:50 -05:00
|
|
|
|
|
|
|
let (combined_reader, std_out_err_handle) = if self.split_output {
|
|
|
|
let (stdout_reader, stdout_writer) = pipe().unwrap();
|
|
|
|
let (stderr_reader, stderr_writer) = pipe().unwrap();
|
|
|
|
command.stdout(stdout_writer);
|
|
|
|
command.stderr(stderr_writer);
|
|
|
|
(
|
|
|
|
None,
|
|
|
|
Some((
|
|
|
|
std::thread::spawn(move || read_pipe_to_string(stdout_reader)),
|
|
|
|
std::thread::spawn(move || read_pipe_to_string(stderr_reader)),
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
let (combined_reader, combined_writer) = pipe().unwrap();
|
|
|
|
command.stdout(combined_writer.try_clone().unwrap());
|
|
|
|
command.stderr(combined_writer);
|
|
|
|
(Some(combined_reader), None)
|
|
|
|
};
|
2023-02-27 15:52:49 -05:00
|
|
|
|
2023-05-26 13:33:38 -04:00
|
|
|
let mut process = command.spawn().expect("Failed spawning command");
|
2023-02-27 15:52:49 -05:00
|
|
|
|
|
|
|
if let Some(input) = &self.stdin {
|
|
|
|
let mut p_stdin = process.stdin.take().unwrap();
|
|
|
|
write!(p_stdin, "{input}").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This parent process is still holding its copies of the write ends,
|
|
|
|
// and we have to close them before we read, otherwise the read end
|
|
|
|
// will never report EOF. The Command object owns the writers now,
|
|
|
|
// and dropping it closes them.
|
|
|
|
drop(command);
|
|
|
|
|
2023-03-06 09:16:50 -05:00
|
|
|
let combined = combined_reader
|
|
|
|
.map(|pipe| sanitize_output(read_pipe_to_string(pipe), &args));
|
2023-02-27 15:52:49 -05:00
|
|
|
|
2023-03-06 09:16:50 -05:00
|
|
|
let status = process.wait().unwrap();
|
|
|
|
let std_out_err = std_out_err_handle.map(|(stdout, stderr)| {
|
|
|
|
(
|
|
|
|
sanitize_output(stdout.join().unwrap(), &args),
|
|
|
|
sanitize_output(stderr.join().unwrap(), &args),
|
|
|
|
)
|
|
|
|
});
|
2023-02-27 15:52:49 -05:00
|
|
|
let exit_code = status.code();
|
|
|
|
#[cfg(unix)]
|
|
|
|
let signal = {
|
|
|
|
use std::os::unix::process::ExitStatusExt;
|
|
|
|
status.signal()
|
|
|
|
};
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
let signal = None;
|
|
|
|
|
|
|
|
TestCommandOutput {
|
|
|
|
exit_code,
|
|
|
|
signal,
|
2023-03-06 09:16:50 -05:00
|
|
|
combined,
|
|
|
|
std_out_err,
|
2023-02-27 15:52:49 -05:00
|
|
|
testdata_dir: self.context.testdata_dir.clone(),
|
|
|
|
asserted_exit_code: RefCell::new(false),
|
2023-03-06 09:16:50 -05:00
|
|
|
asserted_stdout: RefCell::new(false),
|
|
|
|
asserted_stderr: RefCell::new(false),
|
|
|
|
asserted_combined: RefCell::new(false),
|
2023-02-27 15:52:49 -05:00
|
|
|
_test_context: self.context.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TestCommandOutput {
|
2023-03-06 09:16:50 -05:00
|
|
|
combined: Option<String>,
|
|
|
|
std_out_err: Option<(String, String)>,
|
2023-02-27 15:52:49 -05:00
|
|
|
exit_code: Option<i32>,
|
|
|
|
signal: Option<i32>,
|
2023-06-10 11:09:45 -04:00
|
|
|
testdata_dir: PathRef,
|
2023-03-06 09:16:50 -05:00
|
|
|
asserted_stdout: RefCell<bool>,
|
|
|
|
asserted_stderr: RefCell<bool>,
|
|
|
|
asserted_combined: RefCell<bool>,
|
2023-02-27 15:52:49 -05:00
|
|
|
asserted_exit_code: RefCell<bool>,
|
|
|
|
// keep alive for the duration of the output reference
|
|
|
|
_test_context: TestContext,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TestCommandOutput {
|
2023-04-06 21:41:19 -04:00
|
|
|
// assert the output and exit code was asserted
|
2023-02-27 15:52:49 -05:00
|
|
|
fn drop(&mut self) {
|
2023-03-06 09:16:50 -05:00
|
|
|
fn panic_unasserted_output(text: &str) {
|
|
|
|
println!("OUTPUT\n{text}\nOUTPUT");
|
2023-03-28 17:49:00 -04:00
|
|
|
panic!(concat!(
|
|
|
|
"The non-empty text of the command was not asserted. ",
|
|
|
|
"Call `output.skip_output_check()` to skip if necessary.",
|
|
|
|
),);
|
2023-03-06 09:16:50 -05:00
|
|
|
}
|
|
|
|
|
2023-02-27 15:52:49 -05:00
|
|
|
if std::thread::panicking() {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-06 09:16:50 -05:00
|
|
|
|
|
|
|
// either the combined output needs to be asserted or both stdout and stderr
|
|
|
|
if let Some(combined) = &self.combined {
|
|
|
|
if !*self.asserted_combined.borrow() && !combined.is_empty() {
|
|
|
|
panic_unasserted_output(combined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some((stdout, stderr)) = &self.std_out_err {
|
|
|
|
if !*self.asserted_stdout.borrow() && !stdout.is_empty() {
|
|
|
|
panic_unasserted_output(stdout);
|
|
|
|
}
|
|
|
|
if !*self.asserted_stderr.borrow() && !stderr.is_empty() {
|
|
|
|
panic_unasserted_output(stderr);
|
|
|
|
}
|
2023-02-27 15:52:49 -05:00
|
|
|
}
|
2023-04-06 21:41:19 -04:00
|
|
|
|
|
|
|
// now ensure the exit code was asserted
|
|
|
|
if !*self.asserted_exit_code.borrow() && self.exit_code != Some(0) {
|
2023-10-25 14:39:00 -04:00
|
|
|
self.print_output();
|
2023-04-06 21:41:19 -04:00
|
|
|
panic!(
|
|
|
|
"The non-zero exit code of the command was not asserted: {:?}",
|
|
|
|
self.exit_code,
|
|
|
|
)
|
|
|
|
}
|
2023-02-27 15:52:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestCommandOutput {
|
2023-06-10 11:09:45 -04:00
|
|
|
pub fn testdata_dir(&self) -> &PathRef {
|
2023-02-27 15:52:49 -05:00
|
|
|
&self.testdata_dir
|
|
|
|
}
|
|
|
|
|
2023-07-26 18:52:31 -04:00
|
|
|
pub fn skip_output_check(&self) -> &Self {
|
2023-03-06 09:16:50 -05:00
|
|
|
*self.asserted_combined.borrow_mut() = true;
|
|
|
|
*self.asserted_stdout.borrow_mut() = true;
|
|
|
|
*self.asserted_stderr.borrow_mut() = true;
|
2023-07-26 18:52:31 -04:00
|
|
|
self
|
2023-02-27 15:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn skip_exit_code_check(&self) {
|
|
|
|
*self.asserted_exit_code.borrow_mut() = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exit_code(&self) -> Option<i32> {
|
|
|
|
self.skip_exit_code_check();
|
|
|
|
self.exit_code
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn signal(&self) -> Option<i32> {
|
|
|
|
self.signal
|
|
|
|
}
|
|
|
|
|
2023-03-06 09:16:50 -05:00
|
|
|
pub fn combined_output(&self) -> &str {
|
2023-02-27 15:52:49 -05:00
|
|
|
self.skip_output_check();
|
2023-03-06 09:16:50 -05:00
|
|
|
self
|
|
|
|
.combined
|
|
|
|
.as_deref()
|
|
|
|
.expect("not available since .split_output() was called")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stdout(&self) -> &str {
|
|
|
|
*self.asserted_stdout.borrow_mut() = true;
|
|
|
|
self
|
|
|
|
.std_out_err
|
|
|
|
.as_ref()
|
|
|
|
.map(|(stdout, _)| stdout.as_str())
|
|
|
|
.expect("call .split_output() on the builder")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stderr(&self) -> &str {
|
|
|
|
*self.asserted_stderr.borrow_mut() = true;
|
|
|
|
self
|
|
|
|
.std_out_err
|
|
|
|
.as_ref()
|
|
|
|
.map(|(_, stderr)| stderr.as_str())
|
|
|
|
.expect("call .split_output() on the builder")
|
2023-02-27 15:52:49 -05:00
|
|
|
}
|
2023-02-28 14:10:12 -05:00
|
|
|
|
2023-03-28 17:49:00 -04:00
|
|
|
#[track_caller]
|
2023-02-28 14:10:12 -05:00
|
|
|
pub fn assert_exit_code(&self, expected_exit_code: i32) -> &Self {
|
|
|
|
let actual_exit_code = self.exit_code();
|
|
|
|
|
|
|
|
if let Some(exit_code) = &actual_exit_code {
|
|
|
|
if *exit_code != expected_exit_code {
|
2023-03-06 09:16:50 -05:00
|
|
|
self.print_output();
|
2023-02-28 14:10:12 -05:00
|
|
|
panic!(
|
2023-03-28 17:49:00 -04:00
|
|
|
"bad exit code, expected: {:?}, actual: {:?}",
|
|
|
|
expected_exit_code, exit_code,
|
2023-02-28 14:10:12 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2023-03-06 09:16:50 -05:00
|
|
|
self.print_output();
|
2023-02-28 14:10:12 -05:00
|
|
|
if let Some(signal) = self.signal() {
|
|
|
|
panic!(
|
2023-03-28 17:49:00 -04:00
|
|
|
"process terminated by signal, expected exit code: {:?}, actual signal: {:?}",
|
2023-02-28 14:10:12 -05:00
|
|
|
actual_exit_code,
|
|
|
|
signal,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
panic!(
|
2023-03-28 17:49:00 -04:00
|
|
|
"process terminated without status code on non unix platform, expected exit code: {:?}",
|
2023-02-28 14:10:12 -05:00
|
|
|
actual_exit_code,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-03-06 09:16:50 -05:00
|
|
|
pub fn print_output(&self) {
|
|
|
|
if let Some(combined) = &self.combined {
|
|
|
|
println!("OUTPUT\n{combined}\nOUTPUT");
|
|
|
|
} else if let Some((stdout, stderr)) = &self.std_out_err {
|
|
|
|
println!("STDOUT OUTPUT\n{stdout}\nSTDOUT OUTPUT");
|
|
|
|
println!("STDERR OUTPUT\n{stderr}\nSTDERR OUTPUT");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 17:49:00 -04:00
|
|
|
#[track_caller]
|
2023-02-28 14:10:12 -05:00
|
|
|
pub fn assert_matches_text(&self, expected_text: impl AsRef<str>) -> &Self {
|
2023-03-06 09:16:50 -05:00
|
|
|
self.inner_assert_matches_text(self.combined_output(), expected_text)
|
|
|
|
}
|
|
|
|
|
2023-03-28 17:49:00 -04:00
|
|
|
#[track_caller]
|
2023-03-06 09:16:50 -05:00
|
|
|
pub fn assert_matches_file(&self, file_path: impl AsRef<Path>) -> &Self {
|
|
|
|
self.inner_assert_matches_file(self.combined_output(), file_path)
|
|
|
|
}
|
|
|
|
|
2023-03-28 17:49:00 -04:00
|
|
|
#[track_caller]
|
2023-03-06 09:16:50 -05:00
|
|
|
pub fn assert_stdout_matches_text(
|
|
|
|
&self,
|
|
|
|
expected_text: impl AsRef<str>,
|
|
|
|
) -> &Self {
|
|
|
|
self.inner_assert_matches_text(self.stdout(), expected_text)
|
|
|
|
}
|
2023-02-28 14:10:12 -05:00
|
|
|
|
2023-03-28 17:49:00 -04:00
|
|
|
#[track_caller]
|
2023-03-06 09:16:50 -05:00
|
|
|
pub fn assert_stdout_matches_file(
|
|
|
|
&self,
|
|
|
|
file_path: impl AsRef<Path>,
|
|
|
|
) -> &Self {
|
|
|
|
self.inner_assert_matches_file(self.stdout(), file_path)
|
|
|
|
}
|
|
|
|
|
2023-03-28 17:49:00 -04:00
|
|
|
#[track_caller]
|
2023-03-06 09:16:50 -05:00
|
|
|
pub fn assert_stderr_matches_text(
|
|
|
|
&self,
|
|
|
|
expected_text: impl AsRef<str>,
|
|
|
|
) -> &Self {
|
|
|
|
self.inner_assert_matches_text(self.stderr(), expected_text)
|
|
|
|
}
|
|
|
|
|
2023-03-28 17:49:00 -04:00
|
|
|
#[track_caller]
|
2023-06-26 09:10:27 -04:00
|
|
|
pub fn assert_stderr_matches_file(
|
2023-03-06 09:16:50 -05:00
|
|
|
&self,
|
|
|
|
file_path: impl AsRef<Path>,
|
|
|
|
) -> &Self {
|
|
|
|
self.inner_assert_matches_file(self.stderr(), file_path)
|
|
|
|
}
|
|
|
|
|
2023-03-28 17:49:00 -04:00
|
|
|
#[track_caller]
|
2023-03-06 09:16:50 -05:00
|
|
|
fn inner_assert_matches_text(
|
|
|
|
&self,
|
|
|
|
actual: &str,
|
|
|
|
expected: impl AsRef<str>,
|
|
|
|
) -> &Self {
|
2023-06-26 15:53:17 -04:00
|
|
|
assert_wildcard_match(actual, expected.as_ref());
|
2023-02-28 14:10:12 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-03-28 17:49:00 -04:00
|
|
|
#[track_caller]
|
2023-03-06 09:16:50 -05:00
|
|
|
fn inner_assert_matches_file(
|
|
|
|
&self,
|
|
|
|
actual: &str,
|
|
|
|
file_path: impl AsRef<Path>,
|
|
|
|
) -> &Self {
|
2023-02-28 14:10:12 -05:00
|
|
|
let output_path = self.testdata_dir().join(file_path);
|
2023-06-10 11:09:45 -04:00
|
|
|
println!("output path {}", output_path);
|
2023-06-26 15:53:17 -04:00
|
|
|
let expected_text = output_path.read_to_string();
|
2023-03-06 09:16:50 -05:00
|
|
|
self.inner_assert_matches_text(actual, expected_text)
|
2023-02-28 14:10:12 -05:00
|
|
|
}
|
|
|
|
}
|