diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-04-01 11:15:37 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-01 11:15:37 -0400 |
commit | 1c37ac33526dc45ad0b3f83ca8294dbb55548096 (patch) | |
tree | 426978168928c5e7c2223e4906005fa23fb34cd6 /test_util/src | |
parent | 8ca4c1819f3e7a8716c68034e256355334d53b44 (diff) |
chore(tests): use custom temp dir creation for the tests (#14153)
Diffstat (limited to 'test_util/src')
-rw-r--r-- | test_util/src/lib.rs | 49 | ||||
-rw-r--r-- | test_util/src/lsp.rs | 2 | ||||
-rw-r--r-- | test_util/src/temp_dir.rs | 87 |
3 files changed, 126 insertions, 12 deletions
diff --git a/test_util/src/lib.rs b/test_util/src/lib.rs index e865572d2..ee8c363eb 100644 --- a/test_util/src/lib.rs +++ b/test_util/src/lib.rs @@ -28,6 +28,8 @@ use std::io::Read; use std::io::Write; use std::mem::replace; use std::net::SocketAddr; +use std::ops::Deref; +use std::ops::DerefMut; use std::path::PathBuf; use std::pin::Pin; use std::process::Child; @@ -40,7 +42,6 @@ use std::sync::Mutex; use std::sync::MutexGuard; use std::task::Context; use std::task::Poll; -use tempfile::TempDir; use tokio::io::AsyncWriteExt; use tokio::net::TcpListener; use tokio::net::TcpStream; @@ -50,6 +51,9 @@ use tokio_tungstenite::accept_async; pub mod lsp; pub mod pty; +mod temp_dir; + +pub use temp_dir::TempDir; const PORT: u16 = 4545; const TEST_AUTH_TOKEN: &str = "abcdef123456789"; @@ -1654,20 +1658,42 @@ pub fn run_and_collect_output_with_args( } pub fn new_deno_dir() -> TempDir { - TempDir::new().expect("tempdir fail") + TempDir::new() +} + +pub struct DenoCmd { + // keep the deno dir directory alive for the duration of the command + _deno_dir: TempDir, + cmd: Command, +} + +impl Deref for DenoCmd { + type Target = Command; + fn deref(&self) -> &Command { + &self.cmd + } } -pub fn deno_cmd() -> Command { +impl DerefMut for DenoCmd { + fn deref_mut(&mut self) -> &mut Command { + &mut self.cmd + } +} + +pub fn deno_cmd() -> DenoCmd { let deno_dir = new_deno_dir(); - deno_cmd_with_deno_dir(deno_dir.path()) + deno_cmd_with_deno_dir(&deno_dir) } -pub fn deno_cmd_with_deno_dir(deno_dir: &std::path::Path) -> Command { - let e = deno_exe_path(); - assert!(e.exists()); - let mut c = Command::new(e); - c.env("DENO_DIR", deno_dir); - c +pub fn deno_cmd_with_deno_dir(deno_dir: &TempDir) -> DenoCmd { + let exe_path = deno_exe_path(); + assert!(exe_path.exists()); + let mut cmd = Command::new(exe_path); + cmd.env("DENO_DIR", deno_dir.path()); + DenoCmd { + _deno_dir: deno_dir.clone(), + cmd, + } } pub fn run_powershell_script_file( @@ -1735,7 +1761,8 @@ impl CheckOutputIntegrationTest { let (mut reader, writer) = pipe().unwrap(); let testdata_dir = testdata_path(); - let mut command = deno_cmd(); + let deno_dir = new_deno_dir(); // keep this alive for the test + let mut command = deno_cmd_with_deno_dir(&deno_dir); println!("deno_exe args {}", self.args); println!("deno_exe testdata path {:?}", &testdata_dir); command.args(args.iter()); diff --git a/test_util/src/lsp.rs b/test_util/src/lsp.rs index c898856bf..1a3104542 100644 --- a/test_util/src/lsp.rs +++ b/test_util/src/lsp.rs @@ -1,6 +1,7 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. use super::new_deno_dir; +use super::TempDir; use anyhow::Result; use lazy_static::lazy_static; @@ -23,7 +24,6 @@ use std::process::Stdio; use std::sync::Arc; use std::time::Duration; use std::time::Instant; -use tempfile::TempDir; lazy_static! { static ref CONTENT_TYPE_REG: Regex = diff --git a/test_util/src/temp_dir.rs b/test_util/src/temp_dir.rs new file mode 100644 index 000000000..da2b2a06e --- /dev/null +++ b/test_util/src/temp_dir.rs @@ -0,0 +1,87 @@ +use std::path::Path; +use std::path::PathBuf; +use std::sync::atomic::AtomicU32; +use std::sync::atomic::Ordering; +use std::sync::Arc; +use std::time::SystemTime; + +use anyhow::Context; +use once_cell::sync::OnceCell; + +static TEMP_DIR_SESSION: OnceCell<TempDirSession> = OnceCell::new(); + +struct TempDirSession { + default_prefix: String, + counter: AtomicU32, +} + +/// For creating temporary directories in tests. +/// +/// This was done because `tempfiles::TempDir` was very slow on Windows. +/// +/// Note: Do not use this in actual code as this does not protect against +/// "insecure temporary file" security vulnerabilities. +#[derive(Clone)] +pub struct TempDir(Arc<TempDirInner>); + +struct TempDirInner(PathBuf); + +impl Drop for TempDirInner { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +impl Default for TempDir { + fn default() -> Self { + Self::new() + } +} + +impl TempDir { + pub fn new() -> Self { + Self::new_inner(&std::env::temp_dir(), None) + } + + pub fn new_in(path: &Path) -> Self { + Self::new_inner(path, None) + } + + pub fn new_with_prefix(prefix: &str) -> Self { + Self::new_inner(&std::env::temp_dir(), Some(prefix)) + } + + fn new_inner(parent_dir: &Path, prefix: Option<&str>) -> Self { + let session = TEMP_DIR_SESSION.get_or_init(|| { + let default_prefix = format!( + "deno-cli-test-{}", + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_millis() + ); + TempDirSession { + default_prefix, + counter: Default::default(), + } + }); + Self({ + let count = session.counter.fetch_add(1, Ordering::SeqCst); + let path = parent_dir.join(format!( + "{}{}-{}", + prefix.unwrap_or(""), + session.default_prefix, + count, + )); + std::fs::create_dir_all(&path) + .with_context(|| format!("Error creating temp dir: {}", path.display())) + .unwrap(); + Arc::new(TempDirInner(path)) + }) + } + + pub fn path(&self) -> &Path { + let inner = &self.0; + inner.0.as_path() + } +} |