Skip to content

Commit

Permalink
feat(tests_fixtures): Make tmp_path compatible for web
Browse files Browse the repository at this point in the history
  • Loading branch information
FirelightFlagboy committed Feb 20, 2025
1 parent b7809aa commit 7739f08
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions libparsec/crates/tests_fixtures/src/tmp_path.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS

use std::path::PathBuf;

use uuid::Uuid;

use libparsec_tests_lite::prelude::*;

/// A temporary path that will be removed on drop.
///
/// ## For web environment
///
/// In web environment, we cannot use the `std::fs` api (since we do not have access to the
/// filesystem). So we will neither create nor remove the tmp directory.
///
/// In this case the temp path could be used as a discriminant since it's value is random.
pub struct TmpPath(PathBuf);

impl TmpPath {
pub fn create() -> Self {
let mut path = std::env::temp_dir();
let mut path = if cfg!(target_arch = "wasm32") {
PathBuf::default()
} else {
std::env::temp_dir()
};

path.extend(["parsec-tests", &Uuid::new_v4().to_string()]);

std::fs::create_dir_all(&path).expect("Cannot create tmp_path dir");
#[cfg(not(target_arch = "wasm32"))]
std::fs::create_dir_all(&*path).expect("Cannot create tmp_path dir");

TmpPath(path)
}
Expand All @@ -28,19 +37,23 @@ impl std::ops::Deref for TmpPath {
}
}

impl Drop for TmpPath {
fn drop(&mut self) {
if let Err(err) = std::fs::remove_dir_all(&self.0) {
if matches!(err.kind(), std::io::ErrorKind::NotFound) {
// TmpPath was already removed
return;
}
// Cannot remove the directory :'(
// If we are on Windows, it most likely means a file in the directory
// is still opened. Typically a SQLite database is still opened because
// the SQLiteExecutor's drop doesn't wait
let content = {
match std::fs::read_dir(&self.0) {
#[cfg(not(target_arch = "wasm32"))]
mod native {
use super::TmpPath;

impl Drop for TmpPath {
fn drop(&mut self) {
if let Err(err) = std::fs::remove_dir_all(&self.0) {
if matches!(err.kind(), std::io::ErrorKind::NotFound) {
// TmpPath was already removed
return;
}
// Cannot remove the directory :'(
// If we are on Windows, it most likely means a file in the directory
// is still opened. Typically a SQLite database is still opened because
// the SQLiteExecutor's drop doesn't wait
let content = {
match std::fs::read_dir(&self.0) {
Ok(items) => items
.into_iter()
.map(|item| match item {
Expand All @@ -53,18 +66,19 @@ impl Drop for TmpPath {
Err(_) => vec!["<empty>".to_owned()],
}
.join(" ")
};
panic!(
"Cannot remove {:?}: {}\n\
};
panic!(
"Cannot remove {:?}: {}\n\
Content: {}\n\
Have you done a gracious close of resources in your test ?",
&self.0, &err, content
);
&self.0, &err, content
);
}
}
}
}

#[fixture]
#[libparsec_tests_lite::rstest::fixture]
pub fn tmp_path() -> TmpPath {
TmpPath::create()
}

0 comments on commit 7739f08

Please sign in to comment.