Skip to content

Commit

Permalink
Sort by name, then specifiers in uv add
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Nov 13, 2024
1 parent 709e45f commit 286ffbe
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
25 changes: 19 additions & 6 deletions crates/uv-workspace/src/pyproject_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,19 @@ pub fn add_dependency(

match to_replace.as_slice() {
[] => {
/// Split a requirement into the package name and its dependency specifiers.
///
/// E.g., given `flask>=1.0`, this function returns `("flask", ">=1.0")`. But given
/// `Flask>=1.0`, this function returns `("Flask", ">=1.0")`.
///
/// Extras are retained, such that `flask[dotenv]>=1.0` returns `("flask[dotenv]", ">=1.0")`.
fn split_specifiers(req: &str) -> (&str, &str) {
req.find(|c: char| matches!(c, '>' | '<' | '=' | '~' | '!' | '@')).map_or_else(|| (req.trim(), ""), |pos| {
let (name, specifiers) = req.split_at(pos);
(name.trim(), specifiers.trim())
})
}

#[derive(Debug, Copy, Clone)]
enum Sort {
/// The list is sorted in a case-sensitive manner.
Expand All @@ -906,6 +919,7 @@ pub fn add_dependency(
Unsorted,
}


// Determine if the dependency list is sorted prior to
// adding the new dependency; the new dependency list
// will be sorted only when the original list is sorted
Expand All @@ -920,14 +934,13 @@ pub fn add_dependency(
.all(Value::is_str)
.then(|| {
if deps.iter().tuple_windows().all(|(a, b)| {
a.as_str().map(str::to_lowercase) <= b.as_str().map(str::to_lowercase)
a.as_str().map(str::to_lowercase).as_deref().map(split_specifiers)
<= b.as_str().map(str::to_lowercase).as_deref().map(split_specifiers)
}) {
Some(Sort::CaseInsensitive)
} else if deps
.iter()
.tuple_windows()
.all(|(a, b)| a.as_str() <= b.as_str())
{
} else if deps.iter().tuple_windows().all(|(a, b)| {
a.as_str().map(split_specifiers) <= b.as_str().map(split_specifiers)
}) {
Some(Sort::CaseSensitive)
} else {
None
Expand Down
65 changes: 65 additions & 0 deletions crates/uv/tests/it/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5842,6 +5842,71 @@ fn case_sensitive_sorted_dependencies() -> Result<()> {
Ok(())
}

/// Ensure that sorting is based on the name, rather than the combined name-and-specifiers.
#[test]
fn sorted_dependencies_name_specifiers() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {r#"
[project]
name = "project"
version = "0.1.0"
description = "Add your description here"
requires-python = ">=3.12"
dependencies = [
"pylint>=3",
"pylint-module-boundaries>=1",
]
"#})?;

uv_snapshot!(context.filters(), context.add().args(["typing-extensions", "anyio"]), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 14 packages in [TIME]
Prepared 12 packages in [TIME]
Installed 12 packages in [TIME]
+ anyio==4.3.0
+ astroid==3.1.0
+ dill==0.3.8
+ idna==3.6
+ isort==5.13.2
+ mccabe==0.7.0
+ platformdirs==4.2.0
+ pylint==3.1.0
+ pylint-module-boundaries==1.3.1
+ sniffio==1.3.1
+ tomlkit==0.12.4
+ typing-extensions==4.10.0
"###);

let pyproject_toml = context.read("pyproject.toml");

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject_toml, @r###"
[project]
name = "project"
version = "0.1.0"
description = "Add your description here"
requires-python = ">=3.12"
dependencies = [
"anyio>=4.3.0",
"pylint>=3",
"pylint-module-boundaries>=1",
"typing-extensions>=4.10.0",
]
"###
);
});
Ok(())
}

/// Ensure that the custom ordering of the dependencies is preserved
/// after adding a package.
#[test]
Expand Down

0 comments on commit 286ffbe

Please sign in to comment.