repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

SM-003 slice: `xtask check` orchestrator (fmt + clippy + test + doc) (#112)

f0dad88f256904db36bc097d21622d948991161d

agent-smith-bugabinga-adc[bot] <306488075+agent-smith-bugabinga-adc[bot]@users.noreply.github.com> ·

SSH signature · not checked

Browse filesCompare with HEAD

Implements #89. Built by Codex (cross-family builder); rides the normal
review + merge-gate.

---------

Co-authored-by: agent-smith-bugabinga-adc[bot] <agent-smith-bugabinga-adc[bot]@users.noreply.github.com>
Co-authored-by: Oliver Jan Krylow <oliver@bugabinga.net>

Changed files (1)

+180 / −4 text lines

Diff layout

xtask/src/main.rs

Modified

+180 / −4 text lines

11//! Cargo task entry point for Smith.
3#[allow(
4 clippy::missing_const_for_fn,
5 reason = "Rust entry points cannot be const, and test builds lint main as a regular function"
3use std::{
4 env,
5 error::Error,
6 fmt, io,
7 process::{Command, ExitCode},
8};
9
10const CHECK_GATES: [Gate; 4] = [
11 Gate::new("fmt", &["fmt", "--check"]),
12 Gate::new(
13 "clippy",
14 &[
15 "clippy",
16 "--workspace",
17 "--all-targets",
18 "--all-features",
19 "--",
20 "-D",
21 "warnings",
22 ],
23 ),
24 Gate::new("test", &["nextest", "run", "--workspace"]),
25 Gate::new("doc", &["test", "--doc", "--workspace"]),
26];
28struct Gate {
29 name: &'static str,
30 arguments: &'static [&'static str],
31}
33impl Gate {
34 const fn new(name: &'static str, arguments: &'static [&'static str]) -> Self {
35 Self { name, arguments }
36 }
37}
39#[derive(Debug)]
40enum GateError {
41 Spawn(io::Error),
42 Failed,
43}
45impl fmt::Display for GateError {
46 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
47 match self {
48 Self::Spawn(error) => write!(formatter, "could not start cargo: {error}"),
49 Self::Failed => formatter.write_str("returned a non-zero status"),
50 }
51 }
52}
54impl Error for GateError {
55 fn source(&self) -> Option<&(dyn Error + 'static)> {
56 match self {
57 Self::Spawn(error) => Some(error),
58 Self::Failed => None,
59 }
60 }
61}
63struct CheckError {
64 gate: &'static str,
65 source: GateError,
66}
68impl fmt::Display for CheckError {
69 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
70 write!(formatter, "{} gate {}", self.gate, self.source)
71 }
72}
74#[expect(
75 clippy::print_stderr,
76 reason = "A failed gate needs a concise diagnostic before xtask exits non-zero."
677)]
7fn main() {}
78fn main() -> ExitCode {
79 let mut arguments = env::args().skip(1);
80 let command = arguments.next();
82 if command.as_deref() == Some("check") && arguments.next().is_none() {
83 return match run_check(run_cargo) {
84 Ok(()) => ExitCode::SUCCESS,
85 Err(error) => {
86 eprintln!("xtask check: {error}");
87 ExitCode::FAILURE
88 }
89 };
90 }
92 eprintln!("usage: cargo run -p xtask -- check");
93 ExitCode::FAILURE
94}
96fn run_check(
97 mut run_gate: impl FnMut(&'static [&'static str]) -> Result<(), GateError>,
98) -> Result<(), CheckError> {
99 for gate in CHECK_GATES {
100 run_gate(gate.arguments).map_err(|source| CheckError {
101 gate: gate.name,
102 source,
103 })?;
104 }
106 Ok(())
107}
109fn run_cargo(arguments: &[&str]) -> Result<(), GateError> {
110 let status = Command::new("cargo")
111 .args(arguments)
112 .status()
113 .map_err(GateError::Spawn)?;
115 if status.success() {
116 Ok(())
117 } else {
118 Err(GateError::Failed)
119 }
120}
122#[cfg(test)]
123mod tests {
124 use super::{GateError, run_check};
126 #[test]
127 fn check_runs_stable_gates_in_order() {
128 let mut invocations = Vec::new();
130 let result = run_check(|arguments| {
131 invocations.push(arguments);
132 Ok(())
133 });
135 assert!(result.is_ok());
136 let expected: [&[&str]; 4] = [
137 &["fmt", "--check"],
138 &[
139 "clippy",
140 "--workspace",
141 "--all-targets",
142 "--all-features",
143 "--",
144 "-D",
145 "warnings",
146 ],
147 &["nextest", "run", "--workspace"],
148 &["test", "--doc", "--workspace"],
149 ];
150 assert_eq!(invocations, expected);
151 }
153 #[test]
154 fn check_stops_when_a_gate_fails() {
155 let mut invocations = Vec::new();
156 let mut calls = 0;
158 let result = run_check(|arguments| {
159 invocations.push(arguments);
160 calls += 1;
161 if calls == 2 {
162 Err(GateError::Failed)
163 } else {
164 Ok(())
165 }
166 });
168 assert!(matches!(result, Err(error) if error.gate == "clippy"));
169 let expected: [&[&str]; 2] = [
170 &["fmt", "--check"],
171 &[
172 "clippy",
173 "--workspace",
174 "--all-targets",
175 "--all-features",
176 "--",
177 "-D",
178 "warnings",
179 ],
180 ];
181 assert_eq!(invocations, expected);
182 }
183}