Skip to content

Commit b8ff32f

Browse files
authored
Respect markers on constraints (#282)
Closes #252
1 parent 8123e1a commit b8ff32f

File tree

3 files changed

+82
-11
lines changed

3 files changed

+82
-11
lines changed

crates/puffin-cli/tests/pip_compile.rs

+50
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,56 @@ fn compile_constraints_inline() -> Result<()> {
228228
Ok(())
229229
}
230230

231+
/// Resolve a package from a `requirements.in` file, with a `constraints.txt` file that
232+
/// uses markers.
233+
#[test]
234+
fn compile_constraints_markers() -> Result<()> {
235+
let temp_dir = assert_fs::TempDir::new()?;
236+
let cache_dir = assert_fs::TempDir::new()?;
237+
let venv = temp_dir.child(".venv");
238+
239+
Command::new(get_cargo_bin(BIN_NAME))
240+
.arg("venv")
241+
.arg(venv.as_os_str())
242+
.arg("--cache-dir")
243+
.arg(cache_dir.path())
244+
.current_dir(&temp_dir)
245+
.assert()
246+
.success();
247+
venv.assert(predicates::path::is_dir());
248+
249+
let requirements_in = temp_dir.child("requirements.in");
250+
requirements_in.touch()?;
251+
requirements_in.write_str("anyio")?;
252+
253+
// Constrain a transitive dependency based on the Python version
254+
let constraints_txt = temp_dir.child("constraints.txt");
255+
constraints_txt.touch()?;
256+
// If constraints are ignored, these will conflict
257+
constraints_txt.write_str("sniffio==1.2.0;python_version<='3.7'")?;
258+
constraints_txt.write_str("sniffio==1.3.0;python_version>'3.7'")?;
259+
260+
insta::with_settings!({
261+
filters => vec![
262+
(r"(\d|\.)+(ms|s)", "[TIME]"),
263+
(r"# .* pip-compile", "# [BIN_PATH] pip-compile"),
264+
(r"--cache-dir .*", "--cache-dir [CACHE_DIR]"),
265+
]
266+
}, {
267+
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
268+
.arg("pip-compile")
269+
.arg("requirements.in")
270+
.arg("--constraint")
271+
.arg("constraints.txt")
272+
.arg("--cache-dir")
273+
.arg(cache_dir.path())
274+
.env("VIRTUAL_ENV", venv.as_os_str())
275+
.current_dir(&temp_dir));
276+
});
277+
278+
Ok(())
279+
}
280+
231281
/// Resolve a package from an optional dependency group in a `pyproject.toml` file.
232282
#[test]
233283
fn compile_pyproject_toml_extra() -> Result<()> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
source: crates/puffin-cli/tests/pip_compile.rs
3+
info:
4+
program: puffin
5+
args:
6+
- pip-compile
7+
- requirements.in
8+
- "--constraint"
9+
- constraints.txt
10+
- "--cache-dir"
11+
- /var/folders/bc/qlsk3t6x7c9fhhbvvcg68k9c0000gp/T/.tmp1Ts53o
12+
env:
13+
VIRTUAL_ENV: /var/folders/bc/qlsk3t6x7c9fhhbvvcg68k9c0000gp/T/.tmp1vzBQa/.venv
14+
---
15+
success: true
16+
exit_code: 0
17+
----- stdout -----
18+
# This file was autogenerated by Puffin v0.0.1 via the following command:
19+
# [BIN_PATH] pip-compile requirements.in --constraint constraints.txt --cache-dir [CACHE_DIR]
20+
anyio==4.0.0
21+
idna==3.4
22+
# via anyio
23+
sniffio==1.3.0
24+
# via anyio
25+
26+
----- stderr -----
27+
Resolved 3 packages in [TIME]
28+

crates/puffin-resolver/src/resolver.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -484,19 +484,12 @@ impl<'a, Context: BuildContext + Sync> Resolver<'a, Context> {
484484
}
485485
}
486486

487-
debug!("Got constraints: {:#?}", constraints);
488-
489487
// If any requirements were further constrained by the user, add those constraints.
490-
for constraint in &self.constraints {
491-
let package = PubGrubPackage::Package(
492-
PackageName::normalize(&constraint.name),
493-
None,
494-
None,
495-
);
488+
for (package, version) in
489+
iter_requirements(self.constraints.iter(), None, None, self.markers)
490+
{
496491
if let Some(range) = constraints.get_mut(&package) {
497-
*range = range.intersection(
498-
&version_range(constraint.version_or_url.as_ref()).unwrap(),
499-
);
492+
*range = range.intersection(&version);
500493
}
501494
}
502495

0 commit comments

Comments
 (0)