From 5c55a144ca1551a677fd4bffbce8ee47c941b0b9 Mon Sep 17 00:00:00 2001 From: "Philipp A." Date: Wed, 31 Jul 2024 13:42:48 +0200 Subject: [PATCH 1/7] Fix arrays being exploded without reason --- Cargo.lock | 25 ++++++++++++++- Cargo.toml | 1 + rust/src/build_system.rs | 1 + rust/src/data/column-width.expected.toml | 13 ++++++++ rust/src/data/column-width.start.toml | 9 ++++++ rust/src/global.rs | 1 + rust/src/helpers/array.rs | 19 ++++++++--- rust/src/helpers/pep508.rs | 1 + rust/src/main.rs | 40 ++++++++++++++++++------ rust/src/project.rs | 1 + rust/src/ruff.rs | 1 + tests/test_main.py | 16 +++------- 12 files changed, 101 insertions(+), 27 deletions(-) create mode 100644 rust/src/data/column-width.expected.toml create mode 100644 rust/src/data/column-width.start.toml diff --git a/Cargo.lock b/Cargo.lock index 13c18a9..b8de5d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -96,6 +96,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "either" version = "1.11.0" @@ -446,6 +452,16 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +[[package]] +name = "pretty_assertions" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "proc-macro2" version = "1.0.81" @@ -520,12 +536,13 @@ dependencies = [ [[package]] name = "pyproject-fmt-rust" -version = "1.1.4" +version = "1.1.5" dependencies = [ "indoc", "lexical-sort", "pep440_rs", "pep508_rs", + "pretty_assertions", "pyo3", "regex", "rstest", @@ -989,6 +1006,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" + [[package]] name = "zerocopy" version = "0.7.32" diff --git a/Cargo.toml b/Cargo.toml index eafb65b..55236aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,3 +32,4 @@ nursery = "warn" [dev-dependencies] rstest = { version = "0.19.0" } # parametrized tests indoc = { version = "2.0.5" } # dedented test cases for literal strings +pretty_assertions = "1.4.0" diff --git a/rust/src/build_system.rs b/rust/src/build_system.rs index 88c160c..a981dd4 100644 --- a/rust/src/build_system.rs +++ b/rust/src/build_system.rs @@ -24,6 +24,7 @@ pub fn fix(tables: &mut Tables, keep_full_version: bool) { #[cfg(test)] mod tests { use indoc::indoc; + use pretty_assertions::assert_eq; use rstest::rstest; use taplo::formatter::{format_syntax, Options}; use taplo::parser::parse; diff --git a/rust/src/data/column-width.expected.toml b/rust/src/data/column-width.expected.toml new file mode 100644 index 0000000..aac785d --- /dev/null +++ b/rust/src/data/column-width.expected.toml @@ -0,0 +1,13 @@ +[build-system] +build-backend = "backend" +requires = [ "c>=1.5", "d==2" ] + +[project] +name = "beta" +classifiers = [ + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.12", +] +dependencies = [ + "e>=1.5", +] diff --git a/rust/src/data/column-width.start.toml b/rust/src/data/column-width.start.toml new file mode 100644 index 0000000..e38ce8b --- /dev/null +++ b/rust/src/data/column-width.start.toml @@ -0,0 +1,9 @@ +[build-system] +build-backend = "backend" +requires = ["c>=1.5", "d == 2" ] + +[project] +name = "beta" +dependencies = [ + "e>=1.5", +] diff --git a/rust/src/global.rs b/rust/src/global.rs index c32c2f8..51dc8fb 100644 --- a/rust/src/global.rs +++ b/rust/src/global.rs @@ -71,6 +71,7 @@ pub fn reorder_tables(root_ast: &SyntaxNode, tables: &mut Tables) { #[cfg(test)] mod tests { use indoc::indoc; + use pretty_assertions::assert_eq; use rstest::rstest; use taplo::formatter::{format_syntax, Options}; use taplo::parser::parse; diff --git a/rust/src/helpers/array.rs b/rust/src/helpers/array.rs index 3cf4814..a5bf61f 100644 --- a/rust/src/helpers/array.rs +++ b/rust/src/helpers/array.rs @@ -5,9 +5,11 @@ use lexical_sort::{natural_lexical_cmp, StringSort}; use taplo::syntax::SyntaxKind::{ARRAY, COMMA, NEWLINE, STRING, VALUE, WHITESPACE}; use taplo::syntax::{SyntaxElement, SyntaxKind, SyntaxNode}; -use crate::helpers::create::{make_comma, make_newline}; +use crate::helpers::create::make_comma; use crate::helpers::string::{load_text, update_content}; +use super::create::make_newline; + pub fn transform(node: &SyntaxNode, transform: &F) where F: Fn(&str) -> String, @@ -37,6 +39,10 @@ where .filter(|x| *x == COMMA || *x == VALUE) .last() == Some(COMMA); + let multiline = array_node + .children_with_tokens() + .find(|e| e.kind() == NEWLINE) + .is_some(); let mut value_set = Vec::>::new(); let entry_set = RefCell::new(Vec::::new()); let mut key_to_pos = HashMap::::new(); @@ -67,7 +73,9 @@ where match &entry.kind() { SyntaxKind::BRACKET_START => { entries.push(entry); - entries.push(make_newline()); + if multiline { + entries.push(make_newline()); + } previous_is_bracket_open = true; } SyntaxKind::BRACKET_END => { @@ -80,7 +88,9 @@ where } VALUE => { if has_value { - entry_set.borrow_mut().push(make_newline()); + if multiline { + entry_set.borrow_mut().push(make_newline()); + } add_to_value_set(entry_value.clone()); } has_value = true; @@ -117,7 +127,7 @@ where let mut order: Vec = key_to_pos.clone().into_keys().collect(); order.string_sort_unstable(natural_lexical_cmp); - let end = entries.split_off(2); + let end = entries.split_off(if multiline { 2 } else { 1 }); for key in order { entries.extend(value_set[key_to_pos[&key]].clone()); } @@ -140,6 +150,7 @@ where #[cfg(test)] mod tests { use indoc::indoc; + use pretty_assertions::assert_eq; use rstest::rstest; use taplo::formatter::{format_syntax, Options}; use taplo::parser::parse; diff --git a/rust/src/helpers/pep508.rs b/rust/src/helpers/pep508.rs index 4710001..460a1a3 100644 --- a/rust/src/helpers/pep508.rs +++ b/rust/src/helpers/pep508.rs @@ -89,6 +89,7 @@ pub fn get_canonic_requirement_name(value: &str) -> String { #[cfg(test)] mod tests { + use pretty_assertions::assert_eq; use rstest::rstest; use crate::helpers::pep508::{format_requirement, get_canonic_requirement_name}; diff --git a/rust/src/main.rs b/rust/src/main.rs index 1920e95..f597c98 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -63,16 +63,16 @@ pub fn format_toml(content: &str, opt: &Settings) -> String { reorder_tables(&root_ast, &mut tables); let options = Options { - align_entries: false, // do not align by = - align_comments: true, // align inline comments - align_single_comments: true, // align comments after entries - array_trailing_comma: true, // ensure arrays finish with trailing comma - array_auto_expand: true, // arrays go to multi line for easier diffs - array_auto_collapse: false, // do not collapse for easier diffs - compact_arrays: false, // do not compact for easier diffs - compact_inline_tables: false, // do not compact for easier diffs - compact_entries: false, // do not compact for easier diffs - column_width: opt.column_width, // always expand arrays per https://github.com/tamasfe/taplo/issues/390 + align_entries: false, // do not align by = + align_comments: true, // align inline comments + align_single_comments: true, // align comments after entries + array_trailing_comma: true, // ensure arrays finish with trailing comma + array_auto_expand: true, // arrays go to multi line when too long + array_auto_collapse: false, // do not collapse for easier diffs + compact_arrays: false, // leave whitespace + compact_inline_tables: false, // leave whitespace + compact_entries: false, // leave whitespace + column_width: opt.column_width, indent_tables: false, indent_entries: false, inline_table_expand: true, @@ -104,6 +104,7 @@ mod tests { use std::path::{Path, PathBuf}; use indoc::indoc; + use pretty_assertions::assert_eq; use rstest::{fixture, rstest}; use crate::{format_toml, Settings}; @@ -316,4 +317,23 @@ mod tests { let second = format_toml(got.as_str(), &settings); assert_eq!(second, got); } + + /// Test that the column width is respected, + /// and that arrays are neither exploded nor collapsed without reason + #[rstest] + fn test_column_width(data: PathBuf) { + let start = read_to_string(data.join("column-width.start.toml")).unwrap(); + let settings = Settings { + column_width: 80, + indent: 4, + keep_full_version: false, + max_supported_python: (3, 12), + min_supported_python: (3, 12), + }; + let got = format_toml(start.as_str(), &settings); + let expected = read_to_string(data.join("column-width.expected.toml")).unwrap(); + assert_eq!(got, expected); + let second = format_toml(got.as_str(), &settings); + assert_eq!(second, got); + } } diff --git a/rust/src/project.rs b/rust/src/project.rs index 44c4ac0..2d058bc 100644 --- a/rust/src/project.rs +++ b/rust/src/project.rs @@ -371,6 +371,7 @@ mod tests { use taplo::formatter::{format_syntax, Options}; use taplo::parser::parse; use taplo::syntax::SyntaxElement; + use pretty_assertions::assert_eq; use crate::helpers::table::Tables; use crate::project::fix; diff --git a/rust/src/ruff.rs b/rust/src/ruff.rs index 850c850..93bca4b 100644 --- a/rust/src/ruff.rs +++ b/rust/src/ruff.rs @@ -197,6 +197,7 @@ mod tests { use taplo::formatter::{format_syntax, Options}; use taplo::parser::parse; use taplo::syntax::SyntaxElement; + use pretty_assertions::assert_eq; use crate::helpers::table::Tables; use crate::ruff::fix; diff --git a/tests/test_main.py b/tests/test_main.py index 468bdee..7fa16a3 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -8,15 +8,11 @@ def test_format_toml() -> None: txt = """ [project] - keywords = [ - "A", - ] + keywords = [ "A" ] classifiers = [ "Programming Language :: Python :: 3 :: Only", ] - dynamic = [ - "B", - ] + dynamic = [ "B" ] dependencies = [ "requests>=2.0", ] @@ -33,17 +29,13 @@ def test_format_toml() -> None: expected = """\ [project] - keywords = [ - "A", - ] + keywords = [ "A" ] classifiers = [ "Programming Language :: Python :: 3 :: Only", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", ] - dynamic = [ - "B", - ] + dynamic = [ "B" ] dependencies = [ "requests>=2.0", ] From a60401d8d430ccb48403aa3dd524e57c319c39be Mon Sep 17 00:00:00 2001 From: "Philipp A." Date: Wed, 31 Jul 2024 14:07:02 +0200 Subject: [PATCH 2/7] cosmetic fixes --- Cargo.toml | 2 +- rust/src/helpers/array.rs | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 55236aa..04b6172 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,4 +32,4 @@ nursery = "warn" [dev-dependencies] rstest = { version = "0.19.0" } # parametrized tests indoc = { version = "2.0.5" } # dedented test cases for literal strings -pretty_assertions = "1.4.0" +pretty_assertions = "1.4.0" # diffs in test assertions diff --git a/rust/src/helpers/array.rs b/rust/src/helpers/array.rs index a5bf61f..e1b2d9e 100644 --- a/rust/src/helpers/array.rs +++ b/rust/src/helpers/array.rs @@ -5,11 +5,9 @@ use lexical_sort::{natural_lexical_cmp, StringSort}; use taplo::syntax::SyntaxKind::{ARRAY, COMMA, NEWLINE, STRING, VALUE, WHITESPACE}; use taplo::syntax::{SyntaxElement, SyntaxKind, SyntaxNode}; -use crate::helpers::create::make_comma; +use crate::helpers::create::{make_comma, make_newline}; use crate::helpers::string::{load_text, update_content}; -use super::create::make_newline; - pub fn transform(node: &SyntaxNode, transform: &F) where F: Fn(&str) -> String, From 840f9f03bb0b7de8214dad452adc2e432c2c1d3b Mon Sep 17 00:00:00 2001 From: "Philipp A." Date: Wed, 31 Jul 2024 20:02:17 +0200 Subject: [PATCH 3/7] address comments --- .vscode/settings.json | 6 +++++ Cargo.lock | 23 ----------------- Cargo.toml | 1 - rust/src/build_system.rs | 1 - rust/src/data/column-width.expected.toml | 13 ---------- rust/src/data/column-width.start.toml | 9 ------- rust/src/global.rs | 1 - rust/src/helpers/array.rs | 1 - rust/src/helpers/pep508.rs | 1 - rust/src/main.rs | 33 ++++++++++++++++++++---- rust/src/project.rs | 5 ++-- rust/src/ruff.rs | 1 - 12 files changed, 37 insertions(+), 58 deletions(-) create mode 100644 .vscode/settings.json delete mode 100644 rust/src/data/column-width.expected.toml delete mode 100644 rust/src/data/column-width.start.toml diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0196fd7 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "[rust]": { + "editor.defaultFormatter": "rust-lang.rust-analyzer", + "editor.formatOnSave": true + } +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index b8de5d1..63ca0bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -96,12 +96,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "diff" -version = "0.1.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" - [[package]] name = "either" version = "1.11.0" @@ -452,16 +446,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" -[[package]] -name = "pretty_assertions" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" -dependencies = [ - "diff", - "yansi", -] - [[package]] name = "proc-macro2" version = "1.0.81" @@ -542,7 +526,6 @@ dependencies = [ "lexical-sort", "pep440_rs", "pep508_rs", - "pretty_assertions", "pyo3", "regex", "rstest", @@ -1006,12 +989,6 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" -[[package]] -name = "yansi" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" - [[package]] name = "zerocopy" version = "0.7.32" diff --git a/Cargo.toml b/Cargo.toml index 04b6172..eafb65b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,4 +32,3 @@ nursery = "warn" [dev-dependencies] rstest = { version = "0.19.0" } # parametrized tests indoc = { version = "2.0.5" } # dedented test cases for literal strings -pretty_assertions = "1.4.0" # diffs in test assertions diff --git a/rust/src/build_system.rs b/rust/src/build_system.rs index a981dd4..88c160c 100644 --- a/rust/src/build_system.rs +++ b/rust/src/build_system.rs @@ -24,7 +24,6 @@ pub fn fix(tables: &mut Tables, keep_full_version: bool) { #[cfg(test)] mod tests { use indoc::indoc; - use pretty_assertions::assert_eq; use rstest::rstest; use taplo::formatter::{format_syntax, Options}; use taplo::parser::parse; diff --git a/rust/src/data/column-width.expected.toml b/rust/src/data/column-width.expected.toml deleted file mode 100644 index aac785d..0000000 --- a/rust/src/data/column-width.expected.toml +++ /dev/null @@ -1,13 +0,0 @@ -[build-system] -build-backend = "backend" -requires = [ "c>=1.5", "d==2" ] - -[project] -name = "beta" -classifiers = [ - "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.12", -] -dependencies = [ - "e>=1.5", -] diff --git a/rust/src/data/column-width.start.toml b/rust/src/data/column-width.start.toml deleted file mode 100644 index e38ce8b..0000000 --- a/rust/src/data/column-width.start.toml +++ /dev/null @@ -1,9 +0,0 @@ -[build-system] -build-backend = "backend" -requires = ["c>=1.5", "d == 2" ] - -[project] -name = "beta" -dependencies = [ - "e>=1.5", -] diff --git a/rust/src/global.rs b/rust/src/global.rs index 51dc8fb..c32c2f8 100644 --- a/rust/src/global.rs +++ b/rust/src/global.rs @@ -71,7 +71,6 @@ pub fn reorder_tables(root_ast: &SyntaxNode, tables: &mut Tables) { #[cfg(test)] mod tests { use indoc::indoc; - use pretty_assertions::assert_eq; use rstest::rstest; use taplo::formatter::{format_syntax, Options}; use taplo::parser::parse; diff --git a/rust/src/helpers/array.rs b/rust/src/helpers/array.rs index e1b2d9e..bc96672 100644 --- a/rust/src/helpers/array.rs +++ b/rust/src/helpers/array.rs @@ -148,7 +148,6 @@ where #[cfg(test)] mod tests { use indoc::indoc; - use pretty_assertions::assert_eq; use rstest::rstest; use taplo::formatter::{format_syntax, Options}; use taplo::parser::parse; diff --git a/rust/src/helpers/pep508.rs b/rust/src/helpers/pep508.rs index 460a1a3..4710001 100644 --- a/rust/src/helpers/pep508.rs +++ b/rust/src/helpers/pep508.rs @@ -89,7 +89,6 @@ pub fn get_canonic_requirement_name(value: &str) -> String { #[cfg(test)] mod tests { - use pretty_assertions::assert_eq; use rstest::rstest; use crate::helpers::pep508::{format_requirement, get_canonic_requirement_name}; diff --git a/rust/src/main.rs b/rust/src/main.rs index f597c98..93d7e3a 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -104,7 +104,6 @@ mod tests { use std::path::{Path, PathBuf}; use indoc::indoc; - use pretty_assertions::assert_eq; use rstest::{fixture, rstest}; use crate::{format_toml, Settings}; @@ -321,8 +320,18 @@ mod tests { /// Test that the column width is respected, /// and that arrays are neither exploded nor collapsed without reason #[rstest] - fn test_column_width(data: PathBuf) { - let start = read_to_string(data.join("column-width.start.toml")).unwrap(); + fn test_column_width() { + let start = indoc! {r#" + [build-system] + build-backend = "backend" + requires = ["c>=1.5", "d == 2" ] + + [project] + name = "beta" + dependencies = [ + "e>=1.5", + ] + "#}; let settings = Settings { column_width: 80, indent: 4, @@ -330,8 +339,22 @@ mod tests { max_supported_python: (3, 12), min_supported_python: (3, 12), }; - let got = format_toml(start.as_str(), &settings); - let expected = read_to_string(data.join("column-width.expected.toml")).unwrap(); + let got = format_toml(start, &settings); + let expected = indoc! {r#" + [build-system] + build-backend = "backend" + requires = [ "c>=1.5", "d==2" ] + + [project] + name = "beta" + classifiers = [ + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.12", + ] + dependencies = [ + "e>=1.5", + ] + "#}; assert_eq!(got, expected); let second = format_toml(got.as_str(), &settings); assert_eq!(second, got); diff --git a/rust/src/project.rs b/rust/src/project.rs index 2d058bc..b5015dc 100644 --- a/rust/src/project.rs +++ b/rust/src/project.rs @@ -55,7 +55,9 @@ pub fn fix( } "dependencies" | "optional-dependencies" => { 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)); + sort(entry, |e| { + get_canonic_requirement_name(e).to_lowercase() + " " + &format_requirement(e, keep_full_version) + }); } "dynamic" | "keywords" => { transform(entry, &|s| String::from(s)); @@ -371,7 +373,6 @@ mod tests { use taplo::formatter::{format_syntax, Options}; use taplo::parser::parse; use taplo::syntax::SyntaxElement; - use pretty_assertions::assert_eq; use crate::helpers::table::Tables; use crate::project::fix; diff --git a/rust/src/ruff.rs b/rust/src/ruff.rs index 93bca4b..850c850 100644 --- a/rust/src/ruff.rs +++ b/rust/src/ruff.rs @@ -197,7 +197,6 @@ mod tests { use taplo::formatter::{format_syntax, Options}; use taplo::parser::parse; use taplo::syntax::SyntaxElement; - use pretty_assertions::assert_eq; use crate::helpers::table::Tables; use crate::ruff::fix; From 9fe34924b16e2c3056a5d372f27ba3ee733b262d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 31 Jul 2024 18:03:56 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .vscode/settings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 0196fd7..428398d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,4 +3,4 @@ "editor.defaultFormatter": "rust-lang.rust-analyzer", "editor.formatOnSave": true } -} \ No newline at end of file +} From 7d34c2481fd30a6b9293ae18a4bee0cd308533f2 Mon Sep 17 00:00:00 2001 From: "Philipp A." Date: Wed, 31 Jul 2024 20:12:09 +0200 Subject: [PATCH 5/7] parametrize --- .vscode/settings.json | 9 ++++- tests/test_main.py | 93 ++++++++++++++++++++++++++++++------------- 2 files changed, 72 insertions(+), 30 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 428398d..fbf62a9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,11 @@ { "[rust]": { "editor.defaultFormatter": "rust-lang.rust-analyzer", - "editor.formatOnSave": true - } + "editor.formatOnSave": true, + }, + "[python]": { + "editor.defaultFormatter": "charliermarsh.ruff", + "editor.formatOnSave": true, + }, + "python.testing.pytestEnabled": true } diff --git a/tests/test_main.py b/tests/test_main.py index 7fa16a3..8216701 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -2,22 +2,73 @@ from textwrap import dedent -from pyproject_fmt_rust import Settings, format_toml +import pytest +from pyproject_fmt_rust import Settings, format_toml -def test_format_toml() -> None: - txt = """ - [project] - keywords = [ "A" ] - classifiers = [ - "Programming Language :: Python :: 3 :: Only", - ] - dynamic = [ "B" ] - dependencies = [ - "requests>=2.0", - ] - """ +@pytest.mark.parametrize( + ("start", "expected"), + [ + pytest.param( + """ + [project] + keywords = [ + "A", + ] + classifiers = [ + "Programming Language :: Python :: 3 :: Only", + ] + dynamic = [ + "B", + ] + dependencies = [ + "requests>=2.0", + ] + """, + """\ + [project] + keywords = [ + "A", + ] + classifiers = [ + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + ] + dynamic = [ + "B", + ] + dependencies = [ + "requests>=2.0", + ] + """, + id="expanded", + ), + pytest.param( + """ + [project] + keywords = ["A"] + classifiers = ["Programming Language :: Python :: 3 :: Only"] + dynamic = ["B"] + dependencies = ["requests>=2.0"] + """, + """\ + [project] + keywords = [ "A" ] + classifiers = [ + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + ] + dynamic = [ "B" ] + dependencies = [ "requests>=2.0" ] + """, + id="collapsed", + ), + ], +) +def test_format_toml(start: str, expected: str) -> None: settings = Settings( column_width=120, indent=4, @@ -25,19 +76,5 @@ def test_format_toml() -> None: min_supported_python=(3, 7), max_supported_python=(3, 8), ) - res = format_toml(dedent(txt), settings) - - expected = """\ - [project] - keywords = [ "A" ] - classifiers = [ - "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", - ] - dynamic = [ "B" ] - dependencies = [ - "requests>=2.0", - ] - """ + res = format_toml(dedent(start), settings) assert res == dedent(expected) From 5a16235e78dc25414cdb119eb10010654a443502 Mon Sep 17 00:00:00 2001 From: "Philipp A." Date: Wed, 31 Jul 2024 20:12:41 +0200 Subject: [PATCH 6/7] =?UTF-8?q?don=E2=80=99t=20commit=20settings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index fbf62a9..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "[rust]": { - "editor.defaultFormatter": "rust-lang.rust-analyzer", - "editor.formatOnSave": true, - }, - "[python]": { - "editor.defaultFormatter": "charliermarsh.ruff", - "editor.formatOnSave": true, - }, - "python.testing.pytestEnabled": true -} From 6568f028282b83ce3bbb91efbd1611c6bb4579c1 Mon Sep 17 00:00:00 2001 From: "Philipp A." Date: Wed, 31 Jul 2024 20:14:03 +0200 Subject: [PATCH 7/7] Discard changes to rust/src/project.rs --- rust/src/project.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/rust/src/project.rs b/rust/src/project.rs index b5015dc..44c4ac0 100644 --- a/rust/src/project.rs +++ b/rust/src/project.rs @@ -55,9 +55,7 @@ pub fn fix( } "dependencies" | "optional-dependencies" => { 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) - }); + sort(entry, |e| get_canonic_requirement_name(e).to_lowercase() + " " + &format_requirement(e, keep_full_version)); } "dynamic" | "keywords" => { transform(entry, &|s| String::from(s));