Files
fermata/tests/cli_check.rs
T
g4borg 11328da30f fix(fermata): vendor/ covers what lies below it; relative paths and links meet the rules; the payload's cwd decides
.botignore matches a path or any parent, with the path tried first, so
!vendor/keep beats vendor/. Policy::check takes a relative path against the
root and also checks the path as the file system resolves it, spelled from the
root as given, so a link or a root behind a link hides nothing. session_dir: the
payload's cwd if absolute, else the process directory, for both hooks; fermata
check and the hook make paths absolute.
2026-11-07 00:59:04 +01:00

97 lines
3.3 KiB
Rust

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
#[test]
fn check_blocks_botignore_match() {
let tmp = tempfile::tempdir().unwrap();
fs::write(tmp.path().join(".botignore"), ".env\n").unwrap();
let target = tmp.path().join(".env");
fs::write(&target, "").unwrap();
Command::cargo_bin("fermata")
.unwrap()
.args(["check", "--op", "read", target.to_str().unwrap()])
.assert()
.failure()
.code(1)
.stdout(predicate::str::contains(".env"));
}
#[test]
fn check_allows_unmatched() {
let tmp = tempfile::tempdir().unwrap();
fs::write(tmp.path().join(".botignore"), ".env\n").unwrap();
let target = tmp.path().join("src.rs");
fs::write(&target, "").unwrap();
Command::cargo_bin("fermata")
.unwrap()
.args(["check", "--op", "read", target.to_str().unwrap()])
.assert()
.success();
}
#[test]
fn check_emits_json_with_flag() {
let tmp = tempfile::tempdir().unwrap();
fs::write(tmp.path().join(".botignore"), ".env\n").unwrap();
let target = tmp.path().join(".env");
fs::write(&target, "").unwrap();
let out = Command::cargo_bin("fermata")
.unwrap()
.args(["check", "--op", "read", "--json", target.to_str().unwrap()])
.assert()
.failure()
.get_output()
.stdout
.clone();
let v: serde_json::Value = serde_json::from_slice(&out).unwrap();
assert_eq!(v["kind"], "deny");
}
#[test]
fn check_exits_2_and_names_the_line_of_a_broken_config() {
for (config, line) in [("[polcy]\nread = 1\n", 1), ("[files]\n[polcy]\nread = 1\n", 2), ("[enforcement]\nmode = \"strict\"\n", 2)] {
let tmp = tempfile::tempdir().unwrap();
let home = tempfile::tempdir().unwrap();
fs::write(tmp.path().join(".botsecrets"), config).unwrap();
let target = tmp.path().join("a.txt");
fs::write(&target, "").unwrap();
Command::cargo_bin("fermata")
.unwrap()
.args(["check", "--op", "read", target.to_str().unwrap()])
.env("HOME", home.path())
.assert()
.code(2)
.stderr(predicate::str::contains(".botsecrets").and(predicate::str::contains(format!("line {line}"))));
}
}
/// A relative path is taken against the working directory, as a shell does.
#[test]
fn check_takes_a_relative_path_against_the_working_directory() {
let tmp = tempfile::tempdir().unwrap();
let home = tempfile::tempdir().unwrap();
fs::write(tmp.path().join(".botignore"), ".env\nvendor/\n").unwrap();
fs::write(tmp.path().join(".env"), "").unwrap();
fs::write(tmp.path().join("a.txt"), "").unwrap();
fs::create_dir(tmp.path().join("sub")).unwrap();
let check = |dir: &std::path::Path, op: &str, path: &str| {
Command::cargo_bin("fermata")
.unwrap()
.args(["check", "--op", op, path])
.current_dir(dir)
.env("HOME", home.path())
.assert()
};
check(tmp.path(), "read", ".env").code(1).stdout(predicate::str::contains(".env"));
check(tmp.path(), "read", "./.env").code(1);
check(tmp.path(), "write", "vendor/new").code(1);
check(&tmp.path().join("sub"), "read", "../.env").code(1);
check(tmp.path(), "read", "a.txt").code(0);
}