use std::process::{Command, Stdio};
#[test]
fn repon_config_example_prints_the_annotated_example_and_exits_zero_with_no_config_resolved() {
let missing_config_dir = tempfile::tempdir()
.expect("create tempdir")
.path()
.join("does-not-exist-at-all");
let output = Command::new(env!("CARGO_BIN_EXE_repon"))
.arg("config")
.arg("--example")
.env("REPON_CONFIG", &missing_config_dir)
.stdin(Stdio::null())
.output()
.expect("run repon config --example");
assert!(
output.status.success(),
"expected a clean exit even though REPON_CONFIG names a directory that does not \
exist, got: {output:?}"
);
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("theme = \"default\""),
"expected the annotated example's own top-level theme key, got: {stdout:?}"
);
assert!(
stdout.contains("[[set]]"),
"expected the annotated example's own Set declarations, got: {stdout:?}"
);
assert!(
toml::from_str::<toml::Value>(&stdout).is_ok(),
"expected the printed example to parse as TOML, got: {stdout:?}"
);
}
#[test]
fn repon_config_reports_the_config_file_as_missing_when_none_is_present() {
let config_dir = tempfile::tempdir().expect("create config dir");
let data_dir = tempfile::tempdir().expect("create data dir");
let output = Command::new(env!("CARGO_BIN_EXE_repon"))
.arg("config")
.env("REPON_CONFIG", config_dir.path())
.env("REPON_DATA", data_dir.path())
.stdin(Stdio::null())
.output()
.expect("run repon config");
assert!(
output.status.success(),
"expected a clean exit, got: {output:?}"
);
let stdout = String::from_utf8_lossy(&output.stdout);
let config_file_line = stdout
.lines()
.find(|line| line.starts_with("config file:"))
.unwrap_or_else(|| panic!("expected a \"config file:\" line, got: {stdout:?}"));
assert!(
config_file_line.contains(config_dir.path().to_str().expect("utf8 path"))
&& config_file_line.contains("(missing)"),
"expected the resolved path under REPON_CONFIG marked missing, got: {config_file_line:?}"
);
let log_file_line = stdout
.lines()
.find(|line| line.starts_with("log file:"))
.unwrap_or_else(|| panic!("expected a \"log file:\" line, got: {stdout:?}"));
assert!(
log_file_line.contains("(missing)"),
"expected the log file marked missing with no run having written to it, \
got: {log_file_line:?}"
);
}
#[test]
fn repon_config_reports_the_config_file_as_existing_once_one_is_written() {
let config_dir = tempfile::tempdir().expect("create config dir");
let data_dir = tempfile::tempdir().expect("create data dir");
std::fs::write(
config_dir.path().join("config.toml"),
"theme = \"default\"\n",
)
.expect("write config.toml");
let output = Command::new(env!("CARGO_BIN_EXE_repon"))
.arg("config")
.env("REPON_CONFIG", config_dir.path())
.env("REPON_DATA", data_dir.path())
.stdin(Stdio::null())
.output()
.expect("run repon config");
assert!(
output.status.success(),
"expected a clean exit, got: {output:?}"
);
let stdout = String::from_utf8_lossy(&output.stdout);
let config_file_line = stdout
.lines()
.find(|line| line.starts_with("config file:"))
.unwrap_or_else(|| panic!("expected a \"config file:\" line, got: {stdout:?}"));
assert!(
config_file_line.contains("(exists)"),
"expected the written config.toml marked exists, got: {config_file_line:?}"
);
}
#[test]
fn repon_config_reports_a_missing_config_flag_path_rather_than_exiting_non_zero() {
let missing_file = tempfile::tempdir()
.expect("create tempdir")
.path()
.join("nowhere.toml");
let output = Command::new(env!("CARGO_BIN_EXE_repon"))
.arg("--config")
.arg(&missing_file)
.arg("config")
.stdin(Stdio::null())
.output()
.expect("run repon config");
assert!(
output.status.success(),
"expected repon config to report on the path rather than fail, got: {output:?}"
);
let stdout = String::from_utf8_lossy(&output.stdout);
let config_file_line = stdout
.lines()
.find(|line| line.starts_with("config file:"))
.unwrap_or_else(|| panic!("expected a \"config file:\" line, got: {stdout:?}"));
assert!(
config_file_line.contains(missing_file.to_str().expect("utf8 path"))
&& config_file_line.contains("(missing)"),
"expected the --config-named path reported missing, got: {config_file_line:?}"
);
}
#[test]
fn repon_sets_still_exits_non_zero_on_the_same_missing_config_flag_path() {
let missing_file = tempfile::tempdir()
.expect("create tempdir")
.path()
.join("nowhere.toml");
let output = Command::new(env!("CARGO_BIN_EXE_repon"))
.arg("--config")
.arg(&missing_file)
.arg("sets")
.stdin(Stdio::null())
.output()
.expect("run repon sets");
assert!(
!output.status.success(),
"expected a non-zero exit for a --config path that does not exist, got: {output:?}"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("--config") && stderr.contains(missing_file.to_str().expect("utf8 path")),
"expected the missing --config path named in the error, got: {stderr:?}"
);
}