Skip to content

Commit

Permalink
Merge pull request #289 from DaniPopes/pub-diagnostics-extractors
Browse files Browse the repository at this point in the history
Export default diagnostics extractors
  • Loading branch information
oli-obk authored Oct 14, 2024
2 parents 02ba57d + 0911fb4 commit fb3c75b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 40 deletions.
18 changes: 6 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#[cfg(feature = "rustc")]
use crate::{
aux_builds::AuxBuilder, custom_flags::run::Run, custom_flags::rustfix::RustfixMode,
custom_flags::Flag, filter::Match, rustc_stderr,
aux_builds::AuxBuilder, custom_flags::edition::Edition, custom_flags::run::Run,
custom_flags::rustfix::RustfixMode, custom_flags::Flag, filter::Match,
};
use crate::{
diagnostics::Diagnostics,
diagnostics::{self, Diagnostics},
parser::CommandParserFunc,
per_test_config::{Comments, Condition},
CommandBuilder, Error, Errors,
Expand Down Expand Up @@ -91,11 +91,7 @@ impl Config {
comment_defaults,
comment_start: "//",
custom_comments: Default::default(),
diagnostic_extractor: |_, _| Diagnostics {
rendered: Default::default(),
messages: Default::default(),
messages_from_unknown_file_or_line: Default::default(),
},
diagnostic_extractor: diagnostics::default_diagnostics_extractor,
abort_check: Default::default(),
}
}
Expand All @@ -104,8 +100,6 @@ impl Config {
/// `rustc` on the test files.
#[cfg(feature = "rustc")]
pub fn rustc(root_dir: impl Into<PathBuf>) -> Self {
use crate::custom_flags::edition::Edition;

let mut comment_defaults = Comments::default();

#[derive(Debug)]
Expand Down Expand Up @@ -175,7 +169,7 @@ impl Config {
comment_defaults,
comment_start: "//",
custom_comments: Default::default(),
diagnostic_extractor: rustc_stderr::process,
diagnostic_extractor: diagnostics::rustc::rustc_diagnostics_extractor,
abort_check: Default::default(),
};
config
Expand Down Expand Up @@ -249,7 +243,7 @@ impl Config {
let mut this = Self {
program: CommandBuilder::cargo(),
custom_comments: Default::default(),
diagnostic_extractor: rustc_stderr::process_cargo,
diagnostic_extractor: diagnostics::rustc::cargo_diagnostics_extractor,
comment_start: "#",
..Self::rustc(root_dir)
};
Expand Down
32 changes: 12 additions & 20 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
//! Data structures for handling diagnostic output from tests.
use std::path::Path;

#[cfg(feature = "rustc")]
use cargo_metadata::diagnostic::DiagnosticLevel;
pub mod rustc;

/// Default diagnostics extractor that does nothing.
pub fn default_diagnostics_extractor(_path: &Path, _stderr: &[u8]) -> Diagnostics {
Diagnostics::default()
}

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

#[cfg(feature = "rustc")]
impl From<DiagnosticLevel> for Level {
fn from(value: DiagnosticLevel) -> Self {
match value {
DiagnosticLevel::Ice => Level::Ice,
DiagnosticLevel::Error => Level::Error,
DiagnosticLevel::Warning => Level::Warn,
DiagnosticLevel::FailureNote => Level::FailureNote,
DiagnosticLevel::Note => Level::Note,
DiagnosticLevel::Help => Level::Help,
other => panic!("rustc got a new kind of diagnostic level: {other:?}"),
}
}
}

impl std::str::FromStr for Level {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand All @@ -50,8 +42,8 @@ impl std::str::FromStr for Level {
}
}

#[derive(Debug)]
/// A diagnostic message.
#[derive(Debug)]
pub struct Message {
/// The diagnostic level at which this message was emitted
pub level: Level,
Expand All @@ -65,8 +57,8 @@ pub struct Message {
pub code: Option<String>,
}

#[derive(Debug)]
/// All the diagnostics that were emitted in a test
/// All the diagnostics that were emitted in a test.
#[derive(Default, Debug)]
pub struct Diagnostics {
/// Rendered and concatenated version of all diagnostics.
/// This is equivalent to non-json diagnostics.
Expand Down
29 changes: 25 additions & 4 deletions src/rustc_stderr.rs → src/diagnostics/rustc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use crate::diagnostics::{Diagnostics, Message};
//! `rustc` and `cargo` diagnostics extractors.
//!
//! These parse diagnostics from the respective stderr JSON output using the
//! data structures defined in [`cargo_metadata::diagnostic`].
use super::{Diagnostics, Level, Message};
use bstr::ByteSlice;
use cargo_metadata::diagnostic::{Diagnostic, DiagnosticSpan};
use cargo_metadata::diagnostic::{Diagnostic, DiagnosticLevel, DiagnosticSpan};
use regex::Regex;
use std::{
path::{Path, PathBuf},
Expand Down Expand Up @@ -92,7 +97,8 @@ fn filter_annotations_from_rendered(rendered: &str) -> std::borrow::Cow<'_, str>
.replace_all(rendered, "")
}

pub(crate) fn process(file: &Path, stderr: &[u8]) -> Diagnostics {
/// `rustc` diagnostics extractor.
pub fn rustc_diagnostics_extractor(file: &Path, stderr: &[u8]) -> Diagnostics {
let mut rendered = Vec::new();
let mut messages = vec![];
let mut messages_from_unknown_file_or_line = vec![];
Expand Down Expand Up @@ -123,7 +129,8 @@ pub(crate) fn process(file: &Path, stderr: &[u8]) -> Diagnostics {
}
}

pub(crate) fn process_cargo(file: &Path, stderr: &[u8]) -> Diagnostics {
/// `cargo` diagnostics extractor.
pub fn cargo_diagnostics_extractor(file: &Path, stderr: &[u8]) -> Diagnostics {
let mut rendered = Vec::new();
let mut messages = vec![];
let mut messages_from_unknown_file_or_line = vec![];
Expand Down Expand Up @@ -155,3 +162,17 @@ pub(crate) fn process_cargo(file: &Path, stderr: &[u8]) -> Diagnostics {
messages_from_unknown_file_or_line,
}
}

impl From<DiagnosticLevel> for Level {
fn from(value: DiagnosticLevel) -> Self {
match value {
DiagnosticLevel::Ice => Level::Ice,
DiagnosticLevel::Error => Level::Error,
DiagnosticLevel::Warning => Level::Warn,
DiagnosticLevel::FailureNote => Level::FailureNote,
DiagnosticLevel::Note => Level::Note,
DiagnosticLevel::Help => Level::Help,
other => panic!("rustc got a new kind of diagnostic level: {other:?}"),
}
}
}
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ mod mode;
pub mod nextest;
mod parser;
pub mod per_test_config;
#[cfg(feature = "rustc")]
mod rustc_stderr;
pub mod status_emitter;
pub mod test_result;

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

/// The filter used by `run_tests` to only run on `.rs` files that are
/// specified by [`Config::filter_files`] and [`Config::skip_files`].
///
/// Returns `None` if there is no extension or the extension is not `.rs`.
pub fn default_file_filter(path: &Path, config: &Config) -> Option<bool> {
path.extension().filter(|&ext| ext == "rs")?;
Expand Down
5 changes: 3 additions & 2 deletions src/per_test_config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! This module allows you to configure the default settings for all
//! tests. All data structures here are normally parsed from `@` comments
//! This module allows you to configure the default settings for all tests.
//!
//! All data structures here are normally parsed from `@` comments
//! in the files. These comments still overwrite the defaults, although
//! some boolean settings have no way to disable them.
Expand Down

0 comments on commit fb3c75b

Please sign in to comment.