use super::*;
#[cfg(unix)]
#[test]
fn starting_an_errored_sheep_by_name_reports_the_same_failure_as_by_path() {
let dir = tempfile::tempdir().unwrap();
let script = dir.path().join("broken.sh");
std::fs::write(&script, "#!/bin/sh\necho hi\n").unwrap();
let mut perms = std::fs::metadata(&script).unwrap().permissions();
perms.set_mode(0o644);
std::fs::set_permissions(&script, perms).unwrap();
let mut guard = DaemonGuard::default();
let by_path = shep(dir.path())
.arg("--format")
.arg("json")
.arg("start")
.arg(&script)
.output()
.unwrap();
guard.adopt_home(dir.path());
assert_json_error(&by_path, 7, "spawn_failed");
let flock = poll_flock(dir.path(), |info| info["status"] == "errored");
assert_eq!(
flock["status"], "errored",
"the by-path failure must leave the sheep registered as errored: {flock}"
);
let name = script.file_stem().unwrap().to_str().unwrap();
let by_name = shep(dir.path())
.arg("--format")
.arg("json")
.arg("start")
.arg(name)
.output()
.unwrap();
assert_json_error(&by_name, 7, "spawn_failed");
graceful_kill(dir.path());
}
#[cfg(unix)]
#[test]
fn restarting_a_sheep_that_cannot_spawn_reports_it_rather_than_exiting_zero() {
let dir = tempfile::tempdir().unwrap();
let script = dir.path().join("noexec");
std::fs::write(&script, "#!/bin/sh\nsleep 30\n").unwrap();
let mut perms = std::fs::metadata(&script).unwrap().permissions();
perms.set_mode(0o644);
std::fs::set_permissions(&script, perms).unwrap();
let mut guard = DaemonGuard::default();
let by_path = shep(dir.path())
.arg("--format")
.arg("json")
.arg("start")
.arg(&script)
.output()
.unwrap();
guard.adopt_home(dir.path());
assert_json_error(&by_path, 7, "spawn_failed");
let flock = poll_flock(dir.path(), |info| info["status"] == "errored");
assert_eq!(
flock["status"], "errored",
"the by-path failure must leave the sheep registered as errored: {flock}"
);
let name = script.file_stem().unwrap().to_str().unwrap();
let restarted = shep(dir.path())
.arg("--format")
.arg("json")
.arg("restart")
.arg(name)
.output()
.unwrap();
assert_json_error(&restarted, 7, "spawn_failed");
graceful_kill(dir.path());
}
#[cfg(unix)]
#[test]
fn a_js_flockfile_without_node_says_so_and_says_what_to_do() {
let dir = tempfile::tempdir().unwrap();
let flockfile = dir.path().join("Flockfile.js");
std::fs::write(
&flockfile,
"module.exports = { app: [{ name: 'web', script: './server.js' }] };\n",
)
.unwrap();
let mut guard = DaemonGuard::default();
let output = shep(dir.path())
.env("PATH", "")
.arg("start")
.arg("--flockfile")
.arg(&flockfile)
.output()
.unwrap();
guard.adopt_home(dir.path());
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
!output.status.success(),
"a Flockfile that cannot be read must not succeed: {stderr}"
);
assert!(
stderr.contains("node was not found on PATH"),
"the message names the cause: {stderr}"
);
assert!(
stderr.contains("install node, or convert"),
"and what to do about it: {stderr}"
);
assert!(
!stderr.contains('\u{2014}') && !stderr.contains('\u{2013}'),
"no em or en dash in copy a user reads: {stderr}"
);
graceful_kill(dir.path());
}