From 298ad61a4618e5318ce9826d13b977e2b1494ee5 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Fri, 9 Aug 2024 11:53:56 +0200 Subject: [PATCH] update strategies --- src/strategies/bazel.rs | 13 +++-- src/strategies/cargo_toml.rs | 15 ++++-- src/strategies/generic.rs | 15 ++++-- src/strategies/gitignore.rs | 13 +++-- tests/bazel.rs | 100 +++++++++++++++++++++++++++++++++++ tests/cargo_toml.rs | 81 ++++++++++++++++++++++++++++ tests/codeowners.rs | 46 ++++++++++++++++ tests/gitignore.rs | 70 ++++++++++++++++++++++++ 8 files changed, 335 insertions(+), 18 deletions(-) diff --git a/src/strategies/bazel.rs b/src/strategies/bazel.rs index 5783544..d1fd29a 100644 --- a/src/strategies/bazel.rs +++ b/src/strategies/bazel.rs @@ -11,6 +11,7 @@ pub(crate) fn process(lines: Vec) -> io::Result> { let mut block = Vec::new(); let mut is_scope = false; let mut is_sorting_block = false; + let mut is_ignore_block_prev_line = false; for line in lines { // Trim the input line @@ -24,13 +25,17 @@ pub(crate) fn process(lines: Vec) -> io::Result> { output_lines.push(line); } else if is_scope { if re.is_match(&line) { + if let Some(prev_line) = output_lines.last() { + is_ignore_block_prev_line = is_ignore_block(&[prev_line.clone()]); + } is_sorting_block = true; output_lines.push(line); } else if is_sorting_block && (line_without_comment.contains(']') || line.trim().is_empty()) { + block = sort(block, is_ignore_block_prev_line); + is_ignore_block_prev_line = false; is_sorting_block = false; - block = sort(block); output_lines.append(&mut block); output_lines.push(line); } else if is_sorting_block { @@ -44,7 +49,7 @@ pub(crate) fn process(lines: Vec) -> io::Result> { } if is_sorting_block { - block = sort(block); + block = sort(block, is_ignore_block_prev_line); output_lines.append(&mut block); } @@ -59,8 +64,8 @@ struct Item { } /// Sorts a block of lines, keeping associated comments with their items. -fn sort(block: Vec) -> Vec { - if is_ignore_block(&block) { +fn sort(block: Vec, is_ignore_block_prev_line: bool) -> Vec { + if is_ignore_block_prev_line || is_ignore_block(&block) { return block; } let n = block.len(); diff --git a/src/strategies/cargo_toml.rs b/src/strategies/cargo_toml.rs index 24093a6..d2be23a 100644 --- a/src/strategies/cargo_toml.rs +++ b/src/strategies/cargo_toml.rs @@ -3,22 +3,27 @@ use std::io; use crate::is_ignore_block; pub(crate) fn process(lines: Vec) -> io::Result> { - let mut output_lines = Vec::new(); + let mut output_lines: Vec = Vec::new(); let mut block = Vec::new(); let mut is_sorting_block = false; + let mut is_ignore_block_prev_line = false; for line in lines { let trimmed = line.trim(); let line_without_comment = trimmed.split('#').next().unwrap_or("").trim(); if is_block_start(&line) { + if let Some(prev_line) = output_lines.last() { + is_ignore_block_prev_line = is_ignore_block(&[prev_line.clone()]); + } is_sorting_block = true; output_lines.push(line); } else if is_sorting_block && (line.trim().is_empty() || line_without_comment.starts_with('[')) { + block = sort(block, is_ignore_block_prev_line); + is_ignore_block_prev_line = false; is_sorting_block = false; - block = sort(block); output_lines.append(&mut block); output_lines.push(line); } else if is_sorting_block { @@ -29,7 +34,7 @@ pub(crate) fn process(lines: Vec) -> io::Result> { } if is_sorting_block { - block = sort(block); + block = sort(block, is_ignore_block_prev_line); output_lines.append(&mut block); } @@ -64,8 +69,8 @@ struct Item { } /// Sorts a block of lines, keeping associated comments with their items. -fn sort(block: Vec) -> Vec { - if is_ignore_block(&block) { +fn sort(block: Vec, is_ignore_block_prev_line: bool) -> Vec { + if is_ignore_block_prev_line || is_ignore_block(&block) { return block; } let n = block.len(); diff --git a/src/strategies/generic.rs b/src/strategies/generic.rs index d4de28a..58e62d4 100644 --- a/src/strategies/generic.rs +++ b/src/strategies/generic.rs @@ -6,17 +6,22 @@ use crate::is_ignore_block; pub(crate) fn process(lines: Vec) -> io::Result> { let re = Regex::new(r"^\s*#\s*Keep\s*sorted\.\s*$") .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; - let mut output_lines = Vec::new(); + let mut output_lines: Vec = Vec::new(); let mut block = Vec::new(); let mut is_sorting_block = false; + let mut is_ignore_block_prev_line = false; for line in lines { if re.is_match(&line) { + if let Some(prev_line) = output_lines.last() { + is_ignore_block_prev_line = is_ignore_block(&[prev_line.clone()]); + } is_sorting_block = true; output_lines.push(line); } else if is_sorting_block && line.trim().is_empty() { + block = sort(block, is_ignore_block_prev_line); + is_ignore_block_prev_line = false; is_sorting_block = false; - block = sort(block); output_lines.append(&mut block); output_lines.push(line); } else if is_sorting_block { @@ -27,7 +32,7 @@ pub(crate) fn process(lines: Vec) -> io::Result> { } if is_sorting_block { - block = sort(block); + block = sort(block, is_ignore_block_prev_line); output_lines.append(&mut block); } @@ -41,8 +46,8 @@ struct Item { } /// Sorts a block of lines, keeping associated comments with their items. -fn sort(block: Vec) -> Vec { - if is_ignore_block(&block) { +fn sort(block: Vec, is_ignore_block_prev_line: bool) -> Vec { + if is_ignore_block_prev_line || is_ignore_block(&block) { return block; } let n = block.len(); diff --git a/src/strategies/gitignore.rs b/src/strategies/gitignore.rs index e8f5c7f..0c62cb1 100644 --- a/src/strategies/gitignore.rs +++ b/src/strategies/gitignore.rs @@ -6,6 +6,7 @@ pub(crate) fn process(lines: Vec) -> io::Result> { let mut output_lines = Vec::new(); let mut block = Vec::new(); let mut is_sorting_block = false; + let mut is_ignore_block_prev_line = false; for line in lines { if !line.trim().is_empty() { @@ -13,12 +14,16 @@ pub(crate) fn process(lines: Vec) -> io::Result> { // Skip opening comment. output_lines.push(line); } else { + if let Some(prev_line) = output_lines.last() { + is_ignore_block_prev_line = is_ignore_block(&[prev_line.clone()]); + } is_sorting_block = true; block.push(line); } } else if is_sorting_block { + block = sort(block, is_ignore_block_prev_line); + is_ignore_block_prev_line = false; is_sorting_block = false; - block = sort(block); output_lines.append(&mut block); output_lines.push(line); } else { @@ -27,7 +32,7 @@ pub(crate) fn process(lines: Vec) -> io::Result> { } if is_sorting_block { - block = sort(block); + block = sort(block, is_ignore_block_prev_line); output_lines.append(&mut block); } @@ -41,8 +46,8 @@ struct Item { } /// Sorts a block of lines, keeping associated comments with their items. -fn sort(block: Vec) -> Vec { - if is_ignore_block(&block) { +fn sort(block: Vec, is_ignore_block_prev_line: bool) -> Vec { + if is_ignore_block_prev_line || is_ignore_block(&block) { return block; } let n = block.len(); diff --git a/tests/bazel.rs b/tests/bazel.rs index bd09216..3b2d307 100644 --- a/tests/bazel.rs +++ b/tests/bazel.rs @@ -219,6 +219,106 @@ block_2 = [ ); } +#[test] +fn bazel_ignore_file() { + test_inner!( + Bazel, + r#" +# keepsorted:ignore-file +block_1 = [ + # Keep sorted. + "b", + "a", +], +block_2 = [ + # Keep sorted. + "y", + "x", +], + "#, + r#" +# keepsorted:ignore-file +block_1 = [ + # Keep sorted. + "b", + "a", +], +block_2 = [ + # Keep sorted. + "y", + "x", +], + "# + ); +} + +#[test] +fn bazel_ignore_block_inside() { + test_inner!( + Bazel, + r#" +block_1 = [ + # Keep sorted. + # keepsorted:ignore-block + "b", + "a", +], +block_2 = [ + # Keep sorted. + "y", + "x", +], + "#, + r#" +block_1 = [ + # Keep sorted. + # keepsorted:ignore-block + "b", + "a", +], +block_2 = [ + # Keep sorted. + "x", + "y", +], + "# + ); +} + +#[test] +fn bazel_ignore_block_before() { + test_inner!( + Bazel, + r#" +block_1 = [ + # keepsorted:ignore-block + # Keep sorted. + "b", + "a", +], +block_2 = [ + # Keep sorted. + "y", + "x", +], + "#, + r#" +block_1 = [ + # keepsorted:ignore-block + # Keep sorted. + "b", + "a", +], +block_2 = [ + # Keep sorted. + "x", + "y", +], + "# + ); +} + + #[test] fn bazel_blocks_with_select() { test_inner!( diff --git a/tests/cargo_toml.rs b/tests/cargo_toml.rs index 08bf5a2..98c2915 100644 --- a/tests/cargo_toml.rs +++ b/tests/cargo_toml.rs @@ -131,6 +131,87 @@ y = "4" ); } +#[test] +fn cargo_toml_ignore_file() { + test_inner!( + CargoToml, + r#" +# keepsorted:ignore-file +[dependencies] +b = "2" +a = "1" + +[dev-dependencies] +y = "4" +x = "3" + "#, + r#" +# keepsorted:ignore-file +[dependencies] +b = "2" +a = "1" + +[dev-dependencies] +y = "4" +x = "3" + "# + ); +} + +#[test] +fn cargo_toml_ignore_block_inside() { + test_inner!( + CargoToml, + r#" +[dependencies] +# keepsorted:ignore-block +b = "2" +a = "1" + +[dev-dependencies] +y = "4" +x = "3" + "#, + r#" +[dependencies] +# keepsorted:ignore-block +b = "2" +a = "1" + +[dev-dependencies] +x = "3" +y = "4" + "# + ); +} + +#[test] +fn cargo_toml_ignore_block_before() { + test_inner!( + CargoToml, + r#" +# keepsorted:ignore-block +[dependencies] +b = "2" +a = "1" + +[dev-dependencies] +y = "4" +x = "3" + "#, + r#" +# keepsorted:ignore-block +[dependencies] +b = "2" +a = "1" + +[dev-dependencies] +x = "3" +y = "4" + "# + ); +} + #[test] fn cargo_toml_nested_list() { test_inner!( diff --git a/tests/codeowners.rs b/tests/codeowners.rs index 1438d07..b5476b5 100644 --- a/tests/codeowners.rs +++ b/tests/codeowners.rs @@ -43,6 +43,52 @@ fn codeowners_two_blocks() { ); } +#[test] +fn codeowners_ignore_file() { + test_inner!( + Gitignore, + r#" +# keepsorted:ignore-file +/.d/ @company/teams/a +/.c/ @company/teams/b + +/.b/workflows @company/teams/c @company/teams/d +/.a/CODEOWNERS @company/teams/e + "#, + r#" +# keepsorted:ignore-file +/.d/ @company/teams/a +/.c/ @company/teams/b + +/.b/workflows @company/teams/c @company/teams/d +/.a/CODEOWNERS @company/teams/e + "# + ); +} + +#[test] +fn codeowners_ignore_block() { + test_inner!( + Gitignore, + r#" +# keepsorted:ignore-block +/.d/ @company/teams/a +/.c/ @company/teams/b + +/.b/workflows @company/teams/c @company/teams/d +/.a/CODEOWNERS @company/teams/e + "#, + r#" +# keepsorted:ignore-block +/.d/ @company/teams/a +/.c/ @company/teams/b + +/.a/CODEOWNERS @company/teams/e +/.b/workflows @company/teams/c @company/teams/d + "# + ); +} + #[test] fn codeowners_1() { test_inner!( diff --git a/tests/gitignore.rs b/tests/gitignore.rs index 80e032e..42cffc1 100644 --- a/tests/gitignore.rs +++ b/tests/gitignore.rs @@ -35,3 +35,73 @@ fn gitignore_1() { "# ); } + +#[test] +fn gitignore_ignore_file() { + test_inner!( + Gitignore, + r#" +# keepsorted:ignore-file + +/b +/a + +# [Bazel] +/b +/a + +# [Rust] +/b +/a + "#, + r#" +# keepsorted:ignore-file + +/b +/a + +# [Bazel] +/b +/a + +# [Rust] +/b +/a + "# + ); +} + +#[test] +fn gitignore_ignore_block_after_header_comment() { + test_inner!( + Gitignore, + r#" + +/b +/a + +# [Bazel] +# keepsorted:ignore-block +/b +/a + +# [Rust] +/b +/a + "#, + r#" + +/a +/b + +# [Bazel] +# keepsorted:ignore-block +/b +/a + +# [Rust] +/a +/b + "# + ); +} \ No newline at end of file