Lighter weight tempdir

This commit is contained in:
Aleksey Kladov 2020-07-23 22:26:25 +02:00
parent bd44f3a620
commit be06aaecde
5 changed files with 75 additions and 34 deletions

24
Cargo.lock generated
View file

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

View file

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

View file

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

View file

@ -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<TempDir>,
tmp_dir: Option<TestDir>,
roots: Vec<PathBuf>,
config: Option<Box<dyn Fn(&mut Config)>>,
}
@ -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()

View file

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