From be06aaecdebabf5a3a60a367bbd672508a9cb8f8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 23 Jul 2020 22:26:25 +0200 Subject: Lighter weight tempdir --- crates/rust-analyzer/Cargo.toml | 1 - crates/rust-analyzer/tests/heavy_tests/main.rs | 9 ++-- crates/rust-analyzer/tests/heavy_tests/support.rs | 13 ++--- crates/rust-analyzer/tests/heavy_tests/testdir.rs | 62 +++++++++++++++++++++++ 4 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 crates/rust-analyzer/tests/heavy_tests/testdir.rs (limited to 'crates') diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index 758aa1c5d..3f9c820c5 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 93448834f..28e896648 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 e51796d36..e152264d3 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 000000000..7487e7429 --- /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) +} -- cgit v1.2.3