From be06aaecdebabf5a3a60a367bbd672508a9cb8f8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 23 Jul 2020 22:26:25 +0200 Subject: [PATCH] Lighter weight tempdir --- Cargo.lock | 24 ------- crates/rust-analyzer/Cargo.toml | 1 - .../rust-analyzer/tests/heavy_tests/main.rs | 9 ++- .../tests/heavy_tests/support.rs | 13 ++-- .../tests/heavy_tests/testdir.rs | 62 +++++++++++++++++++ 5 files changed, 75 insertions(+), 34 deletions(-) create mode 100644 crates/rust-analyzer/tests/heavy_tests/testdir.rs diff --git a/Cargo.lock b/Cargo.lock index 007b0d6a55a..0a35eb79329 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1326,15 +1326,6 @@ version = "0.6.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" -[[package]] -name = "remove_dir_all" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" -dependencies = [ - "winapi 0.3.9", -] - [[package]] name = "rowan" version = "0.10.0" @@ -1386,7 +1377,6 @@ dependencies = [ "serde", "serde_json", "stdx", - "tempfile", "test_utils", "threadpool", "vfs", @@ -1606,20 +1596,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "tempfile" -version = "3.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" -dependencies = [ - "cfg-if", - "libc", - "rand", - "redox_syscall", - "remove_dir_all", - "winapi 0.3.9", -] - [[package]] name = "termcolor" version = "1.1.0" diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index 758aa1c5d7d..3f9c820c5d3 100644 --- a/crates/rust-analyzer/Cargo.toml +++ b/crates/rust-analyzer/Cargo.toml @@ -58,7 +58,6 @@ ra_proc_macro_srv = { path = "../ra_proc_macro_srv" } winapi = "0.3.8" [dev-dependencies] -tempfile = "3.1.0" expect = { path = "../expect" } test_utils = { path = "../test_utils" } mbe = { path = "../ra_mbe", package = "ra_mbe" } diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs index 93448834f74..28e89664873 100644 --- a/crates/rust-analyzer/tests/heavy_tests/main.rs +++ b/crates/rust-analyzer/tests/heavy_tests/main.rs @@ -1,3 +1,4 @@ +mod testdir; mod support; use std::{collections::HashMap, path::PathBuf, time::Instant}; @@ -12,10 +13,12 @@ use lsp_types::{ }; use rust_analyzer::lsp_ext::{OnEnter, Runnables, RunnablesParams}; use serde_json::json; -use tempfile::TempDir; use test_utils::skip_slow_tests; -use crate::support::{project, Project}; +use crate::{ + support::{project, Project}, + testdir::TestDir, +}; const PROFILE: &str = ""; // const PROFILE: &'static str = "*@3>100"; @@ -308,7 +311,7 @@ fn test_missing_module_code_action_in_json_project() { return; } - let tmp_dir = TempDir::new().unwrap(); + let tmp_dir = TestDir::new(); let path = tmp_dir.path(); diff --git a/crates/rust-analyzer/tests/heavy_tests/support.rs b/crates/rust-analyzer/tests/heavy_tests/support.rs index e51796d36e0..e152264d369 100644 --- a/crates/rust-analyzer/tests/heavy_tests/support.rs +++ b/crates/rust-analyzer/tests/heavy_tests/support.rs @@ -19,14 +19,15 @@ use rust_analyzer::{ }; use serde::Serialize; use serde_json::{to_string_pretty, Value}; -use tempfile::TempDir; use test_utils::{find_mismatch, Fixture}; use vfs::AbsPathBuf; +use crate::testdir::TestDir; + pub struct Project<'a> { fixture: &'a str, with_sysroot: bool, - tmp_dir: Option, + tmp_dir: Option, roots: Vec, config: Option>, } @@ -36,7 +37,7 @@ impl<'a> Project<'a> { Project { fixture, tmp_dir: None, roots: vec![], with_sysroot: false, config: None } } - pub fn tmp_dir(mut self, tmp_dir: TempDir) -> Project<'a> { + pub fn tmp_dir(mut self, tmp_dir: TestDir) -> Project<'a> { self.tmp_dir = Some(tmp_dir); self } @@ -57,7 +58,7 @@ impl<'a> Project<'a> { } pub fn server(self) -> Server { - let tmp_dir = self.tmp_dir.unwrap_or_else(|| TempDir::new().unwrap()); + let tmp_dir = self.tmp_dir.unwrap_or_else(|| TestDir::new()); static INIT: Once = Once::new(); INIT.call_once(|| { env_logger::builder().is_test(true).try_init().unwrap(); @@ -112,11 +113,11 @@ pub struct Server { _thread: jod_thread::JoinHandle<()>, client: Connection, /// XXX: remove the tempdir last - dir: TempDir, + dir: TestDir, } impl Server { - fn new(dir: TempDir, config: Config) -> Server { + fn new(dir: TestDir, config: Config) -> Server { let (connection, client) = Connection::memory(); let _thread = jod_thread::Builder::new() diff --git a/crates/rust-analyzer/tests/heavy_tests/testdir.rs b/crates/rust-analyzer/tests/heavy_tests/testdir.rs new file mode 100644 index 00000000000..7487e742947 --- /dev/null +++ b/crates/rust-analyzer/tests/heavy_tests/testdir.rs @@ -0,0 +1,62 @@ +use std::{ + fs, io, + path::{Path, PathBuf}, + sync::atomic::{AtomicUsize, Ordering}, +}; + +pub struct TestDir { + path: PathBuf, + keep: bool, +} + +impl TestDir { + pub fn new() -> TestDir { + let base = std::env::temp_dir().join("testdir"); + let pid = std::process::id(); + + static CNT: AtomicUsize = AtomicUsize::new(0); + for _ in 0..100 { + let cnt = CNT.fetch_add(1, Ordering::Relaxed); + let path = base.join(format!("{}_{}", pid, cnt)); + if path.is_dir() { + continue; + } + fs::create_dir_all(&path).unwrap(); + return TestDir { path, keep: false }; + } + panic!("Failed to create a temporary directory") + } + #[allow(unused)] + pub fn keep(mut self) -> TestDir { + self.keep = true; + self + } + pub fn path(&self) -> &Path { + &self.path + } +} + +impl Drop for TestDir { + fn drop(&mut self) { + if self.keep { + return; + } + remove_dir_all(&self.path).unwrap() + } +} + +#[cfg(not(windows))] +fn remove_dir_all(path: &Path) -> io::Result<()> { + fs::remove_dir_all(path) +} + +#[cfg(windows)] +fn remove_dir_all(path: &Path) -> io::Result<()> { + for _ in 0..99 { + if fs::remove_dir_all(path).is_ok() { + return Ok(()); + } + std::thread::sleep(std::time::Duration::from_millis(10)) + } + fs::remove_dir_all(path) +}