|
| 1 | +use super::RevisionStyle; |
| 2 | +use super::StatusEmitter; |
| 3 | +use super::Summary; |
| 4 | +use super::TestStatus; |
| 5 | +use crate::test_result::TestResult; |
| 6 | +use crate::TestOk; |
| 7 | + |
| 8 | +use std::boxed::Box; |
| 9 | +use std::fmt::Debug; |
| 10 | +use std::path::{Path, PathBuf}; |
| 11 | + |
| 12 | +use bstr::ByteSlice; |
| 13 | + |
| 14 | +// MAINTENANCE REGION START |
| 15 | + |
| 16 | +// When integrating with a new libtest version, update all emit_xxx functions. |
| 17 | + |
| 18 | +fn emit_suite_end(failed: usize, filtered_out: usize, ignored: usize, passed: usize, status: &str) { |
| 19 | + // Adapted from test::formatters::json::write_run_finish(). |
| 20 | + println!( |
| 21 | + r#"{{ "type": "suite", "event": "{status}", "passed": {passed}, "failed": {failed}, "ignored": {ignored}, "measured": 0, "filtered_out": {filtered_out} }}"# |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +fn emit_suite_start() { |
| 26 | + // Adapted from test::formatters::json::write_run_start(). |
| 27 | + println!(r#"{{ "type": "suite", "event": "started" }}"#); |
| 28 | +} |
| 29 | + |
| 30 | +fn emit_test_end(name: &String, revision: &String, path: &Path, status: &str, diags: &str) { |
| 31 | + let displayed_path = path.display(); |
| 32 | + let stdout = if diags.is_empty() { |
| 33 | + String::new() |
| 34 | + } else { |
| 35 | + let triaged_diags = serde_json::to_string(diags).unwrap(); |
| 36 | + format!(r#", "stdout": {triaged_diags}"#) |
| 37 | + }; |
| 38 | + |
| 39 | + // Adapted from test::formatters::json::write_event(). |
| 40 | + println!( |
| 41 | + r#"{{ "type": "test", "event": "{status}", "name": "{name} ({revision}) - {displayed_path}"{stdout} }}"# |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +fn emit_test_start(name: &String, revision: &String, path: &Path) { |
| 46 | + let displayed_path = path.display(); |
| 47 | + |
| 48 | + // Adapted from test::formatters::json::write_test_start(). |
| 49 | + println!( |
| 50 | + r#"{{ "type": "test", "event": "started", "name": "{name} ({revision}) - {displayed_path}" }}"# |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +// MAINTENANCE REGION END |
| 55 | + |
| 56 | +/// A JSON output emitter. |
| 57 | +#[derive(Clone)] |
| 58 | +pub struct JSON {} |
| 59 | + |
| 60 | +impl JSON { |
| 61 | + /// Create a new instance of a JSON output emitter. |
| 62 | + pub fn new() -> Self { |
| 63 | + emit_suite_start(); |
| 64 | + |
| 65 | + JSON {} |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl Default for JSON { |
| 70 | + fn default() -> Self { |
| 71 | + Self::new() |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl StatusEmitter for JSON { |
| 76 | + /// Create a report about the entire test run at the end. |
| 77 | + fn finalize( |
| 78 | + &self, |
| 79 | + failed: usize, |
| 80 | + succeeded: usize, |
| 81 | + ignored: usize, |
| 82 | + filtered: usize, |
| 83 | + aborted: bool, |
| 84 | + ) -> Box<dyn Summary> { |
| 85 | + let status = if aborted || failed > 0 { |
| 86 | + "failed" |
| 87 | + } else { |
| 88 | + "ok" |
| 89 | + }; |
| 90 | + |
| 91 | + emit_suite_end(failed, filtered, ignored, succeeded, status); |
| 92 | + |
| 93 | + Box::new(()) |
| 94 | + } |
| 95 | + |
| 96 | + /// Invoked the moment we know a test will later be run. |
| 97 | + /// Emits a JSON start event. |
| 98 | + fn register_test(&self, path: PathBuf) -> Box<dyn TestStatus + 'static> { |
| 99 | + let name = path.to_str().unwrap().to_string(); |
| 100 | + let revision = String::new(); |
| 101 | + |
| 102 | + emit_test_start(&name, &revision, &path); |
| 103 | + |
| 104 | + Box::new(JSONStatus { |
| 105 | + name, |
| 106 | + path, |
| 107 | + revision: String::new(), |
| 108 | + style: RevisionStyle::Show, |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/// Information about a specific test run. |
| 114 | +pub struct JSONStatus { |
| 115 | + name: String, |
| 116 | + path: PathBuf, |
| 117 | + revision: String, |
| 118 | + style: RevisionStyle, |
| 119 | +} |
| 120 | + |
| 121 | +impl TestStatus for JSONStatus { |
| 122 | + /// A test has finished, handle the result immediately. |
| 123 | + fn done(&self, result: &TestResult, aborted: bool) { |
| 124 | + let status = if aborted { |
| 125 | + "timeout" |
| 126 | + } else { |
| 127 | + match result { |
| 128 | + Ok(TestOk::Ignored) => "ignored", |
| 129 | + Ok(TestOk::Ok) => "ok", |
| 130 | + Err(_) => "failed", |
| 131 | + } |
| 132 | + }; |
| 133 | + let diags = if let Err(errored) = result { |
| 134 | + let command = errored.command.as_str(); |
| 135 | + let stdout = errored.stderr.to_str_lossy(); |
| 136 | + let stderr = errored.stdout.to_str_lossy(); |
| 137 | + |
| 138 | + format!(r#"command: <{command}> stdout: <{stdout}> stderr: <{stderr}>"#) |
| 139 | + } else { |
| 140 | + String::new() |
| 141 | + }; |
| 142 | + |
| 143 | + emit_test_end(&self.name, &self.revision, self.path(), status, &diags); |
| 144 | + } |
| 145 | + |
| 146 | + /// Invoked before each failed test prints its errors along with a drop guard that can |
| 147 | + /// get invoked afterwards. |
| 148 | + fn failed_test<'a>( |
| 149 | + &'a self, |
| 150 | + _cmd: &'a str, |
| 151 | + _stderr: &'a [u8], |
| 152 | + _stdout: &'a [u8], |
| 153 | + ) -> Box<dyn Debug + 'a> { |
| 154 | + Box::new(()) |
| 155 | + } |
| 156 | + |
| 157 | + /// Create a copy of this test for a new path. |
| 158 | + fn for_path(&self, path: &Path) -> Box<dyn TestStatus> { |
| 159 | + let status = JSONStatus { |
| 160 | + name: self.name.clone(), |
| 161 | + path: path.to_path_buf(), |
| 162 | + revision: self.revision.clone(), |
| 163 | + style: self.style, |
| 164 | + }; |
| 165 | + Box::new(status) |
| 166 | + } |
| 167 | + |
| 168 | + /// Create a copy of this test for a new revision. |
| 169 | + fn for_revision(&self, revision: &str, style: RevisionStyle) -> Box<dyn TestStatus> { |
| 170 | + let status = JSONStatus { |
| 171 | + name: self.name.clone(), |
| 172 | + path: self.path.clone(), |
| 173 | + revision: revision.to_owned(), |
| 174 | + style, |
| 175 | + }; |
| 176 | + Box::new(status) |
| 177 | + } |
| 178 | + |
| 179 | + /// The path of the test file. |
| 180 | + fn path(&self) -> &Path { |
| 181 | + &self.path |
| 182 | + } |
| 183 | + |
| 184 | + /// The revision, usually an empty string. |
| 185 | + fn revision(&self) -> &str { |
| 186 | + &self.revision |
| 187 | + } |
| 188 | +} |
0 commit comments