Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ pub mod engines;
pub mod instance;
pub mod rolling;
pub mod state;
#[cfg(test)]
pub(crate) mod test;
pub mod util;
63 changes: 63 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026 Nutanix, Inc.
//
// Author: Leonardo Forchini <leonardo.forchini@nutanix.com>
//
//! Shared infrastructure for crate unit tests.

use std::{cell::RefCell, path::PathBuf};

use rstest::fixture;

thread_local! {
pub static ROOT_OVERRIDE: RefCell<Option<PathBuf>> = const { RefCell::new(None) };
}

/// Scoped root override for tests that access absolute paths through
/// `util::Path`.
pub(crate) struct RootOverride {
previous: Option<PathBuf>,
}

impl Drop for RootOverride {
fn drop(&mut self) {
ROOT_OVERRIDE.with(|root| {
root.replace(self.previous.take());
});
}
}

/// Override the root used by `util::Path` on the current test thread.
pub(crate) fn override_root(root: impl Into<PathBuf>) -> RootOverride {
let previous = ROOT_OVERRIDE.with(|slot| slot.replace(Some(root.into())));
RootOverride { previous }
}

/// Temporary mock root installed as the current test thread's `util::Path`
/// prefix.
pub(crate) struct MockDir {
_dir: tempfile::TempDir,
_root_override: RootOverride,
// path: PathBuf,
}

impl MockDir {
pub(crate) fn new() -> Self {
let dir = tempfile::tempdir().unwrap();

Self {
_root_override: override_root(dir.path()),
// path: dir.path().into(),
_dir: dir,
}
}

// pub(crate) fn join(&self, path: impl AsRef<Path>) -> PathBuf {
// self.path.join(path.as_ref())
// }
}

#[fixture]
pub(crate) fn mock_dir() -> MockDir {
MockDir::new()
}
13 changes: 11 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,29 @@ impl fmt::Display for Path {
}
}

static IO_THREAD_CONTROLLER_ROOT_PATH: LazyLock<std::path::PathBuf> = LazyLock::new(|| {
static ROOT_PATH: LazyLock<std::path::PathBuf> = LazyLock::new(|| {
std::path::PathBuf::from(
env::var("IO_THREAD_CONTROLLER_ROOT_PATH").unwrap_or(String::from("/")),
)
});

fn effective_root() -> std::path::PathBuf {
#[cfg(test)]
if let Some(root) = crate::test::ROOT_OVERRIDE.with(|slot| slot.borrow().clone()) {
return root;
}

ROOT_PATH.clone()
}

impl Path {
pub fn new<S>(s: &S) -> Self
where
S: AsRef<OsStr> + ?Sized,
{
let mut pb = std::path::PathBuf::from(s);
if pb.is_absolute() {
pb = IO_THREAD_CONTROLLER_ROOT_PATH.join(pb.strip_prefix("/").expect("absolute path"))
pb = effective_root().join(pb.strip_prefix("/").expect("absolute path"))
}
Self { path: pb }
}
Expand Down
Loading