Skip to content

Commit

Permalink
add a test for the root constraint error printing
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Nov 1, 2024
1 parent e2ba786 commit 78a803a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/conflict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ impl<'i, I: Interner> fmt::Display for DisplayUnsat<'i, I> {
&ConflictCause::Constrains(version_set_id) => {
writeln!(
f,
"{indent}The constraint at the root level {version_set} could not be fulfilled",
"{indent}root constraint '{version_set}' cannot be fulfilled",
version_set = self.interner.display_version_set(version_set_id),
)?;
}
Expand Down
42 changes: 34 additions & 8 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,19 +356,45 @@ impl<'s> SnapshotProvider<'s> {
}

/// Adds another requirement that matches any version of a package
pub fn add_package_requirement(&mut self, name: NameId) -> VersionSetId {
pub fn add_package_requirement(&mut self, name: NameId, matcher: &str) -> VersionSetId {
let id = self.snapshot.version_sets.max() + self.additional_version_sets.len();

let package = self.package(name);

let version_set = VersionSet {
name,
display: "*".to_string(),
matching_candidates: package.solvables.iter().copied().collect(),
};
// match packages if not `*`
if matcher != "*" {
let matching_candidates = package
.solvables

Check warning on line 367 in src/snapshot.rs

View workflow job for this annotation

GitHub Actions / Format and Lint

Diff in /home/runner/work/resolvo/resolvo/src/snapshot.rs
.iter()
.copied()
.filter(|&s| {
self.solvable(s)
.display
.contains(matcher)
})
.collect();

let version_set = VersionSet {
name,
display: format!("{} {}", package.name, matcher),
matching_candidates,
};

self.additional_version_sets.push(version_set);
return VersionSetId::from_usize(id);
} else {
// if `*` match all packages
let matching_candidates = package.solvables.iter().copied().collect();

let version_set = VersionSet {
name,
display: "*".to_string(),
matching_candidates,
};

self.additional_version_sets.push(version_set);
VersionSetId::from_usize(id)
self.additional_version_sets.push(version_set);
return VersionSetId::from_usize(id);
}
}

fn solvable(&self, solvable: SolvableId) -> &Solvable {
Expand Down
8 changes: 8 additions & 0 deletions tests/snapshots/solver__root_constraints.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: tests/solver.rs
expression: "solve_for_snapshot(snapshot_provider, &[union_req], &[icons_req])"
---
The following packages are incompatible
└─ union * can be installed with any of the following options:
└─ union union=1
├─ root constraint 'union 5' cannot be fulfilled
43 changes: 36 additions & 7 deletions tests/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,9 +1332,9 @@ fn test_snapshot() {

let mut snapshot_provider = snapshot.provider();

let menu_req = snapshot_provider.add_package_requirement(menu_name_id);
let menu_req = snapshot_provider.add_package_requirement(menu_name_id, "*");

assert_snapshot!(solve_for_snapshot(snapshot_provider, &[menu_req]));
assert_snapshot!(solve_for_snapshot(snapshot_provider, &[menu_req], &[]));
}

#[test]
Expand All @@ -1354,12 +1354,40 @@ fn test_snapshot_union_requirements() {

let mut snapshot_provider = snapshot.provider();

let intl_req = snapshot_provider.add_package_requirement(intl_name_id);
let union_req = snapshot_provider.add_package_requirement(union_name_id);
let intl_req = snapshot_provider.add_package_requirement(intl_name_id, "*");
let union_req = snapshot_provider.add_package_requirement(union_name_id, "*");

assert_snapshot!(solve_for_snapshot(
snapshot_provider,
&[intl_req, union_req]
&[intl_req, union_req],
&[]
));
}

#[test]
fn test_root_constraints() {
let provider = BundleBoxProvider::from_packages(&[
("icons", 2, vec![]),
("icons", 1, vec![]),
("intl", 5, vec![]),
("intl", 3, vec![]),
("union", 1, vec!["icons"]),
]);

let icons_name_id = provider.package_name("icons");
let union_name_id = provider.package_name("union");

let snapshot = provider.into_snapshot();

let mut snapshot_provider = snapshot.provider();

let union_req = snapshot_provider.add_package_requirement(union_name_id, "*");
let icons_req = snapshot_provider.add_package_requirement(union_name_id, "5");

assert_snapshot!(solve_for_snapshot(
snapshot_provider,
&[union_req],
&[icons_req]
));
}

Expand Down Expand Up @@ -1401,9 +1429,10 @@ fn serialize_snapshot(snapshot: &DependencySnapshot, destination: impl AsRef<std
serde_json::to_writer_pretty(file, snapshot).unwrap()

Check warning on line 1429 in tests/solver.rs

View workflow job for this annotation

GitHub Actions / Format and Lint

Diff in /home/runner/work/resolvo/resolvo/tests/solver.rs
}

fn solve_for_snapshot(provider: SnapshotProvider, root_reqs: &[VersionSetId]) -> String {
fn solve_for_snapshot(provider: SnapshotProvider, root_reqs: &[VersionSetId], root_constraints: &[VersionSetId]) -> String {
let mut solver = Solver::new(provider);
let problem = Problem::new().requirements(root_reqs.iter().copied().map(Into::into).collect());
let problem = Problem::new().requirements(root_reqs.iter().copied().map(Into::into).collect())
.constraints(root_constraints.iter().copied().map(Into::into).collect());
match solver.solve(problem) {
Ok(solvables) => transaction_to_string(solver.provider(), &solvables),
Err(UnsolvableOrCancelled::Unsolvable(conflict)) => {
Expand Down
2 changes: 1 addition & 1 deletion tools/solve-snapshot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn main() {
0 => {
// Add a package requirement
let (package, _) = snapshot.packages.iter().choose(&mut rng).unwrap();
let package_requirement = provider.add_package_requirement(package);
let package_requirement = provider.add_package_requirement(package, "*");
requirements.push(package_requirement.into());
}
1 => {
Expand Down

0 comments on commit 78a803a

Please sign in to comment.