Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

drift: keep the Arguments line one code span when an argument holds a backtick (#1393)

Hashem Alkhanaizi743f3bb5438d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a7ea9864..8cec86f9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,9 @@
## Unreleased
+- The `Arguments:` line of `drift-report.md` stays one code span when an argument holds a
+ backtick: it opens and closes with more backticks than any run inside, as CommonMark asks, where
+ a single backtick used to end the span early (#1323).
- `setup --dry-run` and `setup --remove --dry-run` list each file as `would add`, `would update` or
`would remove`, instead of `added`, `updated` or `removed` as if it had been written; the real
run and `--json` are unchanged (#1371).
diff --git a/src/drift.rs b/src/drift.rs
index 3e0013e1..a6338a99 100644
--- a/src/drift.rs
+++ b/src/drift.rs
@@ -187,13 +187,25 @@ fn quoted(words: &[String]) -> String {
.join(" ")
}
+/// `text` as one Markdown code span: CommonMark closes a span only at a run of
+/// backticks as long as the one that opened it, and drops one space on each
+/// side, so the padding keeps a backtick at either end apart from the fence.
+fn code_span(text: &str) -> String {
+ let longest = text.split(|c| c != '`').map(str::len).max().unwrap_or(0);
+ if longest == 0 {
+ return format!("`{text}`");
+ }
+ let ticks = "`".repeat(longest + 1);
+ format!("{ticks} {text} {ticks}")
+}
+
fn diffs(out: &mut String, drifted: &[Drifted], replay: &ReplayOutcome) {
for d in drifted {
*out += &format!(
- "\n### {} ({})\n\nArguments: `{}`\n",
+ "\n### {} ({})\n\nArguments: {}\n",
d.case,
d.verdict,
- quoted(&d.args)
+ code_span(&quoted(&d.args))
);
let Some(f) = replay.failures.iter().find(|f| f.case == d.case) else {
continue;
@@ -263,7 +275,7 @@ fn report(
mod tests {
use std::path::PathBuf;
- use super::{DriftOutcome, Drifted, report};
+ use super::{DriftOutcome, Drifted, code_span, report};
use crate::app::{Failure, Problem, ReplayOutcome};
fn replay(build: &[&str], passed: usize, failures: Vec<Failure>) -> ReplayOutcome {
@@ -440,4 +452,21 @@ mod tests {
assert_eq!(inside, "-`\u{200b}``js\n+`\u{200b}``ts\n");
assert_eq!(text.matches("```").count(), 2, "{text}");
}
+
+ #[test]
+ fn a_backtick_in_the_arguments_cannot_close_their_code_span() {
+ let original = replay(&["node", "cli.js"], 2, vec![]);
+ let mut case = drifted("md");
+ case.args = vec!["a`b c".into()];
+ let text = report(&outcome(vec![case], vec![], vec![]), &original, None);
+ assert!(text.contains("\nArguments: `` 'a`b c' ``\n"), "{text}");
+ }
+
+ #[test]
+ fn code_spans_open_with_more_backticks_than_they_hold() {
+ assert_eq!(code_span("--name 'a b'"), "`--name 'a b'`");
+ assert_eq!(code_span("'a``b'"), "``` 'a``b' ```");
+ assert_eq!(code_span("`a"), "`` `a ``");
+ assert_eq!(code_span("a`"), "`` a` ``");
+ }
}