diff --git a/pyproject-fmt/rust/src/dependency_groups.rs b/pyproject-fmt/rust/src/dependency_groups.rs new file mode 100644 index 0000000..959b1a8 --- /dev/null +++ b/pyproject-fmt/rust/src/dependency_groups.rs @@ -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", + ], + ); +} diff --git a/pyproject-fmt/rust/src/main.rs b/pyproject-fmt/rust/src/main.rs index 6bdd4a9..1871cd3 100644 --- a/pyproject-fmt/rust/src/main.rs +++ b/pyproject-fmt/rust/src/main.rs @@ -9,6 +9,7 @@ use crate::global::reorder_tables; use common::table::Tables; mod build_system; +mod dependency_groups; mod project; mod global; @@ -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); diff --git a/pyproject-fmt/rust/src/tests/dependency_groups_tests.rs b/pyproject-fmt/rust/src/tests/dependency_groups_tests.rs new file mode 100644 index 0000000..6f79a23 --- /dev/null +++ b/pyproject-fmt/rust/src/tests/dependency_groups_tests.rs @@ -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::>(); + 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); +} diff --git a/pyproject-fmt/rust/src/tests/main_tests.rs b/pyproject-fmt/rust/src/tests/main_tests.rs index 4f8b82c..5e89b55 100644 --- a/pyproject-fmt/rust/src/tests/main_tests.rs +++ b/pyproject-fmt/rust/src/tests/main_tests.rs @@ -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" "#}, @@ -45,6 +47,11 @@ use crate::{format_toml, Settings}; "e>=1.5", ] + [dependency-groups] + test = [ + "p>1", + ] + [tool.mypy] mk = "mv" "#}, diff --git a/pyproject-fmt/rust/src/tests/mod.rs b/pyproject-fmt/rust/src/tests/mod.rs index 11e29e4..55a18bc 100644 --- a/pyproject-fmt/rust/src/tests/mod.rs +++ b/pyproject-fmt/rust/src/tests/mod.rs @@ -1,4 +1,5 @@ mod build_systems_tests; +mod dependency_groups_tests; mod global_tests; mod main_tests; mod project_tests;