diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-07-23 21:27:22 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-07-23 21:27:22 +0100 |
commit | 45eb95d40ab5b169395685519e790eedd0082d53 (patch) | |
tree | 16380b4c5741210e5564b0731639564767187f9d | |
parent | bd44f3a6203b0dcd2e87cc391d652925501b7e57 (diff) | |
parent | be06aaecdebabf5a3a60a367bbd672508a9cb8f8 (diff) |
Merge #5510
5510: Lighter weight tempdir r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r-- | Cargo.lock | 24 | ||||
-rw-r--r-- | crates/rust-analyzer/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/rust-analyzer/tests/heavy_tests/main.rs | 9 | ||||
-rw-r--r-- | crates/rust-analyzer/tests/heavy_tests/support.rs | 13 | ||||
-rw-r--r-- | crates/rust-analyzer/tests/heavy_tests/testdir.rs | 62 |
5 files changed, 75 insertions, 34 deletions
diff --git a/Cargo.lock b/Cargo.lock index 007b0d6a5..0a35eb793 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -1327,15 +1327,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
1327 | checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" | 1327 | checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" |
1328 | 1328 | ||
1329 | [[package]] | 1329 | [[package]] |
1330 | name = "remove_dir_all" | ||
1331 | version = "0.5.3" | ||
1332 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1333 | checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" | ||
1334 | dependencies = [ | ||
1335 | "winapi 0.3.9", | ||
1336 | ] | ||
1337 | |||
1338 | [[package]] | ||
1339 | name = "rowan" | 1330 | name = "rowan" |
1340 | version = "0.10.0" | 1331 | version = "0.10.0" |
1341 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1332 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1386,7 +1377,6 @@ dependencies = [ | |||
1386 | "serde", | 1377 | "serde", |
1387 | "serde_json", | 1378 | "serde_json", |
1388 | "stdx", | 1379 | "stdx", |
1389 | "tempfile", | ||
1390 | "test_utils", | 1380 | "test_utils", |
1391 | "threadpool", | 1381 | "threadpool", |
1392 | "vfs", | 1382 | "vfs", |
@@ -1607,20 +1597,6 @@ dependencies = [ | |||
1607 | ] | 1597 | ] |
1608 | 1598 | ||
1609 | [[package]] | 1599 | [[package]] |
1610 | name = "tempfile" | ||
1611 | version = "3.1.0" | ||
1612 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1613 | checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" | ||
1614 | dependencies = [ | ||
1615 | "cfg-if", | ||
1616 | "libc", | ||
1617 | "rand", | ||
1618 | "redox_syscall", | ||
1619 | "remove_dir_all", | ||
1620 | "winapi 0.3.9", | ||
1621 | ] | ||
1622 | |||
1623 | [[package]] | ||
1624 | name = "termcolor" | 1600 | name = "termcolor" |
1625 | version = "1.1.0" | 1601 | version = "1.1.0" |
1626 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1602 | source = "registry+https://github.com/rust-lang/crates.io-index" |
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" } | |||
58 | winapi = "0.3.8" | 58 | winapi = "0.3.8" |
59 | 59 | ||
60 | [dev-dependencies] | 60 | [dev-dependencies] |
61 | tempfile = "3.1.0" | ||
62 | expect = { path = "../expect" } | 61 | expect = { path = "../expect" } |
63 | test_utils = { path = "../test_utils" } | 62 | test_utils = { path = "../test_utils" } |
64 | mbe = { path = "../ra_mbe", package = "ra_mbe" } | 63 | 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 @@ | |||
1 | mod testdir; | ||
1 | mod support; | 2 | mod support; |
2 | 3 | ||
3 | use std::{collections::HashMap, path::PathBuf, time::Instant}; | 4 | use std::{collections::HashMap, path::PathBuf, time::Instant}; |
@@ -12,10 +13,12 @@ use lsp_types::{ | |||
12 | }; | 13 | }; |
13 | use rust_analyzer::lsp_ext::{OnEnter, Runnables, RunnablesParams}; | 14 | use rust_analyzer::lsp_ext::{OnEnter, Runnables, RunnablesParams}; |
14 | use serde_json::json; | 15 | use serde_json::json; |
15 | use tempfile::TempDir; | ||
16 | use test_utils::skip_slow_tests; | 16 | use test_utils::skip_slow_tests; |
17 | 17 | ||
18 | use crate::support::{project, Project}; | 18 | use crate::{ |
19 | support::{project, Project}, | ||
20 | testdir::TestDir, | ||
21 | }; | ||
19 | 22 | ||
20 | const PROFILE: &str = ""; | 23 | const 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 | }; |
20 | use serde::Serialize; | 20 | use serde::Serialize; |
21 | use serde_json::{to_string_pretty, Value}; | 21 | use serde_json::{to_string_pretty, Value}; |
22 | use tempfile::TempDir; | ||
23 | use test_utils::{find_mismatch, Fixture}; | 22 | use test_utils::{find_mismatch, Fixture}; |
24 | use vfs::AbsPathBuf; | 23 | use vfs::AbsPathBuf; |
25 | 24 | ||
25 | use crate::testdir::TestDir; | ||
26 | |||
26 | pub struct Project<'a> { | 27 | pub 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 | ||
118 | impl Server { | 119 | impl 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 @@ | |||
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 | } | ||