Skip to content

Commit be06aae

Browse files
committed
Lighter weight tempdir
1 parent bd44f3a commit be06aae

File tree

5 files changed

+75
-34
lines changed

5 files changed

+75
-34
lines changed

Cargo.lock

Lines changed: 0 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rust-analyzer/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ ra_proc_macro_srv = { path = "../ra_proc_macro_srv" }
5858
winapi = "0.3.8"
5959

6060
[dev-dependencies]
61-
tempfile = "3.1.0"
6261
expect = { path = "../expect" }
6362
test_utils = { path = "../test_utils" }
6463
mbe = { path = "../ra_mbe", package = "ra_mbe" }

crates/rust-analyzer/tests/heavy_tests/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod testdir;
12
mod support;
23

34
use std::{collections::HashMap, path::PathBuf, time::Instant};
@@ -12,10 +13,12 @@ use lsp_types::{
1213
};
1314
use rust_analyzer::lsp_ext::{OnEnter, Runnables, RunnablesParams};
1415
use serde_json::json;
15-
use tempfile::TempDir;
1616
use test_utils::skip_slow_tests;
1717

18-
use crate::support::{project, Project};
18+
use crate::{
19+
support::{project, Project},
20+
testdir::TestDir,
21+
};
1922

2023
const PROFILE: &str = "";
2124
// const PROFILE: &'static str = "*@3>100";
@@ -308,7 +311,7 @@ fn test_missing_module_code_action_in_json_project() {
308311
return;
309312
}
310313

311-
let tmp_dir = TempDir::new().unwrap();
314+
let tmp_dir = TestDir::new();
312315

313316
let path = tmp_dir.path();
314317

crates/rust-analyzer/tests/heavy_tests/support.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ use rust_analyzer::{
1919
};
2020
use serde::Serialize;
2121
use serde_json::{to_string_pretty, Value};
22-
use tempfile::TempDir;
2322
use test_utils::{find_mismatch, Fixture};
2423
use vfs::AbsPathBuf;
2524

25+
use crate::testdir::TestDir;
26+
2627
pub struct Project<'a> {
2728
fixture: &'a str,
2829
with_sysroot: bool,
29-
tmp_dir: Option<TempDir>,
30+
tmp_dir: Option<TestDir>,
3031
roots: Vec<PathBuf>,
3132
config: Option<Box<dyn Fn(&mut Config)>>,
3233
}
@@ -36,7 +37,7 @@ impl<'a> Project<'a> {
3637
Project { fixture, tmp_dir: None, roots: vec![], with_sysroot: false, config: None }
3738
}
3839

39-
pub fn tmp_dir(mut self, tmp_dir: TempDir) -> Project<'a> {
40+
pub fn tmp_dir(mut self, tmp_dir: TestDir) -> Project<'a> {
4041
self.tmp_dir = Some(tmp_dir);
4142
self
4243
}
@@ -57,7 +58,7 @@ impl<'a> Project<'a> {
5758
}
5859

5960
pub fn server(self) -> Server {
60-
let tmp_dir = self.tmp_dir.unwrap_or_else(|| TempDir::new().unwrap());
61+
let tmp_dir = self.tmp_dir.unwrap_or_else(|| TestDir::new());
6162
static INIT: Once = Once::new();
6263
INIT.call_once(|| {
6364
env_logger::builder().is_test(true).try_init().unwrap();
@@ -112,11 +113,11 @@ pub struct Server {
112113
_thread: jod_thread::JoinHandle<()>,
113114
client: Connection,
114115
/// XXX: remove the tempdir last
115-
dir: TempDir,
116+
dir: TestDir,
116117
}
117118

118119
impl Server {
119-
fn new(dir: TempDir, config: Config) -> Server {
120+
fn new(dir: TestDir, config: Config) -> Server {
120121
let (connection, client) = Connection::memory();
121122

122123
let _thread = jod_thread::Builder::new()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::{
2+
fs, io,
3+
path::{Path, PathBuf},
4+
sync::atomic::{AtomicUsize, Ordering},
5+
};
6+
7+
pub struct TestDir {
8+
path: PathBuf,
9+
keep: bool,
10+
}
11+
12+
impl TestDir {
13+
pub fn new() -> TestDir {
14+
let base = std::env::temp_dir().join("testdir");
15+
let pid = std::process::id();
16+
17+
static CNT: AtomicUsize = AtomicUsize::new(0);
18+
for _ in 0..100 {
19+
let cnt = CNT.fetch_add(1, Ordering::Relaxed);
20+
let path = base.join(format!("{}_{}", pid, cnt));
21+
if path.is_dir() {
22+
continue;
23+
}
24+
fs::create_dir_all(&path).unwrap();
25+
return TestDir { path, keep: false };
26+
}
27+
panic!("Failed to create a temporary directory")
28+
}
29+
#[allow(unused)]
30+
pub fn keep(mut self) -> TestDir {
31+
self.keep = true;
32+
self
33+
}
34+
pub fn path(&self) -> &Path {
35+
&self.path
36+
}
37+
}
38+
39+
impl Drop for TestDir {
40+
fn drop(&mut self) {
41+
if self.keep {
42+
return;
43+
}
44+
remove_dir_all(&self.path).unwrap()
45+
}
46+
}
47+
48+
#[cfg(not(windows))]
49+
fn remove_dir_all(path: &Path) -> io::Result<()> {
50+
fs::remove_dir_all(path)
51+
}
52+
53+
#[cfg(windows)]
54+
fn remove_dir_all(path: &Path) -> io::Result<()> {
55+
for _ in 0..99 {
56+
if fs::remove_dir_all(path).is_ok() {
57+
return Ok(());
58+
}
59+
std::thread::sleep(std::time::Duration::from_millis(10))
60+
}
61+
fs::remove_dir_all(path)
62+
}

0 commit comments

Comments
 (0)