Skip to content

Use ui_test's Windows path backslash heuristic #11336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 48 additions & 71 deletions tests/compile-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
#![warn(rust_2018_idioms, unused_lifetimes)]
#![allow(unused_extern_crates)]

use compiletest::{status_emitter, Args, CommandBuilder, OutputConflictHandling};
use ui_test as compiletest;
use ui_test::Mode as TestMode;
use ui_test::{status_emitter, Args, CommandBuilder, Config, Match, Mode, OutputConflictHandling};

use std::collections::BTreeMap;
use std::env::{self, remove_var, set_var, var_os};
use std::env::{self, set_var, var_os};
use std::ffi::{OsStr, OsString};
use std::fs;
use std::path::{Path, PathBuf};
Expand All @@ -29,6 +27,8 @@ extern crate quote;
extern crate syn;
extern crate tokio;

mod test_utils;

/// All crates used in UI tests are listed here
static TEST_DEPENDENCIES: &[&str] = &[
"clippy_lints",
Expand Down Expand Up @@ -104,8 +104,6 @@ static EXTERN_FLAGS: LazyLock<Vec<String>> = LazyLock::new(|| {
.collect()
});

mod test_utils;

// whether to run internal tests or not
const RUN_INTERNAL_TESTS: bool = cfg!(feature = "internal");

Expand All @@ -115,7 +113,7 @@ fn canonicalize(path: impl AsRef<Path>) -> PathBuf {
fs::canonicalize(path).unwrap_or_else(|err| panic!("{} cannot be canonicalized: {err}", path.display()))
}

fn base_config(test_dir: &str) -> (compiletest::Config, Args) {
fn base_config(test_dir: &str) -> (Config, Args) {
let bless = var_os("RUSTC_BLESS").is_some_and(|v| v != "0") || env::args().any(|arg| arg == "--bless");

let args = Args {
Expand All @@ -131,18 +129,18 @@ fn base_config(test_dir: &str) -> (compiletest::Config, Args) {
skip: Vec::new(),
};

let mut config = compiletest::Config {
mode: TestMode::Yolo { rustfix: true },
stderr_filters: vec![],
let mut config = Config {
mode: Mode::Yolo { rustfix: true },
stderr_filters: vec![(Match::PathBackslash, b"/")],
stdout_filters: vec![],
output_conflict_handling: if bless {
OutputConflictHandling::Bless
} else {
OutputConflictHandling::Error("cargo uibless".into())
},
target: None,
out_dir: canonicalize(std::env::var_os("CARGO_TARGET_DIR").unwrap_or_else(|| "target".into())).join("ui_test"),
..compiletest::Config::rustc(Path::new("tests").join(test_dir))
out_dir: canonicalize(var_os("CARGO_TARGET_DIR").unwrap_or_else(|| "target".into())).join("ui_test"),
..Config::rustc(Path::new("tests").join(test_dir))
};
let current_exe_path = env::current_exe().unwrap();
let deps_path = current_exe_path.parent().unwrap();
Expand All @@ -167,10 +165,6 @@ fn base_config(test_dir: &str) -> (compiletest::Config, Args) {
config.program.args.push(dep.into());
}

// Normalize away slashes in windows paths.
config.stderr_filter(r"\\", "/");

//config.build_base = profile_path.join("test").join(test_dir);
config.program.program = profile_path.join(if cfg!(windows) {
"clippy-driver.exe"
} else {
Expand All @@ -180,18 +174,19 @@ fn base_config(test_dir: &str) -> (compiletest::Config, Args) {
}

fn run_ui() {
let (config, args) = base_config("ui");
// use tests/clippy.toml
let _g = VarGuard::set("CARGO_MANIFEST_DIR", canonicalize("tests"));
let _threads = VarGuard::set("RUST_TEST_THREADS", args.threads.to_string());
let (mut config, args) = base_config("ui");
config
.program
.envs
.push(("CLIPPY_CONF_DIR".into(), Some(canonicalize("tests").into())));

let quiet = args.quiet;

compiletest::run_tests_generic(
ui_test::run_tests_generic(
vec![config],
args,
compiletest::default_file_filter,
compiletest::default_per_file_config,
ui_test::default_file_filter,
ui_test::default_per_file_config,
if quiet {
status_emitter::Text::quiet()
} else {
Expand All @@ -212,11 +207,11 @@ fn run_internal_tests() {
}
let quiet = args.quiet;

compiletest::run_tests_generic(
ui_test::run_tests_generic(
vec![config],
args,
compiletest::default_file_filter,
compiletest::default_per_file_config,
ui_test::default_file_filter,
ui_test::default_per_file_config,
if quiet {
status_emitter::Text::quiet()
} else {
Expand All @@ -229,24 +224,27 @@ fn run_internal_tests() {
fn run_ui_toml() {
let (mut config, args) = base_config("ui-toml");

config.stderr_filter(
&regex::escape(
&canonicalize("tests")
.parent()
.unwrap()
.display()
.to_string()
.replace('\\', "/"),
config.stderr_filters = vec![
(
Match::Exact(
canonicalize("tests")
.parent()
.unwrap()
.to_string_lossy()
.as_bytes()
.to_vec(),
),
b"$DIR",
),
"$$DIR",
);
(Match::Exact(b"\\".to_vec()), b"/"),
];

let quiet = args.quiet;

ui_test::run_tests_generic(
vec![config],
args,
compiletest::default_file_filter,
ui_test::default_file_filter,
|config, path, _file_contents| {
config
.program
Expand Down Expand Up @@ -285,17 +283,20 @@ fn run_ui_cargo() {
});
config.edition = None;

config.stderr_filter(
&regex::escape(
&canonicalize("tests")
.parent()
.unwrap()
.display()
.to_string()
.replace('\\', "/"),
config.stderr_filters = vec![
(
Match::Exact(
canonicalize("tests")
.parent()
.unwrap()
.to_string_lossy()
.as_bytes()
.to_vec(),
),
b"$DIR",
),
"$$DIR",
);
(Match::Exact(b"\\".to_vec()), b"/"),
];

let quiet = args.quiet;

Expand Down Expand Up @@ -410,27 +411,3 @@ fn ui_cargo_toml_metadata() {
);
}
}

/// Restores an env var on drop
#[must_use]
struct VarGuard {
key: &'static str,
value: Option<OsString>,
}

impl VarGuard {
fn set(key: &'static str, val: impl AsRef<OsStr>) -> Self {
let value = var_os(key);
set_var(key, val);
Self { key, value }
}
}

impl Drop for VarGuard {
fn drop(&mut self) {
match self.value.as_deref() {
None => remove_var(self.key),
Some(value) => set_var(self.key, value),
}
}
}
12 changes: 6 additions & 6 deletions tests/ui/char_lit_as_u8_suggestions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ LL | let _ = 'a' as u8;
error: casting a character literal to `u8` truncates
--> $DIR/char_lit_as_u8_suggestions.rs:5:13
|
LL | let _ = '/n' as u8;
| ^^^^^^^^^^ help: use a byte literal instead: `b'/n'`
LL | let _ = '\n' as u8;
| ^^^^^^^^^^ help: use a byte literal instead: `b'\n'`
|
= note: `char` is four bytes wide, but `u8` is a single byte

error: casting a character literal to `u8` truncates
--> $DIR/char_lit_as_u8_suggestions.rs:6:13
|
LL | let _ = '/0' as u8;
| ^^^^^^^^^^ help: use a byte literal instead: `b'/0'`
LL | let _ = '\0' as u8;
| ^^^^^^^^^^ help: use a byte literal instead: `b'\0'`
|
= note: `char` is four bytes wide, but `u8` is a single byte

error: casting a character literal to `u8` truncates
--> $DIR/char_lit_as_u8_suggestions.rs:7:13
|
LL | let _ = '/x01' as u8;
| ^^^^^^^^^^^^ help: use a byte literal instead: `b'/x01'`
LL | let _ = '\x01' as u8;
| ^^^^^^^^^^^^ help: use a byte literal instead: `b'\x01'`
|
= note: `char` is four bytes wide, but `u8` is a single byte

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/crashes/ice-9405.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
warning: multiple lines skipped by escaped newline
--> $DIR/ice-9405.rs:6:10
|
LL | "/
LL | "\
| __________^
LL | |
LL | | {}",
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/doc/doc-fixable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ LL | /// The foo_bar function does _nothing_. See also `foo::bar`. (note the dot
error: item in documentation is missing backticks
--> $DIR/doc-fixable.rs:10:83
|
LL | /// Markdown is _weird_. I mean _really weird_. This /_ is ok. So is `_`. But not Foo::some_fun
LL | /// Markdown is _weird_. I mean _really weird_. This \_ is ok. So is `_`. But not Foo::some_fun
| ^^^^^^^^^^^^^
|
help: try
|
LL | /// Markdown is _weird_. I mean _really weird_. This /_ is ok. So is `_`. But not `Foo::some_fun`
LL | /// Markdown is _weird_. I mean _really weird_. This \_ is ok. So is `_`. But not `Foo::some_fun`
| ~~~~~~~~~~~~~~~

error: item in documentation is missing backticks
Expand Down
32 changes: 16 additions & 16 deletions tests/ui/eprint_with_newline.stderr
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
error: using `eprint!()` with a format string that ends in a single newline
--> $DIR/eprint_with_newline.rs:5:5
|
LL | eprint!("Hello/n");
LL | eprint!("Hello\n");
| ^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::print-with-newline` implied by `-D warnings`
help: use `eprintln!` instead
|
LL - eprint!("Hello/n");
LL - eprint!("Hello\n");
LL + eprintln!("Hello");
|

error: using `eprint!()` with a format string that ends in a single newline
--> $DIR/eprint_with_newline.rs:6:5
|
LL | eprint!("Hello {}/n", "world");
LL | eprint!("Hello {}\n", "world");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use `eprintln!` instead
|
LL - eprint!("Hello {}/n", "world");
LL - eprint!("Hello {}\n", "world");
LL + eprintln!("Hello {}", "world");
|

error: using `eprint!()` with a format string that ends in a single newline
--> $DIR/eprint_with_newline.rs:7:5
|
LL | eprint!("Hello {} {}/n", "world", "#2");
LL | eprint!("Hello {} {}\n", "world", "#2");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use `eprintln!` instead
|
LL - eprint!("Hello {} {}/n", "world", "#2");
LL - eprint!("Hello {} {}\n", "world", "#2");
LL + eprintln!("Hello {} {}", "world", "#2");
|

error: using `eprint!()` with a format string that ends in a single newline
--> $DIR/eprint_with_newline.rs:8:5
|
LL | eprint!("{}/n", 1265);
LL | eprint!("{}\n", 1265);
| ^^^^^^^^^^^^^^^^^^^^^
|
help: use `eprintln!` instead
|
LL - eprint!("{}/n", 1265);
LL - eprint!("{}\n", 1265);
LL + eprintln!("{}", 1265);
|

error: using `eprint!()` with a format string that ends in a single newline
--> $DIR/eprint_with_newline.rs:9:5
|
LL | eprint!("/n");
LL | eprint!("\n");
| ^^^^^^^^^^^^^
|
help: use `eprintln!` instead
|
LL - eprint!("/n");
LL - eprint!("\n");
LL + eprintln!();
|

error: using `eprint!()` with a format string that ends in a single newline
--> $DIR/eprint_with_newline.rs:28:5
|
LL | eprint!("///n"); // should fail
LL | eprint!("\\\n"); // should fail
| ^^^^^^^^^^^^^^^
|
help: use `eprintln!` instead
|
LL - eprint!("///n"); // should fail
LL + eprintln!("//"); // should fail
LL - eprint!("\\\n"); // should fail
LL + eprintln!("\\"); // should fail
|

error: using `eprint!()` with a format string that ends in a single newline
Expand Down Expand Up @@ -104,13 +104,13 @@ LL ~
error: using `eprint!()` with a format string that ends in a single newline
--> $DIR/eprint_with_newline.rs:47:5
|
LL | eprint!("//r/n");
LL | eprint!("\\r\n");
| ^^^^^^^^^^^^^^^^
|
help: use `eprintln!` instead
|
LL - eprint!("//r/n");
LL + eprintln!("//r");
LL - eprint!("\\r\n");
LL + eprintln!("\\r");
|

error: aborting due to 9 previous errors
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/explicit_write.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ LL | std::io::stderr().write_fmt(format_args!("test")).unwrap();
error: use of `writeln!(stdout(), ...).unwrap()`
--> $DIR/explicit_write.rs:31:9
|
LL | writeln!(std::io::stdout(), "test/ntest").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `println!("test/ntest")`
LL | writeln!(std::io::stdout(), "test\ntest").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `println!("test\ntest")`

error: use of `writeln!(stderr(), ...).unwrap()`
--> $DIR/explicit_write.rs:32:9
|
LL | writeln!(std::io::stderr(), "test/ntest").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `eprintln!("test/ntest")`
LL | writeln!(std::io::stderr(), "test\ntest").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `eprintln!("test\ntest")`

error: use of `writeln!(stderr(), ...).unwrap()`
--> $DIR/explicit_write.rs:35:9
Expand Down
Loading