summaryrefslogtreecommitdiff
path: root/test_util/src/builders.rs
diff options
context:
space:
mode:
Diffstat (limited to 'test_util/src/builders.rs')
-rw-r--r--test_util/src/builders.rs71
1 files changed, 40 insertions, 31 deletions
diff --git a/test_util/src/builders.rs b/test_util/src/builders.rs
index 049ccbd67..582380bac 100644
--- a/test_util/src/builders.rs
+++ b/test_util/src/builders.rs
@@ -2,6 +2,7 @@
use std::cell::RefCell;
use std::collections::HashMap;
+use std::ffi::OsStr;
use std::io::Read;
use std::io::Write;
use std::path::Path;
@@ -13,9 +14,9 @@ use std::rc::Rc;
use os_pipe::pipe;
use pretty_assertions::assert_eq;
-use crate::copy_dir_recursive;
use crate::deno_exe_path;
use crate::env_vars_for_npm_tests_no_sync_download;
+use crate::fs::PathRef;
use crate::http_server;
use crate::lsp::LspClientBuilder;
use crate::new_deno_dir;
@@ -38,7 +39,7 @@ pub struct TestContextBuilder {
copy_temp_dir: Option<String>,
cwd: Option<String>,
envs: HashMap<String, String>,
- deno_exe: Option<PathBuf>,
+ deno_exe: Option<PathRef>,
}
impl TestContextBuilder {
@@ -129,17 +130,17 @@ impl TestContextBuilder {
temp_dir
};
let testdata_dir = if let Some(temp_copy_dir) = &self.copy_temp_dir {
- let test_data_path = testdata_path().join(temp_copy_dir);
+ let test_data_path = PathRef::new(testdata_path()).join(temp_copy_dir);
let temp_copy_dir = temp_dir.path().join(temp_copy_dir);
- std::fs::create_dir_all(&temp_copy_dir).unwrap();
- copy_dir_recursive(&test_data_path, &temp_copy_dir).unwrap();
- temp_dir.path().to_owned()
+ temp_copy_dir.create_dir_all();
+ test_data_path.copy_to_recursive(&temp_copy_dir);
+ temp_dir.path().clone()
} else {
- testdata_path()
+ PathRef::new(testdata_path())
};
let deno_exe = self.deno_exe.clone().unwrap_or_else(deno_exe_path);
- println!("deno_exe path {}", deno_exe.display());
+ println!("deno_exe path {}", deno_exe);
let http_server_guard = if self.use_http_server {
Some(Rc::new(http_server()))
@@ -162,14 +163,14 @@ impl TestContextBuilder {
#[derive(Clone)]
pub struct TestContext {
- deno_exe: PathBuf,
+ deno_exe: PathRef,
envs: HashMap<String, String>,
use_temp_cwd: bool,
cwd: Option<String>,
_http_server_guard: Option<Rc<HttpServerGuard>>,
deno_dir: TempDir,
temp_dir: TempDir,
- testdata_dir: PathBuf,
+ testdata_dir: PathRef,
}
impl Default for TestContext {
@@ -183,7 +184,7 @@ impl TestContext {
TestContextBuilder::default().use_http_server().build()
}
- pub fn testdata_path(&self) -> &PathBuf {
+ pub fn testdata_path(&self) -> &PathRef {
&self.testdata_dir
}
@@ -197,7 +198,7 @@ impl TestContext {
pub fn new_command(&self) -> TestCommandBuilder {
TestCommandBuilder {
- command_name: self.deno_exe.to_string_lossy().to_string(),
+ command_name: self.deno_exe.to_string(),
args: Default::default(),
args_vec: Default::default(),
stdin: Default::default(),
@@ -229,8 +230,8 @@ pub struct TestCommandBuilder {
}
impl TestCommandBuilder {
- pub fn command_name(mut self, name: impl AsRef<str>) -> Self {
- self.command_name = name.as_ref().to_string();
+ pub fn command_name(mut self, name: impl AsRef<OsStr>) -> Self {
+ self.command_name = name.as_ref().to_string_lossy().to_string();
self
}
@@ -261,10 +262,15 @@ impl TestCommandBuilder {
self
}
- pub fn env(mut self, key: impl AsRef<str>, value: impl AsRef<str>) -> Self {
- self
- .envs
- .insert(key.as_ref().to_string(), value.as_ref().to_string());
+ 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(),
+ );
self
}
@@ -273,12 +279,12 @@ impl TestCommandBuilder {
self
}
- pub fn cwd(mut self, cwd: impl AsRef<str>) -> Self {
- self.cwd = Some(cwd.as_ref().to_string());
+ pub fn cwd(mut self, cwd: impl AsRef<OsStr>) -> Self {
+ self.cwd = Some(cwd.as_ref().to_string_lossy().to_string());
self
}
- fn build_cwd(&self) -> PathBuf {
+ fn build_cwd(&self) -> PathRef {
let cwd = self.cwd.as_ref().or(self.context.cwd.as_ref());
if self.context.use_temp_cwd {
assert!(cwd.is_none());
@@ -290,12 +296,12 @@ impl TestCommandBuilder {
}
}
- fn build_command_path(&self) -> PathBuf {
+ fn build_command_path(&self) -> PathRef {
let command_name = &self.command_name;
if command_name == "deno" {
deno_exe_path()
} else {
- PathBuf::from(command_name)
+ PathRef::new(PathBuf::from(command_name))
}
}
@@ -317,7 +323,10 @@ impl TestCommandBuilder {
}
.iter()
.map(|arg| {
- arg.replace("$TESTDATA", &self.context.testdata_dir.to_string_lossy())
+ arg.replace(
+ "$TESTDATA",
+ &self.context.testdata_dir.as_path().to_string_lossy(),
+ )
})
.collect::<Vec<_>>()
}
@@ -352,9 +361,9 @@ impl TestCommandBuilder {
}
action(Pty::new(
- &self.build_command_path(),
+ self.build_command_path().as_path(),
&args,
- &self.build_cwd(),
+ self.build_cwd().as_path(),
Some(envs),
))
}
@@ -383,7 +392,7 @@ impl TestCommandBuilder {
let mut command = Command::new(self.build_command_path());
println!("command {} {}", self.command_name, args.join(" "));
- println!("command cwd {:?}", &cwd);
+ println!("command cwd {}", cwd);
command.args(args.iter());
if self.env_clear {
command.env_clear();
@@ -464,7 +473,7 @@ pub struct TestCommandOutput {
std_out_err: Option<(String, String)>,
exit_code: Option<i32>,
signal: Option<i32>,
- testdata_dir: PathBuf,
+ testdata_dir: PathRef,
asserted_stdout: RefCell<bool>,
asserted_stderr: RefCell<bool>,
asserted_combined: RefCell<bool>,
@@ -514,7 +523,7 @@ impl Drop for TestCommandOutput {
}
impl TestCommandOutput {
- pub fn testdata_dir(&self) -> &PathBuf {
+ pub fn testdata_dir(&self) -> &PathRef {
&self.testdata_dir
}
@@ -669,10 +678,10 @@ impl TestCommandOutput {
file_path: impl AsRef<Path>,
) -> &Self {
let output_path = self.testdata_dir().join(file_path);
- println!("output path {}", output_path.display());
+ println!("output path {}", output_path);
let expected_text =
std::fs::read_to_string(&output_path).unwrap_or_else(|err| {
- panic!("failed loading {}\n\n{err:#}", output_path.display())
+ panic!("failed loading {}\n\n{err:#}", output_path)
});
self.inner_assert_matches_text(actual, expected_text)
}