Skip to content

Commit 58dcce0

Browse files
committed
test: add CWD guards
This was the source of the race.
1 parent 38e8f61 commit 58dcce0

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

tests/namedtempfile.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@ use std::io::{self, Read, Seek, SeekFrom, Write};
66
use std::path::{Path, PathBuf};
77
use tempfile::{env, tempdir, Builder, NamedTempFile, TempPath};
88

9+
struct CWDGuard<'a> {
10+
#[allow(unused)]
11+
guard: std::sync::MutexGuard<'a, ()>,
12+
old_cwd: PathBuf,
13+
}
14+
15+
impl<'a> Drop for CWDGuard<'a> {
16+
fn drop(&mut self) {
17+
let _ = std::env::set_current_dir(&self.old_cwd);
18+
}
19+
}
20+
21+
static CWD_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
22+
23+
fn cwd_lock<'a>() -> CWDGuard<'a> {
24+
let guard = CWD_LOCK.lock().unwrap();
25+
let old_cwd = std::env::current_dir().unwrap();
26+
CWDGuard { guard, old_cwd }
27+
}
28+
929
fn exists<P: AsRef<Path>>(path: P) -> bool {
1030
std::fs::metadata(path.as_ref()).is_ok()
1131
}
@@ -318,27 +338,25 @@ fn temp_path_from_argument_types() {
318338
#[cfg(not(any(target_os = "redox", target_os = "wasi", windows)))]
319339
fn test_temp_path_resolve_missing_cwd() {
320340
configure_wasi_temp_dir();
321-
use std::time::Duration;
341+
let _guard = cwd_lock();
322342

323343
// Intentionally delete the current working directory
324344
let tmpdir = tempdir().unwrap();
325345
std::env::set_current_dir(&tmpdir).expect("failed to change to the temporary directory");
326346
tmpdir.close().unwrap();
327-
// It can sometimes take a bit for the OS to realize the CWD is removed. I'm not sure why, but
328-
// this test fails occasionally without this.
329-
while std::env::current_dir().is_ok() {
330-
std::thread::sleep(Duration::from_millis(1));
331-
}
332347

333348
#[allow(deprecated)]
334349
let path = TempPath::from_path("foo");
335-
assert_eq!(&*path, "foo");
350+
assert_eq!(&*path, Path::new("foo"));
336351

337352
TempPath::try_from_path("foo").expect_err("should have failed to make path absolute file");
338353
}
339354

340355
#[test]
341356
fn test_temp_path_resolve_existing_cwd() {
357+
configure_wasi_temp_dir();
358+
let _guard = cwd_lock();
359+
342360
let tmpdir = tempdir().unwrap();
343361
std::env::set_current_dir(&tmpdir).expect("failed to change to directory");
344362

@@ -356,7 +374,7 @@ fn test_temp_path_resolve_existing_cwd() {
356374

357375
#[allow(deprecated)]
358376
let path = TempPath::from_path("");
359-
assert_eq!(&*path, "");
377+
assert_eq!(&*path, Path::new(""));
360378

361379
TempPath::try_from_path("").expect_err("empty paths should fail");
362380
}
@@ -372,6 +390,7 @@ fn test_write_after_close() {
372390
#[test]
373391
fn test_change_dir() {
374392
configure_wasi_temp_dir();
393+
let _guard = cwd_lock();
375394

376395
let dir_a = tempdir().unwrap();
377396
let dir_b = tempdir().unwrap();
@@ -390,6 +409,7 @@ fn test_change_dir() {
390409
#[test]
391410
fn test_change_dir_make() {
392411
configure_wasi_temp_dir();
412+
let _guard = cwd_lock();
393413

394414
let dir_a = tempdir().unwrap();
395415
let dir_b = tempdir().unwrap();

0 commit comments

Comments
 (0)