aboutsummaryrefslogtreecommitdiff
path: root/crates/rust-analyzer/tests/slow-tests/testdir.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rust-analyzer/tests/slow-tests/testdir.rs')
-rw-r--r--crates/rust-analyzer/tests/slow-tests/testdir.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/crates/rust-analyzer/tests/slow-tests/testdir.rs b/crates/rust-analyzer/tests/slow-tests/testdir.rs
new file mode 100644
index 000000000..36271344b
--- /dev/null
+++ b/crates/rust-analyzer/tests/slow-tests/testdir.rs
@@ -0,0 +1,62 @@
1use std::{
2 fs, io,
3 path::{Path, PathBuf},
4 sync::atomic::{AtomicUsize, Ordering},
5};
6
7pub(crate) struct TestDir {
8 path: PathBuf,
9 keep: bool,
10}
11
12impl TestDir {
13 pub(crate) 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(crate) fn keep(mut self) -> TestDir {
31 self.keep = true;
32 self
33 }
34 pub(crate) 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}