Skip to content

Commit 0911fb4

Browse files
committed
Export default diagnostics extractors
1 parent 02ba57d commit 0911fb4

File tree

5 files changed

+47
-40
lines changed

5 files changed

+47
-40
lines changed

src/config.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#[cfg(feature = "rustc")]
22
use crate::{
3-
aux_builds::AuxBuilder, custom_flags::run::Run, custom_flags::rustfix::RustfixMode,
4-
custom_flags::Flag, filter::Match, rustc_stderr,
3+
aux_builds::AuxBuilder, custom_flags::edition::Edition, custom_flags::run::Run,
4+
custom_flags::rustfix::RustfixMode, custom_flags::Flag, filter::Match,
55
};
66
use crate::{
7-
diagnostics::Diagnostics,
7+
diagnostics::{self, Diagnostics},
88
parser::CommandParserFunc,
99
per_test_config::{Comments, Condition},
1010
CommandBuilder, Error, Errors,
@@ -91,11 +91,7 @@ impl Config {
9191
comment_defaults,
9292
comment_start: "//",
9393
custom_comments: Default::default(),
94-
diagnostic_extractor: |_, _| Diagnostics {
95-
rendered: Default::default(),
96-
messages: Default::default(),
97-
messages_from_unknown_file_or_line: Default::default(),
98-
},
94+
diagnostic_extractor: diagnostics::default_diagnostics_extractor,
9995
abort_check: Default::default(),
10096
}
10197
}
@@ -104,8 +100,6 @@ impl Config {
104100
/// `rustc` on the test files.
105101
#[cfg(feature = "rustc")]
106102
pub fn rustc(root_dir: impl Into<PathBuf>) -> Self {
107-
use crate::custom_flags::edition::Edition;
108-
109103
let mut comment_defaults = Comments::default();
110104

111105
#[derive(Debug)]
@@ -175,7 +169,7 @@ impl Config {
175169
comment_defaults,
176170
comment_start: "//",
177171
custom_comments: Default::default(),
178-
diagnostic_extractor: rustc_stderr::process,
172+
diagnostic_extractor: diagnostics::rustc::rustc_diagnostics_extractor,
179173
abort_check: Default::default(),
180174
};
181175
config
@@ -249,7 +243,7 @@ impl Config {
249243
let mut this = Self {
250244
program: CommandBuilder::cargo(),
251245
custom_comments: Default::default(),
252-
diagnostic_extractor: rustc_stderr::process_cargo,
246+
diagnostic_extractor: diagnostics::rustc::cargo_diagnostics_extractor,
253247
comment_start: "#",
254248
..Self::rustc(root_dir)
255249
};

src/diagnostics.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
//! Data structures for handling diagnostic output from tests.
22
3+
use std::path::Path;
4+
35
#[cfg(feature = "rustc")]
4-
use cargo_metadata::diagnostic::DiagnosticLevel;
6+
pub mod rustc;
7+
8+
/// Default diagnostics extractor that does nothing.
9+
pub fn default_diagnostics_extractor(_path: &Path, _stderr: &[u8]) -> Diagnostics {
10+
Diagnostics::default()
11+
}
512

6-
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
713
/// The different levels of diagnostic messages and their relative ranking.
14+
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
815
pub enum Level {
916
/// internal compiler errors
1017
Ice = 5,
@@ -20,21 +27,6 @@ pub enum Level {
2027
FailureNote = 0,
2128
}
2229

23-
#[cfg(feature = "rustc")]
24-
impl From<DiagnosticLevel> for Level {
25-
fn from(value: DiagnosticLevel) -> Self {
26-
match value {
27-
DiagnosticLevel::Ice => Level::Ice,
28-
DiagnosticLevel::Error => Level::Error,
29-
DiagnosticLevel::Warning => Level::Warn,
30-
DiagnosticLevel::FailureNote => Level::FailureNote,
31-
DiagnosticLevel::Note => Level::Note,
32-
DiagnosticLevel::Help => Level::Help,
33-
other => panic!("rustc got a new kind of diagnostic level: {other:?}"),
34-
}
35-
}
36-
}
37-
3830
impl std::str::FromStr for Level {
3931
type Err = String;
4032
fn from_str(s: &str) -> Result<Self, Self::Err> {
@@ -50,8 +42,8 @@ impl std::str::FromStr for Level {
5042
}
5143
}
5244

53-
#[derive(Debug)]
5445
/// A diagnostic message.
46+
#[derive(Debug)]
5547
pub struct Message {
5648
/// The diagnostic level at which this message was emitted
5749
pub level: Level,
@@ -65,8 +57,8 @@ pub struct Message {
6557
pub code: Option<String>,
6658
}
6759

68-
#[derive(Debug)]
69-
/// All the diagnostics that were emitted in a test
60+
/// All the diagnostics that were emitted in a test.
61+
#[derive(Default, Debug)]
7062
pub struct Diagnostics {
7163
/// Rendered and concatenated version of all diagnostics.
7264
/// This is equivalent to non-json diagnostics.

src/rustc_stderr.rs renamed to src/diagnostics/rustc.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
use crate::diagnostics::{Diagnostics, Message};
1+
//! `rustc` and `cargo` diagnostics extractors.
2+
//!
3+
//! These parse diagnostics from the respective stderr JSON output using the
4+
//! data structures defined in [`cargo_metadata::diagnostic`].
5+
6+
use super::{Diagnostics, Level, Message};
27
use bstr::ByteSlice;
3-
use cargo_metadata::diagnostic::{Diagnostic, DiagnosticSpan};
8+
use cargo_metadata::diagnostic::{Diagnostic, DiagnosticLevel, DiagnosticSpan};
49
use regex::Regex;
510
use std::{
611
path::{Path, PathBuf},
@@ -92,7 +97,8 @@ fn filter_annotations_from_rendered(rendered: &str) -> std::borrow::Cow<'_, str>
9297
.replace_all(rendered, "")
9398
}
9499

95-
pub(crate) fn process(file: &Path, stderr: &[u8]) -> Diagnostics {
100+
/// `rustc` diagnostics extractor.
101+
pub fn rustc_diagnostics_extractor(file: &Path, stderr: &[u8]) -> Diagnostics {
96102
let mut rendered = Vec::new();
97103
let mut messages = vec![];
98104
let mut messages_from_unknown_file_or_line = vec![];
@@ -123,7 +129,8 @@ pub(crate) fn process(file: &Path, stderr: &[u8]) -> Diagnostics {
123129
}
124130
}
125131

126-
pub(crate) fn process_cargo(file: &Path, stderr: &[u8]) -> Diagnostics {
132+
/// `cargo` diagnostics extractor.
133+
pub fn cargo_diagnostics_extractor(file: &Path, stderr: &[u8]) -> Diagnostics {
127134
let mut rendered = Vec::new();
128135
let mut messages = vec![];
129136
let mut messages_from_unknown_file_or_line = vec![];
@@ -155,3 +162,17 @@ pub(crate) fn process_cargo(file: &Path, stderr: &[u8]) -> Diagnostics {
155162
messages_from_unknown_file_or_line,
156163
}
157164
}
165+
166+
impl From<DiagnosticLevel> for Level {
167+
fn from(value: DiagnosticLevel) -> Self {
168+
match value {
169+
DiagnosticLevel::Ice => Level::Ice,
170+
DiagnosticLevel::Error => Level::Error,
171+
DiagnosticLevel::Warning => Level::Warn,
172+
DiagnosticLevel::FailureNote => Level::FailureNote,
173+
DiagnosticLevel::Note => Level::Note,
174+
DiagnosticLevel::Help => Level::Help,
175+
other => panic!("rustc got a new kind of diagnostic level: {other:?}"),
176+
}
177+
}
178+
}

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ mod mode;
5656
pub mod nextest;
5757
mod parser;
5858
pub mod per_test_config;
59-
#[cfg(feature = "rustc")]
60-
mod rustc_stderr;
6159
pub mod status_emitter;
6260
pub mod test_result;
6361

@@ -104,6 +102,7 @@ pub fn run_tests(mut config: Config) -> Result<()> {
104102

105103
/// The filter used by `run_tests` to only run on `.rs` files that are
106104
/// specified by [`Config::filter_files`] and [`Config::skip_files`].
105+
///
107106
/// Returns `None` if there is no extension or the extension is not `.rs`.
108107
pub fn default_file_filter(path: &Path, config: &Config) -> Option<bool> {
109108
path.extension().filter(|&ext| ext == "rs")?;

src/per_test_config.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
//! This module allows you to configure the default settings for all
2-
//! tests. All data structures here are normally parsed from `@` comments
1+
//! This module allows you to configure the default settings for all tests.
2+
//!
3+
//! All data structures here are normally parsed from `@` comments
34
//! in the files. These comments still overwrite the defaults, although
45
//! some boolean settings have no way to disable them.
56

0 commit comments

Comments
 (0)