Skip to content

Commit cc4c3e7

Browse files
committed
test: separate resolver tests into multiple files
1 parent 2e309bd commit cc4c3e7

File tree

6 files changed

+1244
-1206
lines changed

6 files changed

+1244
-1206
lines changed

crates/resolver-tests/src/helpers.rs

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
use std::collections::BTreeMap;
2+
use std::fmt::Debug;
3+
use std::sync::OnceLock;
4+
5+
use cargo::core::dependency::DepKind;
6+
use cargo::core::{Dependency, GitReference, PackageId, SourceId, Summary};
7+
use cargo::util::IntoUrl;
8+
9+
pub trait ToDep {
10+
fn to_dep(self) -> Dependency;
11+
fn to_opt_dep(self) -> Dependency;
12+
fn to_dep_with(self, features: &[&'static str]) -> Dependency;
13+
}
14+
15+
impl ToDep for &'static str {
16+
fn to_dep(self) -> Dependency {
17+
Dependency::parse(self, Some("1.0.0"), registry_loc()).unwrap()
18+
}
19+
fn to_opt_dep(self) -> Dependency {
20+
let mut dep = self.to_dep();
21+
dep.set_optional(true);
22+
dep
23+
}
24+
fn to_dep_with(self, features: &[&'static str]) -> Dependency {
25+
let mut dep = self.to_dep();
26+
dep.set_default_features(false);
27+
dep.set_features(features.into_iter().copied());
28+
dep
29+
}
30+
}
31+
32+
impl ToDep for Dependency {
33+
fn to_dep(self) -> Dependency {
34+
self
35+
}
36+
fn to_opt_dep(mut self) -> Dependency {
37+
self.set_optional(true);
38+
self
39+
}
40+
fn to_dep_with(mut self, features: &[&'static str]) -> Dependency {
41+
self.set_default_features(false);
42+
self.set_features(features.into_iter().copied());
43+
self
44+
}
45+
}
46+
47+
pub trait ToPkgId {
48+
fn to_pkgid(&self) -> PackageId;
49+
}
50+
51+
impl ToPkgId for PackageId {
52+
fn to_pkgid(&self) -> PackageId {
53+
*self
54+
}
55+
}
56+
57+
impl<'a> ToPkgId for &'a str {
58+
fn to_pkgid(&self) -> PackageId {
59+
PackageId::try_new(*self, "1.0.0", registry_loc()).unwrap()
60+
}
61+
}
62+
63+
impl<T: AsRef<str>, U: AsRef<str>> ToPkgId for (T, U) {
64+
fn to_pkgid(&self) -> PackageId {
65+
let (name, vers) = self;
66+
PackageId::try_new(name.as_ref(), vers.as_ref(), registry_loc()).unwrap()
67+
}
68+
}
69+
70+
#[macro_export]
71+
macro_rules! pkg {
72+
($pkgid:expr => [$($deps:expr),* $(,)? ]) => ({
73+
use $crate::helpers::ToDep;
74+
let d: Vec<Dependency> = vec![$($deps.to_dep()),*];
75+
$crate::helpers::pkg_dep($pkgid, d)
76+
});
77+
78+
($pkgid:expr) => ({
79+
$crate::helpers::pkg($pkgid)
80+
})
81+
}
82+
83+
fn registry_loc() -> SourceId {
84+
static EXAMPLE_DOT_COM: OnceLock<SourceId> = OnceLock::new();
85+
let example_dot = EXAMPLE_DOT_COM.get_or_init(|| {
86+
SourceId::for_registry(&"https://example.com".into_url().unwrap()).unwrap()
87+
});
88+
*example_dot
89+
}
90+
91+
pub fn pkg<T: ToPkgId>(name: T) -> Summary {
92+
pkg_dep(name, Vec::new())
93+
}
94+
95+
pub fn pkg_dep<T: ToPkgId>(name: T, dep: Vec<Dependency>) -> Summary {
96+
let pkgid = name.to_pkgid();
97+
let link = if pkgid.name().ends_with("-sys") {
98+
Some(pkgid.name())
99+
} else {
100+
None
101+
};
102+
Summary::new(name.to_pkgid(), dep, &BTreeMap::new(), link, None).unwrap()
103+
}
104+
105+
pub fn pkg_dep_with<T: ToPkgId>(
106+
name: T,
107+
dep: Vec<Dependency>,
108+
features: &[(&'static str, &[&'static str])],
109+
) -> Summary {
110+
let pkgid = name.to_pkgid();
111+
let link = if pkgid.name().ends_with("-sys") {
112+
Some(pkgid.name())
113+
} else {
114+
None
115+
};
116+
let features = features
117+
.into_iter()
118+
.map(|&(name, values)| (name.into(), values.into_iter().map(|&v| v.into()).collect()))
119+
.collect();
120+
Summary::new(name.to_pkgid(), dep, &features, link, None).unwrap()
121+
}
122+
123+
pub fn pkg_id(name: &str) -> PackageId {
124+
PackageId::try_new(name, "1.0.0", registry_loc()).unwrap()
125+
}
126+
127+
fn pkg_id_loc(name: &str, loc: &str) -> PackageId {
128+
let remote = loc.into_url();
129+
let master = GitReference::Branch("master".to_string());
130+
let source_id = SourceId::for_git(&remote.unwrap(), master).unwrap();
131+
132+
PackageId::try_new(name, "1.0.0", source_id).unwrap()
133+
}
134+
135+
pub fn pkg_loc(name: &str, loc: &str) -> Summary {
136+
let link = if name.ends_with("-sys") {
137+
Some(name)
138+
} else {
139+
None
140+
};
141+
Summary::new(
142+
pkg_id_loc(name, loc),
143+
Vec::new(),
144+
&BTreeMap::new(),
145+
link,
146+
None,
147+
)
148+
.unwrap()
149+
}
150+
151+
pub fn remove_dep(sum: &Summary, ind: usize) -> Summary {
152+
let mut deps = sum.dependencies().to_vec();
153+
deps.remove(ind);
154+
// note: more things will need to be copied over in the future, but it works for now.
155+
Summary::new(sum.package_id(), deps, &BTreeMap::new(), sum.links(), None).unwrap()
156+
}
157+
158+
pub fn dep(name: &str) -> Dependency {
159+
dep_req(name, "*")
160+
}
161+
162+
pub fn dep_req(name: &str, req: &str) -> Dependency {
163+
Dependency::parse(name, Some(req), registry_loc()).unwrap()
164+
}
165+
166+
pub fn dep_req_kind(name: &str, req: &str, kind: DepKind) -> Dependency {
167+
let mut dep = dep_req(name, req);
168+
dep.set_kind(kind);
169+
dep
170+
}
171+
172+
pub fn dep_loc(name: &str, location: &str) -> Dependency {
173+
let url = location.into_url().unwrap();
174+
let master = GitReference::Branch("master".to_string());
175+
let source_id = SourceId::for_git(&url, master).unwrap();
176+
Dependency::parse(name, Some("1.0.0"), source_id).unwrap()
177+
}
178+
179+
pub fn dep_kind(name: &str, kind: DepKind) -> Dependency {
180+
dep(name).set_kind(kind).clone()
181+
}
182+
183+
pub fn registry(pkgs: Vec<Summary>) -> Vec<Summary> {
184+
pkgs
185+
}
186+
187+
pub fn names<P: ToPkgId>(names: &[P]) -> Vec<PackageId> {
188+
names.iter().map(|name| name.to_pkgid()).collect()
189+
}
190+
191+
pub fn loc_names(names: &[(&'static str, &'static str)]) -> Vec<PackageId> {
192+
names
193+
.iter()
194+
.map(|&(name, loc)| pkg_id_loc(name, loc))
195+
.collect()
196+
}
197+
198+
/// Assert `xs` contains `elems`
199+
#[track_caller]
200+
pub fn assert_contains<A: PartialEq + Debug>(xs: &[A], elems: &[A]) {
201+
for elem in elems {
202+
assert!(
203+
xs.contains(elem),
204+
"missing element\nset: {xs:?}\nmissing: {elem:?}"
205+
);
206+
}
207+
}
208+
209+
#[track_caller]
210+
pub fn assert_same<A: PartialEq + Debug>(a: &[A], b: &[A]) {
211+
assert_eq!(a.len(), b.len(), "not equal\n{a:?}\n{b:?}");
212+
assert_contains(b, a);
213+
}

0 commit comments

Comments
 (0)