Skip to content
Closed
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
91 changes: 84 additions & 7 deletions crates/ripr/src/analysis/classify/related_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ pub(in crate::analysis) fn find_related_tests<'a>(
let mut related: Vec<(&TestSummary, RelationReason)> = Vec::new();
let owner_name = owner_fn.map(|f| f.name.as_str()).unwrap_or("");
let probe_tokens = extract_identifier_tokens(&probe.expression);
let file_name = probe
.location
.file
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("");
let file_name = normalized_file_stem(&probe.location.file);
let owner_package_prefix = owner_fn.and_then(|owner| package_prefix(&owner.file));

// #2971 ambiguity rule: a call to `owner_name` can bypass the
Expand Down Expand Up @@ -146,7 +141,8 @@ pub(in crate::analysis) fn find_related_tests<'a>(

let test_name = test.name.to_ascii_lowercase();
let owner_name_lc = owner_name.to_ascii_lowercase();
let file_path_matches = normalize_path(&test.file).contains(file_name);
let file_path_matches =
!file_name.is_empty() && normalize_path(&test.file).contains(&file_name);
let owner_name_in_test = !owner_name_lc.is_empty() && test_name.contains(&owner_name_lc);
let token_in_test_name = probe_tokens
.iter()
Expand Down Expand Up @@ -212,6 +208,19 @@ fn normalize_path(path: &Path) -> String {
.to_string()
}

/// Extract a source-file stem after normalizing separators from either host.
/// Analysis inputs can contain paths produced on a different platform than the
/// host running the association pass.
fn normalized_file_stem(path: &Path) -> String {
normalize_path(path)
.rsplit('/')
.next()
.and_then(|file| Path::new(file).file_stem())
.and_then(|stem| stem.to_str())
.unwrap_or_default()
.to_string()
Comment on lines +215 to +221

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The custom logic to extract the file stem differs from std::path::Path::file_stem's behavior for dotfiles (e.g., for a file like .config, it would produce an empty stem instead of "config").

To ensure correctness and improve maintainability, it's better to reuse the standard library's well-tested logic. You can do this by applying Path::file_stem to the filename component after normalizing the path separators.

    normalize_path(path)
        .rsplit('/')
        .next()
        .and_then(|filename| Path::new(filename).file_stem())
        .and_then(|stem| stem.to_str())
        .unwrap_or_default()
        .to_string()

}

/// #3235: one authority for recognizing an absolute path form on any host —
/// Unix-rooted, Windows drive-prefixed, or host-absolute. Shared by
/// `package_prefix` (which cannot derive a package scope from such a path
Expand Down Expand Up @@ -738,6 +747,74 @@ mod tests {
assert_eq!(related[0].0.name, "repo_lane_deserializes_fields_correctly");
}

#[test]
fn given_foreign_separator_file_stem_when_associated_then_assertion_text_is_preserved() {
let assertion_text = "assert_eq!(value, 3);";
let oracle_test = test_with_assertions(
"crates/ripr/tests/config.rs",
"checks_value",
assertion_text,
vec![oracle_fact(
assertion_text,
OracleKind::ExactValue,
OracleStrength::Strong,
)],
);
let index = RustIndex {
tests: vec![oracle_test],
..RustIndex::default()
};
let forward_probe = struct_field_probe("crates/ripr/src/config.rs", "marker");
let windows_probe = struct_field_probe("crates\\ripr\\src\\config.rs", "marker");

let forward_related = find_related_tests(&forward_probe, None, &index, true, None);
let windows_related = find_related_tests(&windows_probe, None, &index, true, None);

assert_eq!(forward_related.len(), 1);
assert_eq!(windows_related.len(), 1);
assert_eq!(forward_related[0].0.name, windows_related[0].0.name);
assert_eq!(forward_related[0].1, RelationReason::SameTestFile);
assert_eq!(windows_related[0].1, RelationReason::SameTestFile);
assert_eq!(
forward_related[0].0.assertions[0].text, assertion_text,
"path normalization must not rewrite assertion text"
);
assert_eq!(
windows_related[0].0.assertions[0].text, assertion_text,
"path normalization must not rewrite assertion text"
);
}

#[test]
fn given_dotfile_probe_when_associated_then_only_matching_companion_is_related() {
let matching_test = test_with_assertions(
"crates/ripr/tests/.config.rs",
"checks_config_value",
"assert_eq!(value, 3);",
Vec::new(),
);
let unrelated_test = test_with_assertions(
"crates/ripr/tests/config.rs",
"checks_value",
"assert_eq!(value, 3);",
Vec::new(),
);
let index = RustIndex {
tests: vec![matching_test, unrelated_test],
..RustIndex::default()
};
let forward_probe = struct_field_probe("crates/ripr/src/.config", "marker");
let windows_probe = struct_field_probe("crates\\ripr\\src\\.config", "marker");

for probe in [forward_probe, windows_probe] {
let related = find_related_tests(&probe, None, &index, true, None);

assert_eq!(related.len(), 1);
assert_eq!(related[0].0.file, Path::new("crates/ripr/tests/.config.rs"));
assert_eq!(related[0].1, RelationReason::SameTestFile);
}
}

// --- #1054 repro ---
// Two sibling struct-field probes (`open_in` and `open_cap`) are both
// asserted by the SAME exact-value oracle test. Both MUST select that test
Expand Down