-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
571de0c
commit 0a169f6
Showing
5 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
pyproject-fmt/rust/src/tests/dependency_groups_tests.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|