aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-07-23 21:26:25 +0100
committerAleksey Kladov <[email protected]>2020-07-23 21:26:25 +0100
commitbe06aaecdebabf5a3a60a367bbd672508a9cb8f8 (patch)
tree16380b4c5741210e5564b0731639564767187f9d /crates
parentbd44f3a6203b0dcd2e87cc391d652925501b7e57 (diff)
Lighter weight tempdir
Diffstat (limited to 'crates')
-rw-r--r--crates/rust-analyzer/Cargo.toml1
-rw-r--r--crates/rust-analyzer/tests/heavy_tests/main.rs9
-rw-r--r--crates/rust-analyzer/tests/heavy_tests/support.rs13
-rw-r--r--crates/rust-analyzer/tests/heavy_tests/testdir.rs62
4 files changed, 75 insertions, 10 deletions
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" }
58winapi = "0.3.8" 58winapi = "0.3.8"
59 59
60[dev-dependencies] 60[dev-dependencies]
61tempfile = "3.1.0"
62expect = { path = "../expect" } 61expect = { path = "../expect" }
63test_utils = { path = "../test_utils" } 62test_utils = { path = "../test_utils" }
64mbe = { path = "../ra_mbe", package = "ra_mbe" } 63mbe = { 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 @@
1mod testdir;
1mod support; 2mod support;
2 3
3use std::{collections::HashMap, path::PathBuf, time::Instant}; 4use std::{collections::HashMap, path::PathBuf, time::Instant};
@@ -12,10 +13,12 @@ use lsp_types::{
12}; 13};
13use rust_analyzer::lsp_ext::{OnEnter, Runnables, RunnablesParams}; 14use rust_analyzer::lsp_ext::{OnEnter, Runnables, RunnablesParams};
14use serde_json::json; 15use serde_json::json;
15use tempfile::TempDir;
16use test_utils::skip_slow_tests; 16use test_utils::skip_slow_tests;
17 17
18use crate::support::{project, Project}; 18use crate::{
19 support::{project, Project},
20 testdir::TestDir,
21};
19 22
20const PROFILE: &str = ""; 23const PROFILE: &str = "";
21// const PROFILE: &'static str = "*@3>100"; 24// const PROFILE: &'static str = "*@3>100";
@@ -308,7 +311,7 @@ fn test_missing_module_code_action_in_json_project() {
308 return; 311 return;
309 } 312 }
310 313
311 let tmp_dir = TempDir::new().unwrap(); 314 let tmp_dir = TestDir::new();
312 315
313 let path = tmp_dir.path(); 316 let path = tmp_dir.path();
314 317
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::{
19}; 19};
20use serde::Serialize; 20use serde::Serialize;
21use serde_json::{to_string_pretty, Value}; 21use serde_json::{to_string_pretty, Value};
22use tempfile::TempDir;
23use test_utils::{find_mismatch, Fixture}; 22use test_utils::{find_mismatch, Fixture};
24use vfs::AbsPathBuf; 23use vfs::AbsPathBuf;
25 24
25use crate::testdir::TestDir;
26
26pub struct Project<'a> { 27pub struct Project<'a> {
27 fixture: &'a str, 28 fixture: &'a str,
28 with_sysroot: bool, 29 with_sysroot: bool,
29 tmp_dir: Option<TempDir>, 30 tmp_dir: Option<TestDir>,
30 roots: Vec<PathBuf>, 31 roots: Vec<PathBuf>,
31 config: Option<Box<dyn Fn(&mut Config)>>, 32 config: Option<Box<dyn Fn(&mut Config)>>,
32} 33}
@@ -36,7 +37,7 @@ impl<'a> Project<'a> {
36 Project { fixture, tmp_dir: None, roots: vec![], with_sysroot: false, config: None } 37 Project { fixture, tmp_dir: None, roots: vec![], with_sysroot: false, config: None }
37 } 38 }
38 39
39 pub fn tmp_dir(mut self, tmp_dir: TempDir) -> Project<'a> { 40 pub fn tmp_dir(mut self, tmp_dir: TestDir) -> Project<'a> {
40 self.tmp_dir = Some(tmp_dir); 41 self.tmp_dir = Some(tmp_dir);
41 self 42 self
42 } 43 }
@@ -57,7 +58,7 @@ impl<'a> Project<'a> {
57 } 58 }
58 59
59 pub fn server(self) -> Server { 60 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());
61 static INIT: Once = Once::new(); 62 static INIT: Once = Once::new();
62 INIT.call_once(|| { 63 INIT.call_once(|| {
63 env_logger::builder().is_test(true).try_init().unwrap(); 64 env_logger::builder().is_test(true).try_init().unwrap();
@@ -112,11 +113,11 @@ pub struct Server {
112 _thread: jod_thread::JoinHandle<()>, 113 _thread: jod_thread::JoinHandle<()>,
113 client: Connection, 114 client: Connection,
114 /// XXX: remove the tempdir last 115 /// XXX: remove the tempdir last
115 dir: TempDir, 116 dir: TestDir,
116} 117}
117 118
118impl Server { 119impl Server {
119 fn new(dir: TempDir, config: Config) -> Server { 120 fn new(dir: TestDir, config: Config) -> Server {
120 let (connection, client) = Connection::memory(); 121 let (connection, client) = Connection::memory();
121 122
122 let _thread = jod_thread::Builder::new() 123 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 @@
1use std::{
2 fs, io,
3 path::{Path, PathBuf},
4 sync::atomic::{AtomicUsize, Ordering},
5};
6
7pub struct TestDir {
8 path: PathBuf,
9 keep: bool,
10}
11
12impl 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
39impl 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))]
49fn remove_dir_all(path: &Path) -> io::Result<()> {
50 fs::remove_dir_all(path)
51}
52
53#[cfg(windows)]
54fn 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}