mirror of
https://github.com/Momoko-Ayase/Senbei.git
synced 2026-11-24 02:52:47 +08:00
feat: localize console output and add a run observer
Engine errors, integrity findings, and driver outcomes carry message codes from senbei-i18n, with their English unchanged, so reports, logs, and --json stay byte-identical. Verbose pipeline steps go through a progress sink instead of println!, and an error chain is translated through its first coded cause. The CLI takes the display language from SENBEI_LANG, or else from the system's preferred UI language, and translates console text: usage errors, help, per-file lines, the run summary, and the summary lines of the inspect report. Structural research evidence and saved logs stay English. When a translation leaves out context the English chain has, the English follows on a muted line. A front end can attach a RunObserver and a CancelToken to the Collector to follow a run input by input, receive verbose steps as events, and stop before the next input. A cancelled report says so. The CLI never cancels, so its JSON is unchanged.
This commit is contained in:
1 parent
9481b0ccf9
commit
4853fe22c0
39 files changed
+2016
-684
No files matched your search
@@ -10,6 +10,7 @@ fn senbei() -> Command {
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_senbei"));
|
||||
command
|
||||
.arg("--no-pause")
|
||||
.env("SENBEI_LANG", "en")
|
||||
.env_remove("NO_COLOR")
|
||||
.env("TERM", "xterm-256color");
|
||||
command
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
//! The console follows SENBEI_LANG (or the system language); saved logs and
|
||||
//! `--json` reports stay English in every language.
|
||||
|
||||
#[path = "support/fixtures.rs"]
|
||||
mod fixtures;
|
||||
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
use senbei_i18n::{Cli, Lang, Localize};
|
||||
|
||||
fn senbei(lang: &str) -> Command {
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_senbei"));
|
||||
command.arg("--no-pause").env("SENBEI_LANG", lang);
|
||||
command
|
||||
}
|
||||
|
||||
fn text(bytes: &[u8]) -> &str {
|
||||
std::str::from_utf8(bytes).expect("UTF-8 console output")
|
||||
}
|
||||
|
||||
fn fixture(root: &Path) -> std::path::PathBuf {
|
||||
let path = root.join("global-metadata.dat");
|
||||
std::fs::write(&path, fixtures::obfuscated_metadata()).unwrap();
|
||||
path
|
||||
}
|
||||
|
||||
fn without_timings(json: &str) -> String {
|
||||
let mut value: serde_json::Value = serde_json::from_str(json).expect("one JSON document");
|
||||
fn strip(value: &mut serde_json::Value) {
|
||||
match value {
|
||||
serde_json::Value::Object(map) => {
|
||||
map.remove("duration_ms");
|
||||
map.values_mut().for_each(strip);
|
||||
}
|
||||
serde_json::Value::Array(items) => items.iter_mut().for_each(strip),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
strip(&mut value);
|
||||
value.to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn usage_errors_and_help_follow_the_language() {
|
||||
for lang in [Lang::ZhCn, Lang::ZhTw, Lang::Ja, Lang::Ko] {
|
||||
let output = senbei(lang.tag()).arg("--bogus").output().unwrap();
|
||||
assert_eq!(output.status.code(), Some(2));
|
||||
let expected = format!(
|
||||
"{}: {}",
|
||||
Cli::Error.localize(lang),
|
||||
Cli::UnknownOption {
|
||||
option: "--bogus".into()
|
||||
}
|
||||
.localize(lang)
|
||||
);
|
||||
assert!(text(&output.stderr).contains(&expected), "{lang:?}");
|
||||
assert!(text(&output.stdout).contains(&*Cli::HelpJson.localize(lang)));
|
||||
assert_ne!(
|
||||
Cli::HelpJson.localize(lang),
|
||||
Cli::HelpJson.localize(Lang::En),
|
||||
"{lang:?} is still English"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unknown_language_falls_back_to_english() {
|
||||
for value in ["fr", "C", "x-unknown"] {
|
||||
let output = senbei(value).arg("--bogus").output().unwrap();
|
||||
assert!(
|
||||
text(&output.stderr).contains("error: unknown option '--bogus'"),
|
||||
"{value}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_console_is_translated_while_logs_and_json_stay_english() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let input = fixture(temp.path());
|
||||
let out = temp.path().join("out");
|
||||
let output = senbei("zh-CN")
|
||||
.arg(&input)
|
||||
.arg("--out")
|
||||
.arg(&out)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(output.status.success(), "{}", text(&output.stderr));
|
||||
let summary = Cli::Summary {
|
||||
unpacked: "0".into(),
|
||||
skipped: "0".into(),
|
||||
errors: "0".into(),
|
||||
suspect: "0".into(),
|
||||
metadata: "1".into(),
|
||||
};
|
||||
let stdout = text(&output.stdout);
|
||||
assert!(stdout.contains(&*summary.localize(Lang::ZhCn)), "{stdout}");
|
||||
assert!(!stdout.contains("unpacked"), "{stdout}");
|
||||
|
||||
let log = std::fs::read_dir(&out)
|
||||
.unwrap()
|
||||
.flatten()
|
||||
.map(|entry| entry.path())
|
||||
.find(|path| path.extension().is_some_and(|value| value == "log"))
|
||||
.expect("a run log");
|
||||
let log = std::fs::read_to_string(log).unwrap();
|
||||
assert!(
|
||||
log.contains("summary: 0 unpacked · 0 skipped · 0 errors · 0 suspect · 1 metadata"),
|
||||
"{log}"
|
||||
);
|
||||
|
||||
let json = |lang: &str| {
|
||||
let output = senbei(lang)
|
||||
.args(["inspect", "--json"])
|
||||
.arg(&input)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(output.status.success());
|
||||
without_timings(text(&output.stdout))
|
||||
};
|
||||
assert_eq!(json("ko"), json("en"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inspect_text_translates_summary_lines_but_keeps_values() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let input = fixture(temp.path());
|
||||
let output = senbei("ja").arg("inspect").arg(&input).output().unwrap();
|
||||
assert!(output.status.success());
|
||||
let stdout = text(&output.stdout);
|
||||
let recovery = Cli::StaticRecovery {
|
||||
status: senbei_i18n::Term::Succeeded.localize(Lang::Ja).into_owned(),
|
||||
};
|
||||
assert!(stdout.contains(&*recovery.localize(Lang::Ja)), "{stdout}");
|
||||
assert!(!stdout.contains("static recovery"), "{stdout}");
|
||||
// Paths and the structural evidence are values, never translated.
|
||||
assert!(stdout.contains(&*input.display().to_string()), "{stdout}");
|
||||
assert!(stdout.contains("tried_seeds"), "{stdout}");
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
//! A front end follows a run through an observer and can stop it between
|
||||
//! inputs. The CLI uses neither, so its output and reports are unaffected.
|
||||
|
||||
#[path = "support/fixtures.rs"]
|
||||
mod fixtures;
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use senbei_engine::android::ResourceLimits;
|
||||
use senbei_io::job::{self, Collector, Mode};
|
||||
use senbei_io::observe::{CancelToken, RunObserver};
|
||||
use senbei_io::report::InputReport;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Recorder {
|
||||
events: Mutex<Vec<String>>,
|
||||
/// Cancelled when the first input finishes.
|
||||
stop_after_first: Option<CancelToken>,
|
||||
}
|
||||
|
||||
impl Recorder {
|
||||
fn push(&self, event: String) {
|
||||
self.events.lock().unwrap().push(event);
|
||||
}
|
||||
|
||||
fn events(&self) -> Vec<String> {
|
||||
self.events.lock().unwrap().clone()
|
||||
}
|
||||
}
|
||||
|
||||
fn parent_name(path: &Path) -> String {
|
||||
path.parent()
|
||||
.and_then(Path::file_name)
|
||||
.map(|name| name.to_string_lossy().into_owned())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
impl RunObserver for Recorder {
|
||||
fn scanned(&self, inputs: usize) {
|
||||
self.push(format!("scanned {inputs}"));
|
||||
}
|
||||
|
||||
fn input_started(&self, path: &Path) {
|
||||
self.push(format!("started {}", parent_name(path)));
|
||||
}
|
||||
|
||||
fn progress(&self, event: &senbei_engine::progress::Event) {
|
||||
if let senbei_engine::progress::Event::Step { step, .. } = event {
|
||||
self.push(
|
||||
format!("step {step:?}")
|
||||
.split(' ')
|
||||
.take(2)
|
||||
.collect::<Vec<_>>()
|
||||
.join(" "),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn input_finished(&self, input: &InputReport) {
|
||||
self.push(format!("finished {}", parent_name(Path::new(&input.path))));
|
||||
if let Some(cancel) = &self.stop_after_first {
|
||||
cancel.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Two applications, each with its own obfuscated metadata blob.
|
||||
fn two_apps(root: &Path) {
|
||||
for app in ["a", "b"] {
|
||||
let dir = root.join(app);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::fs::write(
|
||||
dir.join("global-metadata.dat"),
|
||||
fixtures::obfuscated_metadata(),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn run(root: &Path, out: &Path, collector: &mut Collector) {
|
||||
job::run_folder_reported(
|
||||
root,
|
||||
Some(out),
|
||||
2,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
&ResourceLimits::default(),
|
||||
collector,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_observer_sees_each_input_start_progress_and_finish() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
two_apps(temp.path());
|
||||
let recorder = Arc::new(Recorder::default());
|
||||
let mut collector = Collector::new(Mode::Unpack).with_observer(recorder.clone());
|
||||
run(temp.path(), &temp.path().join("out"), &mut collector);
|
||||
assert_eq!(
|
||||
recorder.events(),
|
||||
[
|
||||
"scanned 2",
|
||||
"started a",
|
||||
"step WriteTo",
|
||||
"finished a",
|
||||
"started b",
|
||||
"step WriteTo",
|
||||
"finished b",
|
||||
]
|
||||
);
|
||||
let report = collector.finish();
|
||||
assert_eq!(report.inputs.len(), 2);
|
||||
assert!(!report.cancelled);
|
||||
assert!(!report.to_json().contains("cancelled"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancelling_stops_before_the_next_input_and_is_reported() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
two_apps(temp.path());
|
||||
let cancel = CancelToken::new();
|
||||
let recorder = Arc::new(Recorder {
|
||||
stop_after_first: Some(cancel.clone()),
|
||||
..Recorder::default()
|
||||
});
|
||||
let mut collector = Collector::new(Mode::Unpack)
|
||||
.with_observer(recorder.clone())
|
||||
.with_cancel(cancel);
|
||||
let out = temp.path().join("out");
|
||||
run(temp.path(), &out, &mut collector);
|
||||
assert_eq!(
|
||||
recorder.events(),
|
||||
["scanned 2", "started a", "step WriteTo", "finished a"]
|
||||
);
|
||||
let report = collector.finish();
|
||||
assert_eq!(report.inputs.len(), 1);
|
||||
assert!(report.cancelled);
|
||||
assert!(report.to_json().contains("\"cancelled\":true"));
|
||||
assert!(out.join("a").join("global-metadata.unpack.dat").is_file());
|
||||
assert!(!out.join("b").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_run_cancelled_before_it_starts_processes_nothing() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
two_apps(temp.path());
|
||||
let cancel = CancelToken::new();
|
||||
cancel.cancel();
|
||||
let mut collector = Collector::new(Mode::DryRun).with_cancel(cancel);
|
||||
run(temp.path(), &temp.path().join("out"), &mut collector);
|
||||
let report = collector.finish();
|
||||
assert!(report.inputs.is_empty());
|
||||
assert!(report.cancelled);
|
||||
}
|
||||
@@ -32,7 +32,7 @@ fn unreadable_unpack_input_blocks_routes_instead_of_inventing_an_attempt() {
|
||||
|
||||
fn senbei() -> Command {
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_senbei"));
|
||||
command.arg("--no-pause");
|
||||
command.arg("--no-pause").env("SENBEI_LANG", "en");
|
||||
command
|
||||
}
|
||||
|
||||
@@ -638,6 +638,7 @@ fn a_relative_path_is_reported_verbatim() {
|
||||
let td = tempfile::tempdir().unwrap();
|
||||
write_plain(td.path(), "plain.exe");
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_senbei"))
|
||||
.env("SENBEI_LANG", "en")
|
||||
.current_dir(td.path())
|
||||
.args(["--no-pause", "inspect", "plain.exe", "--json"])
|
||||
.output()
|
||||
|
||||
@@ -2,7 +2,9 @@ use std::process::Command;
|
||||
|
||||
fn command() -> Command {
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_senbei"));
|
||||
command.args(["--no-pause", "--no-log"]);
|
||||
command
|
||||
.args(["--no-pause", "--no-log"])
|
||||
.env("SENBEI_LANG", "en");
|
||||
command
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,9 @@ use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
fn senbei() -> Command {
|
||||
Command::new(env!("CARGO_BIN_EXE_senbei"))
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_senbei"));
|
||||
command.env("SENBEI_LANG", "en");
|
||||
command
|
||||
}
|
||||
|
||||
fn run(args: &[&str]) -> std::process::Output {
|
||||
|
||||
@@ -835,10 +835,16 @@ fn run_pe(
|
||||
output.detected_kind = Some(kind_name(image.kind).to_owned());
|
||||
output.bytes = Some(image.bytes.len() as u64);
|
||||
output.sha256 = Some(sha256_hex(&image.bytes));
|
||||
let issues: Vec<String> = image
|
||||
.integrity
|
||||
.issues
|
||||
.iter()
|
||||
.map(|issue| issue.text().to_owned())
|
||||
.collect();
|
||||
output.integrity = Some(IntegrityInfo {
|
||||
ok: image.integrity.ok(),
|
||||
fatal: image.integrity.has_fatal(),
|
||||
issues: image.integrity.issues.clone(),
|
||||
issues: issues.clone(),
|
||||
});
|
||||
if expect_failure {
|
||||
output.fail("expected failure, but the input unpacked");
|
||||
@@ -847,13 +853,7 @@ fn run_pe(
|
||||
"expected failure, but the input unpacked",
|
||||
);
|
||||
} else {
|
||||
compare_output(
|
||||
&mut output,
|
||||
&image.bytes,
|
||||
expected,
|
||||
allow_suspect,
|
||||
&image.integrity.issues,
|
||||
);
|
||||
compare_output(&mut output, &image.bytes, expected, allow_suspect, &issues);
|
||||
finish_case(report, &mut output, "unpacked");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
use std::process::Command;
|
||||
|
||||
fn senbei() -> Command {
|
||||
Command::new(env!("CARGO_BIN_EXE_senbei"))
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_senbei"));
|
||||
command.env("SENBEI_LANG", "en");
|
||||
command
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user