Repository navigation
Add MockDir to use util::Path in unit tests #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: fix-double-path-bug
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| // Copyright (c) 2026 Nutanix, Inc. | ||
| // | ||
| // Author: Leonardo Forchini <leonardo.forchini@nutanix.com> | ||
| // | ||
| //! `tempfile::TempDir` wrapper which controls the prefix of | ||
| //! `crate::path::Path`s. This is intended to allow unit tests to be run in | ||
| //! parallel with independent path prefixes. Each test is able to get its own | ||
| //! `TempDir` which `Path`s will implicitly treat as their root and can use as | ||
| //! their working directories. | ||
|
|
||
| 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't get what you mean by "scoped" here.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The per-thread prefix is only overwritten for as long as this guard exists so I think scoped is the right word here. |
||
| /// `crate::path::Path`. | ||
| pub(crate) struct RootOverride { | ||
| previous: Option<std::path::PathBuf>, | ||
| } | ||
|
|
||
| impl Drop for RootOverride { | ||
| fn drop(&mut self) { | ||
| ROOT_OVERRIDE.with(|root| { | ||
| root.replace(self.previous.take()); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /// Override the root used by new `crate::path::Path`s on the current test | ||
| /// thread. Any new absolute `Path`s will start with `"{root}/"`. | ||
| 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 | ||
| /// `crate::util::Path` prefix. | ||
| pub(crate) struct MockDir { | ||
| path: std::path::PathBuf, | ||
| _root_override: RootOverride, | ||
| _dir: tempfile::TempDir, | ||
| } | ||
|
|
||
| impl MockDir { | ||
| pub(crate) fn new() -> Self { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| let path = dir.path(); | ||
|
|
||
| Self { | ||
| path: path.to_path_buf(), | ||
| _root_override: override_root(path), | ||
| _dir: dir, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Create a new `MockDir` as an `rstest` fixture | ||
| #[fixture] | ||
| pub(crate) fn mockdir() -> MockDir { | ||
| MockDir::new() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use rstest::rstest; | ||
|
|
||
| use super::*; | ||
| use crate::path::{Path, effective_root}; | ||
|
|
||
| /// Ensure that a new `MockDir` actually changes the effective root. | ||
| #[test] | ||
| fn test_mockdir_changes_effective_root() { | ||
| let old_root = effective_root(); | ||
|
|
||
| let _mockdir = MockDir::new(); | ||
| assert_ne!(old_root, effective_root()); | ||
| } | ||
|
|
||
| /// Ensure new `util::Paths`s use a given `MockDir`'s prefix. | ||
| #[rstest] | ||
| fn test_mockdir_modifies_util_paths(mockdir: MockDir) { | ||
| let util_path = Path::from("/etc/passwd"); | ||
| assert_ne!(mockdir.path, std::path::Path::new("/")); | ||
| assert_eq!(util_path.to_path_buf(), mockdir.path.join("etc/passwd")); | ||
| } | ||
|
|
||
| /// Ensure paths created before a new `MockDir` are not prefixed with its | ||
| /// dir. | ||
| // TODO is this the behaviour we want? MockDirs will mostly be used as | ||
| // fixtures so I don't think it should come up too often. | ||
| #[test] | ||
| fn test_mockdir_preserves_existing_paths() { | ||
| let util_path = Path::from("/etc/passwd"); | ||
|
|
||
| let mockdir = MockDir::new(); | ||
| assert_ne!(util_path.to_path_buf(), mockdir.path.join("etc/passwd")); | ||
| } | ||
|
|
||
| /// Ensure once a `MockDir` goes out of scope the effective root is | ||
| /// restored. | ||
| #[test] | ||
| fn test_mockdir_reverts_on_drop() { | ||
| let _outer = MockDir::new(); | ||
| let outer_path = effective_root(); | ||
|
|
||
| let inner_path = { | ||
| let _inner = MockDir::new(); | ||
| effective_root() | ||
| }; | ||
|
|
||
| let post_drop_path = effective_root(); | ||
|
|
||
| assert_ne!(outer_path, inner_path); | ||
| assert_eq!(outer_path, post_drop_path); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| // Copyright (c) 2026 Nutanix, Inc. | ||
| // | ||
| // Author: Leonardo Forchini <leonardo.forchini@nutanix.com> | ||
| // | ||
|
|
||
| pub mod mockdir; | ||
|
|
||
| pub use mockdir::*; |
Uh oh!
There was an error while loading. Please reload this page.