use predicates::prelude::PredicateBooleanExt;
fn alba() -> assert_cmd::Command {
assert_cmd::Command::cargo_bin("alba").unwrap()
}
fn project(beamfile: &str) -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("Beamfile"), beamfile).unwrap();
dir
}
const ALTERNATE_SCREEN: &str = "\u{1b}[?1049";
const CLEAR_SCREEN: &str = "\u{1b}[2J";
fn assert_no_escape_codes(stdout: &str, context: &str) {
for sequence in [ALTERNATE_SCREEN, CLEAR_SCREEN] {
assert!(
!stdout.contains(sequence),
"{context} must not carry a terminal escape sequence, got:\n{stdout:?}"
);
}
}
#[test]
fn a_pipe_gets_headless_output_by_default() {
let dir = project("beam ok { run \"echo hello-from-the-beam\" }\n");
let assert = alba()
.current_dir(&dir)
.args(["run", "ok"])
.assert()
.success();
let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
assert!(
stdout.contains("hello-from-the-beam"),
"the beam's output is missing from:\n{stdout}"
);
assert_no_escape_codes(&stdout, "a piped run");
}
#[test]
fn asking_for_no_ui_down_a_pipe_is_an_ordinary_run() {
let dir = project("beam ok { run \"echo hello-from-the-beam\" }\n");
alba()
.current_dir(&dir)
.args(["run", "ok", "--no-ui"])
.assert()
.success()
.stdout(predicates::str::contains("hello-from-the-beam"));
}
#[test]
fn forcing_the_ui_without_a_terminal_exits_2() {
let dir = project("beam ok { run \"echo hello-from-the-beam\" }\n");
alba()
.current_dir(&dir)
.args(["run", "ok", "--ui"])
.assert()
.code(2)
.stderr(predicates::str::contains("--ui needs a terminal"))
.stdout(predicates::str::contains("hello-from-the-beam").not());
}
#[test]
fn ui_and_no_ui_together_are_rejected() {
let dir = project("beam ok { run \"echo hello-from-the-beam\" }\n");
alba()
.current_dir(&dir)
.env("NO_COLOR", "1")
.args(["run", "ok", "--ui", "--no-ui"])
.assert()
.failure()
.stderr(predicates::str::contains("cannot be used with"));
}
#[test]
fn json_mode_never_opens_the_interface() {
let dir = project("beam ok { run \"echo hello-from-the-beam\" }\n");
let assert = alba()
.current_dir(&dir)
.args(["run", "ok", "--log-format", "json"])
.assert()
.success();
let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
for line in stdout.lines() {
serde_json::from_str::<serde_json::Value>(line)
.unwrap_or_else(|error| panic!("stdout line is not JSON ({error}): {line:?}"));
}
assert_no_escape_codes(&stdout, "the JSON stream");
}
#[test]
fn an_explicit_output_style_stays_text() {
let dir = project("beam ok { run \"echo hello-from-the-beam\" }\n");
let assert = alba()
.current_dir(&dir)
.args(["run", "ok", "--output", "grouped"])
.assert()
.success();
let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
assert!(
stdout.contains("hello-from-the-beam"),
"the beam's output is missing from:\n{stdout}"
);
assert_no_escape_codes(&stdout, "an explicit text layout");
}