|
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | +//! Checkout-only CI check; deliberately outside Cargo's integration tests. |
| 3 | +use std::{collections::BTreeMap, path::Path, process::Command}; |
| 4 | + |
| 5 | +fn portable_component(component: &str) -> bool { |
| 6 | + if component.is_empty() |
| 7 | + || component.ends_with(['.', ' ']) |
| 8 | + || component |
| 9 | + .chars() |
| 10 | + .any(|c| c.is_control() || "<>:\"\\|?*".contains(c)) |
| 11 | + { |
| 12 | + return false; |
| 13 | + } |
| 14 | + let stem = component |
| 15 | + .split('.') |
| 16 | + .next() |
| 17 | + .unwrap_or("") |
| 18 | + .to_ascii_uppercase(); |
| 19 | + let numbered_device = stem |
| 20 | + .strip_prefix("COM") |
| 21 | + .or_else(|| stem.strip_prefix("LPT")); |
| 22 | + !matches!( |
| 23 | + stem.as_str(), |
| 24 | + "CON" | "PRN" | "AUX" | "NUL" | "CONIN$" | "CONOUT$" |
| 25 | + ) && !numbered_device.is_some_and(|n| { |
| 26 | + matches!( |
| 27 | + n, |
| 28 | + "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "¹" | "²" | "³" |
| 29 | + ) |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +fn inventory_errors(paths: &[&str]) -> Vec<String> { |
| 34 | + let mut errors = Vec::new(); |
| 35 | + let mut seen = BTreeMap::<String, (String, bool)>::new(); |
| 36 | + for path in paths { |
| 37 | + let components: Vec<_> = path.split('/').collect(); |
| 38 | + let mut prefix = String::new(); |
| 39 | + for (index, component) in components.iter().enumerate() { |
| 40 | + if !portable_component(component) { |
| 41 | + errors.push(format!("invalid component in {path:?}")); |
| 42 | + } |
| 43 | + if !prefix.is_empty() { |
| 44 | + prefix.push('/'); |
| 45 | + } |
| 46 | + prefix.push_str(component); |
| 47 | + let file = index + 1 == components.len(); |
| 48 | + let key = prefix.to_uppercase(); |
| 49 | + if let Some((old, old_file)) = seen.get(&key) { |
| 50 | + if old != &prefix || *old_file != file { |
| 51 | + errors.push(format!( |
| 52 | + "case or file/directory collision: {old:?} and {prefix:?}" |
| 53 | + )); |
| 54 | + } |
| 55 | + } else { |
| 56 | + seen.insert(key, (prefix.clone(), file)); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + errors |
| 61 | +} |
| 62 | + |
| 63 | +fn check_checkout(root: &Path) -> Result<(), String> { |
| 64 | + let root = root.canonicalize().map_err(|e| e.to_string())?; |
| 65 | + let top = Command::new("git") |
| 66 | + .current_dir(&root) |
| 67 | + .args(["rev-parse", "--show-toplevel"]) |
| 68 | + .output() |
| 69 | + .map_err(|e| format!("git is required: {e}"))?; |
| 70 | + if !top.status.success() { |
| 71 | + return Err("the requested root is not a Git checkout".into()); |
| 72 | + } |
| 73 | + let top = std::str::from_utf8(&top.stdout) |
| 74 | + .map_err(|e| e.to_string())? |
| 75 | + .trim(); |
| 76 | + if Path::new(top).canonicalize().map_err(|e| e.to_string())? != root { |
| 77 | + return Err("refusing to inspect an enclosing repository's index".into()); |
| 78 | + } |
| 79 | + let output = Command::new("git") |
| 80 | + .current_dir(&root) |
| 81 | + .args(["ls-files", "-z"]) |
| 82 | + .output() |
| 83 | + .map_err(|e| e.to_string())?; |
| 84 | + if !output.status.success() { |
| 85 | + return Err("git ls-files failed".into()); |
| 86 | + } |
| 87 | + let text = std::str::from_utf8(&output.stdout).map_err(|e| e.to_string())?; |
| 88 | + let paths: Vec<_> = text.split('\0').filter(|p| !p.is_empty()).collect(); |
| 89 | + if paths.is_empty() { |
| 90 | + return Err("empty inventory cannot establish portability".into()); |
| 91 | + } |
| 92 | + let errors = inventory_errors(&paths); |
| 93 | + if !errors.is_empty() { |
| 94 | + return Err(errors.join("\n")); |
| 95 | + } |
| 96 | + println!( |
| 97 | + "{} tracked paths passed the portability checks", |
| 98 | + paths.len() |
| 99 | + ); |
| 100 | + Ok(()) |
| 101 | +} |
| 102 | + |
| 103 | +fn main() -> Result<(), String> { |
| 104 | + let root = std::env::args_os() |
| 105 | + .nth(1) |
| 106 | + .ok_or("usage: check-repository-paths REPOSITORY_ROOT")?; |
| 107 | + check_checkout(Path::new(&root)) |
| 108 | +} |
| 109 | + |
| 110 | +#[cfg(test)] |
| 111 | +mod tests { |
| 112 | + use super::*; |
| 113 | + #[test] |
| 114 | + fn windows_device_and_component_controls() { |
| 115 | + for bad in [ |
| 116 | + "report:12.json", |
| 117 | + "CON", |
| 118 | + "nul.txt", |
| 119 | + "COM1.log", |
| 120 | + "LPT9", |
| 121 | + "COM¹.txt", |
| 122 | + "COM²", |
| 123 | + "COM³", |
| 124 | + "LPT¹", |
| 125 | + "LPT²", |
| 126 | + "LPT³", |
| 127 | + "CONIN$", |
| 128 | + "CONOUT$", |
| 129 | + "tail.", |
| 130 | + "tail ", |
| 131 | + "a?b", |
| 132 | + "a\\b", |
| 133 | + "line\nbreak", |
| 134 | + ] { |
| 135 | + assert!(!portable_component(bad), "accepted {bad:?}"); |
| 136 | + } |
| 137 | + for good in [ |
| 138 | + "assemblyline-2026-04-12T11-23-37Z.json", |
| 139 | + ".github", |
| 140 | + "Cargo.toml", |
| 141 | + "com10.txt", |
| 142 | + ] { |
| 143 | + assert!(portable_component(good), "rejected {good:?}"); |
| 144 | + } |
| 145 | + } |
| 146 | + #[test] |
| 147 | + fn inventory_detects_case_and_file_directory_collisions() { |
| 148 | + for paths in [ |
| 149 | + vec!["reports/Run.json", "reports/run.json"], |
| 150 | + vec!["src/Foo/a.rs", "src/foo/b.rs"], |
| 151 | + vec!["foo", "FOO/bar.rs"], |
| 152 | + ] { |
| 153 | + assert!( |
| 154 | + !inventory_errors(&paths).is_empty(), |
| 155 | + "accepted collision: {paths:?}" |
| 156 | + ); |
| 157 | + } |
| 158 | + assert!(inventory_errors(&["src/a.rs", "src/b.rs"]).is_empty()); |
| 159 | + } |
| 160 | +} |
0 commit comments