Skip to content

Commit

Permalink
Merge pull request #138 from dtolnay/inputfile
Browse files Browse the repository at this point in the history
Strip line numbers outside of the one input file
  • Loading branch information
dtolnay authored Oct 26, 2021
2 parents df0dcc7 + ae88a10 commit 0895db4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 14 deletions.
45 changes: 34 additions & 11 deletions src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ mod tests;

use crate::directory::Directory;
use crate::run::PathDependency;
use std::path::Path;

#[derive(Copy, Clone)]
pub struct Context<'a> {
pub krate: &'a str,
pub source_dir: &'a Directory,
pub workspace: &'a Directory,
pub input_file: &'a Path,
pub path_dependencies: &'a [PathDependency],
}

Expand Down Expand Up @@ -56,6 +58,7 @@ pub fn diagnostics(output: Vec<u8>, context: Context) -> Variations {
CargoRegistry,
ArrowOtherCrate,
RelativeToDir,
LinesOutsideInputFile,
]
.iter()
.map(|normalization| apply(&from_bytes, *normalization, context))
Expand Down Expand Up @@ -93,6 +96,7 @@ enum Normalization {
CargoRegistry,
ArrowOtherCrate,
RelativeToDir,
LinesOutsideInputFile,
// New normalization steps are to be inserted here at the end so that any
// snapshots saved before your normalization change remain passing.
}
Expand Down Expand Up @@ -164,24 +168,43 @@ impl<'a> Filter<'a> {
.to_string_lossy()
.to_ascii_lowercase()
.replace('\\', "/");
let mut other_crate = false;
if let Some(i) = line_lower.find(&source_dir_pat) {
if self.normalization >= RelativeToDir && i == indent + 4 {
line.replace_range(i..i + source_dir_pat.len(), "");
if self.normalization < LinesOutsideInputFile {
return Some(line);
}
let input_file_pat = self
.context
.input_file
.to_string_lossy()
.to_ascii_lowercase()
.replace('\\', "/");
if line_lower[i + source_dir_pat.len()..].starts_with(&input_file_pat) {
// Keep line numbers only within the input file (the
// path passed to our `fn compile_fail`. All other
// source files get line numbers erased below.
return Some(line);
}
} else {
line.replace_range(i..i + source_dir_pat.len() - 1, "$DIR");
if self.normalization < LinesOutsideInputFile {
return Some(line);
}
}
return Some(line);
}
let mut other_crate = false;
let workspace_pat = self
.context
.workspace
.to_string_lossy()
.to_ascii_lowercase()
.replace('\\', "/");
if let Some(i) = line_lower.find(&workspace_pat) {
line.replace_range(i..i + workspace_pat.len() - 1, "$WORKSPACE");
other_crate = true;
} else {
let workspace_pat = self
.context
.workspace
.to_string_lossy()
.to_ascii_lowercase()
.replace('\\', "/");
if let Some(i) = line_lower.find(&workspace_pat) {
line.replace_range(i..i + workspace_pat.len() - 1, "$WORKSPACE");
other_crate = true;
}
}
if self.normalization >= PathDependencies && !other_crate {
for path_dep in self.context.path_dependencies {
Expand Down
1 change: 1 addition & 0 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ impl Test {
krate: &name.0,
source_dir: &project.source_dir,
workspace: &project.workspace,
input_file: &self.path,
path_dependencies: &project.path_dependencies,
},
);
Expand Down
45 changes: 42 additions & 3 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::directory::Directory;
use crate::run::PathDependency;
use std::path::Path;

macro_rules! test_normalize {
($name:ident $(DIR=$dir:literal)? $(WORKSPACE=$workspace:literal)? $original:literal $expected:literal) => {
($name:ident $(DIR=$dir:literal)? $(WORKSPACE=$workspace:literal)? $(INPUT=$input:literal)? $original:literal $expected:literal) => {
#[test]
fn $name() {
let context = super::Context {
krate: "trybuild000",
input_file: Path::new({ "tests/ui/error.rs" $(; $input)? }),
source_dir: &Directory::new({ "/git/trybuild/test_suite" $(; $dir)? }),
workspace: &Directory::new({ "/git/trybuild" $(; $workspace)? }),
path_dependencies: &[PathDependency {
Expand Down Expand Up @@ -54,7 +56,9 @@ error[E0277]: the trait bound `QueryParams: serde::de::Deserialize<'de>` is not
--> tests/ui/error.rs:22:61
"}

test_normalize! {test_rust_lib "
test_normalize! {test_rust_lib
INPUT="tests/ui/not-repeatable.rs"
"
error[E0599]: no method named `quote_into_iter` found for struct `std::net::Ipv4Addr` in the current scope
--> /git/trybuild/test_suite/tests/ui/not-repeatable.rs:6:13
|
Expand Down Expand Up @@ -84,7 +88,9 @@ error[E0599]: no method named `quote_into_iter` found for struct `std::net::Ipv4
| doesn't satisfy `std::net::Ipv4Addr: quote::to_tokens::ToTokens`
"}

test_normalize! {test_type_dir_backslash "
test_normalize! {test_type_dir_backslash
INPUT="tests/ui/compile-fail-3.rs"
"
error[E0277]: `*mut _` cannot be shared between threads safely
--> /git/trybuild/test_suite/tests/ui/compile-fail-3.rs:7:5
|
Expand Down Expand Up @@ -269,3 +275,36 @@ Additional crates such as `pyo3-asyncio` can be used to integrate async Rust and
10 | async fn async_function() {}
| ^^^^^
"}

test_normalize! {test_dropshot_required_by
DIR="/git/dropshot/dropshot"
WORKSPACE="/git/dropshot"
INPUT="tests/fail/bad_endpoint4.rs"
"
error[E0277]: the trait bound `QueryParams: schemars::JsonSchema` is not satisfied
--> /git/dropshot/dropshot/tests/fail/bad_endpoint4.rs:24:14
|
24 | _params: Query<QueryParams>,
| ^^^^^^^^^^^^^^^^^^ the trait `schemars::JsonSchema` is not implemented for `QueryParams`
|
note: required by a bound in `dropshot::Query`
--> /git/dropshot/dropshot/src/handler.rs:547:48
|
547 | pub struct Query<QueryType: DeserializeOwned + JsonSchema + Send + Sync> {
| ^^^^^^^^^^ required by this bound in `dropshot::Query`
"
// TODO: it would be nice to also unindent the column of `|` by one column.
// https://github.com/dtolnay/trybuild/issues/86
"
error[E0277]: the trait bound `QueryParams: schemars::JsonSchema` is not satisfied
--> tests/fail/bad_endpoint4.rs:24:14
|
24 | _params: Query<QueryParams>,
| ^^^^^^^^^^^^^^^^^^ the trait `schemars::JsonSchema` is not implemented for `QueryParams`
|
note: required by a bound in `dropshot::Query`
--> src/handler.rs
|
| pub struct Query<QueryType: DeserializeOwned + JsonSchema + Send + Sync> {
| ^^^^^^^^^^ required by this bound in `dropshot::Query`
"}

0 comments on commit 0895db4

Please sign in to comment.