Skip to content

Commit

Permalink
sort the dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ChannyClaus committed Aug 21, 2024
1 parent 720814c commit d3687df
Showing 2 changed files with 95 additions and 10 deletions.
18 changes: 13 additions & 5 deletions crates/uv-workspace/src/pyproject_mut.rs
Original file line number Diff line number Diff line change
@@ -3,10 +3,10 @@ use std::str::FromStr;
use std::{fmt, mem};

use pep440_rs::{Version, VersionSpecifier, VersionSpecifiers};
use pep508_rs::{ExtraName, MarkerTree, PackageName, Requirement, VersionOrUrl};
use pep508_rs::{ExtraName, MarkerTree, PackageName, Requirement, VerbatimUrl, VersionOrUrl};
use thiserror::Error;
use toml_edit::{Array, DocumentMut, Item, RawString, Table, TomlError, Value};
use uv_fs::PortablePath;
use uv_fs::{PortablePath, CWD};

use crate::pyproject::{DependencyType, Source};

@@ -503,9 +503,8 @@ pub fn add_dependency(
deps: &mut Array,
has_source: bool,
) -> Result<ArrayEdit, Error> {
// Find matching dependencies.
let mut to_replace = find_dependencies(&req.name, Some(&req.marker), deps);
match to_replace.as_slice() {
let edit = match to_replace.as_slice() {
[] => {
deps.push(req.to_string());
reformat_array_multiline(deps);
@@ -520,7 +519,16 @@ pub fn add_dependency(
}
// Cannot perform ambiguous updates.
_ => Err(Error::Ambiguous),
}
};

deps.sort_by_key(|d| {
pep508_rs::Requirement::<VerbatimUrl>::parse(d.clone().as_str().unwrap(), &*CWD)
.unwrap()
.name
.clone()
});

edit
}

/// Update an existing requirement.
87 changes: 82 additions & 5 deletions crates/uv/tests/edit.rs
Original file line number Diff line number Diff line change
@@ -3384,8 +3384,8 @@ fn add_requirements_file() -> Result<()> {
# ...
requires-python = ">=3.12"
dependencies = [
"anyio @ git+https://github.com/agronholm/anyio.git@4.4.0",
"flask==2.3.2",
"anyio",
]
[tool.uv.sources]
@@ -3464,9 +3464,9 @@ fn add_script() -> Result<()> {
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "anyio",
# "requests<3",
# "rich",
# "anyio",
# ]
# ///
@@ -3516,8 +3516,8 @@ fn add_script_without_metadata_table() -> Result<()> {
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "rich",
# "requests<3",
# "rich",
# ]
# ///
import requests
@@ -3568,8 +3568,8 @@ fn add_script_without_metadata_table_with_shebang() -> Result<()> {
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "rich",
# "requests<3",
# "rich",
# ]
# ///
import requests
@@ -3675,8 +3675,8 @@ fn add_script_without_metadata_table_with_docstring() -> Result<()> {
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "rich",
# "requests<3",
# "rich",
# ]
# ///
"""This is a script."""
@@ -3873,3 +3873,80 @@ fn add_git_to_script() -> Result<()> {
});
Ok(())
}

/// Ensure that the added dependencies are sorted.
#[test]
fn sorted_dependencies() -> 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 = [
"CacheControl[filecache]>=0.14,<0.15",
"mwparserfromhell",
"pywikibot",
"sentry-sdk",
"yarl",
]
"#})?;

uv_snapshot!(context.filters(), context.add(&["pydantic"]), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 19 packages in [TIME]
Prepared 19 packages in [TIME]
Installed 19 packages in [TIME]
+ annotated-types==0.6.0
+ cachecontrol==0.14.0
+ certifi==2024.2.2
+ charset-normalizer==3.3.2
+ filelock==3.13.1
+ idna==3.6
+ msgpack==1.0.8
+ multidict==6.0.5
+ mwparserfromhell==0.6.6
+ packaging==24.0
+ project==0.1.0 (from file://[TEMP_DIR]/)
+ pydantic==2.6.4
+ pydantic-core==2.16.3
+ pywikibot==9.0.0
+ requests==2.31.0
+ sentry-sdk==1.43.0
+ typing-extensions==4.10.0
+ urllib3==2.2.1
+ yarl==1.9.4
"###);

let pyproject_toml = fs_err::read_to_string(context.temp_dir.join("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 = [
"CacheControl[filecache]>=0.14,<0.15",
"mwparserfromhell",
"pydantic",
"pywikibot",
"sentry-sdk",
"yarl>=2.6.4",
]
"###
);
});
Ok(())
}

0 comments on commit d3687df

Please sign in to comment.