From cebefa87832244682edeacae81c05b38c2414ca0 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Wed, 22 Mar 2023 12:55:19 -0600 Subject: [PATCH] chore(test_util): replace tempdir code w/tempdir crate (#18340) --- Cargo.lock | 1 + Cargo.toml | 1 + cli/Cargo.toml | 2 +- cli/bench/main.rs | 2 +- cli/tools/standalone.rs | 2 +- cli/tools/upgrade.rs | 4 +-- test_util/Cargo.toml | 1 + test_util/src/temp_dir.rs | 57 +++++++-------------------------------- 8 files changed, 17 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e18d644cf4..5a498de262 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4632,6 +4632,7 @@ dependencies = [ "serde", "serde_json", "tar", + "tempfile", "tokio", "tokio-rustls", "tokio-tungstenite", diff --git a/Cargo.toml b/Cargo.toml index 4299604693..6ad0da58d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -123,6 +123,7 @@ sha2 = { version = "0.10.6", features = ["oid"] } smallvec = "1.8" socket2 = "0.4.7" tar = "=0.4.38" +tempfile = "3.4.0" thiserror = "=1.0.38" tokio = { version = "=1.25.0", features = ["full"] } tokio-rustls = "0.23.3" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index f9214c999d..7fd9046812 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -91,11 +91,11 @@ regex.workspace = true ring.workspace = true rustyline = { version = "=10.0.0", default-features = false, features = ["custom-bindings"] } rustyline-derive = "=0.7.0" -secure_tempfile = { version = "=3.4.0", package = "tempfile" } # different name to discourage use in tests serde.workspace = true serde_repr.workspace = true shell-escape = "=0.1.5" tar.workspace = true +tempfile.workspace = true text-size = "=1.1.0" text_lines = "=0.6.0" thiserror.workspace = true diff --git a/cli/bench/main.rs b/cli/bench/main.rs index ac1798b021..02c775e8fb 100644 --- a/cli/bench/main.rs +++ b/cli/bench/main.rs @@ -502,7 +502,7 @@ async fn main() -> Result<()> { let mut syscall_count = HashMap::::new(); for (name, args, expected_exit_code) in EXEC_TIME_BENCHMARKS { - let mut file = secure_tempfile::NamedTempFile::new()?; + let mut file = tempfile::NamedTempFile::new()?; let exit_status = Command::new("strace") .args([ diff --git a/cli/tools/standalone.rs b/cli/tools/standalone.rs index 93c3aebf0e..31f472b789 100644 --- a/cli/tools/standalone.rs +++ b/cli/tools/standalone.rs @@ -120,7 +120,7 @@ async fn get_base_binary( } let archive_data = tokio::fs::read(binary_path).await?; - let temp_dir = secure_tempfile::TempDir::new()?; + let temp_dir = tempfile::TempDir::new()?; let base_binary_path = crate::tools::upgrade::unpack_into_dir( archive_data, target.contains("windows"), diff --git a/cli/tools/upgrade.rs b/cli/tools/upgrade.rs index 039e00b003..7ce9e77b9e 100644 --- a/cli/tools/upgrade.rs +++ b/cli/tools/upgrade.rs @@ -379,7 +379,7 @@ pub async fn upgrade( log::info!("Deno is upgrading to version {}", &install_version); - let temp_dir = secure_tempfile::TempDir::new()?; + let temp_dir = tempfile::TempDir::new()?; let new_exe_path = unpack_into_dir(archive_data, cfg!(windows), &temp_dir)?; fs::set_permissions(&new_exe_path, permissions)?; check_exe(&new_exe_path)?; @@ -476,7 +476,7 @@ async fn download_package( pub fn unpack_into_dir( archive_data: Vec, is_windows: bool, - temp_dir: &secure_tempfile::TempDir, + temp_dir: &tempfile::TempDir, ) -> Result { const EXE_NAME: &str = "deno"; let temp_dir_path = temp_dir.path(); diff --git a/test_util/Cargo.toml b/test_util/Cargo.toml index 705ccda408..a6e985b6dc 100644 --- a/test_util/Cargo.toml +++ b/test_util/Cargo.toml @@ -36,6 +36,7 @@ semver = "=1.0.14" serde.workspace = true serde_json.workspace = true tar.workspace = true +tempfile.workspace = true tokio.workspace = true tokio-rustls.workspace = true tokio-tungstenite.workspace = true diff --git a/test_util/src/temp_dir.rs b/test_util/src/temp_dir.rs index b800e425d2..db3c246dc5 100644 --- a/test_util/src/temp_dir.rs +++ b/test_util/src/temp_dir.rs @@ -2,22 +2,10 @@ use std::fs; 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 lsp_types::Url; -use once_cell::sync::OnceCell; - -static TEMP_DIR_SESSION: OnceCell = OnceCell::new(); - -struct TempDirSession { - default_prefix: String, - counter: AtomicU32, -} /// For creating temporary directories in tests. /// @@ -26,15 +14,7 @@ struct TempDirSession { /// 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); - -struct TempDirInner(PathBuf); - -impl Drop for TempDirInner { - fn drop(&mut self) { - let _ = std::fs::remove_dir_all(&self.0); - } -} +pub struct TempDir(Arc); impl Default for TempDir { fn default() -> Self { @@ -55,33 +35,14 @@ impl TempDir { Self::new_inner(&std::env::temp_dir(), Some(prefix)) } + /// Create a new temporary directory with the given prefix as part of its name, if specified. 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)) - }) + let mut builder = tempfile::Builder::new(); + builder.prefix(prefix.unwrap_or("deno-cli-test")); + let dir = builder + .tempdir_in(parent_dir) + .expect("Failed to create a temporary directory"); + Self(dir.into()) } pub fn uri(&self) -> Url { @@ -90,7 +51,7 @@ impl TempDir { pub fn path(&self) -> &Path { let inner = &self.0; - inner.0.as_path() + inner.path() } pub fn create_dir_all(&self, path: impl AsRef) {