feat(xatu): set CLI exit statuses
This commit is contained in:
@@ -41,6 +41,17 @@ rust_test(
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
sh_test(
|
||||||
|
name = "xatu_cli_test",
|
||||||
|
srcs = ["xatu_cli_test.sh"],
|
||||||
|
data = [
|
||||||
|
":xatu",
|
||||||
|
"testdata/clean.nix",
|
||||||
|
"testdata/invalid.nix",
|
||||||
|
"testdata/lint_findings.nix",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
rust_binary(
|
rust_binary(
|
||||||
name = "xatu",
|
name = "xatu",
|
||||||
crate_name = "xatu_cli",
|
crate_name = "xatu_cli",
|
||||||
|
|||||||
65
src/xatu/xatu_cli_test.sh
Executable file
65
src/xatu/xatu_cli_test.sh
Executable file
@@ -0,0 +1,65 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
xatu="$TEST_SRCDIR/$TEST_WORKSPACE/src/xatu/xatu"
|
||||||
|
fixture="$TEST_SRCDIR/$TEST_WORKSPACE/src/xatu/testdata"
|
||||||
|
tmp="${TEST_TMPDIR:-$(mktemp -d)}"
|
||||||
|
|
||||||
|
cp "$fixture/clean.nix" "$tmp/clean.nix"
|
||||||
|
cp "$fixture/invalid.nix" "$tmp/invalid.nix"
|
||||||
|
cp "$fixture/lint_findings.nix" "$tmp/lint_findings.nix"
|
||||||
|
|
||||||
|
run_xatu() {
|
||||||
|
local name="$1"
|
||||||
|
local expected_status="$2"
|
||||||
|
shift 2
|
||||||
|
|
||||||
|
set +e
|
||||||
|
"$xatu" "$@" >"$tmp/$name.stdout" 2>"$tmp/$name.stderr"
|
||||||
|
local actual_status="$?"
|
||||||
|
set -e
|
||||||
|
|
||||||
|
if [[ "$actual_status" != "$expected_status" ]]; then
|
||||||
|
echo "expected $name to exit $expected_status, got $actual_status" >&2
|
||||||
|
echo "stdout:" >&2
|
||||||
|
cat "$tmp/$name.stdout" >&2
|
||||||
|
echo "stderr:" >&2
|
||||||
|
cat "$tmp/$name.stderr" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
run_xatu clean 0 "$tmp/clean.nix"
|
||||||
|
if [[ "$(<"$tmp/clean.stdout")" != "No findings." ]]; then
|
||||||
|
echo "expected clean output to report no findings" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [[ -s "$tmp/clean.stderr" ]]; then
|
||||||
|
echo "expected clean stderr to be empty" >&2
|
||||||
|
cat "$tmp/clean.stderr" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
run_xatu findings 1 "$tmp/lint_findings.nix"
|
||||||
|
if ! grep -Fq "deprecated_to_path" "$tmp/findings.stdout"; then
|
||||||
|
echo "expected findings on stdout" >&2
|
||||||
|
cat "$tmp/findings.stdout" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [[ -s "$tmp/findings.stderr" ]]; then
|
||||||
|
echo "expected findings stderr to be empty" >&2
|
||||||
|
cat "$tmp/findings.stderr" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
run_xatu fatal 2 "$tmp/invalid.nix"
|
||||||
|
if [[ -s "$tmp/fatal.stdout" ]]; then
|
||||||
|
echo "expected fatal stdout to be empty" >&2
|
||||||
|
cat "$tmp/fatal.stdout" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if ! grep -Fq "FailedPrecondition: failed to parse" "$tmp/fatal.stderr"; then
|
||||||
|
echo "expected fatal error on stderr" >&2
|
||||||
|
cat "$tmp/fatal.stderr" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use status::ResultExt;
|
use status::ResultExt;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use xatu::SourceFile;
|
use std::process::ExitCode;
|
||||||
|
use xatu::{Report, SourceFile};
|
||||||
|
|
||||||
fn is_nix_file(path: &Path) -> bool {
|
fn is_nix_file(path: &Path) -> bool {
|
||||||
path.extension().is_some_and(|extension| extension == "nix")
|
path.extension().is_some_and(|extension| extension == "nix")
|
||||||
@@ -66,9 +67,7 @@ struct Args {
|
|||||||
paths: Vec<PathBuf>,
|
paths: Vec<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> status::Result<()> {
|
fn analyze(args: Args) -> status::Result<Report> {
|
||||||
let args = Args::parse();
|
|
||||||
|
|
||||||
let paths: Vec<PathBuf> = args
|
let paths: Vec<PathBuf> = args
|
||||||
.paths
|
.paths
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -83,8 +82,22 @@ fn main() -> status::Result<()> {
|
|||||||
.flatten()
|
.flatten()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let report = xatu::future_sight(files)?;
|
xatu::future_sight(files)
|
||||||
print!("{report}");
|
}
|
||||||
|
|
||||||
Ok(())
|
fn main() -> ExitCode {
|
||||||
|
match analyze(Args::parse()) {
|
||||||
|
Ok(report) if report.is_clean() => {
|
||||||
|
println!("No findings.");
|
||||||
|
ExitCode::SUCCESS
|
||||||
|
}
|
||||||
|
Ok(report) => {
|
||||||
|
print!("{report}");
|
||||||
|
ExitCode::from(1)
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
eprintln!("{error}");
|
||||||
|
ExitCode::from(2)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user