use std::process::{Command, Stdio};
fn ae() -> std::path::PathBuf {
let mut p = std::env::current_exe().expect("test exe path");
p.pop();
if p.ends_with("deps") {
p.pop();
}
p.join(format!("ae{}", std::env::consts::EXE_SUFFIX))
}
fn run(args: &[&str], env: &[(&str, &str)]) -> (String, String) {
let mut cmd = Command::new(ae());
cmd.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.env_remove("NO_COLOR")
.env_remove("FORCE_COLOR")
.env_remove("CLICOLOR_FORCE")
.env_remove("AETHER_MODE");
for (k, v) in env {
cmd.env(k, v);
}
let out = cmd.output().expect("run ae");
(
String::from_utf8_lossy(&out.stdout).to_string(),
String::from_utf8_lossy(&out.stderr).to_string(),
)
}
fn assert_no_ansi(what: &str, s: &str) {
assert!(
!s.contains('\u{1b}'),
"{what} contains an ANSI escape when writing to a pipe: {s:?}"
);
}
#[test]
fn values_written_to_a_pipe_carry_no_escape_codes() {
for expr in ["1 + 2", "[1, 2, 3]", "{a: 1}", "true", "null", "3.5"] {
let (stdout, _) = run(&["-c", expr], &[]);
assert_no_ansi(&format!("`ae -c {expr:?}` stdout"), &stdout);
}
}
#[test]
fn arithmetic_is_exactly_the_number() {
let (stdout, stderr) = run(&["-c", "1 + 2"], &[]);
assert_eq!(
stdout.trim(),
"3",
"stdout was {stdout:?} (stderr: {stderr:?})"
);
}
#[test]
fn diagnostics_written_to_a_pipe_carry_no_escape_codes() {
let (_, stderr) = run(&["-c", "nope("], &[]);
assert!(!stderr.is_empty(), "a parse error produced no diagnostic");
assert_no_ansi("stderr", &stderr);
}
#[test]
fn no_color_is_honoured() {
let (stdout, _) = run(&["-c", "[1, 2, 3]"], &[("NO_COLOR", "1")]);
assert_no_ansi("stdout under NO_COLOR", &stdout);
}
#[test]
fn colour_can_be_forced_back_on() {
let (plain, _) = run(&["-c", "1 + 2"], &[]);
let (forced, _) = run(&["-c", "1 + 2"], &[("FORCE_COLOR", "1")]);
assert_no_ansi("unforced stdout", &plain);
assert!(
forced.contains('\u{1b}'),
"FORCE_COLOR produced no colour ({forced:?}), so there is no way to get \
colour through a pager"
);
}
#[test]
fn no_color_beats_force_color() {
let (out, _) = run(&["-c", "1 + 2"], &[("FORCE_COLOR", "1"), ("NO_COLOR", "1")]);
assert_no_ansi("stdout with both NO_COLOR and FORCE_COLOR", &out);
}