use super::*;
#[test]
fn plan_for_an_undeclared_milestone_is_usage() {
let m = |a: &[&str]| run_args(a.iter().map(|s| (*s).to_string()).collect());
assert_eq!(m(&["plan", "--milestone", "M999"]), ExitCode::from(2));
assert_eq!(m(&["plan", "--milestone"]), ExitCode::from(2));
}
#[test]
fn plan_format_is_positional_agnostic_and_strict() {
let fmt = |a: &[&str]| {
let mut v = vec!["sherd", "plan"];
v.extend(a);
match parse::Cli::try_parse_from(v).map(|c| c.cmd) {
Ok(Some(Cmd::Plan {
format, milestone, ..
})) => Some((format.format.is_json(), milestone)),
_ => None,
}
};
let m1 = Some("M1".to_string());
let both = ["--milestone", "M1", "--format", "json"];
assert_eq!(fmt(&both), Some((true, m1.clone())));
assert_eq!(
fmt(&["--format", "json", "--milestone", "M1"]),
Some((true, m1))
);
assert_eq!(fmt(&["--format", "text"]), Some((false, None)));
assert_eq!(fmt(&[]), Some((false, None)));
let m = |a: &[&str]| run_args(a.iter().map(|x| (*x).to_string()).collect());
assert_eq!(m(&["plan", "--format", "yaml"]), ExitCode::from(2));
assert_eq!(m(&["plan", "--format"]), ExitCode::from(2));
assert_eq!(
m(&["plan", "--triage", "--format", "json"]),
ExitCode::from(2)
);
}
fn milestone_fixture() -> Result<crate::testrepo::TestRepo, String> {
let r = crate::testrepo::TestRepo::new("cli-plan-ms")?;
r.write(
"SPEC.md",
"# SPEC\n\n## \u{a7}G GOAL\n\ntoy\n\n## \u{a7}F FEDERATION\n\n\
dir|owns|\u{22a5}owns|tokens\na|alpha|beta|-\nb|beta|alpha|-\n",
)?;
r.write(
"a/SPEC.md",
"# SPEC\n\n## \u{a7}G GOAL\n\nalpha\n\n## \u{a7}T TASKS\n\n\
| id | scope | tasks | done-when |\n|----|-------|-------|-----------|\n\
| M1 | first | T1 | shipped |\n\nid|status|task|cites\nT1|.|one|-\n",
)?;
r.write(
"b/SPEC.md",
"# SPEC\n\n## \u{a7}G GOAL\n\nbeta\n\n## \u{a7}T TASKS\n\n\
id|status|task|cites\nT1|.|unscheduled|-\n",
)?;
Ok(r)
}
#[test]
fn a_milestone_plan_renders_in_text_and_json() -> Result<(), String> {
let r = milestone_fixture()?;
assert_eq!(plan_cmd(r.path(), Some("M1"), false), ExitCode::SUCCESS);
assert_eq!(plan_cmd(r.path(), Some("M1"), true), ExitCode::SUCCESS);
assert_eq!(plan_cmd(r.path(), None, true), ExitCode::SUCCESS);
Ok(())
}
#[test]
fn plan_leaves_the_state_store_untouched() -> Result<(), String> {
let r = milestone_fixture()?;
let store = r.path().join("store");
let before =
"# a store written by someone else\nplan 1 a T9 STALE\napplied x 1\n";
std::fs::write(&store, before).map_err(|e| e.to_string())?;
let st = crate::state::State::at(&store);
assert_eq!(plan_with(r.path(), None, false, &st), ExitCode::SUCCESS);
assert_eq!(
plan_with(r.path(), Some("M1"), true, &st),
ExitCode::SUCCESS
);
let after = std::fs::read_to_string(&store).map_err(|e| e.to_string())?;
assert_eq!(after, before);
Ok(())
}