1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// SPDX-License-Identifier: Apache-2.0
use std::path::PathBuf;
use std::process::ExitCode;
use bearout::{Command, HistoryMode, HistoryReport, Mode, Options, Report, Source, TestReport};
use clap::{Args, Parser, Subcommand, ValueEnum};
#[derive(Debug, Parser)]
#[command(name = "bearout", version, about)]
struct Cli {
/// Report format.
#[arg(long, global = true, value_enum, default_value_t = Format::Text)]
format: Format,
/// Run the formatters bearout.toml declares. They are trusted host
/// programs chosen by the repository, not confined by Bearout.
#[arg(long, global = true)]
allow_formatters: bool,
#[command(subcommand)]
command: Subcommands,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
enum Format {
/// One finding per line on standard error and a summary.
Text,
/// One JSON report on standard output, for every outcome.
Json,
}
/// Where to read the project from. Without a selection, the working
/// directory is read. The Git-backed sources are experimental, read-only,
/// and require the `git` executable.
#[derive(Debug, Default, Args)]
struct TreeArgs {
/// Read the Git index (staged content) instead of the working directory.
#[arg(long, conflicts_with = "revision")]
index: bool,
/// Read one Git revision (a commit, tag, branch, or tree) instead of the
/// working directory. The name is resolved once, at the start.
#[arg(long, value_name = "REV")]
revision: Option<String>,
}
impl TreeArgs {
fn source(&self) -> Source {
match (self.index, &self.revision) {
(true, _) => Source::Index,
(false, Some(revision)) => Source::Revision(revision.clone()),
(false, None) => Source::WorkingDirectory,
}
}
}
/// The source plus an optional comparison baseline.
#[derive(Debug, Default, Args)]
struct SourceArgs {
#[command(flatten)]
tree: TreeArgs,
/// Compare against one Git revision of the same repository, resolved
/// once. Nothing is inferred; name the baseline explicitly.
#[arg(long, value_name = "REV")]
baseline: Option<String>,
}
#[derive(Debug, Subcommand)]
enum Subcommands {
/// Validate a Bearout project and its resource graph.
Check {
/// Project directory containing bearout.toml.
#[arg(default_value = ".")]
path: PathBuf,
#[command(flatten)]
source: SourceArgs,
},
/// Rewrite the selected files so they satisfy the configured hygiene
/// and formatters. Working directory only; runs no repository policy.
Format {
/// Project directory containing bearout.toml.
#[arg(default_value = ".")]
path: PathBuf,
},
/// Validate a project, then render its generators' outputs.
Generate {
/// Project directory containing bearout.toml.
#[arg(default_value = ".")]
path: PathBuf,
/// Verify committed outputs instead of writing them. Required with
/// --index or --revision, which are read-only.
#[arg(long)]
check: bool,
#[command(flatten)]
source: SourceArgs,
},
/// Run the contract fixture cases bearout.toml declares in [fixtures]
/// against virtual mutations of the selected source. Read-only: never
/// formats or delivers; each case decides whether the unmodified
/// source is its comparison baseline. Experimental.
Test {
/// Project directory containing bearout.toml.
#[arg(default_value = ".")]
path: PathBuf,
#[command(flatten)]
source: TreeArgs,
},
/// Run the repository's history checks over Git commits: a commit
/// range for CI, or the pending commit for a commit-msg hook. The
/// policy is read from the resolved head or the captured index, never
/// from the working tree; only history checks run. Experimental.
History {
#[command(subcommand)]
mode: HistoryCommands,
},
}
#[derive(Debug, Subcommand)]
enum HistoryCommands {
/// Check the commits reachable from --head but not from --base; every
/// commit reachable from --head without a base. Names are resolved
/// once; the base itself is excluded; merges are included. Nothing is
/// read from the environment: name the revisions explicitly.
Range {
/// Project directory containing bearout.toml.
#[arg(default_value = ".")]
path: PathBuf,
/// The revision whose commits are excluded, resolved once.
#[arg(long, value_name = "REV")]
base: Option<String>,
/// The revision to check, resolved once. Defaults to HEAD.
#[arg(long, value_name = "REV")]
head: Option<String>,
},
/// Check the pending commit a commit-msg hook is about to make: the
/// exact message in --file (a regular file inside the repository's
/// Git directory), the author Git would record, HEAD and the heads of
/// a merge in progress as parents, and the staged changes of the
/// captured index, which also supplies the policy.
Message {
/// Project directory containing bearout.toml.
#[arg(default_value = ".")]
path: PathBuf,
/// The message file, as Git passes it to the commit-msg hook.
#[arg(long, value_name = "FILE")]
file: PathBuf,
},
}
fn main() -> ExitCode {
let cli = Cli::parse();
let (path, command, verb, source) = match cli.command {
Subcommands::Check { path, source } => (path, Command::Check, "checked", source),
Subcommands::Format { path } => (path, Command::Format, "formatted", SourceArgs::default()),
Subcommands::Test { path, source } => {
let options = Options {
source: source.source(),
allow_formatters: cli.allow_formatters,
..Options::default()
};
let report = bearout::test(&path, &options);
return match cli.format {
Format::Json => print_json(&report, report.fatal.is_some(), report.ok),
Format::Text => {
print_test_text(&report);
exit_code(report.fatal.is_some(), report.ok)
}
};
}
Subcommands::History { mode } => {
let (path, mode) = match mode {
HistoryCommands::Range { path, base, head } => {
(path, HistoryMode::Range { base, head })
}
HistoryCommands::Message { path, file } => (path, HistoryMode::Message { file }),
};
let report = bearout::history(&path, &mode, &Options::default());
return match cli.format {
Format::Json => print_json(&report, report.fatal.is_some(), report.ok),
Format::Text => {
print_history_text(&report);
exit_code(report.fatal.is_some(), report.ok)
}
};
}
Subcommands::Generate {
path,
check: false,
source,
} => (path, Command::Generate(Mode::Write), "generated", source),
Subcommands::Generate {
path,
check: true,
source,
} => (path, Command::Generate(Mode::Check), "verified", source),
};
let options = Options {
source: source.tree.source(),
baseline: source.baseline,
allow_formatters: cli.allow_formatters,
..Options::default()
};
let report = bearout::run(&path, command, &options);
match cli.format {
Format::Json => print_json(&report, report.fatal.is_some(), report.is_clean()),
Format::Text => {
print_text(&report, verb);
exit_code(report.fatal.is_some(), report.is_clean())
}
}
}
/// 0 for a clean outcome, 1 for findings or failed cases, 2 for a fatal
/// outcome.
fn exit_code(fatal: bool, ok: bool) -> ExitCode {
if fatal {
ExitCode::from(2)
} else if ok {
ExitCode::SUCCESS
} else {
ExitCode::from(1)
}
}
/// Print one JSON document for any outcome, so JSON stays valid on every
/// path, then exit by the report's own `fatal` and `ok`.
fn print_json(report: &impl serde::Serialize, fatal: bool, ok: bool) -> ExitCode {
match serde_json::to_string_pretty(report) {
Ok(text) => println!("{text}"),
Err(error) => {
println!(
"{{\"ok\":false,\"fatal\":{}}}",
serde_json::Value::String(error.to_string())
);
return ExitCode::from(2);
}
}
exit_code(fatal, ok)
}
/// Findings on standard error, one per line, then a summary: on standard
/// output when nothing was found, on standard error otherwise. A fatal
/// run prints only its reason.
fn print_history_text(report: &HistoryReport) {
if let Some(fatal) = &report.fatal {
for diagnostic in &report.diagnostics {
eprintln!("{diagnostic}");
}
eprintln!("bearout: {fatal}");
return;
}
for diagnostic in &report.diagnostics {
eprintln!("{diagnostic}");
}
let subject = if report.mode == "message" {
"checked the pending commit".to_owned()
} else {
format!("checked {} commit(s)", report.commits)
};
if report.ok {
println!("{subject}: clean");
} else {
eprintln!("{subject}: {} finding(s)", report.diagnostics.len());
}
}
/// One line per case on standard output, the details of each failed case
/// beneath it, and a summary: on standard output when every case passed,
/// on standard error otherwise. A fatal suite prints only its reason.
fn print_test_text(report: &TestReport) {
if let Some(fatal) = &report.fatal {
eprintln!("bearout: {fatal}");
return;
}
for case in &report.cases {
let status = if case.passed { "ok " } else { "FAIL" };
println!("{status} {} ({})", case.name, case.file);
if case.passed {
continue;
}
if case.expected != case.actual {
println!(" expected {}, got {}", case.expected, case.actual);
}
if let Some(fatal) = &case.fatal {
println!(" fatal: {fatal}");
}
if let (Some(expected), true) =
(&case.expected_fatal, case.actual == bearout::Outcome::Fatal)
{
println!(" expected the fatal message to contain {expected:?}");
}
for expectation in &case.missing {
println!(" missing: {expectation}");
}
for diagnostic in &case.unexpected {
println!(" unexpected: {diagnostic}");
}
}
let summary = format!(
"tested {} case(s): {} passed, {} failed",
report.total, report.passed, report.failed
);
if report.ok {
println!("{summary}");
} else {
eprintln!("{summary}");
}
}
fn print_text(report: &Report, verb: &str) {
if let Some(fatal) = &report.fatal {
eprintln!("bearout: {fatal}");
return;
}
for diagnostic in &report.diagnostics {
eprintln!("{diagnostic}");
}
if verb == "formatted" {
for path in &report.formatted {
println!("formatted {path}");
}
if report.is_clean() {
println!(
"formatted {} of {} selected file(s)",
report.formatted.len(),
report.files
);
} else {
eprintln!(
"formatted {} of {} selected file(s): {} error(s)",
report.formatted.len(),
report.files,
report.errors()
);
}
return;
}
let outputs = if report.outputs.is_empty() {
String::new()
} else {
format!(", {} output(s) {verb}", report.outputs.len())
};
let documents = if report.documents == 0 {
String::new()
} else {
format!(" and {} document(s)", report.documents)
};
if report.is_clean() {
println!(
"checked {} resource(s){documents}: clean{outputs}",
report.resources
);
} else {
eprintln!(
"checked {} resource(s){documents}: {} error(s){outputs}",
report.resources,
report.errors()
);
}
}