diff options
Diffstat (limited to 'crates/rust-analyzer/tests/heavy_tests/testdir.rs')
-rw-r--r-- | crates/rust-analyzer/tests/heavy_tests/testdir.rs | 62 |
1 files changed, 62 insertions, 0 deletions
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 | } | ||