From 11d10b4e473a99d005365b04f3633b79d513a38e Mon Sep 17 00:00:00 2001 From: Slack Coder Date: Tue, 18 Apr 2023 16:38:12 -0500 Subject: [PATCH] cli-test: Support '[..]'s which preserve spacing By mapping all '[.. ]'s with any number of spaces inside them into '[..]'s, examples can preserve text formatting while using this glob pattern. The downside is failed output will show '[..]' in place of the original version with spaces. --- radicle-cli-test/src/lib.rs | 65 ++++++++++++++++++- radicle-cli/examples/rad-patch.md | 10 +-- .../workflow/4-patching-contributor.md | 4 +- .../workflow/5-patching-maintainer.md | 12 ++-- 4 files changed, 76 insertions(+), 15 deletions(-) diff --git a/radicle-cli-test/src/lib.rs b/radicle-cli-test/src/lib.rs index b6b3cad2..1c36f6e2 100644 --- a/radicle-cli-test/src/lib.rs +++ b/radicle-cli-test/src/lib.rs @@ -163,6 +163,31 @@ impl TestFormula { Ok(self) } + /// Convert instances of '[.. ]' to '[..]' where the number of ' 's are arbitrary. + /// + /// Supporting these bracket types help support using the '[..]' pattern while preserving + /// spaces important for text alignment. + fn map_spaced_brackets(s: &str) -> String { + let mut ret = String::new(); + let mut pos = 0; + + for c in s.chars() { + match (c, pos) { + ('[', 0) => pos += 1, + (' ', 1) => continue, + ('.', 1) => pos += 1, + ('.', 2) => pos += 1, + ('.', 3) => continue, + (' ', 3) => continue, + (']', 3) => pos = 0, + (_, _) => pos = 0, + } + ret.push(c); + } + + ret + } + pub fn run(&mut self) -> Result { let assert = Assert::new().substitutions(self.subs.clone()); @@ -213,12 +238,14 @@ impl TestFormula { match result { Ok(output) => { let assert = OutputAssert::new(output).with_assert(assert.clone()); + let expected = Self::map_spaced_brackets(&assertion.expected); + let matches = assert.stdout_matches(&expected); match assertion.exit { ExitStatus::Success => { - assert.stdout_matches(&assertion.expected).success(); + matches.success(); } ExitStatus::Failure => { - assert.stdout_matches(&assertion.expected).failure(); + matches.failure(); } } } @@ -340,4 +367,38 @@ name = "radicle-cli-test" .unwrap(); formula.run().unwrap(); } + + #[test] + fn test_example_spaced_brackets() { + let input = r#" +Running a simple command such as `head`: +``` +$ echo " hello" +[..]hello +$ echo " hello" +[.. ]hello +$ echo " hello" +[ ..]hello +$ echo "[bug, good-first-issue]" +[bug, good-first-issue] +$ echo "[bug, good-first-issue]" +[bug, [ .. ]-issue] +$ echo "[bug, good-first-issue]" +[bug, [ ... ]-issue] +``` +"# + .trim() + .as_bytes() + .to_owned(); + + let mut formula = TestFormula::new(); + formula + .cwd(env!("CARGO_MANIFEST_DIR")) + .read( + Path::new("test.md"), + io::BufReader::new(io::Cursor::new(input)), + ) + .unwrap(); + formula.run().unwrap(); + } } diff --git a/radicle-cli/examples/rad-patch.md b/radicle-cli/examples/rad-patch.md index 765f5166..84027964 100644 --- a/radicle-cli/examples/rad-patch.md +++ b/radicle-cli/examples/rad-patch.md @@ -44,7 +44,7 @@ $ rad patch ╭─────────────────────────────────────────────────────────────────────────────────────────╮ │ Define power requirements 191a14e R0 3e674d1 (flux-capacitor-power) ahead 1, behind 0 │ ├─────────────────────────────────────────────────────────────────────────────────────────┤ -│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) 3 months ago │ +│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) [.. ]│ ╰─────────────────────────────────────────────────────────────────────────────────────────╯ ``` ``` @@ -57,7 +57,7 @@ $ rad patch show 191a14e520f2eeff7c0e3ee0a5523c5217eecb89 │ │ │ See details. │ ├─────────────────────────────────────────────────────────────────────────────────────────┤ -│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) 3 months ago │ +│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) [.. ]│ ╰─────────────────────────────────────────────────────────────────────────────────────────╯ commit 3e674d1a1df90807e934f9ae5da2591dd6848a33 @@ -117,8 +117,8 @@ $ rad patch ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Define power requirements 191a14e R1 27857ec (flux-capacitor-power, patch/191a14e) ahead 2, behind 0 │ ├──────────────────────────────────────────────────────────────────────────────────────────────────────┤ -│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) 3 months ago │ -│ ↑ updated to b8f7bfbbb3c6a207b349e9f45bf535c706805871 (27857ec) 3 months ago │ -│ ✓ accepted by z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) 3 months ago │ +│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) [.. ]│ +│ ↑ updated to b8f7bfbbb3c6a207b349e9f45bf535c706805871 (27857ec) [.. ]│ +│ ✓ accepted by z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) [.. ]│ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯ ``` diff --git a/radicle-cli/examples/workflow/4-patching-contributor.md b/radicle-cli/examples/workflow/4-patching-contributor.md index 62bd7f77..3f0946a5 100644 --- a/radicle-cli/examples/workflow/4-patching-contributor.md +++ b/radicle-cli/examples/workflow/4-patching-contributor.md @@ -44,7 +44,7 @@ $ rad patch ╭─────────────────────────────────────────────────────────────────────────────────────────╮ │ Define power requirements a07ef77 R0 3e674d1 (flux-capacitor-power) ahead 1, behind 0 │ ├─────────────────────────────────────────────────────────────────────────────────────────┤ -│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (you) 3 months ago │ +│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (you) [.. ]│ ╰─────────────────────────────────────────────────────────────────────────────────────────╯ $ rad patch show a07ef7743a32a2e902672ea3526d1db6ee08108a ╭─────────────────────────────────────────────────────────────────────────────────────────╮ @@ -55,7 +55,7 @@ $ rad patch show a07ef7743a32a2e902672ea3526d1db6ee08108a │ │ │ See details. │ ├─────────────────────────────────────────────────────────────────────────────────────────┤ -│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (you) 3 months ago │ +│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (you) [.. ]│ ╰─────────────────────────────────────────────────────────────────────────────────────────╯ commit 3e674d1a1df90807e934f9ae5da2591dd6848a33 diff --git a/radicle-cli/examples/workflow/5-patching-maintainer.md b/radicle-cli/examples/workflow/5-patching-maintainer.md index 1ccab5cf..68cc8218 100644 --- a/radicle-cli/examples/workflow/5-patching-maintainer.md +++ b/radicle-cli/examples/workflow/5-patching-maintainer.md @@ -30,8 +30,8 @@ $ rad patch ╭───────────────────────────────────────────────────────────────────────────────────╮ │ Define power requirements a07ef77 R1 27857ec ahead 2, behind 0 │ ├───────────────────────────────────────────────────────────────────────────────────┤ -│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk 3 months ago │ -│ ↑ updated to 11483929d8714a92992229f65433e06288f3b760 (27857ec) 3 months ago │ +│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk [.. ]│ +│ ↑ updated to 11483929d8714a92992229f65433e06288f3b760 (27857ec) [.. ]│ ╰───────────────────────────────────────────────────────────────────────────────────╯ ``` @@ -80,9 +80,9 @@ $ rad patch --merged ╭───────────────────────────────────────────────────────────────────────────────────────────────╮ │ Define power requirements a07ef77 R2 f6484e0 (flux-capacitor-power, master) ahead 3, behind 0 │ ├───────────────────────────────────────────────────────────────────────────────────────────────┤ -│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk 3 months ago │ -│ ↑ updated to 11483929d8714a92992229f65433e06288f3b760 (27857ec) 3 months ago │ -│ ↑ updated to 0795d619232479e910f95bb9c873ee1ec305c43c (f6484e0) 3 months ago │ -│ ✓ merged by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) 3 months ago │ +│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk [.. ]│ +│ ↑ updated to 11483929d8714a92992229f65433e06288f3b760 (27857ec) [.. ]│ +│ ↑ updated to 0795d619232479e910f95bb9c873ee1ec305c43c (f6484e0) [.. ]│ +│ ✓ merged by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) [.. ]│ ╰───────────────────────────────────────────────────────────────────────────────────────────────╯ ```