From 19f7ed0d9d98a379f8fb4f75abf89c6a1d41837e Mon Sep 17 00:00:00 2001 From: JosephDenman Date: Wed, 25 Mar 2026 13:21:16 -0500 Subject: [PATCH] Deduplicate file arguments in format and fixup commands Signed-off-by: JosephDenman --- .../fixup/std_fixup_verbose_one_file.txt | 1 + tools/compact/src/bin/compact.rs | 24 ++++++---- tools/compact/tests/test_scenarios_four.rs | 45 +++++++++++++++++-- 3 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 tools/compact/output/fixup/std_fixup_verbose_one_file.txt diff --git a/tools/compact/output/fixup/std_fixup_verbose_one_file.txt b/tools/compact/output/fixup/std_fixup_verbose_one_file.txt new file mode 100644 index 00000000..449a8b63 --- /dev/null +++ b/tools/compact/output/fixup/std_fixup_verbose_one_file.txt @@ -0,0 +1 @@ +[CONTRACT_PATH]: [STATUS] diff --git a/tools/compact/src/bin/compact.rs b/tools/compact/src/bin/compact.rs index 9f0e6a56..41c1a28b 100644 --- a/tools/compact/src/bin/compact.rs +++ b/tools/compact/src/bin/compact.rs @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::{path::PathBuf, str::FromStr, sync::Arc}; +use std::{collections::HashSet, path::PathBuf, str::FromStr, sync::Arc}; use anyhow::{Context as _, Result, anyhow, bail}; use axoupdater::AxoUpdater; @@ -307,17 +307,20 @@ async fn format(cfg: &CommandLineArguments, command: &FormatCommand) -> Result<( let bin = Arc::new(bin); let check_mode = command.check; + let mut seen = HashSet::new(); for file_path in &command.files { let path = PathBuf::from_str(file_path).unwrap(); if path.is_dir() { for path in formatter::compact_files_excluding_gitignore(&path) { - let bin = Arc::clone(&bin); + if seen.insert(path.clone()) { + let bin = Arc::clone(&bin); - join_set.spawn(async move { format_file(&bin, check_mode, path).await }); + join_set.spawn(async move { format_file(&bin, check_mode, path).await }); + } } - } else { + } else if seen.insert(path.clone()) { let bin = Arc::clone(&bin); join_set.spawn(async move { format_file(&bin, check_mode, path).await }); @@ -416,18 +419,21 @@ async fn fixup(cfg: &CommandLineArguments, command: &FixupCommand) -> Result<()> let check_mode = command.check; let update_uint_ranges = command.update_uint_ranges; let vscode = command.vscode; + let mut seen = HashSet::new(); for file_path in &command.files { let path = PathBuf::from_str(file_path).unwrap(); if path.is_dir() { for path in fixup::compact_files_excluding_gitignore(&path) { - let bin = Arc::clone(&bin); - join_set.spawn(async move { - fixup_file(&bin, check_mode, path, update_uint_ranges, vscode).await - }); + if seen.insert(path.clone()) { + let bin = Arc::clone(&bin); + join_set.spawn(async move { + fixup_file(&bin, check_mode, path, update_uint_ranges, vscode).await + }); + } } - } else { + } else if seen.insert(path.clone()) { let bin = Arc::clone(&bin); join_set.spawn(async move { fixup_file(&bin, check_mode, path, update_uint_ranges, vscode).await diff --git a/tools/compact/tests/test_scenarios_four.rs b/tools/compact/tests/test_scenarios_four.rs index 3a9e5b6c..1f1c5046 100644 --- a/tools/compact/tests/test_scenarios_four.rs +++ b/tools/compact/tests/test_scenarios_four.rs @@ -232,13 +232,11 @@ fn test_sc34_update_latest_format_verbose_same_file_twice() { "--verbose", ], None, - Some("./output/format/std_format_verbose_two_files.txt"), + Some("./output/format/std_format_verbose_one_file.txt"), None, &[ ("[CONTRACT_PATH]", formated_contract_as_string), ("[STATUS]", "formatted"), - ("[CONTRACT_PATH_TWO]", formated_contract_as_string), - ("[STATUS_TWO]", "formatted"), ], Some(0), ); @@ -552,3 +550,44 @@ fn test_sc39_update_latest_format_check_unformatted_two_files() { output_contract_two_as_string, ); } + +#[test] +fn test_sc40_update_latest_fixup_verbose_same_file_twice() { + let temp_dir = tempfile::tempdir().unwrap(); + let temp_path = temp_dir.path(); + + run_command( + &["--directory", &format!("{}", temp_path.display()), "update"], + None, + Some("./output/update/std_default.txt"), + None, + &[ + ("[LATEST_COMPACTC_VERSION]", LATEST_COMPACTC_VERSION), + ("[SYSTEM_VERSION]", get_version()), + ], + None, + ); + + copy_file_to_dir("./contract/formatter/input/example_1.compact", temp_path).unwrap(); + let contract = temp_path.join("example_1.compact"); + let contract_as_string = contract.to_str().unwrap(); + + run_command_sorted( + &[ + "--directory", + &format!("{}", temp_path.display()), + "fixup", + contract_as_string, + contract_as_string, + "--verbose", + ], + None, + Some("./output/fixup/std_fixup_verbose_one_file.txt"), + None, + &[ + ("[CONTRACT_PATH]", contract_as_string), + ("[STATUS]", "unchanged"), + ], + Some(0), + ); +}