Skip to content

Commit 55452bc

Browse files
test(which): enhance which filter tests with fixture helpers and PATH setup
- Added helper functions for cache removal tests and cwd_mode resolution to reduce duplication. - Set PATH environment variable in which lookup tests for consistent environment. - Refactored tests to use new helpers improving clarity and maintainability. Co-authored-by: terragon-labs[bot] <terragon-labs[bot]@users.noreply.github.com>
1 parent 1e31937 commit 55452bc

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

src/stdlib/which/lookup/tests.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use super::*;
55
use anyhow::{Context, Result, anyhow, ensure};
66
use rstest::{fixture, rstest};
7-
use std::fs;
7+
use std::{ffi::OsStr, fs};
88
use tempfile::TempDir;
99
use test_support::env::VarGuard;
1010

@@ -62,6 +62,7 @@ fn search_workspace_returns_executable_and_skips_non_exec(workspace: TempWorkspa
6262
let non_exec = workspace.root().join("tool2");
6363
fs::write(non_exec.as_std_path(), b"not exec").context("write non exec")?;
6464

65+
let _guard = VarGuard::set("PATH", OsStr::new(workspace.root().as_str()));
6566
let snapshot =
6667
EnvSnapshot::capture(Some(workspace.root())).expect("capture env for workspace search");
6768
let results = search_workspace(&snapshot, "tool", false)?;
@@ -79,6 +80,7 @@ fn search_workspace_collects_all_matches(workspace: TempWorkspace) -> Result<()>
7980
fs::create_dir_all(subdir.as_std_path()).context("mkdir bin")?;
8081
let second = write_exec(subdir.as_path(), "tool")?;
8182

83+
let _guard = VarGuard::set("PATH", OsStr::new(workspace.root().as_str()));
8284
let snapshot =
8385
EnvSnapshot::capture(Some(workspace.root())).expect("capture env for workspace search");
8486
let mut results = search_workspace(&snapshot, "tool", true)?;
@@ -98,6 +100,7 @@ fn search_workspace_skips_heavy_directories(workspace: TempWorkspace) -> Result<
98100
fs::create_dir_all(heavy.as_std_path()).context("mkdir target")?;
99101
write_exec(heavy.as_path(), "tool")?;
100102

103+
let _guard = VarGuard::set("PATH", OsStr::new(workspace.root().as_str()));
101104
let snapshot =
102105
EnvSnapshot::capture(Some(workspace.root())).expect("capture env for workspace search");
103106
let results = search_workspace(&snapshot, "tool", false)?;
@@ -118,6 +121,7 @@ fn search_workspace_ignores_unreadable_entries(workspace: TempWorkspace) -> Resu
118121
fs::set_permissions(blocked.as_std_path(), perms).context("chmod blocked")?;
119122

120123
let exec = write_exec(workspace.root(), "tool")?;
124+
let _guard = VarGuard::set("PATH", OsStr::new(workspace.root().as_str()));
121125
let snapshot =
122126
EnvSnapshot::capture(Some(workspace.root())).expect("capture env for workspace search");
123127
let results = search_workspace(&snapshot, "tool", false)?;
@@ -244,6 +248,7 @@ fn direct_path_not_executable_raises_direct_not_found(workspace: TempWorkspace)
244248
perms.set_mode(0o644);
245249
fs::set_permissions(script.as_std_path(), perms).context("chmod script")?;
246250

251+
let _path_guard = VarGuard::set("PATH", OsStr::new(workspace.root().as_str()));
247252
let snapshot =
248253
EnvSnapshot::capture(Some(workspace.root())).context("capture env for direct path")?;
249254

tests/std_filter_tests/which_filter_tests.rs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ fn test_cache_after_removal(
4848
Ok(())
4949
}
5050

51+
/// Helper to set up a fixture for cache removal tests and run the test.
52+
fn test_cache_behaviour_after_removal(
53+
second_template_str: &str,
54+
expect_second_err: bool,
55+
) -> Result<()> {
56+
let mut fixture = WhichTestFixture::with_tool_in_dirs(
57+
&ToolName::from("helper"),
58+
&[DirName::from("bin_first"), DirName::from("bin_second")],
59+
)?;
60+
61+
test_cache_after_removal(
62+
&mut fixture,
63+
&Template::from("{{ 'helper' | which }}"),
64+
&Template::from(second_template_str),
65+
expect_second_err,
66+
)
67+
}
68+
5169
fn test_duplicate_paths(
5270
fixture: &mut WhichTestFixture,
5371
canonical: bool,
@@ -70,6 +88,18 @@ fn test_duplicate_paths(
7088
Ok(())
7189
}
7290

91+
/// Helper to test cwd_mode resolution with a given case variant.
92+
fn test_cwd_mode_resolution(cwd_mode_value: &str) -> Result<()> {
93+
let (_temp, root) = support::filter_workspace()?;
94+
let tool = write_tool(&root, &ToolName::from("local"))?;
95+
let _path = PathEnv::new(&[])?;
96+
let (mut env, _state) = fallible::stdlib_env_with_state()?;
97+
let template = Template::from(format!("{{{{ which('local', cwd_mode='{cwd_mode_value}') }}}}"));
98+
let output = render(&mut env, &template)?;
99+
assert_eq!(output, tool.as_str());
100+
Ok(())
101+
}
102+
73103
#[rstest]
74104
fn which_filter_returns_first_match() -> Result<()> {
75105
let mut fixture = WhichTestFixture::with_tool_in_dirs(
@@ -83,30 +113,12 @@ fn which_filter_returns_first_match() -> Result<()> {
83113

84114
#[rstest]
85115
fn which_filter_uses_cached_result_when_executable_removed() -> Result<()> {
86-
let mut fixture = WhichTestFixture::with_tool_in_dirs(
87-
&ToolName::from("helper"),
88-
&[DirName::from("bin_first"), DirName::from("bin_second")],
89-
)?;
90-
test_cache_after_removal(
91-
&mut fixture,
92-
&Template::from("{{ 'helper' | which }}"),
93-
&Template::from("{{ 'helper' | which }}"),
94-
false,
95-
)
116+
test_cache_behaviour_after_removal("{{ 'helper' | which }}", false)
96117
}
97118

98119
#[rstest]
99120
fn which_filter_fresh_bypasses_cache_after_executable_removed() -> Result<()> {
100-
let mut fixture = WhichTestFixture::with_tool_in_dirs(
101-
&ToolName::from("helper"),
102-
&[DirName::from("bin_first"), DirName::from("bin_second")],
103-
)?;
104-
test_cache_after_removal(
105-
&mut fixture,
106-
&Template::from("{{ 'helper' | which }}"),
107-
&Template::from("{{ 'helper' | which(fresh=true) }}"),
108-
true,
109-
)
121+
test_cache_behaviour_after_removal("{{ 'helper' | which(fresh=true) }}", true)
110122
}
111123

112124
#[rstest]
@@ -165,14 +177,7 @@ fn which_filter_all_with_duplicates_deduplicates_canonicalised_paths() -> Result
165177

166178
#[rstest]
167179
fn which_function_honours_cwd_mode() -> Result<()> {
168-
let (_temp, root) = support::filter_workspace()?;
169-
let tool = write_tool(&root, &ToolName::from("local"))?;
170-
let _path = PathEnv::new(&[])?;
171-
let (mut env, _state) = fallible::stdlib_env_with_state()?;
172-
let template = Template::from("{{ which('local', cwd_mode='always') }}");
173-
let output = render(&mut env, &template)?;
174-
assert_eq!(output, tool.as_str());
175-
Ok(())
180+
test_cwd_mode_resolution("always")
176181
}
177182

178183
#[rstest]
@@ -200,14 +205,7 @@ fn which_function_rejects_invalid_cwd_mode() -> Result<()> {
200205

201206
#[rstest]
202207
fn which_function_accepts_case_insensitive_cwd_mode() -> Result<()> {
203-
let (_temp, root) = support::filter_workspace()?;
204-
let tool = write_tool(&root, &ToolName::from("local"))?;
205-
let _path = PathEnv::new(&[])?;
206-
let (mut env, _state) = fallible::stdlib_env_with_state()?;
207-
let template = Template::from("{{ which('local', cwd_mode='ALWAYS') }}");
208-
let output = render(&mut env, &template)?;
209-
assert_eq!(output, tool.as_str());
210-
Ok(())
208+
test_cwd_mode_resolution("ALWAYS")
211209
}
212210

213211
#[rstest]

0 commit comments

Comments
 (0)