Skip to content

Commit

Permalink
Sort dependency-groups table
Browse files Browse the repository at this point in the history
  • Loading branch information
browniebroke committed Oct 26, 2024
1 parent 571de0c commit 0a169f6
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pyproject-fmt/rust/src/dependency_groups.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use common::array::{sort, transform};
use common::pep508::{format_requirement, get_canonic_requirement_name};
use common::table::{collapse_sub_tables, for_entries, reorder_table_keys, Tables};

#[allow(clippy::too_many_lines)]
pub fn fix(tables: &mut Tables,
keep_full_version: bool,) {
collapse_sub_tables(tables, "dependency-groups");
let table_element = tables.get("dependency-groups");
if table_element.is_none() {
return;
}
let table = &mut table_element.unwrap().first().unwrap().borrow_mut();
for_entries(table, &mut |_key, entry| {
transform(entry, &|s| format_requirement(s, keep_full_version));
sort(entry, |e| {
get_canonic_requirement_name(e).to_lowercase() + " " + &format_requirement(e, keep_full_version)
});
});
reorder_table_keys(
table,
&[
"",
"dev",
"test",
"docs",
],
);
}
2 changes: 2 additions & 0 deletions pyproject-fmt/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::global::reorder_tables;
use common::table::Tables;

mod build_system;
mod dependency_groups;
mod project;

mod global;
Expand Down Expand Up @@ -60,6 +61,7 @@ pub fn format_toml(content: &str, opt: &Settings) -> String {
opt.max_supported_python,
opt.min_supported_python,
);
dependency_groups::fix(&mut tables, opt.keep_full_version);
ruff::fix(&mut tables);
reorder_tables(&root_ast, &tables);

Expand Down
141 changes: 141 additions & 0 deletions pyproject-fmt/rust/src/tests/dependency_groups_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
use common::taplo::formatter::{format_syntax, Options};
use common::taplo::parser::parse;
use common::taplo::syntax::SyntaxElement;
use indoc::indoc;
use rstest::rstest;

use crate::dependency_groups::fix;
use common::table::Tables;

fn evaluate(start: &str, keep_full_version: bool) -> String {
let root_ast = parse(start).into_syntax().clone_for_update();
let count = root_ast.children_with_tokens().count();
let mut tables = Tables::from_ast(&root_ast);
fix(&mut tables, keep_full_version);
let entries = tables
.table_set
.iter()
.flat_map(|e| e.borrow().clone())
.collect::<Vec<SyntaxElement>>();
root_ast.splice_children(0..count, entries);
let opt = Options {
column_width: 1,
..Options::default()
};
format_syntax(root_ast, opt)
}

#[rstest]
#[case::no_groups(
indoc ! {r""},
"\n",
false,
)]
#[case::single_group_single_dep(
indoc ! {r#"
[dependency-groups]
test=["a>1.0.0"]
"#},
indoc ! {r#"
[dependency-groups]
test = [
"a>1",
]
"#},
false,
)]
#[case::single_group_single_dep_full_version(
indoc ! {r#"
[dependency-groups]
test=["a>1.0.0"]
"#},
indoc ! {r#"
[dependency-groups]
test = [
"a>1.0.0",
]
"#},
true,
)]
#[case::single_group_multiple_deps(
indoc ! {r#"
[dependency-groups]
test=["b==2.0.*", "a>1"]
"#},
indoc ! {r#"
[dependency-groups]
test = [
"a>1",
"b==2.0.*",
]
"#},
false,
)]
#[case::multiple_groups(
indoc ! {r#"
[dependency-groups]
example=["c<1"]
docs=["b==1"]
test=["a>1"]
dev=["d>=2"]
"#},
indoc ! {r#"
[dependency-groups]
dev = [
"d>=2",
]
test = [
"a>1",
]
docs = [
"b==1",
]
example = [
"c<1",
]
"#},
false,
)]
#[case::include_single_group(
indoc ! {r#"
[dependency-groups]
docs=["b==1"]
test=["a>1",{include-group="docs"}]
"#},
indoc ! {r#"
[dependency-groups]
test = [
"a>1",
{ include-group = "docs" },
]
docs = [
"b==1",
]
"#},
false,
)]
#[case::include_many_groups(
indoc ! {r#"
[dependency-groups]
docs=["b==1"]
test=["a>1"]
all=[{include-group="docs"},{include-group="test"}]
"#},
indoc ! {r#"
[dependency-groups]
test = [
"a>1",
]
docs = [
"b==1",
]
all = [
{ include-group = "docs" },
{ include-group = "test" },
]
"#},
false,
)]
fn test_format_dependency_groups(#[case] start: &str, #[case] expected: &str, #[case] keep_full_version: bool) {
assert_eq!(evaluate(start, keep_full_version), expected);
}
7 changes: 7 additions & 0 deletions pyproject-fmt/rust/src/tests/main_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use crate::{format_toml, Settings};
[build-system]
build-backend="backend"
requires=[" c >= 1.5.0", "d == 2.0.0"]
[dependency-groups]
test=["p>1.0.0"]
[tool.mypy]
mk="mv"
"#},
Expand Down Expand Up @@ -45,6 +47,11 @@ use crate::{format_toml, Settings};
"e>=1.5",
]
[dependency-groups]
test = [
"p>1",
]
[tool.mypy]
mk = "mv"
"#},
Expand Down
1 change: 1 addition & 0 deletions pyproject-fmt/rust/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod build_systems_tests;
mod dependency_groups_tests;
mod global_tests;
mod main_tests;
mod project_tests;
Expand Down

0 comments on commit 0a169f6

Please sign in to comment.