runique 3.0.2

A Django-inspired web framework for Rust with ORM, templates, and comprehensive security middleware
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! `test` command: checks the project is set up for `runique_test`, then runs
//! its tests through `cargo test`, one at a time and with their output shown.
use crate::utils::trad::{t, tf};
use anyhow::{Context, Result, bail};
use std::{
    fs,
    io::{IsTerminal, Read, Write},
    path::Path,
    process::{Command, Stdio},
};

const TEST_DIR: &str = "src/runique_test";

/// Set by `runique test` when it writes to a terminal. Its output goes through
/// a pipe, so the test code can't tell it's a terminal and would drop colors.
pub const COLOR_ENV: &str = "RUNIQUE_TEST_COLOR";

/// Entry point of `runique test [file] [test]`, run from the project root.
///
/// No argument runs every test in `src/runique_test/`, `file` only the tests of
/// `src/runique_test/<file>.rs`, and `file` + `test` that single test.
pub fn run_tests(file: Option<&str>, test: Option<&str>) -> Result<()> {
    let cargo_toml = fs::read_to_string("Cargo.toml").context(t("test_runner.no_cargo_toml"))?;
    match test_utils_placement(&cargo_toml) {
        TestUtils::DevOnly => {}
        TestUtils::Missing => bail!("{}", t("test_runner.test_utils_missing")),
        TestUtils::Shipped => bail!("{}", t("test_runner.test_utils_shipped")),
    }

    let dir = Path::new(TEST_DIR);
    let mod_src = fs::read_to_string(dir.join("mod.rs")).context(t("test_runner.no_mod_file"))?;

    let declaration = ["src/main.rs", "src/lib.rs"]
        .iter()
        .filter_map(|path| fs::read_to_string(path).ok())
        .find_map(|src| test_module_declaration(&src));
    match declaration {
        None => bail!("{}", t("test_runner.module_not_declared")),
        Some(false) if !module_file_is_gated(&mod_src) => {
            bail!("{}", t("test_runner.module_not_gated"))
        }
        Some(_) => {}
    }

    let declared = declared_modules(&mod_src);
    let files = test_files(dir)?;
    for stem in files.iter().filter(|stem| !declared.contains(stem)) {
        eprintln!(
            "{}",
            tf("test_runner.file_not_declared_warning", &[stem, stem])
        );
    }
    if let Some(file) = file {
        if !files.iter().any(|stem| stem == file) {
            bail!(
                "{}",
                tf(
                    "test_runner.no_test_file",
                    &[file, files.join(", ").as_str()]
                )
            );
        }
        if !declared.iter().any(|stem| stem == file) {
            bail!("{}", tf("test_runner.file_not_declared", &[file, file]));
        }
    }

    let (filter, exact) = cargo_filter(file, test);
    if let (Some(file), Some(test)) = (file, test) {
        check_test_exists(file, test, &filter)?;
    }

    let color = std::io::stdout().is_terminal() && std::env::var_os("NO_COLOR").is_none();
    let mut args = vec!["test".to_string(), "--".to_string(), filter];
    if exact {
        args.push("--exact".to_string());
    }
    args.extend(["--nocapture".to_string(), "--test-threads=1".to_string()]);
    if color {
        args.extend(["--color".to_string(), "always".to_string()]);
    }

    let mut command = Command::new("cargo");
    command.args(&args).stdout(Stdio::piped());
    if color {
        command.env(COLOR_ENV, "1");
    }
    let mut child = command
        .spawn()
        .context(t("test_runner.cargo_spawn_failed"))?;
    if let Some(output) = child.stdout.take() {
        forward_spaced(output, &mut std::io::stdout(), color)
            .context(t("test_runner.output_failed"))?;
    }
    let status = child.wait().context(t("test_runner.cargo_wait_failed"))?;
    if !status.success() {
        bail!("{}", t("test_runner.tests_failed"));
    }
    Ok(())
}

/// How cargo opens each test's line, and what ends that opening part.
const HEADER_PREFIX: &[u8] = b"test runique_test::";
const HEADER_END: &[u8] = b" ... ";
/// A soft orange (256-color palette): easy on the eyes on a dark terminal,
/// and apart from the cyan step numbers of the trace underneath.
const TEST_NAME_COLOR: &str = "\x1b[38;5;215m";

/// Copies cargo's output as it comes, adding a blank line after each test's
/// `ok`/`FAILED` so tests don't run into each other, and showing each test's
/// name without the `runique_test::` every one of them shares (in color when
/// `color` is set). Byte by byte rather than line by line: cargo only ends a
/// test's `test … ...` line once the test is done, so waiting for full lines
/// would hide which test is running.
fn forward_spaced(mut from: impl Read, to: &mut impl Write, color: bool) -> std::io::Result<()> {
    let mut buf = [0u8; 4096];
    let mut line = Vec::new();
    let mut out = Vec::new();
    // While the line so far could still be a test's opening, it's held back
    // (it sits in `line`) so it can be rewritten once complete.
    let mut holding = false;
    loop {
        let read = from.read(&mut buf)?;
        if read == 0 {
            if holding {
                to.write_all(&line)?;
            }
            return to.flush();
        }
        for &byte in &buf[..read] {
            if byte == b'\n' {
                if holding {
                    out.extend_from_slice(&line);
                    holding = false;
                }
                out.push(byte);
                if is_result_line(&String::from_utf8_lossy(&line)) {
                    out.push(b'\n');
                }
                line.clear();
                continue;
            }
            line.push(byte);
            if line.len() == 1 {
                holding = byte == HEADER_PREFIX[0];
            }
            if !holding {
                out.push(byte);
            } else if line.starts_with(HEADER_PREFIX) && line.ends_with(HEADER_END) {
                out.extend_from_slice(&test_header(&line, color));
                holding = false;
            } else if !HEADER_PREFIX.starts_with(&line) && !line.starts_with(HEADER_PREFIX) {
                out.extend_from_slice(&line);
                holding = false;
            }
        }
        to.write_all(&out)?;
        to.flush()?;
        out.clear();
    }
}

/// `test runique_test::blog::add ... ` becomes `test blog::add ... `, with the
/// name in color. Only called on a line that starts and ends that way.
fn test_header(line: &[u8], color: bool) -> Vec<u8> {
    let name = &line[HEADER_PREFIX.len()..line.len() - HEADER_END.len()];
    let mut header = b"test ".to_vec();
    if color {
        header.extend_from_slice(TEST_NAME_COLOR.as_bytes());
    }
    header.extend_from_slice(name);
    if color {
        header.extend_from_slice(b"\x1b[0m");
    }
    header.extend_from_slice(HEADER_END);
    header
}

/// A test's verdict line: `ok`/`FAILED` on its own (after a test's output) or
/// at the end of `test … ...` (for a test that printed nothing).
fn is_result_line(line: &str) -> bool {
    let line = strip_ansi(line);
    let line = line.trim();
    matches!(line, "ok" | "FAILED")
        || (line.starts_with("test ") && (line.ends_with(" ok") || line.ends_with(" FAILED")))
}

/// Removes terminal color codes (`ESC [ … letter`), which wrap `ok` when colors are on.
fn strip_ansi(s: &str) -> String {
    let mut plain = String::with_capacity(s.len());
    let mut chars = s.chars();
    while let Some(c) = chars.next() {
        if c == '\x1b' {
            for code in chars.by_ref() {
                if code.is_ascii_alphabetic() {
                    break;
                }
            }
        } else {
            plain.push(c);
        }
    }
    plain
}

/// Asks cargo for the tests of `file` and fails with the list of real names
/// when `test` isn't one of them: cargo alone would just report 0 tests run.
fn check_test_exists(file: &str, test: &str, full_name: &str) -> Result<()> {
    let output = Command::new("cargo")
        .args([
            "test",
            "--quiet",
            "--",
            &format!("runique_test::{file}::"),
            "--list",
        ])
        .stderr(Stdio::inherit())
        .output()
        .context(t("test_runner.cargo_spawn_failed"))?;
    if !output.status.success() {
        bail!("{}", t("test_runner.build_failed"));
    }
    let names = parse_test_list(&String::from_utf8_lossy(&output.stdout));
    if !names.iter().any(|name| name == full_name) {
        let available: Vec<&str> = names
            .iter()
            .map(|name| name.rsplit("::").next().unwrap_or(name))
            .collect();
        bail!(
            "{}",
            tf(
                "test_runner.no_such_test",
                &[test, file, available.join(", ").as_str()]
            )
        );
    }
    Ok(())
}

/// Where Cargo.toml turns `test-utils` on.
#[derive(Debug, PartialEq)]
enum TestUtils {
    Missing,
    /// Only in `[dev-dependencies]`: test builds get it, release builds don't.
    DevOnly,
    /// In a section that reaches release builds (`[dependencies]`,
    /// `[workspace.dependencies]`, a `[features]` entry), even if it's also a dev one.
    Shipped,
}

/// Reads every `runique` entry of Cargo.toml (inline, dotted, or
/// `[…dependencies.runique]` table) and finds out where `test-utils` is enabled.
fn test_utils_placement(cargo_toml: &str) -> TestUtils {
    // Each entry comes with whether it sits in a dev section.
    let mut entries: Vec<(bool, String)> = Vec::new();
    let mut section: Option<bool> = None;
    let mut table: Option<(bool, String)> = None;
    let mut inline: Option<(bool, String)> = None;
    let mut open_braces = 0i32;

    for line in cargo_toml.lines() {
        let trimmed = line.trim();
        if inline.is_none() && trimmed.starts_with('[') {
            entries.extend(table.take());
            section = dependency_section(trimmed, "dependencies");
            table =
                dependency_section(trimmed, "dependencies.runique").map(|dev| (dev, String::new()));
            continue;
        }
        if let Some((_, text)) = &mut table {
            text.push_str(trimmed);
            text.push('\n');
            continue;
        }
        if inline.is_none()
            && let Some(dev) = section
            && trimmed
                .strip_prefix("runique")
                .is_some_and(|rest| rest.trim_start().starts_with(['=', '.']))
        {
            inline = Some((dev, String::new()));
            open_braces = 0;
        }
        if let Some((_, text)) = &mut inline {
            text.push_str(trimmed);
            text.push('\n');
            open_braces += count(trimmed, '{') - count(trimmed, '}');
            if open_braces <= 0 {
                entries.extend(inline.take());
            }
        }
    }
    entries.extend(table);
    entries.extend(inline);

    let enables = |text: &String| text.contains("\"test-utils\"");
    // `[features] x = ["runique/test-utils"]` turns it on for the regular dependency.
    let from_features = cargo_toml.contains("\"runique/test-utils\"")
        || cargo_toml.contains("\"runique?/test-utils\"");
    if from_features || entries.iter().any(|(dev, text)| !dev && enables(text)) {
        TestUtils::Shipped
    } else if entries.iter().any(|(_, text)| enables(text)) {
        TestUtils::DevOnly
    } else {
        TestUtils::Missing
    }
}

/// For a `[…]` header ending in `suffix`: `Some(true)` for a dev section,
/// `Some(false)` for one that reaches release builds, `None` otherwise.
/// Build dependencies never end up in the binary, so they don't count.
fn dependency_section(header: &str, suffix: &str) -> Option<bool> {
    let name = header.strip_prefix('[')?.split(']').next()?;
    let prefix = name.strip_suffix(suffix)?;
    if !(prefix.is_empty() || prefix.ends_with(['.', '-'])) || prefix.ends_with("build-") {
        return None;
    }
    Some(prefix.ends_with("dev-"))
}

fn count(s: &str, c: char) -> i32 {
    i32::try_from(s.matches(c).count()).unwrap_or(i32::MAX)
}

/// `None` when `src` doesn't declare `mod runique_test`, otherwise whether that
/// declaration carries a `cfg(test)`.
fn test_module_declaration(src: &str) -> Option<bool> {
    let file = syn::parse_file(src).ok()?;
    file.items.iter().find_map(|item| match item {
        syn::Item::Mod(module) if module.ident == "runique_test" => {
            Some(module.attrs.iter().any(is_cfg_test))
        }
        _ => None,
    })
}

/// Whether `mod.rs` gates itself with an inner `#![cfg(test)]`.
fn module_file_is_gated(mod_src: &str) -> bool {
    syn::parse_file(mod_src).is_ok_and(|file| file.attrs.iter().any(is_cfg_test))
}

fn is_cfg_test(attr: &syn::Attribute) -> bool {
    match &attr.meta {
        syn::Meta::List(list) if list.path.is_ident("cfg") => list
            .tokens
            .to_string()
            .split(|c: char| !c.is_alphanumeric() && c != '_')
            .any(|word| word == "test"),
        _ => false,
    }
}

/// Names of the `mod x;` declarations in `mod.rs`.
fn declared_modules(mod_src: &str) -> Vec<String> {
    syn::parse_file(mod_src)
        .map(|file| {
            file.items
                .iter()
                .filter_map(|item| match item {
                    syn::Item::Mod(module) => Some(module.ident.to_string()),
                    _ => None,
                })
                .collect()
        })
        .unwrap_or_default()
}

/// Stems of the `.rs` files in `dir`, `mod.rs` excluded, sorted.
fn test_files(dir: &Path) -> Result<Vec<String>> {
    let mut stems: Vec<String> = fs::read_dir(dir)
        .with_context(|| tf("test_runner.dir_read_failed", &[dir.display()]))?
        .filter_map(|entry| entry.ok())
        .map(|entry| entry.path())
        .filter(|path| path.extension().is_some_and(|ext| ext == "rs"))
        .filter_map(|path| {
            path.file_stem()
                .map(|stem| stem.to_string_lossy().into_owned())
        })
        .filter(|stem| stem != "mod")
        .collect();
    stems.sort();
    Ok(stems)
}

/// The `cargo test` name filter, and whether it must match exactly.
fn cargo_filter(file: Option<&str>, test: Option<&str>) -> (String, bool) {
    match (file, test) {
        (Some(file), Some(test)) => (format!("runique_test::{file}::{test}"), true),
        (Some(file), None) => (format!("runique_test::{file}::"), false),
        _ => ("runique_test::".to_string(), false),
    }
}

/// Test names from `cargo test -- --list` (lines like `a::b::c: test`).
fn parse_test_list(stdout: &str) -> Vec<String> {
    stdout
        .lines()
        .filter_map(|line| line.strip_suffix(": test"))
        .map(str::to_string)
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_utils_found_in_inline_dev_dependency() {
        let toml = "[dependencies]\nserde = \"1\"\n\n[dev-dependencies]\nrunique = { path = \"../runique\", features = [\"test-utils\"] }\n";
        assert_eq!(test_utils_placement(toml), TestUtils::DevOnly);
    }

    #[test]
    fn test_utils_found_in_table_form() {
        let toml = "[dev-dependencies.runique]\nversion = \"2.3.0\"\nfeatures = [\"test-utils\"]\n";
        assert_eq!(test_utils_placement(toml), TestUtils::DevOnly);
    }

    #[test]
    fn test_utils_found_in_multiline_inline_table() {
        let toml = "[dev-dependencies]\nrunique = {\n  path = \"../runique\",\n  features = [\"test-utils\"],\n}\n";
        assert_eq!(test_utils_placement(toml), TestUtils::DevOnly);
    }

    #[test]
    fn test_utils_found_in_target_dev_dependencies() {
        let toml = "[target.'cfg(unix)'.dev-dependencies]\nrunique = { version = \"2.3.0\", features = [\"test-utils\"] }\n";
        assert_eq!(test_utils_placement(toml), TestUtils::DevOnly);
    }

    #[test]
    fn test_utils_missing() {
        let toml =
            "[dependencies]\nrunique = { version = \"2.3.0\", features = [\"orm\", \"sqlite\"] }\n";
        assert_eq!(test_utils_placement(toml), TestUtils::Missing);
    }

    #[test]
    fn test_utils_on_another_crate_is_ignored() {
        let toml = "[dev-dependencies]\nother = { version = \"1\", features = [\"test-utils\"] }\n";
        assert_eq!(test_utils_placement(toml), TestUtils::Missing);
    }

    #[test]
    fn test_utils_in_dependencies_is_refused() {
        let toml = "[dependencies]\nrunique = { version = \"2.3.0\", features = [\"orm\", \"test-utils\"] }\n";
        assert_eq!(test_utils_placement(toml), TestUtils::Shipped);
    }

    #[test]
    fn test_utils_in_both_sections_is_refused() {
        let toml = "[dependencies]\nrunique = { version = \"2.3.0\", features = [\"test-utils\"] }\n\n[dev-dependencies]\nrunique = { version = \"2.3.0\", features = [\"test-utils\"] }\n";
        assert_eq!(test_utils_placement(toml), TestUtils::Shipped);
    }

    #[test]
    fn test_utils_in_dependencies_table_is_refused() {
        let toml = "[dependencies.runique]\nversion = \"2.3.0\"\nfeatures = [\"test-utils\"]\n\n[dev-dependencies]\nserde = \"1\"\n";
        assert_eq!(test_utils_placement(toml), TestUtils::Shipped);
    }

    #[test]
    fn test_utils_in_dotted_key_is_refused() {
        let toml =
            "[dependencies]\nrunique.version = \"2.3.0\"\nrunique.features = [\"test-utils\"]\n";
        assert_eq!(test_utils_placement(toml), TestUtils::Shipped);
    }

    #[test]
    fn test_utils_in_workspace_dependencies_is_refused() {
        let toml = "[workspace.dependencies]\nrunique = { version = \"2.3.0\", features = [\"test-utils\"] }\n";
        assert_eq!(test_utils_placement(toml), TestUtils::Shipped);
    }

    #[test]
    fn test_utils_through_a_feature_is_refused() {
        let toml = "[dependencies]\nrunique = { version = \"2.3.0\" }\n\n[features]\ntesting = [\"runique/test-utils\"]\n\n[dev-dependencies]\nrunique = { version = \"2.3.0\", features = [\"test-utils\"] }\n";
        assert_eq!(test_utils_placement(toml), TestUtils::Shipped);
    }

    #[test]
    fn test_utils_in_build_dependencies_is_ignored() {
        let toml = "[build-dependencies]\nrunique = { version = \"2.3.0\", features = [\"test-utils\"] }\n";
        assert_eq!(test_utils_placement(toml), TestUtils::Missing);
    }

    // Written from cargo-mutants survivors (2026-10-02): a multi-line entry must
    // close on its last brace, or the next section's lines land in it.
    #[test]
    fn test_utils_after_a_closed_multiline_entry_stays_dev_only() {
        let toml = "[dependencies]\nrunique = {\n  version = \"2.3.0\",\n}\n\n[dev-dependencies]\nrunique = { version = \"2.3.0\", features = [\"test-utils\"] }\n";
        assert_eq!(test_utils_placement(toml), TestUtils::DevOnly);
    }

    #[test]
    fn only_a_cfg_naming_test_gates_the_module() {
        assert!(module_file_is_gated(
            "#![cfg(all(test, feature = \"x\"))]\n"
        ));
        assert!(!module_file_is_gated("#![allow(dead_code)]\n"));
        assert!(!module_file_is_gated("#![cfg_attr(test, allow(unused))]\n"));
        assert!(!module_file_is_gated("#![cfg(feature = \"my_test\")]\n"));
    }

    #[test]
    fn test_files_lists_rust_files_but_mod() {
        let dir = std::env::temp_dir().join(format!("rq_tests_{}", uuid::Uuid::new_v4()));
        fs::create_dir_all(&dir).unwrap();
        for name in ["user.rs", "mod.rs", "notes.txt", "blog.rs"] {
            fs::write(dir.join(name), "").unwrap();
        }
        assert_eq!(test_files(&dir).unwrap(), ["blog", "user"]);
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn declaration_gated_in_main() {
        let src = "mod views;\n#[cfg(test)]\nmod runique_test;\nfn main() {}\n";
        assert_eq!(test_module_declaration(src), Some(true));
    }

    #[test]
    fn declaration_not_gated() {
        let src = "mod runique_test;\nfn main() {}\n";
        assert_eq!(test_module_declaration(src), Some(false));
    }

    #[test]
    fn declaration_missing() {
        let src = "mod views;\nfn main() {}\n";
        assert_eq!(test_module_declaration(src), None);
    }

    #[test]
    fn mod_file_gated_by_inner_attribute() {
        assert!(module_file_is_gated("#![cfg(test)]\nmod user;\n"));
        assert!(!module_file_is_gated("mod user;\n"));
    }

    #[test]
    fn declared_modules_lists_mod_items() {
        let src = "pub const ENV: &str = \".env\";\nmod user;\nmod livre;\n";
        assert_eq!(declared_modules(src), ["user", "livre"]);
    }

    #[test]
    fn filters_per_argument() {
        assert_eq!(
            cargo_filter(None, None),
            ("runique_test::".to_string(), false)
        );
        assert_eq!(
            cargo_filter(Some("user"), None),
            ("runique_test::user::".to_string(), false)
        );
        assert_eq!(
            cargo_filter(Some("user"), Some("add_email")),
            ("runique_test::user::add_email".to_string(), true)
        );
    }

    #[test]
    fn blank_line_after_each_verdict() {
        let cargo =
            "running 2 tests\ntest a ... \n     1. x\nok\ntest b ... ok\n\ntest result: ok.\n";
        let mut shown = Vec::new();
        forward_spaced(cargo.as_bytes(), &mut shown, false).unwrap();
        assert_eq!(
            String::from_utf8(shown).unwrap(),
            "running 2 tests\ntest a ... \n     1. x\nok\n\ntest b ... ok\n\n\ntest result: ok.\n"
        );
    }

    #[test]
    fn test_name_loses_its_shared_prefix() {
        let cargo = "test runique_test::blog::add ... \n     1. x\nok\ntest result: ok.\n";
        let mut shown = Vec::new();
        forward_spaced(cargo.as_bytes(), &mut shown, false).unwrap();
        assert_eq!(
            String::from_utf8(shown).unwrap(),
            "test blog::add ... \n     1. x\nok\n\ntest result: ok.\n"
        );
    }

    #[test]
    fn test_name_is_colored_and_verdict_left_alone() {
        let cargo = "test runique_test::user::find ... ok\n";
        let mut shown = Vec::new();
        forward_spaced(cargo.as_bytes(), &mut shown, true).unwrap();
        assert_eq!(
            String::from_utf8(shown).unwrap(),
            format!("test {TEST_NAME_COLOR}user::find\x1b[0m ... ok\n\n")
        );
    }

    #[test]
    fn header_split_across_reads_is_still_rewritten() {
        // A reader handing out one byte at a time: the worst case for a pipe.
        struct OneByte<'a>(&'a [u8]);
        impl Read for OneByte<'_> {
            fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
                let Some((first, rest)) = self.0.split_first() else {
                    return Ok(0);
                };
                buf[0] = *first;
                self.0 = rest;
                Ok(1)
            }
        }
        let mut shown = Vec::new();
        forward_spaced(
            OneByte(b"test runique_test::a::b ... \nok\n"),
            &mut shown,
            false,
        )
        .unwrap();
        assert_eq!(String::from_utf8(shown).unwrap(), "test a::b ... \nok\n\n");
    }

    #[test]
    fn other_lines_starting_with_t_are_untouched() {
        let cargo = "test result: ok. 2 passed\ntests\nthe end";
        let mut shown = Vec::new();
        forward_spaced(cargo.as_bytes(), &mut shown, true).unwrap();
        assert_eq!(String::from_utf8(shown).unwrap(), cargo);
    }

    #[test]
    fn colored_verdict_is_recognized() {
        assert!(is_result_line("\x1b[32mok\x1b[0m"));
        assert!(is_result_line("test a ... \x1b[31mFAILED\x1b[0m"));
        assert!(!is_result_line("     1.    0.9 ms  SELECT … FROM \"ok\""));
    }

    #[test]
    fn test_list_is_parsed() {
        let out =
            "runique_test::user::add_email: test\nruntime_check: test\n\n2 tests, 0 benchmarks\n";
        assert_eq!(
            parse_test_list(out),
            ["runique_test::user::add_email", "runtime_check"]
        );
    }
}