test-r-core 12.0.0

Test framework for Rust (core module)
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
use crate::internal::{FlakinessControl, RegisteredTest, TestResult};
use crate::output::progress::StderrProgress;
use crate::output::{write_failure_summary_to_stderr, LogFile, StdoutOrLogFile, TestRunnerOutput};
use ctrf_rs::test::{Status, Test};
use serde_json::json;
use std::collections::{HashMap, HashSet};
use std::io::Write;
use std::path::PathBuf;
use std::sync::Mutex;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

pub(crate) struct Ctrf {
    show_output: bool,
    state: Mutex<CtrfState>,
    progress: StderrProgress,
}

impl Ctrf {
    pub fn new(show_output: bool, logfile_path: Option<PathBuf>) -> Self {
        let target = match logfile_path {
            Some(path) => StdoutOrLogFile::LogFile(LogFile::new(path, false)),
            None => StdoutOrLogFile::stdout(),
        };
        Self {
            show_output,
            state: Mutex::new(CtrfState::new(target)),
            progress: StderrProgress::new(),
        }
    }
}

impl TestRunnerOutput for Ctrf {
    fn start_suite(&self, tests: &[RegisteredTest]) {
        let mut state = self.state.lock().unwrap();
        state.start.replace(SystemTime::now());
        // Reset attempt-local state so a retried suite doesn't carry
        // forward stale per-test entries / counts / stop timestamps
        // from a previous attempt. Without this, intermediate CTRF
        // logfile snapshots during a retry would include the previous
        // attempt's `tests`/`summary` until the suite-end rebuild
        // clears them.
        state.tests.clear();
        state.summary = SummaryCounts::default();
        state.suites.clear();
        state.pending_tests.clear();
        state.pending_stops.clear();
        state.suite_attempt += 1;
        write_ctrf_snapshot(&mut state, false);
        drop(state);
        self.progress.start_suite(tests.len());
    }

    fn start_running_test(&self, registered_test: &RegisteredTest, idx: usize, count: usize) {
        let mut state = self.state.lock().unwrap();
        let mut test = Test::new(
            registered_test.fully_qualified_name(),
            Status::Pending,
            Duration::ZERO,
        );
        test.start = Some(
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_millis() as u64,
        );
        state
            .pending_tests
            .insert(registered_test.fully_qualified_name(), test);
        drop(state);
        self.progress
            .start_running_test(registered_test, idx, count);
    }

    fn repeat_running_test(
        &self,
        registered_test: &RegisteredTest,
        _idx: usize,
        _count: usize,
        _attempt: usize,
        _max_attempts: usize,
        _reason: &str,
    ) {
        let mut state = self.state.lock().unwrap();
        let test = state
            .pending_tests
            .get_mut(&registered_test.fully_qualified_name())
            .expect("repeat_running_test called with a test that has not been started yet");
        test.retries = Some(test.retries.unwrap_or_default() + 1);
        test.flaky = match registered_test.props.flakiness_control {
            FlakinessControl::None => None,
            FlakinessControl::ProveNonFlaky(_) => Some(false),
            FlakinessControl::RetryKnownFlaky(_) => Some(true),
        };
    }

    fn finished_running_test(
        &self,
        registered_test: &RegisteredTest,
        idx: usize,
        count: usize,
        result: &TestResult,
    ) {
        self.progress
            .finished_running_test(registered_test, idx, count, result);
        let mut state = self.state.lock().unwrap();
        let stop = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;
        // Remember the per-test stop time so the suite-end rebuild
        // (which re-creates each `Test` value to pick up host-attributed
        // lines) preserves the original finish timestamp instead of
        // stamping every test with the rebuild-time `SystemTime::now()`.
        state
            .pending_stops
            .insert(registered_test.fully_qualified_name(), stop);
        let (status, suite, value) = {
            let pending_test = state
                .pending_tests
                .get(&registered_test.fully_qualified_name())
                .expect("finished_running_test called on a test that has not been started before");
            build_test_value(
                registered_test,
                result,
                self.show_output,
                pending_test.flaky,
                pending_test.retries,
                pending_test.start,
                Some(stop),
            )
        };
        state.tests.push(value);
        match status {
            Status::Passed => state.summary.passed += 1,
            Status::Failed => state.summary.failed += 1,
            Status::Pending => state.summary.pending += 1,
            Status::Skipped => state.summary.skipped += 1,
            Status::Other => state.summary.other += 1,
        }
        if let Some(s) = suite {
            state.suites.insert(s);
        }

        // Write an intermediate snapshot to the log file (if any) so partial
        // results survive even if the process is killed (e.g. due to a hanging
        // test). For stdout we wait for the final report.
        write_ctrf_snapshot(&mut state, false);
    }

    fn finished_suite(
        &self,
        _registered_tests: &[RegisteredTest],
        results: &[(RegisteredTest, TestResult)],
        exec_time: Duration,
    ) {
        let mut state = self.state.lock().unwrap();

        // Rebuild the cached per-test values from `results`, which by
        // this point include any host-attributed lines added in the
        // host-capture finaliser. The intermediate cache built during
        // `finished_running_test` was captured BEFORE that attribution
        // ran (the finaliser runs in the runner just before this call),
        // so without rebuilding here the host lines would never reach
        // the final CTRF document.
        state.tests.clear();
        state.summary = SummaryCounts::default();
        state.suites.clear();
        for (registered_test, result) in results {
            let qname = registered_test.fully_qualified_name();
            let pending_test = state.pending_tests.get(&qname);
            let flaky = pending_test.and_then(|t| t.flaky);
            let retries = pending_test.and_then(|t| t.retries);
            let start = pending_test.and_then(|t| t.start);
            // Reuse the per-test stop time captured at `finished_running_test`
            // time. Falls back to `now()` only for tests that never reached
            // `finished_running_test` (should not normally happen here).
            let stop = state.pending_stops.get(&qname).copied().unwrap_or_else(|| {
                SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .unwrap()
                    .as_millis() as u64
            });
            let (status, suite, value) = build_test_value(
                registered_test,
                result,
                self.show_output,
                flaky,
                retries,
                start,
                Some(stop),
            );
            state.tests.push(value);
            match status {
                Status::Passed => state.summary.passed += 1,
                Status::Failed => state.summary.failed += 1,
                Status::Pending => state.summary.pending += 1,
                Status::Skipped => state.summary.skipped += 1,
                Status::Other => state.summary.other += 1,
            }
            if let Some(s) = suite {
                state.suites.insert(s);
            }
        }

        write_ctrf_snapshot(&mut state, true);
        // Clear the start marker now that the suite has truly finished.
        state.start = None;

        write_failure_summary_to_stderr(results, exec_time);
    }

    fn test_list(&self, _registered_tests: &[RegisteredTest]) {}
}

/// Builds a CTRF `Test` JSON value from a finished test result, applying the
/// formatter's `show_output` policy to stdout/stderr capture and folding any
/// host-attributed lines into `stdout` with a `[host]` prefix. Returned along
/// with the test's status and (optional) suite name so the caller can update
/// the running summary and suite set without re-deserialising the value.
fn build_test_value(
    registered_test: &RegisteredTest,
    result: &TestResult,
    show_output: bool,
    flaky: Option<bool>,
    retries: Option<usize>,
    start: Option<u64>,
    stop: Option<u64>,
) -> (Status, Option<String>, serde_json::Value) {
    let mut test = Test::new(
        registered_test.fully_qualified_name(),
        match result {
            TestResult::Passed { .. } => Status::Passed,
            TestResult::Benchmarked { .. } => Status::Passed,
            TestResult::Failed { .. } => Status::Failed,
            TestResult::Ignored { .. } => Status::Skipped,
        },
        match result {
            TestResult::Passed { exec_time, .. } => *exec_time,
            TestResult::Failed { exec_time, .. } => *exec_time,
            TestResult::Benchmarked { exec_time, .. } => *exec_time,
            TestResult::Ignored { .. } => Duration::ZERO,
        },
    );

    let mut stdout_lines = vec![];
    let mut stderr_lines = vec![];

    let captures = if result.attempts().len() > 1 && !show_output {
        let mut captures: Vec<_> =
            result
                .attempts()
                .iter()
                .filter(|attempt| attempt.result.is_failed())
                .flat_map(|attempt| attempt.result.captured_output())
                .chain(result.captured_output().iter().filter(|capture| {
                    matches!(capture, crate::internal::CapturedOutput::Host { .. })
                }))
                .collect();
        captures.sort();
        captures
    } else {
        result.captured_output().iter().collect()
    };
    for capture in captures {
        match capture {
            crate::internal::CapturedOutput::Stdout { line, .. } => stdout_lines.push(line.clone()),
            crate::internal::CapturedOutput::Stderr { line, .. } => stderr_lines.push(line.clone()),
            // Host-attributed lines fold into the stdout array with
            // a `[host]` prefix so they show up in CTRF consumers
            // without needing a schema extension.
            crate::internal::CapturedOutput::Host { line, .. } => {
                stdout_lines.push(format!("[host] {line}"))
            }
        }
    }

    if result.is_failed() || show_output {
        test.stdout = stdout_lines;
        test.stderr = stderr_lines;
    }

    test.message = result.failure_message();
    test.suite = Some(registered_test.crate_and_module());
    test.flaky = if result.attempts().len() > 1 {
        match registered_test.props.flakiness_control {
            FlakinessControl::RetryKnownFlaky(_) => Some(true),
            FlakinessControl::ProveNonFlaky(_) => Some(false),
            FlakinessControl::None => flaky,
        }
    } else {
        flaky
    };
    test.retries = if result.attempts().len() > 1 {
        Some(result.attempts().len() - 1)
    } else {
        retries
    };
    test.start = start;
    test.stop = stop;

    let status = test.status();
    let suite = test.suite.clone();
    let mut value = serde_json::to_value(&test).expect("Failed to serialize CTRF test");
    if result.attempts().len() > 1 {
        let attempts: Vec<_> = result
            .attempts()
            .iter()
            .enumerate()
            .map(|(index, attempt)| {
                let (_, _, mut evidence) = build_test_value(
                    registered_test,
                    &attempt.result,
                    show_output,
                    None,
                    None,
                    None,
                    None,
                );
                evidence["attempt"] = json!(index + 1);
                evidence
            })
            .collect();
        value["extra"] = json!({ "testR": { "attempts": attempts } });
        if result
            .attempts()
            .iter()
            .any(|attempt| attempt.result.is_failed())
            || show_output
        {
            // Host records only have a logical-test window, not worker
            // invocation boundaries. Preserve them without inventing attribution.
            let host_output: Vec<_> = result
                .captured_output()
                .iter()
                .filter_map(|capture| {
                    if let crate::internal::CapturedOutput::Host { line, .. } = capture {
                        Some(line)
                    } else {
                        None
                    }
                })
                .collect();
            if !host_output.is_empty() {
                value["extra"]["testR"]["hostOutput"] = json!(host_output);
            }
        }
    }
    (status, suite, value)
}

/// Serializes the currently accumulated CTRF tests and writes them to the
/// configured output. When the target is a log file the file is truncated
/// first so each snapshot is a complete, valid CTRF document. For stdout the
/// snapshot is only written when `is_final` is true to avoid spamming stdout
/// with partial reports.
///
/// The CTRF JSON document is built manually here (rather than via
/// `ctrf_rs::Report`) so we can keep each previously emitted `Test` as a
/// cached `serde_json::Value`. `ctrf_rs::test::Test` is neither `Clone` nor
/// safely round-trippable through `serde_json` (several of its fields use
/// `skip_serializing_if` without `default`), which would otherwise force us
/// to rebuild every test from scratch on every snapshot.
fn write_ctrf_snapshot(state: &mut CtrfState, is_final: bool) {
    let started = match state.start {
        Some(s) => s,
        None => return,
    };

    let reset = state
        .target
        .reset_log_file()
        .expect("Failed to reset CTRF log file");
    if !reset && !is_final {
        // Writing to stdout - only emit on the final call.
        return;
    }

    let now = SystemTime::now();
    let start_ms = started.duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
    let stop_ms = now.duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
    let total = state.summary.passed
        + state.summary.failed
        + state.summary.pending
        + state.summary.skipped
        + state.summary.other;

    let mut summary = json!({
        "tests": total,
        "passed": state.summary.passed,
        "failed": state.summary.failed,
        "pending": state.summary.pending,
        "skipped": state.summary.skipped,
        "other": state.summary.other,
        "start": start_ms,
        "stop": stop_ms,
    });
    if !state.suites.is_empty() {
        summary
            .as_object_mut()
            .unwrap()
            .insert("suites".to_string(), json!(state.suites.len()));
    }

    let mut report = json!({
        "reportFormat": ctrf_rs::report::REPORT_FORMAT,
        "specVersion": ctrf_rs::report::SPEC_VERSION.to_string(),
        "timestamp": format!("{now:?}"),
        "generatedBy": "test-r",
        "results": {
            "tool": { "name": "ctrf-rs" },
            "summary": summary,
            "tests": state.tests,
        },
    });

    // Only prior completed suites live in history: the current suite remains
    // the single source of truth for standard CTRF tests and summary counts.
    if state.suite_attempt > 1 {
        report["results"]["extra"] = json!({ "testR": {
            "suiteAttempt": state.suite_attempt,
            "complete": is_final,
            "previousSuiteAttempts": state.previous_suites,
        }});
    }
    if is_final {
        state.previous_suites.push(json!({
            "suiteAttempt": state.suite_attempt,
            "summary": report["results"]["summary"],
            "tests": state.tests,
        }));
    }

    let raw = serde_json::to_string(&report).expect("Failed to serialize CTRF document");
    let out = &mut state.target;
    writeln!(out, "{}", raw).expect("Failed to write to output");
    out.flush().expect("Failed to flush CTRF output");
}

#[derive(Default)]
struct SummaryCounts {
    passed: usize,
    failed: usize,
    pending: usize,
    skipped: usize,
    other: usize,
}

struct CtrfState {
    pub target: StdoutOrLogFile,
    /// Each completed test is cached here as a serialized `serde_json::Value`
    /// because `ctrf_rs::test::Test` is not `Clone` and we need to be able to
    /// rebuild a complete `Results` document each time we emit an intermediate
    /// snapshot.
    pub tests: Vec<serde_json::Value>,
    pub summary: SummaryCounts,
    pub suites: HashSet<String>,
    pub pending_tests: HashMap<String, Test>,
    /// Per-test finish wall-clock timestamps captured at
    /// `finished_running_test` time, keyed by fully-qualified name.
    /// Reused by the suite-end rebuild so the final CTRF document
    /// preserves true per-test stop times.
    pub pending_stops: HashMap<String, u64>,
    pub start: Option<SystemTime>,
    pub suite_attempt: usize,
    pub previous_suites: Vec<serde_json::Value>,
}

impl CtrfState {
    pub fn new(target: StdoutOrLogFile) -> Self {
        Self {
            target,
            tests: Vec::new(),
            summary: SummaryCounts::default(),
            suites: HashSet::new(),
            pending_tests: HashMap::new(),
            pending_stops: HashMap::new(),
            start: None,
            suite_attempt: 0,
            previous_suites: Vec::new(),
        }
    }
}