use std::path::Path;
use assert_cmd::Command;
use predicates::prelude::*;
fn rigger(data: &Path) -> Command {
let mut cmd = Command::cargo_bin("rigger").unwrap();
cmd.env("RIGGER_DATA_DIR", data);
cmd
}
fn repo(root: &Path, name: &str) {
std::fs::create_dir_all(root).unwrap();
std::fs::write(root.join("Cargo.toml"), format!("[package]\nname = \"{name}\"\nversion = \"0.1.0\"\n")).unwrap();
}
fn record(data: &Path, work: &Path, name: &str) {
rigger(data).arg("init").assert().success();
repo(&work.join(name), name);
rigger(data)
.args(["project", "add"])
.arg(work.join(name))
.args(["--name", name])
.assert()
.success();
}
fn write_rituals(data: &Path, project: &str, title: &str, body: &str) {
rigger(data)
.args(["doc", "add", project, title, "--kind", "rituals", "--body", body])
.assert()
.success();
}
#[test]
fn the_line_and_the_project_are_printed_together() {
let data = tempfile::tempdir().unwrap();
let work = tempfile::tempdir().unwrap();
record(data.path(), work.path(), "widget");
write_rituals(data.path(), "line", "Rituals", "A stage ends in a tag.");
write_rituals(data.path(), "widget", "Rituals", "Widgets are measured before they are claimed.");
rigger(data.path())
.args(["rules", "widget"])
.assert()
.success()
.stdout(predicate::str::contains("A stage ends in a tag.").and(predicate::str::contains("Widgets are measured")));
rigger(data.path())
.arg("rules")
.assert()
.success()
.stdout(predicate::str::contains("A stage ends in a tag.").and(predicate::str::contains("Widgets are measured").not()));
}
#[test]
fn a_project_without_its_own_still_gets_the_lines() {
let data = tempfile::tempdir().unwrap();
let work = tempfile::tempdir().unwrap();
record(data.path(), work.path(), "widget");
write_rituals(data.path(), "line", "Rituals", "A stage ends in a tag.");
rigger(data.path())
.args(["rules", "widget"])
.assert()
.success()
.stdout(predicate::str::contains("A stage ends in a tag."));
}
#[test]
fn with_nothing_written_the_way_to_write_it_is_the_whole_message() {
let data = tempfile::tempdir().unwrap();
let work = tempfile::tempdir().unwrap();
record(data.path(), work.path(), "widget");
rigger(data.path())
.args(["rules", "widget"])
.assert()
.success()
.stdout(predicate::str::contains("rigger doc add line").and(predicate::str::contains("rigger doc add widget")));
}
#[test]
fn rituals_are_found_by_kind_not_by_the_address_a_title_happens_to_make() {
let data = tempfile::tempdir().unwrap();
let work = tempfile::tempdir().unwrap();
record(data.path(), work.path(), "widget");
rigger(data.path())
.args([
"doc",
"add",
"line",
"Ритуалы линейки",
"--kind",
"rituals",
"--slug",
"how-the-line-works",
"--body",
"A stage ends in a tag.",
])
.assert()
.success();
let listed = rigger(data.path()).args(["doc", "list", "line", "--json"]).output().unwrap();
let listed: serde_json::Value = serde_json::from_slice(&listed.stdout).unwrap();
assert_eq!(listed[0]["slug"], "how-the-line-works");
assert_eq!(listed[0]["kind"], "rituals");
rigger(data.path())
.arg("rules")
.assert()
.success()
.stdout(predicate::str::contains("A stage ends in a tag."));
}
#[test]
fn json_names_both_halves() {
let data = tempfile::tempdir().unwrap();
let work = tempfile::tempdir().unwrap();
record(data.path(), work.path(), "widget");
write_rituals(data.path(), "line", "Rituals", "A stage ends in a tag.");
let out = rigger(data.path()).args(["rules", "widget", "--json"]).output().unwrap();
let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
assert_eq!(v["line"], "A stage ends in a tag.");
assert_eq!(v["project"], "widget");
assert!(v["project_rules"].is_null(), "widget has written none of its own");
}
#[test]
fn a_repository_named_after_the_profile_is_refused_rather_than_used() {
let data = tempfile::tempdir().unwrap();
let work = tempfile::tempdir().unwrap();
record(data.path(), work.path(), "line");
rigger(data.path())
.arg("rules")
.assert()
.success()
.stdout(predicate::str::contains("has not written down how the work is done"));
}