fix: tests

This commit is contained in:
n4n5
2025-11-09 17:08:10 -07:00
parent bf9e9880b3
commit 0f10e216cf
5 changed files with 87 additions and 48 deletions
+4
View File
@@ -85,7 +85,11 @@ pub fn main() {
phf_map.entry("sha256sum", map_value.clone());
phf_map.entry("sha384sum", map_value.clone());
phf_map.entry("sha512sum", map_value.clone());
phf_map.entry("sha3sum", map_value.clone());
phf_map.entry("shake128sum", map_value.clone());
phf_map.entry("shake256sum", map_value.clone());
phf_map.entry("b2sum", map_value.clone());
phf_map.entry("b3sum", map_value.clone());
}
_ => {
phf_map.entry(krate, map_value.clone());
+66 -31
View File
@@ -291,20 +291,27 @@ fn main() -> io::Result<()> {
}
println!("Writing to utils");
let hashsum_cmd = utils
.iter()
.find(|n| *n.0 == "hashsum")
.unwrap()
.clone()
.1
.1;
for (&name, (_, command)) in utils {
let name = match name {
let (utils_name, usage_name, command) = match name {
"[" => {
continue;
}
name if is_hashsum_family(name) => {
// These use the hashsum
"hashsum"
("hashsum", name, &hashsum_cmd)
}
n => n,
n => (n, n, command),
};
let p = format!("docs/src/utils/{name}.md");
let p = format!("docs/src/utils/{usage_name}.md");
let fluent = File::open(format!("src/uu/{name}/locales/en-US.ftl"))
let fluent = File::open(format!("src/uu/{utils_name}/locales/en-US.ftl"))
.and_then(|mut f: File| {
let mut s = String::new();
f.read_to_string(&mut s)?;
@@ -316,35 +323,27 @@ fn main() -> io::Result<()> {
MDWriter {
w: Box::new(f),
command: command(),
name,
name: usage_name,
tldr_zip: &mut tldr_zip,
utils_per_platform: &utils_per_platform,
fluent,
fluent_key: utils_name.to_string(),
}
.markdown()?;
println!("Wrote to '{p}'");
} else {
println!("Error writing to {p}");
}
writeln!(summary, "* [{name}](utils/{name}.md)")?;
writeln!(summary, "* [{usage_name}](utils/{usage_name}.md)")?;
}
Ok(())
}
struct MDWriter<'a, 'b> {
w: Box<dyn Write>,
command: Command,
name: &'a str,
tldr_zip: &'b mut Option<ZipArchive<File>>,
utils_per_platform: &'b HashMap<&'b str, Vec<String>>,
fluent: Option<String>,
}
fn fix_usage(name: &str, usage: String) -> String {
match name {
"test" => {
// replace to [ but not the first two line
return usage
usage
.lines()
.enumerate()
.map(|(i, l)| {
@@ -355,21 +354,45 @@ fn fix_usage(name: &str, usage: String) -> String {
}
})
.collect::<Vec<_>>()
.join("\n");
.join("\n")
}
"hashsum" => usage,
name if is_hashsum_family(name) => usage.replace("--<digest> ", ""),
name if is_hashsum_family(name) => {
usage.replace("--<digest> ", "").replace("hashsum", name)
}
_ => usage,
}
}
fn is_hashsum_family(name: &str) -> bool {
match name {
"md5sum" | "sha1sum" | "sha224sum" | "sha256sum" | "sha384sum" | "sha512sum"
| "sha3sum" | "sha3-224sum" | "sha3-256sum" | "sha3-384sum" | "sha3-512sum"
| "shake128sum" | "shake256sum" | "b2sum" | "b3sum" => true,
_ => false,
}
matches!(
name,
"md5sum"
| "sha1sum"
| "sha224sum"
| "sha256sum"
| "sha384sum"
| "sha512sum"
| "sha3sum"
| "sha3-224sum"
| "sha3-256sum"
| "sha3-384sum"
| "sha3-512sum"
| "shake128sum"
| "shake256sum"
| "b2sum"
| "b3sum"
)
}
struct MDWriter<'a, 'b> {
w: Box<dyn Write>,
command: Command,
name: &'a str,
tldr_zip: &'b mut Option<ZipArchive<File>>,
utils_per_platform: &'b HashMap<&'b str, Vec<String>>,
fluent: Option<String>,
fluent_key: String,
}
impl MDWriter<'_, '_> {
@@ -389,7 +412,6 @@ impl MDWriter<'_, '_> {
fn extract_fluent_value(&self, key: &str) -> Option<String> {
let content = self.fluent.as_ref()?;
let resource = parser::parse(content.clone()).ok()?;
for entry in resource.body {
if let Entry::Message(Message {
id,
@@ -400,8 +422,19 @@ impl MDWriter<'_, '_> {
if id.name == key {
// Simple text extraction - just concatenate text elements
let mut result = String::new();
use fluent_syntax::ast::{
Expression, InlineExpression,
PatternElement::{Placeable, TextElement},
};
for element in elements {
if let fluent_syntax::ast::PatternElement::TextElement { value } = element {
if let TextElement { ref value } = element {
result.push_str(&value);
}
if let Placeable {
expression:
Expression::Inline(InlineExpression::StringLiteral { ref value }),
} = element
{
result.push_str(&value);
}
}
@@ -460,7 +493,7 @@ impl MDWriter<'_, '_> {
/// # Errors
/// Returns an error if the writer fails.
fn usage(&mut self) -> io::Result<()> {
if let Some(usage) = self.extract_fluent_value(&format!("{}-usage", self.name)) {
if let Some(usage) = self.extract_fluent_value(&format!("{}-usage", self.fluent_key)) {
let usage = fix_usage(self.name, usage);
writeln!(self.w, "\n```")?;
writeln!(self.w, "{usage}")?;
@@ -473,7 +506,7 @@ impl MDWriter<'_, '_> {
/// # Errors
/// Returns an error if the writer fails.
fn about(&mut self) -> io::Result<()> {
if let Some(about) = self.extract_fluent_value(&format!("{}-about", self.name)) {
if let Some(about) = self.extract_fluent_value(&format!("{}-about", self.fluent_key)) {
writeln!(self.w, "{about}")
} else {
Ok(())
@@ -483,7 +516,9 @@ impl MDWriter<'_, '_> {
/// # Errors
/// Returns an error if the writer fails.
fn after_help(&mut self) -> io::Result<()> {
if let Some(after_help) = self.extract_fluent_value(&format!("{}-after-help", self.name)) {
if let Some(after_help) =
self.extract_fluent_value(&format!("{}-after-help", self.fluent_key))
{
writeln!(self.w, "\n\n{after_help}")
} else {
Ok(())
@@ -554,7 +589,7 @@ impl MDWriter<'_, '_> {
writeln!(self.w, "</dt>")?;
let help_text = arg.get_help().unwrap_or_default().to_string();
// Try to resolve Fluent key if it looks like one, otherwise use as-is
let resolved_help = if help_text.starts_with(&format!("{}-help-", self.name)) {
let resolved_help = if help_text.starts_with(&format!("{}-help-", self.fluent_key)) {
self.extract_fluent_value(&help_text).unwrap_or(help_text)
} else {
help_text
+1 -1
View File
@@ -3,7 +3,7 @@ test-usage = test EXPRESSION
test
{"[ EXPRESSION ]"}
{"[ ]"}
{"[ OPTION ]"}
{"[ OPTION"}
test-after-help = Exit with the status determined by EXPRESSION.
An omitted EXPRESSION defaults to false.
+1 -1
View File
@@ -3,7 +3,7 @@ test-usage = test EXPRESSION
test
{"[ EXPRESSION ]"}
{"[ ]"}
{"[ OPTION ]"}
{"[ OPTION"}
test-after-help = Quitter avec le statut déterminé par EXPRESSION.
Une EXPRESSION omise vaut false par défaut.
+15 -15
View File
@@ -75,22 +75,25 @@ fn get_doc_file_from_output(output: &str) -> (String, String) {
#[test]
fn uudoc_check_test() {
let pages = run_write_doc();
println!("Pages written: {pages:?}\n");
// assert wrote to the correct file
let path_test = pages.iter().find(|line| line.contains("test.md")).unwrap();
let (_correct_path, content) = get_doc_file_from_output(path_test);
let (correct_path, content) = get_doc_file_from_output(path_test);
// open the file
assert!(content.contains(
"```
assert!(
content.contains(
"```
test EXPRESSION
test
[ EXPRESSION ]
[ ]
[ OPTION
```
"
));
",
),
"{} does not contains the required text",
correct_path
);
}
#[test]
@@ -104,24 +107,21 @@ fn uudoc_check_sums() {
"sha384sum",
"sha512sum",
"sha3sum",
"sha3-224sum",
"sha3-256sum",
"sha3-384sum",
"sha3-512sum",
"shake128sum",
"shake256sum",
"b2sum",
"b3sum",
];
for one_sum in sums {
let output_path = pages.iter().find(|line| line.contains(one_sum)).unwrap();
let output_path = pages
.iter()
.find(|one_line| one_line.contains(one_sum))
.expect(&format!("{one_sum} was not generated in {pages:?}"));
let (correct_path, content) = get_doc_file_from_output(output_path);
let formatted = format!("```\n{} [OPTIONS]... [FILE]...\n```", one_sum);
let formatted = format!("```\n{one_sum} [OPTIONS]... [FILE]...\n```");
assert!(
content.contains(&formatted),
"Content of {} does not contain the expected format: {}",
correct_path,
formatted
"Content of {correct_path} does not contain the expected format: {formatted}",
);
}
}