use super::{model_focused, world};
use crate::cli_outbound::Cli;
use crate::test_support::{authoring_new_arm, spawn_guard};
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use tempfile::tempdir;
fn news() -> String {
format!(
"#!/bin/sh\ncase \"$1\" in\n{}esac\nexit 0\n",
authoring_new_arm()
)
}
const FAILS: &str = "#!/bin/sh\nprintf 'boom\\n' 1>&2\nexit 3\n";
fn fake_lernie(dir: &Path, body: &str) -> Cli {
let path = dir.join("lernie");
fs::write(&path, body).unwrap();
let mut perms = fs::metadata(&path).unwrap().permissions();
perms.set_mode(0o755);
fs::set_permissions(&path, perms).unwrap();
Cli::new(path)
}
fn seed(yog_data_root: &Path) {
let lernie = crate::world::layout_under(yog_data_root).lernie;
fs::create_dir_all(&lernie).unwrap();
fs::write(lernie.join("models.yaml"), b"models: {}\n").unwrap();
}
#[test]
fn a_raise_focuses_the_raised_workspace_and_retargets_the_bare_rung() {
let bin = tempdir().unwrap();
let w = world();
seed(&w.roots.yog_data);
let (_c, mut m) = model_focused(&w, &w.ws_cobalt);
assert_eq!(m.focused_workspace(), Some(w.ws_cobalt.as_path()));
let _g = spawn_guard();
let inputs = m.new_workspace_inputs("ops");
let prepared = m
.prepare_start(
&fake_lernie(bin.path(), &news()),
&Cli::new("/no/bl"), &inputs.workspace,
&inputs.payload,
"TS",
)
.unwrap();
assert_eq!(prepared.name, "ops", "the operator's own name (§3.1)");
assert_ne!(
prepared.workspace, w.ws_cobalt,
"the raise always raises a fresh wall"
);
assert!(
prepared.workspace.join("repo.git").is_dir(),
"`lernie new` ran"
);
assert_eq!(
m.focused_workspace(),
Some(prepared.workspace.as_path()),
"focus follows the raise — the tab bar and conversation list move with it"
);
assert_eq!(
m.start_bare_inputs().workspace,
prepared.workspace,
"the bare rung names the start's own workspace"
);
}
#[test]
fn a_failed_prepare_moves_the_focus_nowhere() {
let bin = tempdir().unwrap();
let w = world();
seed(&w.roots.yog_data);
let (_c, mut m) = model_focused(&w, &w.ws_cobalt);
let _g = spawn_guard();
let inputs = m.new_workspace_inputs("ops");
let err = m
.prepare_start(
&fake_lernie(bin.path(), FAILS),
&Cli::new("/no/bl"),
&inputs.workspace,
&inputs.payload,
"TS",
)
.unwrap_err();
assert!(err.contains("boom"), "the refusal rode back");
assert_eq!(
m.focused_workspace(),
Some(w.ws_cobalt.as_path()),
"nothing was resolved, so nothing moved"
);
}