Skip to content

Commit

Permalink
Export default diagnostics extractors
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Oct 14, 2024
1 parent 02ba57d commit 29cf9b1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
18 changes: 8 additions & 10 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,
custom_flags::Flag, filter::Match,
};
use crate::{
diagnostics::Diagnostics,
diagnostics::{default_diagnostics_extractor, 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: default_diagnostics_extractor,
abort_check: Default::default(),
}
}
Expand All @@ -104,7 +100,7 @@ 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;
use crate::{custom_flags::edition::Edition, diagnostics::rustc_diagnostics_extractor};

let mut comment_defaults = Comments::default();

Expand Down Expand Up @@ -175,7 +171,7 @@ impl Config {
comment_defaults,
comment_start: "//",
custom_comments: Default::default(),
diagnostic_extractor: rustc_stderr::process,
diagnostic_extractor: rustc_diagnostics_extractor,
abort_check: Default::default(),
};
config
Expand Down Expand Up @@ -246,10 +242,12 @@ impl Config {
/// `cargo` on the test `Cargo.toml` files.
#[cfg(feature = "rustc")]
pub fn cargo(root_dir: impl Into<PathBuf>) -> Self {
use crate::diagnostics::cargo_diagnostics_extractor;

let mut this = Self {
program: CommandBuilder::cargo(),
custom_comments: Default::default(),
diagnostic_extractor: rustc_stderr::process_cargo,
diagnostic_extractor: cargo_diagnostics_extractor,
comment_start: "#",
..Self::rustc(root_dir)
};
Expand Down
19 changes: 15 additions & 4 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@
#[cfg(feature = "rustc")]
use cargo_metadata::diagnostic::DiagnosticLevel;
use std::path::Path;

#[cfg(feature = "rustc")]
mod rustc_stderr;
#[cfg(feature = "rustc")]
pub use rustc_stderr::{cargo_diagnostics_extractor, rustc_diagnostics_extractor};

/// 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 Down Expand Up @@ -50,8 +61,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 +76,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
11 changes: 9 additions & 2 deletions src/rustc_stderr.rs → src/diagnostics/rustc_stderr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//! `rustc` and `cargo` diagnostics extractors.
//!
//! These parse diagnostics from the respective stderr JSON output using the
//! data structures defined in [`cargo_metadata::diagnostic`].
use crate::diagnostics::{Diagnostics, Message};
use bstr::ByteSlice;
use cargo_metadata::diagnostic::{Diagnostic, DiagnosticSpan};
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
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 29cf9b1

Please sign in to comment.