1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

chore(test_util): replace tempdir code w/tempdir crate (#18340)

This commit is contained in:
Matt Mastracci 2023-03-22 12:55:19 -06:00 committed by GitHub
parent df614ff6e5
commit cebefa8783
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 17 additions and 53 deletions

1
Cargo.lock generated
View file

@ -4632,6 +4632,7 @@ dependencies = [
"serde",
"serde_json",
"tar",
"tempfile",
"tokio",
"tokio-rustls",
"tokio-tungstenite",

View file

@ -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"

View file

@ -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

View file

@ -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([

View file

@ -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"),

View file

@ -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();

View file

@ -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

View file

@ -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>) {