Skip to content

Commit

Permalink
Distinguish cargo_workspace input vie enum
Browse files Browse the repository at this point in the history
This allows to skip the cargo workspace detection based on the value passed
  • Loading branch information
bachp committed Feb 17, 2025
1 parent 59af9d5 commit 7f4f6ec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
37 changes: 28 additions & 9 deletions insta/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,23 +430,35 @@ pub fn snapshot_update_behavior(tool_config: &ToolConfig, unseen: bool) -> Snaps
}
}

pub enum Workspace {
DetectWithCargo(&'static str),
UseAsIs(&'static str),
}

/// Returns the cargo workspace path for a crate manifest, like
/// `/Users/janedoe/projects/insta` when passed
/// `/Users/janedoe/projects/insta/insta/Cargo.toml`.
pub fn get_cargo_workspace(manifest_dir: &str) -> Arc<PathBuf> {
// If INSTA_WORKSPACE_ROOT environment variable is set at runtime, use the value as-is.
// If INSTA_WORKSPACE_ROOT environment variable is set at compile time, use the value as-is.
// If INSTA_WORKSPACE_ROOT environment variable is not set, use `cargo metadata` to find the workspace root.
//
///
/// If `INSTA_WORKSPACE_ROOT` environment variable is set at runtime, use the value as-is.
/// If `INSTA_WORKSPACE_ROOT` environment variable is set at compile time, use the value as-is.
/// If `INSTA_WORKSPACE_ROOT` environment variable is not set, use `cargo metadata` to find the workspace root.
pub fn get_cargo_workspace(workspace: Workspace) -> Arc<PathBuf> {
// This is useful where CARGO_MANIFEST_DIR at compilation points to some
// transient location. This can easily happen when building the test in one
// directory but running it in another.
if let Ok(workspace_root) = env::var("INSTA_WORKSPACE_ROOT") {
return PathBuf::from(workspace_root).into();
} else if let Some(workspace_root) = option_env!("INSTA_WORKSPACE_ROOT") {
return PathBuf::from(workspace_root).into();
}

// Distinguish if we need to run `cargo metadata`` or if we can return the workspace
// as is.
// This is useful if INSTA_WORKSPACE_ROOT was set at compile time, not pointing to
// the cargo manifest directory
let manifest_dir = match workspace {
Workspace::UseAsIs(workspace_root) => return PathBuf::from(workspace_root).into(),
Workspace::DetectWithCargo(manifest_dir) => manifest_dir,
};

let error_message = || {
format!(
"`cargo metadata --format-version=1 --no-deps` in path `{}`",
Expand Down Expand Up @@ -497,12 +509,19 @@ pub fn get_cargo_workspace(manifest_dir: &str) -> Arc<PathBuf> {
}

#[test]
fn test_get_cargo_workspace() {
let workspace = get_cargo_workspace(env!("CARGO_MANIFEST_DIR"));
fn test_get_cargo_workspace_manifest_dir() {
let workspace = get_cargo_workspace(Workspace::DetectWithCargo(env!("CARGO_MANIFEST_DIR")));
// The absolute path of the workspace, like `/Users/janedoe/projects/insta`
assert!(workspace.ends_with("insta"));
}

#[test]
fn test_get_cargo_workspace_insta_workspace() {
let workspace = get_cargo_workspace(Workspace::UseAsIs("/tmp/insta_workspace_root"));
// The absolute path of the workspace, like `/tmp/insta_workspace_root`
assert!(workspace.ends_with("insta_workspace_root"));
}

#[cfg(feature = "_cargo_insta_internal")]
impl std::str::FromStr for TestRunner {
type Err = ();
Expand Down
2 changes: 1 addition & 1 deletion insta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ pub use crate::redaction::{dynamic_redaction, rounded_redaction, sorted_redactio
#[doc(hidden)]
pub mod _macro_support {
pub use crate::content::Content;
pub use crate::env::get_cargo_workspace;
pub use crate::env::{get_cargo_workspace, Workspace};
pub use crate::runtime::{
assert_snapshot, with_allow_duplicates, AutoName, BinarySnapshotValue, InlineValue,
SnapshotValue,
Expand Down
6 changes: 3 additions & 3 deletions insta/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ macro_rules! _get_workspace_root {
// By default the `CARGO_MANIFEST_DIR` environment variable is used as the workspace root.
// If the `INSTA_WORKSPACE_ROOT` environment variable is set at compile time it will override the default.
// This can be useful to avoid including local paths in the binary.
const WORKSPACE_ROOT: &str = if let Some(root) = option_env!("INSTA_WORKSPACE_ROOT") {
root
const WORKSPACE_ROOT: $crate::_macro_support::Workspace = if let Some(root) = option_env!("INSTA_WORKSPACE_ROOT") {
$crate::_macro_support::Workspace::UseAsIs(root)
} else {
env!("CARGO_MANIFEST_DIR")
$crate::_macro_support::Workspace::DetectWithCargo(env!("CARGO_MANIFEST_DIR"))
};
$crate::_macro_support::get_cargo_workspace(WORKSPACE_ROOT)
}};
Expand Down

0 comments on commit 7f4f6ec

Please sign in to comment.