use std::path::PathBuf;
use crate::types::{
GraphEdge, GraphMetrics, GraphNode, GraphReport, ImportKind,
};
use super::GraphQuery;
fn edge(from: &str, to: &str, path: &str) -> GraphEdge {
GraphEdge {
from: from.to_owned(),
to: Some(to.to_owned()),
import_path: path.to_owned(),
resolved: true,
kind: ImportKind::Dependency,
local_gap: false,
cfg_gated: false,
}
}
fn node(path: &str) -> GraphNode {
GraphNode {
file_path: path.to_owned(),
language: Some("Rust"),
imports_count: 1,
imported_by_count: 1,
}
}
fn empty_metrics() -> GraphMetrics {
GraphMetrics {
total_files: 0,
total_internal_edges: 0,
self_references: 0,
total_external_edges: 0,
unresolved_local_edges: 0,
unresolved_local_samples: Vec::new(),
cfg_gated_edges: 0,
dependencies: Vec::new(),
declared_dependency_edges: 0,
local_crate_edges: 0,
builtin_edges: 0,
circular_dependencies: 0,
most_importing: vec![],
most_imported: vec![],
cycles: vec![],
}
}
fn report(nodes: &[&str], edges: &[(&str, &str, &str)]) -> GraphReport {
GraphReport {
base_path: PathBuf::from("."),
focus_paths: vec![],
depth: None,
query: None,
nodes: nodes.iter().copied().map(node).collect(),
edges: edges
.iter()
.map(|(from, to, path)| edge(from, to, path))
.collect(),
metrics: empty_metrics(),
}
}
fn repo_report() -> GraphReport {
report(
&["a.rs", "b.rs", "c.rs", "d.rs"],
&[
("b.rs", "a.rs", "crate::a"),
("c.rs", "a.rs", "crate::a"),
("d.rs", "b.rs", "crate::b"),
],
)
}
fn scoped_report() -> GraphReport {
report(
&[
"target.rs",
"crates/one/a.rs",
"crates/one/b.rs",
"crates/two/user.rs",
"crates/twone/extra.rs",
],
&[
("crates/one/a.rs", "target.rs", "crate::target"),
("crates/one/b.rs", "target.rs", "crate::target"),
("crates/two/user.rs", "target.rs", "crate::target"),
("crates/twone/extra.rs", "target.rs", "crate::target"),
],
)
}
fn path(value: &str) -> String {
value.to_owned()
}
use super::measure as measure_raw;
use super::{
BlastRadius, dependent_count, match_path, measure_all,
measure_scoped as measure_scoped_raw,
};
use crate::resolver::build_focus_set;
fn measure(
report: &GraphReport,
target: &str,
depth: Option<u32>,
) -> BlastRadius {
measure_raw(report, target, depth)
.expect("the fixture report describes this target")
}
fn measure_scoped(
report: &GraphReport,
target: &str,
depth: Option<u32>,
focus: &[String],
) -> BlastRadius {
measure_scoped_raw(report, target, depth, focus)
.expect("the fixture report describes this target")
}
#[test]
fn the_target_is_never_a_dependent_of_itself() {
let with_self_edge = report(
&["a.rs", "b.rs"],
&[("a.rs", "a.rs", "crate::a"), ("b.rs", "a.rs", "crate::a")],
);
let radius = measure(&with_self_edge, "a.rs", None);
assert_eq!(dependent_count(&radius), 1);
assert_eq!(radius.dependents[0].file, "b.rs");
}
#[test]
fn a_file_depending_by_two_names_is_counted_once() {
let report = report(
&["a.rs", "b.rs", "c.rs"],
&[
("b.rs", "a.rs", "crate::a"),
("b.rs", "a.rs", "crate::a::Thing"),
("c.rs", "a.rs", "crate::a"),
],
);
let radius = measure(&report, "a.rs", None);
assert_eq!(dependent_count(&radius), 2, "{radius:?}");
let b = radius
.dependents
.iter()
.find(|dependent| dependent.file == "b.rs")
.expect("b.rs is a dependent");
assert_eq!(b.imports, vec!["crate::a", "crate::a::Thing"]);
}
#[test]
fn a_file_reaching_the_target_two_ways_is_counted_once() {
let radius = measure(&repo_report(), "a.rs", None);
assert_eq!(dependent_count(&radius), 3, "{radius:?}");
}
#[test]
fn a_transitive_dependent_lists_no_direct_import_of_the_target() {
let radius = measure(&repo_report(), "a.rs", None);
let d = radius
.dependents
.iter()
.find(|dependent| dependent.file == "d.rs")
.expect("d.rs is a transitive dependent");
assert!(d.imports.is_empty(), "{d:?}");
}
#[test]
fn an_unresolved_edge_does_not_create_a_dependent() {
let report = GraphReport {
edges: vec![GraphEdge {
from: "b.rs".to_owned(),
to: Some("a.rs".to_owned()),
import_path: "crate::a".to_owned(),
resolved: false,
kind: ImportKind::Dependency,
local_gap: true,
cfg_gated: false,
}],
..report(&["a.rs", "b.rs"], &[])
};
let radius = measure(&report, "a.rs", None);
assert_eq!(dependent_count(&radius), 0, "{radius:?}");
}
#[test]
fn an_unresolved_edge_is_ignored_alongside_a_real_one() {
let report = GraphReport {
edges: vec![
edge("b.rs", "a.rs", "crate::a"),
GraphEdge {
from: "c.rs".to_owned(),
to: Some("a.rs".to_owned()),
import_path: "crate::missing".to_owned(),
resolved: false,
kind: ImportKind::Dependency,
local_gap: true,
cfg_gated: false,
},
],
..report(&["a.rs", "b.rs", "c.rs"], &[])
};
let radius = measure(&report, "a.rs", None);
assert_eq!(dependent_count(&radius), 1, "{radius:?}");
assert_eq!(radius.dependents[0].file, "b.rs");
assert_eq!(radius.dependents[0].imports, vec!["crate::a"]);
}
#[test]
fn a_cycle_back_to_the_target_still_terminates() {
let ring = report(
&["a.rs", "b.rs", "c.rs"],
&[
("b.rs", "a.rs", "crate::a"),
("c.rs", "b.rs", "crate::b"),
("a.rs", "c.rs", "crate::c"),
],
);
let radius = measure(&ring, "a.rs", None);
assert_eq!(dependent_count(&radius), 2, "{radius:?}");
}
#[test]
fn depth_bounds_the_walk_and_is_reported() {
let report = repo_report();
let requested: Vec<String> = vec!["a.rs".to_owned()];
let full = measure_all(&report, &requested, "", None, &[]).unwrap();
let one_hop = measure_all(&report, &requested, "", Some(1), &[]).unwrap();
let two_hops = measure_all(&report, &requested, "", Some(2), &[]).unwrap();
assert_eq!(dependent_count(&full[0]), 3);
assert_eq!(
dependent_count(&one_hop[0]),
2,
"depth 1 must stop at direct importers"
);
assert_eq!(dependent_count(&two_hops[0]), 3);
assert_eq!(one_hop[0].depth, Some(1));
assert_eq!(full[0].depth, None);
}
#[test]
fn depth_applies_to_every_target_not_just_the_first() {
let report = repo_report();
let requested: Vec<String> = vec!["a.rs".to_owned(), "d.rs".to_owned()];
let radii = measure_all(&report, &requested, "", Some(1), &[]).unwrap();
for radius in &radii {
assert_eq!(radius.depth, Some(1), "{radius:?}");
}
assert_eq!(radii.iter().map(dependent_count).sum::<u64>(), 2);
}
#[test]
fn several_targets_come_back_widest_first() {
let requested: Vec<String> =
vec!["d.rs".to_owned(), "b.rs".to_owned(), "a.rs".to_owned()];
let radii = measure_all(&repo_report(), &requested, "", None, &[]).unwrap();
assert_eq!(
radii.iter().map(dependent_count).collect::<Vec<_>>(),
vec![3, 1, 0],
"a reader asking about three files wants the widest at the top"
);
assert_eq!(
radii.iter().map(|r| r.target.as_str()).collect::<Vec<_>>(),
vec!["a.rs", "b.rs", "d.rs"]
);
}
#[test]
fn batching_does_not_change_any_answer() {
let requested: Vec<String> =
vec!["a.rs".to_owned(), "b.rs".to_owned(), "d.rs".to_owned()];
let report = repo_report();
let together = measure_all(&report, &requested, "", None, &[]).unwrap();
let mut together_counts: Vec<u64> =
together.iter().map(dependent_count).collect();
together_counts.sort_unstable();
let mut apart: Vec<u64> = requested
.iter()
.map(|p| dependent_count(&measure(&report, p, None)))
.collect();
apart.sort_unstable();
assert_eq!(together_counts, apart);
}
#[test]
fn one_unknown_path_aborts_and_names_all_of_them() {
let requested: Vec<String> = vec![
"a.rs".to_owned(),
"typo_one.rs".to_owned(),
"typo_two.rs".to_owned(),
];
let error = measure_all(&repo_report(), &requested, "", None, &[])
.unwrap_err()
.to_string();
assert!(error.contains("typo_one.rs"), "{error}");
assert!(error.contains("typo_two.rs"), "{error}");
assert!(
!error.contains("`a.rs`"),
"a path that resolved must not be reported as missing: {error}"
);
}
#[test]
fn focus_narrows_the_answer_without_moving_the_target() {
let radius = measure_scoped(
&scoped_report(),
"target.rs",
None,
&[path("crates/other")],
);
assert_eq!(radius.target, "target.rs");
assert_eq!(dependent_count(&radius), 0);
}
#[test]
fn focus_keeps_only_the_dependents_inside_the_scope() {
let report = scoped_report();
let everything = measure_scoped(&report, "target.rs", None, &[]);
let one_package =
measure_scoped(&report, "target.rs", None, &[path("crates/one")]);
let both = measure_scoped(
&report,
"target.rs",
None,
&[path("crates/one"), path("crates/two")],
);
assert_eq!(dependent_count(&everything), 4);
assert_eq!(dependent_count(&one_package), 2);
assert_eq!(dependent_count(&both), 3);
assert!(
one_package
.dependents
.iter()
.all(|dependent| dependent.file.starts_with("crates/one/")),
"{:?}",
one_package.dependents
);
}
#[test]
fn focus_is_a_directory_boundary_not_a_string_prefix() {
let radius = measure_scoped(
&scoped_report(),
"target.rs",
None,
&[path("crates/two")],
);
assert_eq!(dependent_count(&radius), 1);
assert_eq!(radius.dependents[0].file, "crates/two/user.rs");
}
#[test]
fn focus_and_depth_bound_the_same_walk() {
let mut report = scoped_report();
report.nodes.push(node("crates/one/far.rs"));
report.edges.push(edge(
"crates/one/far.rs",
"crates/two/user.rs",
"crate::two",
));
let scoped_and_bounded =
measure_scoped(&report, "target.rs", Some(1), &[path("crates/one")]);
assert_eq!(dependent_count(&scoped_and_bounded), 2);
let scoped_unbounded =
measure_scoped(&report, "target.rs", None, &[path("crates/one")]);
assert_eq!(dependent_count(&scoped_unbounded), 3);
}
#[test]
fn an_empty_scope_means_everything() {
let report = scoped_report();
assert_eq!(
dependent_count(&measure_scoped(&report, "target.rs", None, &[])),
dependent_count(&measure(&report, "target.rs", None)),
);
}
#[test]
fn a_windows_style_git_path_matches_the_graph() {
let report = report(
&["src/a.rs", "src/b.rs"],
&[("src/b.rs", "src/a.rs", "crate::a")],
);
let matched = match_path(&report, "src\\a.rs", "");
if cfg!(windows) {
assert_eq!(matched.as_deref(), Some("src/a.rs"));
} else {
assert_eq!(
matched, None,
"on Unix `src\\a.rs` is one file name, not a path into `src`"
);
}
}
#[test]
fn a_path_relative_to_the_repository_root_matches_when_analysing_a_subdirectory()
{
let report = report(&["a.rs", "b.rs"], &[("b.rs", "a.rs", "crate::a")]);
assert_eq!(
match_path(&report, "crates/demo/a.rs", "crates/demo").as_deref(),
Some("a.rs"),
"the analysis base prefix must be stripped before matching"
);
}
#[test]
fn a_basename_collision_outside_the_base_does_not_match() {
assert_eq!(match_path(&repo_report(), "elsewhere/a.rs", ""), None);
}
#[test]
fn a_leading_dot_slash_does_not_defeat_matching() {
assert_eq!(
match_path(&repo_report(), "./a.rs", "").as_deref(),
Some("a.rs")
);
}
#[test]
fn measuring_the_query_target_is_not_treated_as_a_different_file() {
let filtered = GraphReport {
query: Some(GraphQuery::DependsOn("a.rs".to_owned())),
..repo_report()
};
let same_file = measure_raw(&filtered, "a.rs", None);
assert_eq!(
same_file
.expect("the query target itself is measurable")
.target,
"a.rs"
);
}
#[test]
fn measuring_a_second_file_on_a_query_filtered_report_is_refused() {
let filtered = GraphReport {
query: Some(GraphQuery::DependsOn("a.rs".to_owned())),
..repo_report()
};
let error = measure_all(&filtered, &["b.rs".to_owned()], "", None, &[])
.unwrap_err()
.to_string();
assert!(
error.contains("a.rs"),
"the error must name the file the report actually describes: {error}"
);
}
#[test]
fn measuring_the_query_target_itself_is_allowed() {
let filtered = GraphReport {
query: Some(GraphQuery::DependsOn("a.rs".to_owned())),
..repo_report()
};
let radii =
measure_all(&filtered, &["a.rs".to_owned()], "", None, &[]).unwrap();
assert_eq!(radii[0].target, "a.rs");
assert_eq!(dependent_count(&radii[0]), 3);
}
#[test]
fn an_absolute_scope_path_is_normalised_before_matching() {
let base = std::env::temp_dir().join("repo");
let inside = base.join("crates").join("one");
let scope = build_focus_set(&base, &[inside])
.into_iter()
.collect::<Vec<_>>();
assert_eq!(
scope,
vec!["crates/one".to_owned()],
"an absolute scope must be rewritten base-relative, or it matches \
nothing"
);
let radius = measure_scoped(&scoped_report(), "target.rs", None, &scope);
assert_eq!(
dependent_count(&radius),
2,
"a normalised absolute scope must reach the same files a relative \
one does"
);
}
#[test]
fn a_relative_base_still_accepts_an_absolute_scope() {
let base = std::env::current_dir().expect("a working directory");
let absolute = base.join("crates").join("one");
let scope = build_focus_set(
std::path::Path::new("."),
std::slice::from_ref(&absolute),
)
.into_iter()
.collect::<Vec<_>>();
assert_eq!(scope, vec!["crates/one".to_owned()]);
let radius = measure_scoped(&scoped_report(), "target.rs", None, &scope);
assert_eq!(
dependent_count(&radius),
2,
"an absolute scope under `--path .` must reach the same dependents"
);
}
#[test]
fn the_repository_prefix_is_empty_when_analysis_starts_at_the_root() {
assert_eq!(
super::base_prefix_for(
std::path::Path::new("/repo"),
std::path::Path::new("/repo")
),
""
);
assert_eq!(
super::base_prefix_for(
std::path::Path::new("/repo"),
std::path::Path::new("/repo/crates/demo")
),
"crates/demo"
);
}
#[test]
fn the_repository_prefix_is_empty_when_the_paths_do_not_nest() {
assert_eq!(
super::base_prefix_for(
std::path::Path::new("/repo"),
std::path::Path::new("/elsewhere")
),
""
);
}