|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use bstr::ByteSlice; |
| 5 | +use clap::Parser; |
| 6 | + |
| 7 | +use crate::hook::Hook; |
| 8 | +use crate::hooks::run_concurrent_file_checks; |
| 9 | +use crate::run::CONCURRENCY; |
| 10 | + |
| 11 | +#[derive(Parser)] |
| 12 | +#[command(disable_help_subcommand = true)] |
| 13 | +#[command(disable_version_flag = true)] |
| 14 | +#[command(disable_help_flag = true)] |
| 15 | +struct Args { |
| 16 | + #[arg(long, conflicts_with = "unique")] |
| 17 | + ignore_case: bool, |
| 18 | + #[arg(long, conflicts_with = "ignore_case")] |
| 19 | + unique: bool, |
| 20 | +} |
| 21 | + |
| 22 | +pub(crate) async fn file_contents_sorter( |
| 23 | + hook: &Hook, |
| 24 | + filenames: &[&Path], |
| 25 | +) -> Result<(i32, Vec<u8>)> { |
| 26 | + let args = Args::try_parse_from(hook.entry.split()?.iter().chain(&hook.args))?; |
| 27 | + let file_base = hook.project().relative_path(); |
| 28 | + |
| 29 | + run_concurrent_file_checks(filenames.iter().copied(), *CONCURRENCY, |filename| { |
| 30 | + sort_file(file_base, filename, args.ignore_case, args.unique) |
| 31 | + }) |
| 32 | + .await |
| 33 | +} |
| 34 | + |
| 35 | +async fn sort_file( |
| 36 | + file_base: &Path, |
| 37 | + filename: &Path, |
| 38 | + ignore_case: bool, |
| 39 | + unique: bool, |
| 40 | +) -> Result<(i32, Vec<u8>)> { |
| 41 | + let file_path = file_base.join(filename); |
| 42 | + let before = fs_err::tokio::read(&file_path).await?; |
| 43 | + let after = sorted_contents(&before, ignore_case, unique); |
| 44 | + |
| 45 | + if before == after { |
| 46 | + return Ok((0, Vec::new())); |
| 47 | + } |
| 48 | + |
| 49 | + fs_err::tokio::write(&file_path, &after).await?; |
| 50 | + Ok((1, format!("Sorting {}\n", filename.display()).into_bytes())) |
| 51 | +} |
| 52 | + |
| 53 | +fn sorted_contents(before: &[u8], ignore_case: bool, unique: bool) -> Vec<u8> { |
| 54 | + let mut lines = before |
| 55 | + .split_inclusive(|&byte| byte == b'\n') |
| 56 | + .filter_map(normalize_line) |
| 57 | + .collect::<Vec<_>>(); |
| 58 | + |
| 59 | + if ignore_case { |
| 60 | + lines.sort_by(|left, right| cmp_ignore_ascii_case(left, right)); |
| 61 | + } else { |
| 62 | + lines.sort_unstable(); |
| 63 | + if unique { |
| 64 | + lines.dedup(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if lines.is_empty() { |
| 69 | + return Vec::new(); |
| 70 | + } |
| 71 | + |
| 72 | + let mut after = |
| 73 | + Vec::with_capacity(lines.iter().map(|line| line.len()).sum::<usize>() + lines.len()); |
| 74 | + for line in lines { |
| 75 | + after.extend_from_slice(line); |
| 76 | + after.push(b'\n'); |
| 77 | + } |
| 78 | + after |
| 79 | +} |
| 80 | + |
| 81 | +fn normalize_line(mut line: &[u8]) -> Option<&[u8]> { |
| 82 | + line = line.trim_end_with(|byte| matches!(byte, '\n' | '\r')); |
| 83 | + |
| 84 | + // Drop empty and whitespace-only lines. |
| 85 | + if line.trim_ascii().is_empty() { |
| 86 | + None |
| 87 | + } else { |
| 88 | + Some(line) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn cmp_ignore_ascii_case(left: &[u8], right: &[u8]) -> std::cmp::Ordering { |
| 93 | + left.iter() |
| 94 | + .map(u8::to_ascii_lowercase) |
| 95 | + .cmp(right.iter().map(u8::to_ascii_lowercase)) |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod tests { |
| 100 | + use super::*; |
| 101 | + |
| 102 | + use std::path::PathBuf; |
| 103 | + use tempfile::tempdir; |
| 104 | + |
| 105 | + async fn create_test_file( |
| 106 | + dir: &tempfile::TempDir, |
| 107 | + name: &str, |
| 108 | + content: &[u8], |
| 109 | + ) -> Result<PathBuf> { |
| 110 | + let file_path = dir.path().join(name); |
| 111 | + fs_err::tokio::write(&file_path, content).await?; |
| 112 | + Ok(file_path) |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn test_sorted_contents_sorts_and_drops_blank_lines() { |
| 117 | + let before = b"beta\n\n \nalpha\r\n"; |
| 118 | + let after = sorted_contents(before, false, false); |
| 119 | + assert_eq!(after, b"alpha\nbeta\n"); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn test_sorted_contents_ignore_case() { |
| 124 | + let before = b"Banana\napple\nApricot\n"; |
| 125 | + let after = sorted_contents(before, true, false); |
| 126 | + assert_eq!(after, b"apple\nApricot\nBanana\n"); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn test_sorted_contents_ignore_case_is_stable_for_equal_keys() { |
| 131 | + let before = b"Apple\napple\n"; |
| 132 | + let after = sorted_contents(before, true, false); |
| 133 | + assert_eq!(after, b"Apple\napple\n"); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn test_sorted_contents_unique() { |
| 138 | + let before = b"beta\nalpha\nbeta\n"; |
| 139 | + let after = sorted_contents(before, false, true); |
| 140 | + assert_eq!(after, b"alpha\nbeta\n"); |
| 141 | + } |
| 142 | + |
| 143 | + #[tokio::test] |
| 144 | + async fn test_sort_file_modifies_unsorted_file() -> Result<()> { |
| 145 | + let dir = tempdir()?; |
| 146 | + let relative = PathBuf::from("allowlist.txt"); |
| 147 | + let file_path = create_test_file(&dir, "allowlist.txt", b"beta\nalpha\n").await?; |
| 148 | + |
| 149 | + let (code, output) = sort_file(dir.path(), &relative, false, false).await?; |
| 150 | + |
| 151 | + assert_eq!(code, 1); |
| 152 | + assert_eq!(String::from_utf8(output)?, "Sorting allowlist.txt\n"); |
| 153 | + assert_eq!(fs_err::tokio::read(&file_path).await?, b"alpha\nbeta\n"); |
| 154 | + |
| 155 | + Ok(()) |
| 156 | + } |
| 157 | + |
| 158 | + #[tokio::test] |
| 159 | + async fn test_sort_file_keeps_sorted_file() -> Result<()> { |
| 160 | + let dir = tempdir()?; |
| 161 | + let relative = PathBuf::from("allowlist.txt"); |
| 162 | + let file_path = create_test_file(&dir, "allowlist.txt", b"alpha\nbeta\n").await?; |
| 163 | + |
| 164 | + let (code, output) = sort_file(dir.path(), &relative, false, false).await?; |
| 165 | + |
| 166 | + assert_eq!(code, 0); |
| 167 | + assert!(output.is_empty()); |
| 168 | + assert_eq!(fs_err::tokio::read(&file_path).await?, b"alpha\nbeta\n"); |
| 169 | + |
| 170 | + Ok(()) |
| 171 | + } |
| 172 | +} |
0 commit comments