Skip to content

Commit 94fc35e

Browse files
authored
Fix feature scoping for pep508 wasm32 support for ruff (#8694)
1 parent c1a0fb3 commit 94fc35e

File tree

7 files changed

+117
-82
lines changed

7 files changed

+117
-82
lines changed

Cargo.lock

+22-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/uv-pep508/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ version-ranges = { workspace = true }
4141

4242
[dev-dependencies]
4343
insta = { version = "1.40.0" }
44-
log = { version = "0.4.22" }
4544
serde_json = { version = "1.0.128" }
46-
testing_logger = { version = "0.1.1" }
45+
tracing-test = { version = "0.2.5" }
4746

4847
[features]
4948
tracing = ["dep:tracing", "uv-pep440/tracing"]

crates/uv-pep508/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
2626
use thiserror::Error;
2727
use url::Url;
2828

29-
use crate::marker::MarkerValueExtra;
3029
use cursor::Cursor;
3130
pub use marker::{
3231
ContainsMarkerTree, ExtraMarkerTree, ExtraOperator, InMarkerTree, MarkerEnvironment,
3332
MarkerEnvironmentBuilder, MarkerExpression, MarkerOperator, MarkerTree, MarkerTreeContents,
34-
MarkerTreeKind, MarkerValue, MarkerValueString, MarkerValueVersion, MarkerWarningKind,
35-
StringMarkerTree, StringVersion, VersionMarkerTree,
33+
MarkerTreeKind, MarkerValue, MarkerValueExtra, MarkerValueString, MarkerValueVersion,
34+
MarkerWarningKind, StringMarkerTree, StringVersion, VersionMarkerTree,
3635
};
3736
pub use origin::RequirementOrigin;
3837
#[cfg(feature = "non-pep508-extensions")]

crates/uv-pep508/src/marker/tree.rs

+48-41
Original file line numberDiff line numberDiff line change
@@ -1937,59 +1937,66 @@ mod test {
19371937

19381938
#[test]
19391939
#[cfg(feature = "tracing")]
1940-
fn warnings() {
1940+
#[tracing_test::traced_test]
1941+
fn warnings1() {
19411942
let env37 = env37();
1942-
testing_logger::setup();
19431943
let compare_keys = MarkerTree::from_str("platform_version == sys_platform").unwrap();
19441944
compare_keys.evaluate(&env37, &[]);
1945-
testing_logger::validate(|captured_logs| {
1946-
assert_eq!(
1947-
captured_logs[0].body,
1948-
"Comparing two markers with each other doesn't make any sense, will evaluate to false"
1949-
);
1950-
assert_eq!(captured_logs[0].level, log::Level::Warn);
1951-
assert_eq!(captured_logs.len(), 1);
1952-
});
1945+
logs_contain(
1946+
"Comparing two markers with each other doesn't make any sense, will evaluate to false",
1947+
);
1948+
}
1949+
1950+
#[test]
1951+
#[cfg(feature = "tracing")]
1952+
#[tracing_test::traced_test]
1953+
fn warnings2() {
1954+
let env37 = env37();
19531955
let non_pep440 = MarkerTree::from_str("python_version >= '3.9.'").unwrap();
19541956
non_pep440.evaluate(&env37, &[]);
1955-
testing_logger::validate(|captured_logs| {
1956-
assert_eq!(
1957-
captured_logs[0].body,
1958-
"Expected PEP 440 version to compare with python_version, found `3.9.`, \
1959-
will evaluate to false: after parsing `3.9`, found `.`, which is \
1960-
not part of a valid version"
1961-
);
1962-
assert_eq!(captured_logs[0].level, log::Level::Warn);
1963-
assert_eq!(captured_logs.len(), 1);
1964-
});
1957+
logs_contain(
1958+
"Expected PEP 440 version to compare with python_version, found `3.9.`, \
1959+
will evaluate to false: after parsing `3.9`, found `.`, which is \
1960+
not part of a valid version",
1961+
);
1962+
}
1963+
1964+
#[test]
1965+
#[cfg(feature = "tracing")]
1966+
#[tracing_test::traced_test]
1967+
fn warnings3() {
1968+
let env37 = env37();
19651969
let string_string = MarkerTree::from_str("'b' >= 'a'").unwrap();
19661970
string_string.evaluate(&env37, &[]);
1967-
testing_logger::validate(|captured_logs| {
1968-
assert_eq!(
1969-
captured_logs[0].body,
1970-
"Comparing two quoted strings with each other doesn't make sense: 'b' >= 'a', will evaluate to false"
1971-
);
1972-
assert_eq!(captured_logs[0].level, log::Level::Warn);
1973-
assert_eq!(captured_logs.len(), 1);
1974-
});
1971+
logs_contain(
1972+
"Comparing two quoted strings with each other doesn't make sense: 'b' >= 'a', will evaluate to false"
1973+
);
1974+
}
1975+
1976+
#[test]
1977+
#[cfg(feature = "tracing")]
1978+
#[tracing_test::traced_test]
1979+
fn warnings4() {
1980+
let env37 = env37();
19751981
let string_string = MarkerTree::from_str(r"os.name == 'posix' and platform.machine == 'x86_64' and platform.python_implementation == 'CPython' and 'Ubuntu' in platform.version and sys.platform == 'linux'").unwrap();
19761982
string_string.evaluate(&env37, &[]);
1977-
testing_logger::validate(|captured_logs| {
1978-
let messages: Vec<_> = captured_logs
1983+
logs_assert(|lines: &[&str]| {
1984+
let lines: Vec<_> = lines
19791985
.iter()
1980-
.map(|message| {
1981-
assert_eq!(message.level, log::Level::Warn);
1982-
&message.body
1983-
})
1986+
.map(|s| s.split_once(" ").unwrap().1)
19841987
.collect();
1985-
let expected = [
1986-
"os.name is deprecated in favor of os_name",
1987-
"platform.machine is deprecated in favor of platform_machine",
1988-
"platform.python_implementation is deprecated in favor of platform_python_implementation",
1989-
"platform.version is deprecated in favor of platform_version",
1990-
"sys.platform is deprecated in favor of sys_platform"
1988+
let expected = [
1989+
"WARN warnings4: uv_pep508: os.name is deprecated in favor of os_name",
1990+
"WARN warnings4: uv_pep508: platform.machine is deprecated in favor of platform_machine",
1991+
"WARN warnings4: uv_pep508: platform.python_implementation is deprecated in favor of",
1992+
"WARN warnings4: uv_pep508: sys.platform is deprecated in favor of sys_platform",
1993+
"WARN warnings4: uv_pep508: Comparing linux and posix lexicographically"
19911994
];
1992-
assert_eq!(messages, &expected);
1995+
if lines == expected {
1996+
Ok(())
1997+
} else {
1998+
Err(format!("{lines:?}"))
1999+
}
19932000
});
19942001
}
19952002

crates/uv-pep508/src/tests.rs

+2
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ fn error_invalid_extra_unnamed_url() {
712712

713713
/// Check that the relative path support feature toggle works.
714714
#[test]
715+
#[cfg(feature = "non-pep508-extensions")]
715716
fn non_pep508_paths() {
716717
let requirements = &[
717718
"foo @ file://./foo",
@@ -748,6 +749,7 @@ fn no_space_after_operator() {
748749
}
749750

750751
#[test]
752+
#[cfg(feature = "non-pep508-extensions")]
751753
fn path_with_fragment() {
752754
let requirements = if cfg!(windows) {
753755
&[

crates/uv-pep508/src/verbatim_url.rs

+41-23
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ impl VerbatimUrl {
140140
}
141141

142142
/// Return the underlying [`Path`], if the URL is a file URL.
143+
#[cfg(feature = "non-pep508-extensions")]
143144
pub fn as_path(&self) -> Result<PathBuf, VerbatimUrlError> {
144145
self.url
145146
.to_file_path()
@@ -212,11 +213,8 @@ impl Pep508Url for VerbatimUrl {
212213
type Err = VerbatimUrlError;
213214

214215
/// Create a `VerbatimUrl` to represent the requirement.
215-
fn parse_url(
216-
url: &str,
217-
#[cfg_attr(not(feature = "non-pep508-extensions"), allow(unused_variables))]
218-
working_dir: Option<&Path>,
219-
) -> Result<Self, Self::Err> {
216+
#[cfg_attr(not(feature = "non-pep508-extensions"), allow(unused_variables))]
217+
fn parse_url(url: &str, working_dir: Option<&Path>) -> Result<Self, Self::Err> {
220218
// Expand environment variables in the URL.
221219
let expanded = expand_env_vars(url);
222220

@@ -225,18 +223,24 @@ impl Pep508Url for VerbatimUrl {
225223
// Ex) `file:///home/ferris/project/scripts/...`, `file://localhost/home/ferris/project/scripts/...`, or `file:../ferris/`
226224
Some(Scheme::File) => {
227225
// Strip the leading slashes, along with the `localhost` host, if present.
228-
let path = strip_host(path);
229226

230227
// Transform, e.g., `/C:/Users/ferris/wheel-0.42.0.tar.gz` to `C:\Users\ferris\wheel-0.42.0.tar.gz`.
231-
let path = normalize_url_path(path);
232-
233228
#[cfg(feature = "non-pep508-extensions")]
234-
if let Some(working_dir) = working_dir {
235-
return Ok(VerbatimUrl::from_path(path.as_ref(), working_dir)?
236-
.with_given(url.to_string()));
237-
}
229+
{
230+
let path = strip_host(path);
231+
232+
let path = normalize_url_path(path);
238233

239-
Ok(VerbatimUrl::from_absolute_path(path.as_ref())?.with_given(url.to_string()))
234+
if let Some(working_dir) = working_dir {
235+
return Ok(VerbatimUrl::from_path(path.as_ref(), working_dir)?
236+
.with_given(url.to_string()));
237+
}
238+
239+
Ok(VerbatimUrl::from_absolute_path(path.as_ref())?
240+
.with_given(url.to_string()))
241+
}
242+
#[cfg(not(feature = "non-pep508-extensions"))]
243+
Ok(VerbatimUrl::parse_url(expanded)?.with_given(url.to_string()))
240244
}
241245

242246
// Ex) `https://download.pytorch.org/whl/torch_stable.html`
@@ -248,24 +252,33 @@ impl Pep508Url for VerbatimUrl {
248252
// Ex) `C:\Users\ferris\wheel-0.42.0.tar.gz`
249253
_ => {
250254
#[cfg(feature = "non-pep508-extensions")]
251-
if let Some(working_dir) = working_dir {
252-
return Ok(VerbatimUrl::from_path(expanded.as_ref(), working_dir)?
253-
.with_given(url.to_string()));
255+
{
256+
if let Some(working_dir) = working_dir {
257+
return Ok(VerbatimUrl::from_path(expanded.as_ref(), working_dir)?
258+
.with_given(url.to_string()));
259+
}
260+
261+
Ok(VerbatimUrl::from_absolute_path(expanded.as_ref())?
262+
.with_given(url.to_string()))
254263
}
255-
256-
Ok(VerbatimUrl::from_absolute_path(expanded.as_ref())?
257-
.with_given(url.to_string()))
264+
#[cfg(not(feature = "non-pep508-extensions"))]
265+
Err(Self::Err::NotAUrl(expanded.to_string()))
258266
}
259267
}
260268
} else {
261269
// Ex) `../editable/`
262270
#[cfg(feature = "non-pep508-extensions")]
263-
if let Some(working_dir) = working_dir {
264-
return Ok(VerbatimUrl::from_path(expanded.as_ref(), working_dir)?
265-
.with_given(url.to_string()));
271+
{
272+
if let Some(working_dir) = working_dir {
273+
return Ok(VerbatimUrl::from_path(expanded.as_ref(), working_dir)?
274+
.with_given(url.to_string()));
275+
}
276+
277+
Ok(VerbatimUrl::from_absolute_path(expanded.as_ref())?.with_given(url.to_string()))
266278
}
267279

268-
Ok(VerbatimUrl::from_absolute_path(expanded.as_ref())?.with_given(url.to_string()))
280+
#[cfg(not(feature = "non-pep508-extensions"))]
281+
Err(Self::Err::NotAUrl(expanded.to_string()))
269282
}
270283
}
271284
}
@@ -288,6 +301,11 @@ pub enum VerbatimUrlError {
288301
/// Received a path that could not be normalized.
289302
#[error("path could not be normalized: {0}")]
290303
Normalization(PathBuf, #[source] std::io::Error),
304+
305+
/// Received a path that could not be normalized.
306+
#[cfg(not(feature = "non-pep508-extensions"))]
307+
#[error("Not a URL (missing scheme): {0}")]
308+
NotAUrl(String),
291309
}
292310

293311
/// Expand all available environment variables.

crates/uv-pypi-types/src/metadata/requires_txt.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use serde::Deserialize;
33
use std::io::BufRead;
44
use std::str::FromStr;
55
use uv_normalize::ExtraName;
6-
use uv_pep508::marker::MarkerValueExtra;
7-
use uv_pep508::{ExtraOperator, MarkerExpression, MarkerTree, Requirement};
6+
use uv_pep508::{ExtraOperator, MarkerExpression, MarkerTree, MarkerValueExtra, Requirement};
87

98
/// `requires.txt` metadata as defined in <https://setuptools.pypa.io/en/latest/deprecated/python_eggs.html#dependency-metadata>.
109
///

0 commit comments

Comments
 (0)