| 1 |
//! Parses output of [libtest](https://github.com/rust-lang/rust/blob/master/library/test/src/formatters/json.rs). |
| 2 |
//! |
| 3 |
//! Since this module parses output in an unstable format, all structs in this module may change at any time, and are exempt from semver guarantees. |
| 4 |
use serde::{Deserialize, Serialize}; |
| 5 |
|
| 6 |
/// Suite related event |
| 7 |
#[derive(Debug, PartialEq, Deserialize, Serialize)] |
| 8 |
#[serde(tag = "event")] |
| 9 |
#[serde(rename_all = "lowercase")] |
| 10 |
/// Suite event |
| 11 |
pub enum SuiteEvent { |
| 12 |
/// emitted on the start of a test run, and the start of the doctests |
| 13 |
Started { |
| 14 |
/// number of tests in this suite |
| 15 |
test_count: usize, |
| 16 |
}, |
| 17 |
/// the suite has finished |
| 18 |
Ok { |
| 19 |
/// the number of tests that passed |
| 20 |
passed: usize, |
| 21 |
/// the number of tests that failed |
| 22 |
failed: usize, |
| 23 |
/// number of tests that were ignored |
| 24 |
ignored: usize, |
| 25 |
/// number of benchmarks run |
| 26 |
measured: usize, |
| 27 |
/// i think this is based on what you specify in the cargo test argument |
| 28 |
filtered_out: usize, |
| 29 |
/// how long the suite took to run |
| 30 |
exec_time: f32, |
| 31 |
}, |
| 32 |
/// the suite has at least one failing test |
| 33 |
Failed { |
| 34 |
/// the number of tests that passed |
| 35 |
passed: usize, |
| 36 |
/// the number of tests that failed |
| 37 |
failed: usize, |
| 38 |
/// number of tests that were ignored |
| 39 |
ignored: usize, |
| 40 |
/// i think its something to do with benchmarks? |
| 41 |
measured: usize, |
| 42 |
/// i think this is based on what you specify in the cargo test argument |
| 43 |
filtered_out: usize, |
| 44 |
/// how long the suite took to run |
| 45 |
exec_time: f32, |
| 46 |
}, |
| 47 |
} |
| 48 |
|
| 49 |
#[derive(Debug, PartialEq, Deserialize, Serialize)] |
| 50 |
#[serde(tag = "event")] |
| 51 |
#[serde(rename_all = "lowercase")] |
| 52 |
/// Test event |
| 53 |
pub enum TestEvent { |
| 54 |
/// a new test starts |
| 55 |
Started { |
| 56 |
/// the name of this test |
| 57 |
name: String, |
| 58 |
}, |
| 59 |
/// the test has finished |
| 60 |
Ok { |
| 61 |
/// which one |
| 62 |
name: String, |
| 63 |
/// in how long |
| 64 |
exec_time: f32, |
| 65 |
/// what did it say? |
| 66 |
stdout: Option<String>, |
| 67 |
}, |
| 68 |
/// the test has failed |
| 69 |
Failed { |
| 70 |
/// which one |
| 71 |
name: String, |
| 72 |
/// in how long |
| 73 |
exec_time: f32, |
| 74 |
/// why? |
| 75 |
stdout: Option<String>, |
| 76 |
/// it timed out? |
| 77 |
reason: Option<String>, |
| 78 |
/// what message |
| 79 |
message: Option<String>, |
| 80 |
}, |
| 81 |
/// the test has been ignored |
| 82 |
Ignored { |
| 83 |
/// which one |
| 84 |
name: String, |
| 85 |
}, |
| 86 |
/// the test has timed out |
| 87 |
Timeout { |
| 88 |
/// which one |
| 89 |
name: String, |
| 90 |
}, |
| 91 |
} |
| 92 |
|
| 93 |
impl TestEvent { |
| 94 |
/// Get the name of this test |
| 95 |
pub fn name(&self) -> &str { |
| 96 |
let (Self::Started { name } |
| 97 |
| Self::Ok { name, .. } |
| 98 |
| Self::Ignored { name } |
| 99 |
| Self::Failed { name, .. } |
| 100 |
| Self::Timeout { name }) = self; |
| 101 |
name |
| 102 |
} |
| 103 |
|
| 104 |
/// Get the stdout of this test, if available. |
| 105 |
pub fn stdout(&self) -> Option<&str> { |
| 106 |
match self { |
| 107 |
Self::Ok { stdout, .. } | Self::Failed { stdout, .. } => stdout.as_deref(), |
| 108 |
_ => None, |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
#[derive(Debug, PartialEq, Deserialize, Serialize)] |
| 114 |
/// Represents the output of `cargo test -- -Zunstable-options --report-time --show-output --format json`. |
| 115 |
/// |
| 116 |
/// requires --report-time |
| 117 |
/// |
| 118 |
/// # Stability |
| 119 |
/// |
| 120 |
/// As this struct is for interfacing with the unstable libtest json output, this struct may change at any time, without semver guarantees. |
| 121 |
#[serde(tag = "type")] |
| 122 |
#[serde(rename_all = "lowercase")] |
| 123 |
pub enum TestMessage { |
| 124 |
/// suite related message |
| 125 |
Suite(SuiteEvent), |
| 126 |
/// test related message |
| 127 |
Test(TestEvent), |
| 128 |
/// bench related message |
| 129 |
Bench { |
| 130 |
/// name of benchmark |
| 131 |
name: String, |
| 132 |
/// distribution |
| 133 |
median: f32, |
| 134 |
/// deviation |
| 135 |
deviation: f32, |
| 136 |
/// thruput in MiB per second |
| 137 |
mib_per_second: Option<f32>, |
| 138 |
}, |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn deser() { |
| 143 |
macro_rules! run { |
| 144 |
($($input:literal parses to $output:expr),+) => { |
| 145 |
$(assert_eq!(dbg!(serde_json::from_str::<TestMessage>($input)).unwrap(), $output);)+ |
| 146 |
}; |
| 147 |
} |
| 148 |
run![ |
| 149 |
r#"{ "type": "suite", "event": "started", "test_count": 2 }"# parses to TestMessage::Suite(SuiteEvent::Started { test_count: 2 }), |
| 150 |
r#"{ "type": "test", "event": "started", "name": "fail" }"# parses to TestMessage::Test(TestEvent::Started { name: "fail".into() }), |
| 151 |
r#"{ "type": "test", "name": "fail", "event": "ok", "exec_time": 0.000003428, "stdout": "hello world" }"# parses to TestMessage::Test(TestEvent::Ok { name: "fail".into(), exec_time: 0.000003428, stdout: Some("hello world".into()) }), |
| 152 |
r#"{ "type": "test", "event": "started", "name": "nope" }"# parses to TestMessage::Test(TestEvent::Started { name: "nope".into() }), |
| 153 |
r#"{ "type": "test", "name": "nope", "event": "ignored" }"# parses to TestMessage::Test(TestEvent::Ignored { name: "nope".into() }), |
| 154 |
r#"{ "type": "suite", "event": "ok", "passed": 1, "failed": 0, "ignored": 1, "measured": 0, "filtered_out": 0, "exec_time": 0.000684028 }"# parses to TestMessage::Suite(SuiteEvent::Ok { passed: 1, failed: 0, ignored: 1, measured: 0, filtered_out: 0, exec_time: 0.000684028 }) |
| 155 |
]; |
| 156 |
|
| 157 |
run![ |
| 158 |
r#"{ "type": "suite", "event": "started", "test_count": 2 }"# parses to TestMessage::Suite(SuiteEvent::Started { test_count: 2 }), |
| 159 |
r#"{ "type": "test", "event": "started", "name": "fail" }"# parses to TestMessage::Test(TestEvent::Started { name: "fail".into() }), |
| 160 |
r#"{ "type": "test", "event": "started", "name": "benc" }"# parses to TestMessage::Test(TestEvent::Started { name: "benc".into() }), |
| 161 |
r#"{ "type": "bench", "name": "benc", "median": 0, "deviation": 0 }"# parses to TestMessage::Bench { name: "benc".into(), median: 0., deviation: 0., mib_per_second: None }, |
| 162 |
r#"{ "type": "test", "name": "fail", "event": "failed", "exec_time": 0.000081092, "stdout": "thread 'fail' panicked" }"# parses to TestMessage::Test(TestEvent::Failed { name: "fail".into(), exec_time: 0.000081092, stdout: Some("thread 'fail' panicked".into()), reason: None, message: None} ), |
| 163 |
r#"{ "type": "suite", "event": "failed", "passed": 0, "failed": 1, "ignored": 0, "measured": 1, "filtered_out": 0, "exec_time": 0.000731068 }"# parses to TestMessage::Suite(SuiteEvent::Failed { passed: 0, failed: 1, ignored: 0, measured: 1, filtered_out: 0, exec_time: 0.000731068 }) |
| 164 |
]; |
| 165 |
} |