summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-03-22 12:55:19 -0600
committerGitHub <noreply@github.com>2023-03-22 12:55:19 -0600
commitcebefa87832244682edeacae81c05b38c2414ca0 (patch)
treefe038b47e130c3867c5bf74e4d1bbdc9f8fe2c89
parentdf614ff6e5cc0b3d48534fb20a348412bebea576 (diff)
chore(test_util): replace tempdir code w/tempdir crate (#18340)
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--cli/Cargo.toml2
-rw-r--r--cli/bench/main.rs2
-rw-r--r--cli/tools/standalone.rs2
-rw-r--r--cli/tools/upgrade.rs4
-rw-r--r--test_util/Cargo.toml1
-rw-r--r--test_util/src/temp_dir.rs57
8 files changed, 17 insertions, 53 deletions
diff --git a/Cargo.lock b/Cargo.lock
index e18d644cf..5a498de26 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 429960469..6ad0da58d 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 f9214c999..7fd904681 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 ac1798b02..02c775e8f 100644
--- a/cli/bench/main.rs
+++ b/cli/bench/main.rs
@@ -502,7 +502,7 @@ async fn main() -> Result<()> {
let mut syscall_count = HashMap::<String, i64>::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 93c3aebf0..31f472b78 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 039e00b00..7ce9e77b9 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<u8>,
is_windows: bool,
- temp_dir: &secure_tempfile::TempDir,
+ temp_dir: &tempfile::TempDir,
) -> Result<PathBuf, std::io::Error> {
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 705ccda40..a6e985b6d 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 b800e425d..db3c246dc 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<TempDirSession> = 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<TempDirInner>);
-
-struct TempDirInner(PathBuf);
-
-impl Drop for TempDirInner {
- fn drop(&mut self) {
- let _ = std::fs::remove_dir_all(&self.0);
- }
-}
+pub struct TempDir(Arc<tempfile::TempDir>);
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<Path>) {