#[allow(dead_code)]
mod support;
use mariadb_cli::i18n::report;
use support::Sandbox;
const REFUSED: [&str; 3] = ["--filter", "broken", "meta"];
fn refusal(sandbox: &Sandbox, language: &str) -> serde_json::Value {
let mut args = vec!["--lang", language];
args.extend(REFUSED);
args.push("version");
let outcome = sandbox.run(&args);
assert_ne!(outcome.code, 0, "the sample invocation must be refused");
assert!(
outcome.out.is_empty(),
"a failure never reaches the primary channel: {}",
outcome.out
);
outcome.failure()
}
#[test]
fn the_mark_on_the_command_line_changes_the_prose_and_nothing_else() {
let sandbox = Sandbox::new();
let english = refusal(&sandbox, "en");
let portuguese = refusal(&sandbox, "pt-BR");
assert_ne!(
english["error"]["message"], portuguese["error"]["message"],
"the language did not reach the reader"
);
for field in ["code", "category", "exit", "retryable"] {
assert_eq!(
english["error"][field], portuguese["error"][field],
"`{field}` must not change with the language"
);
}
assert_eq!(english["error"]["code"], "filter.no_operator");
}
#[test]
fn asking_for_portuguese_is_answered_in_portuguese() {
let sandbox = Sandbox::new();
let message = refusal(&sandbox, "pt-BR")["error"]["message"]
.as_str()
.unwrap_or_default()
.to_owned();
assert!(
message.contains("filtro"),
"the refusal was not written in Portuguese: {message}"
);
}
#[test]
fn asking_for_english_is_answered_in_english() {
let sandbox = Sandbox::new();
let message = refusal(&sandbox, "en")["error"]["message"]
.as_str()
.unwrap_or_default()
.to_owned();
assert!(
message.contains("operator"),
"the refusal was not written in English: {message}"
);
}
#[test]
fn the_mark_reaches_a_refusal_the_parser_itself_produced() {
let sandbox = Sandbox::new();
let unparsable = ["query", "no-such-subcommand-exists"];
let mut english = vec!["--lang", "en"];
english.extend(unparsable);
let mut portuguese = vec!["--lang", "pt-BR"];
portuguese.extend(unparsable);
let english = sandbox.run(&english);
let portuguese = sandbox.run(&portuguese);
for outcome in [&english, &portuguese] {
assert_ne!(outcome.code, 0, "the sample invocation must be refused");
}
let english = english.failure();
let portuguese = portuguese.failure();
assert_eq!(
english["error"]["code"], "usage.invalid_arguments",
"the refusal must be the one the parser produces"
);
assert_ne!(
english["error"]["message"], portuguese["error"]["message"],
"the mark did not reach a refusal the parser produced"
);
let english = english["error"]["message"]
.as_str()
.unwrap_or_default()
.to_owned();
assert!(
english.contains("arguments"),
"the parser's refusal was not written in English: {english}"
);
let joined = sandbox.run(&["--lang=en", "query", "no-such-subcommand-exists"]);
assert_ne!(joined.code, 0, "the sample invocation must be refused");
let joined = joined.failure()["error"]["message"]
.as_str()
.unwrap_or_default()
.to_owned();
assert_eq!(
joined, english,
"`--lang=en` must reach the parser's refusal the same way `--lang en` does"
);
}
#[test]
fn the_stored_key_decides_when_no_mark_was_given() {
let sandbox = Sandbox::new();
let stored = sandbox.run(&["config", "set", "i18n.lang", "pt-BR"]);
assert_eq!(
stored.code, 0,
"the language must be storable: {}",
stored.err
);
let mut args = REFUSED.to_vec();
args.push("version");
let message = sandbox.run(&args).failure()["error"]["message"]
.as_str()
.unwrap_or_default()
.to_owned();
assert!(
message.contains("filtro"),
"the stored language did not reach the reader: {message}"
);
}
#[test]
fn the_mark_outranks_the_stored_key() {
let sandbox = Sandbox::new();
assert_eq!(
sandbox.run(&["config", "set", "i18n.lang", "pt-BR"]).code,
0
);
let message = refusal(&sandbox, "en")["error"]["message"]
.as_str()
.unwrap_or_default()
.to_owned();
assert!(
message.contains("operator"),
"the mark did not outrank the stored key: {message}"
);
}
#[derive(Clone, Default)]
struct Captured(std::sync::Arc<std::sync::Mutex<Vec<u8>>>);
impl Captured {
fn text(&self) -> String {
let held = self
.0
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
String::from_utf8_lossy(&held).into_owned()
}
}
impl std::io::Write for Captured {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
self.0
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.extend_from_slice(bytes);
Ok(bytes.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
impl tracing_subscriber::fmt::MakeWriter<'_> for Captured {
type Writer = Self;
fn make_writer(&self) -> Self::Writer {
self.clone()
}
}
fn progress_line(language: &str) -> serde_json::Value {
let captured = Captured::default();
let channel = tracing_subscriber::fmt()
.json()
.with_max_level(tracing::Level::INFO)
.with_writer(captured.clone())
.finish();
tracing::subscriber::with_default(channel, || {
mariadb_cli::i18n::install(Some(language), None).expect("carried by this build");
report::copy_table("orders");
});
let written = captured.text();
let line =
written.trim_end().lines().next_back().unwrap_or_else(|| {
panic!("no progress reached the auxiliary channel for `{language}`")
});
serde_json::from_str(line)
.unwrap_or_else(|cause| panic!("the progress line must be parseable: {cause}\n{line}"))
}
#[test]
fn a_progress_line_changes_with_the_language_and_keeps_its_identifier() {
let english = progress_line("en");
let portuguese = progress_line("pt-BR");
assert_eq!(
english["fields"]["message_id"],
report::COPY_TABLE,
"the line lost the identifier a collector branches on"
);
assert_eq!(
english["fields"]["message_id"], portuguese["fields"]["message_id"],
"the identifier must not change with the language"
);
let said_in_english = english["fields"]["message"].as_str().unwrap_or_default();
let said_in_portuguese = portuguese["fields"]["message"].as_str().unwrap_or_default();
assert_ne!(
said_in_english, said_in_portuguese,
"the language did not reach the reader of the auxiliary channel"
);
assert!(
said_in_english.contains("copying") && said_in_english.contains("orders"),
"the English progress line read: {said_in_english}"
);
assert!(
said_in_portuguese.contains("copiando") && said_in_portuguese.contains("orders"),
"the Portuguese progress line read: {said_in_portuguese}"
);
}
#[test]
fn the_plain_text_shape_follows_the_language() {
let sandbox = Sandbox::new();
let portuguese = sandbox
.run(&["--lang", "pt-BR", "conn", "list", "--output-format", "text"])
.out;
let english = sandbox
.run(&["--lang", "en", "conn", "list", "--output-format", "text"])
.out;
assert!(
portuguese.contains("sem linhas"),
"the empty table was not written in Portuguese: {portuguese}"
);
assert!(
english.contains("no rows"),
"the empty table was not written in English: {english}"
);
assert_ne!(
portuguese, english,
"the language never reached the primary channel"
);
}
#[test]
fn the_machine_shapes_do_not_move_with_the_language() {
let sandbox = Sandbox::new();
for shape in ["json", "ndjson", "tsv"] {
let portuguese = sandbox
.run(&["--lang", "pt-BR", "conn", "list", "--output-format", shape])
.out;
let english = sandbox
.run(&["--lang", "en", "conn", "list", "--output-format", shape])
.out;
assert_eq!(
portuguese, english,
"the `{shape}` shape moved with the language, and a program reads it"
);
}
}