Skip to content

Commit 5983ba8

Browse files
committed
Move Match into its own module
1 parent df8246c commit 5983ba8

File tree

5 files changed

+77
-70
lines changed

5 files changed

+77
-70
lines changed

src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use regex::bytes::Regex;
22
use spanned::{Span, Spanned};
33

44
use crate::{
5-
dependencies::build_dependencies, per_test_config::Comments, CommandBuilder, Match, Mode,
6-
RustfixMode,
5+
dependencies::build_dependencies, filter::Match, per_test_config::Comments, CommandBuilder,
6+
Mode, RustfixMode,
77
};
88
pub use color_eyre;
99
use color_eyre::eyre::Result;

src/filter.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//! Datastructures and operations used for normalizing test output.
2+
3+
use bstr::ByteSlice;
4+
use lazy_static::lazy_static;
5+
use regex::bytes::{Captures, Regex};
6+
use std::borrow::Cow;
7+
use std::path::Path;
8+
9+
/// A filter's match rule.
10+
#[derive(Clone, Debug)]
11+
pub enum Match {
12+
/// If the regex matches, the filter applies
13+
Regex(Regex),
14+
/// If the exact byte sequence is found, the filter applies
15+
Exact(Vec<u8>),
16+
/// Uses a heuristic to find backslashes in windows style paths
17+
PathBackslash,
18+
}
19+
20+
impl Match {
21+
pub(crate) fn replace_all<'a>(&self, text: &'a [u8], replacement: &[u8]) -> Cow<'a, [u8]> {
22+
match self {
23+
Match::Regex(regex) => regex.replace_all(text, replacement),
24+
Match::Exact(needle) => text.replace(needle, replacement).into(),
25+
Match::PathBackslash => {
26+
lazy_static! {
27+
static ref PATH_RE: Regex = Regex::new(
28+
r"(?x)
29+
(?:
30+
# Match paths to files with extensions that don't include spaces
31+
\\(?:[\pL\pN.\-_']+[/\\])*[\pL\pN.\-_']+\.\pL+
32+
|
33+
# Allow spaces in absolute paths
34+
[A-Z]:\\(?:[\pL\pN.\-_'\ ]+[/\\])+
35+
)",
36+
)
37+
.unwrap();
38+
}
39+
40+
PATH_RE.replace_all(text, |caps: &Captures<'_>| {
41+
caps[0].replace(r"\", replacement)
42+
})
43+
}
44+
}
45+
}
46+
}
47+
48+
impl From<&'_ Path> for Match {
49+
fn from(v: &Path) -> Self {
50+
let mut v = v.display().to_string();
51+
// Normalize away windows canonicalized paths.
52+
if v.starts_with(r"\\?\") {
53+
v.drain(0..4);
54+
}
55+
let mut v = v.into_bytes();
56+
// Normalize paths on windows to use slashes instead of backslashes,
57+
// So that paths are rendered the same on all systems.
58+
for c in &mut v {
59+
if *c == b'\\' {
60+
*c = b'/';
61+
}
62+
}
63+
Self::Exact(v)
64+
}
65+
}
66+
67+
impl From<Regex> for Match {
68+
fn from(v: Regex) -> Self {
69+
Self::Regex(v)
70+
}
71+
}

src/lib.rs

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ pub use color_eyre;
1313
use color_eyre::eyre::{eyre, Result};
1414
use crossbeam_channel::{unbounded, Receiver, Sender};
1515
use dependencies::{Build, BuildManager};
16-
use lazy_static::lazy_static;
1716
use parser::{ErrorMatch, ErrorMatchKind, OptWithLine, Revisioned, Spanned};
18-
use regex::bytes::{Captures, Regex};
1917
use rustc_stderr::{Level, Message};
2018
use spanned::Span;
2119
use status_emitter::{StatusEmitter, TestStatus};
22-
use std::borrow::Cow;
2320
use std::collections::{HashSet, VecDeque};
2421
use std::ffi::OsString;
2522
use std::num::NonZeroUsize;
@@ -34,12 +31,14 @@ mod config;
3431
mod dependencies;
3532
mod diff;
3633
mod error;
34+
pub mod filter;
3735
pub mod github_actions;
3836
mod mode;
3937
mod parser;
4038
pub mod per_test_config;
4139
mod rustc_stderr;
4240
pub mod status_emitter;
41+
4342
#[cfg(test)]
4443
mod tests;
4544

@@ -50,69 +49,6 @@ pub use mode::*;
5049

5150
pub use spanned;
5251

53-
/// A filter's match rule.
54-
#[derive(Clone, Debug)]
55-
pub enum Match {
56-
/// If the regex matches, the filter applies
57-
Regex(Regex),
58-
/// If the exact byte sequence is found, the filter applies
59-
Exact(Vec<u8>),
60-
/// Uses a heuristic to find backslashes in windows style paths
61-
PathBackslash,
62-
}
63-
impl Match {
64-
fn replace_all<'a>(&self, text: &'a [u8], replacement: &[u8]) -> Cow<'a, [u8]> {
65-
match self {
66-
Match::Regex(regex) => regex.replace_all(text, replacement),
67-
Match::Exact(needle) => text.replace(needle, replacement).into(),
68-
Match::PathBackslash => {
69-
lazy_static! {
70-
static ref PATH_RE: Regex = Regex::new(
71-
r"(?x)
72-
(?:
73-
# Match paths to files with extensions that don't include spaces
74-
\\(?:[\pL\pN.\-_']+[/\\])*[\pL\pN.\-_']+\.\pL+
75-
|
76-
# Allow spaces in absolute paths
77-
[A-Z]:\\(?:[\pL\pN.\-_'\ ]+[/\\])+
78-
)",
79-
)
80-
.unwrap();
81-
}
82-
83-
PATH_RE.replace_all(text, |caps: &Captures<'_>| {
84-
caps[0].replace(r"\", replacement)
85-
})
86-
}
87-
}
88-
}
89-
}
90-
91-
impl From<&'_ Path> for Match {
92-
fn from(v: &Path) -> Self {
93-
let mut v = v.display().to_string();
94-
// Normalize away windows canonicalized paths.
95-
if v.starts_with(r"\\?\") {
96-
v.drain(0..4);
97-
}
98-
let mut v = v.into_bytes();
99-
// Normalize paths on windows to use slashes instead of backslashes,
100-
// So that paths are rendered the same on all systems.
101-
for c in &mut v {
102-
if *c == b'\\' {
103-
*c = b'/';
104-
}
105-
}
106-
Self::Exact(v)
107-
}
108-
}
109-
110-
impl From<Regex> for Match {
111-
fn from(v: Regex) -> Self {
112-
Self::Regex(v)
113-
}
114-
}
115-
11652
/// Run all tests as described in the config argument.
11753
/// Will additionally process command line arguments.
11854
pub fn run_tests(mut config: Config) -> Result<()> {

src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
use bstr::{ByteSlice, Utf8Error};
99
use regex::bytes::Regex;
1010

11-
use crate::{rustc_stderr::Level, Error, Errored, Match, Mode};
11+
use crate::{filter::Match, rustc_stderr::Level, Error, Errored, Mode};
1212

1313
use color_eyre::eyre::{Context, Result};
1414

tests/integration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::Path;
22

33
use ui_test::color_eyre::Result;
4-
use ui_test::{spanned::Spanned, *};
4+
use ui_test::{filter::Match, spanned::Spanned, *};
55

66
fn main() -> Result<()> {
77
let path = Path::new(file!()).parent().unwrap();

0 commit comments

Comments
 (0)