Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crates/uv-distribution-types/src/prioritized_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,12 @@ impl PrioritizedDist {
adjusted_wheels.push(wheel.clone());
}

let sdist = self.0.source.as_ref().map(|(sdist, _)| sdist.clone());
let sdist = self
.0
.source
.as_ref()
.filter(|(_, compatibility)| !compatibility.is_excluded())
.map(|(sdist, _)| sdist.clone());
Some(RegistryBuiltDist {
wheels: adjusted_wheels,
best_wheel_index: adjusted_best_index,
Expand All @@ -565,6 +570,7 @@ impl PrioritizedDist {
.0
.wheels
.iter()
.filter(|(_, compatibility)| !compatibility.is_excluded())
.map(|(wheel, _)| wheel.clone())
.collect();
Some(sdist)
Expand Down
20 changes: 10 additions & 10 deletions crates/uv-resolver/src/version_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,18 +729,18 @@ impl VersionMapLazy {
excluded: bool,
upload_time: Option<i64>,
) -> SourceDistCompatibility {
// Check if builds are disabled
if self.no_build {
return SourceDistCompatibility::Incompatible(IncompatibleSource::NoBuild);
}

// Check if after upload time cutoff
if excluded {
return SourceDistCompatibility::Incompatible(IncompatibleSource::ExcludeNewer(
upload_time,
));
}

// Check if builds are disabled
if self.no_build {
return SourceDistCompatibility::Incompatible(IncompatibleSource::NoBuild);
}

// Check if yanked
if let Some(yanked) = yanked {
if yanked.is_yanked()
Expand Down Expand Up @@ -790,16 +790,16 @@ impl VersionMapLazy {
excluded: bool,
upload_time: Option<i64>,
) -> WheelCompatibility {
// Check if binaries are disabled
if self.no_binary {
return WheelCompatibility::Incompatible(IncompatibleWheel::NoBinary);
}

// Check if after upload time cutoff
if excluded {
return WheelCompatibility::Incompatible(IncompatibleWheel::ExcludeNewer(upload_time));
}

// Check if binaries are disabled
if self.no_binary {
return WheelCompatibility::Incompatible(IncompatibleWheel::NoBinary);
}

// Check if yanked
if let Some(yanked) = yanked {
if yanked.is_yanked() && !self.allowed_yanks.contains(name, version) {
Expand Down
7 changes: 7 additions & 0 deletions crates/uv-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,13 @@ impl TestContext {
self
}

/// Add custom filters to the `TestContext`.
#[must_use]
pub fn with_filters(mut self, filters: impl IntoIterator<Item = (String, String)>) -> Self {
self.filters.extend(filters);
self
}

// Unsets the git credential helper using temp home gitconfig
#[must_use]
pub fn with_unset_git_credential_helper(self) -> Self {
Expand Down
52 changes: 43 additions & 9 deletions crates/uv-test/src/packse/scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,19 @@ pub struct PackageMetadata {
#[serde(default)]
pub extras: BTreeMap<ExtraName, Vec<Requirement>>,

/// Whether to produce a source distribution.
#[serde(default = "default_true")]
pub sdist: bool,

/// Whether to produce a wheel.
#[serde(default = "default_true")]
pub wheel: bool,
/// Whether to produce a source distribution, and optionally its metadata.
#[serde(
default = "default_artifact",
deserialize_with = "deserialize_artifact"
)]
pub sdist: Option<ArtifactMetadata>,

/// Whether to produce wheels, and optionally their shared metadata.
#[serde(
default = "default_artifact",
deserialize_with = "deserialize_artifact"
)]
pub wheel: Option<ArtifactMetadata>,

/// Whether this version is yanked.
#[serde(default)]
Expand All @@ -124,6 +130,33 @@ pub struct PackageMetadata {
pub wheel_tags: Vec<WheelTag>,
}

fn deserialize_artifact<'de, D>(
deserializer: D,
) -> std::result::Result<Option<ArtifactMetadata>, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum Helper {
Bool(bool),
Metadata(ArtifactMetadata),
}

match Helper::deserialize(deserializer)? {
Helper::Bool(false) => Ok(None),
Helper::Bool(true) => Ok(Some(ArtifactMetadata::default())),
Helper::Metadata(metadata) => Ok(Some(metadata)),
}
}

/// Metadata advertised for an artifact by the Packse Simple API.
#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ArtifactMetadata {
pub upload_time: Option<String>,
}

/// A validated three-component compatibility tag for generated wheels.
#[derive(Clone, Debug)]
pub struct WheelTag(String);
Expand Down Expand Up @@ -294,8 +327,9 @@ fn default_requires_python() -> Option<VersionSpecifiers> {
Some(VersionSpecifiers::from_str(">=3.12").expect("default requires-python should be valid"))
}

fn default_true() -> bool {
true
#[expect(clippy::unnecessary_wraps)] // Must return `Option` for serde `default`
fn default_artifact() -> Option<ArtifactMetadata> {
Some(ArtifactMetadata::default())
}

fn default_python() -> PythonVersion {
Expand Down
90 changes: 81 additions & 9 deletions crates/uv-test/src/packse/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct DistInfo {
filename: String,
sha256: String,
requires_python: Option<VersionSpecifiers>,
upload_time: &'static str,
upload_time: Option<String>,
yanked: bool,
}

Expand Down Expand Up @@ -73,6 +73,7 @@ struct ServerIndex {
/// When [`PackseServer`] is dropped, the background thread and server are shut down.
pub struct PackseServer {
server: HttpServer,
index: Arc<ServerIndex>,
}

impl PackseServer {
Expand Down Expand Up @@ -105,11 +106,12 @@ impl PackseServer {

fn start(scenario: &Scenario, hashes: bool) -> Self {
let index = Arc::new(build_server_index(scenario));
let server_index = Arc::clone(&index);
let server = HttpServer::start(move |request, server_uri| {
handle_request(request, server_uri, &index, hashes)
handle_request(request, server_uri, &server_index, hashes)
});

Self { server }
Self { server, index }
}

/// The Simple API index URL (e.g., `http://127.0.0.1:PORT/simple/`).
Expand All @@ -121,6 +123,15 @@ impl PackseServer {
pub fn file_url(&self, filename: &str) -> String {
format!("{}/files/{filename}", self.server.url())
}

/// Return the filename and advertised SHA-256 digest of each distribution.
pub fn files(&self) -> impl Iterator<Item = (&str, &str)> {
self.index
.packages
.values()
.flat_map(|package| package.dists.iter())
.map(|dist| (dist.filename.as_str(), dist.sha256.as_str()))
}
}

/// Build the complete [`ServerIndex`] from a scenario and cached build dependencies.
Expand All @@ -132,7 +143,7 @@ fn build_server_index(scenario: &Scenario) -> ServerIndex {
let mut dists = Vec::new();

for (version, meta) in &package.versions {
if meta.wheel {
if let Some(wheel_metadata) = &meta.wheel {
let tags = if meta.wheel_tags.is_empty() {
vec!["py3-none-any"]
} else {
Expand All @@ -154,13 +165,13 @@ fn build_server_index(scenario: &Scenario) -> ServerIndex {
filename,
sha256,
requires_python: meta.requires_python.clone(),
upload_time: PACKSE_UPLOAD_TIME,
upload_time: wheel_metadata.upload_time.clone(),
yanked: meta.yanked,
});
}
}

if meta.sdist {
if let Some(sdist_metadata) = &meta.sdist {
let (filename, bytes) = generate_sdist(
package_name,
version,
Expand All @@ -174,7 +185,7 @@ fn build_server_index(scenario: &Scenario) -> ServerIndex {
filename,
sha256,
requires_python: meta.requires_python.clone(),
upload_time: PACKSE_UPLOAD_TIME,
upload_time: sdist_metadata.upload_time.clone(),
yanked: meta.yanked,
});
}
Expand Down Expand Up @@ -203,7 +214,7 @@ fn build_server_index(scenario: &Scenario) -> ServerIndex {
filename: artifact.filename.to_string(),
sha256: artifact.sha256.to_string(),
requires_python: None,
upload_time: PACKSE_UPLOAD_TIME,
upload_time: None,
yanked: false,
});
}
Expand Down Expand Up @@ -355,7 +366,7 @@ fn build_simple_api_response(
"filename": dist.filename,
"url": url,
"hashes": {},
"upload-time": dist.upload_time,
"upload-time": dist.upload_time.as_deref().unwrap_or(PACKSE_UPLOAD_TIME),
});
if hashes {
file_obj["hashes"] = json!({ "sha256": dist.sha256 });
Expand Down Expand Up @@ -430,6 +441,67 @@ mod tests {
);
}

#[tokio::test]
async fn serves_artifact_upload_times() -> Result<()> {
let scenario = toml::from_str::<Scenario>(
r#"
name = "artifact-upload-times"

[root]
requires = ["a"]

[expected]
satisfiable = true

[packages.a.versions."1.0.0"]
sdist = { upload_time = "2024-03-23T00:00:00Z" }
wheel = { upload_time = "2024-03-26T00:00:00Z" }
wheel_tags = ["py3-none-any", "cp312-abi3-win_amd64"]

[packages.b.versions."1.0.0"]
sdist = false
wheel = {}

[packages.c.versions."1.0.0"]
wheel = false
"#,
)?;
let server = PackseServer::from_scenario(&scenario);

let a: serde_json::Value = reqwest::get(format!("{}a/", server.index_url()))
.await?
.json()
.await?;
assert_eq!(a["files"].as_array().map(Vec::len), Some(3));
assert_eq!(a["files"][0]["filename"], "a-1.0.0-py3-none-any.whl");
assert_eq!(a["files"][0]["upload-time"], "2024-03-26T00:00:00Z");
assert_eq!(
a["files"][1]["filename"],
"a-1.0.0-cp312-abi3-win_amd64.whl"
);
assert_eq!(a["files"][1]["upload-time"], "2024-03-26T00:00:00Z");
assert_eq!(a["files"][2]["filename"], "a-1.0.0.tar.gz");
assert_eq!(a["files"][2]["upload-time"], "2024-03-23T00:00:00Z");

let b: serde_json::Value = reqwest::get(format!("{}b/", server.index_url()))
.await?
.json()
.await?;
assert_eq!(b["files"].as_array().map(Vec::len), Some(1));
assert_eq!(b["files"][0]["filename"], "b-1.0.0-py3-none-any.whl");
assert_eq!(b["files"][0]["upload-time"], "2024-03-24T00:00:00Z");

let c: serde_json::Value = reqwest::get(format!("{}c/", server.index_url()))
.await?
.json()
.await?;
assert_eq!(c["files"].as_array().map(Vec::len), Some(1));
assert_eq!(c["files"][0]["filename"], "c-1.0.0.tar.gz");
assert_eq!(c["files"][0]["upload-time"], "2024-03-24T00:00:00Z");

Ok(())
}

#[tokio::test]
async fn file_requests_support_byte_ranges() -> Result<()> {
let scenario = toml::from_str::<Scenario>(
Expand Down
Loading
Loading