Skip to content

Commit 710db18

Browse files
committed
Auto merge of #11336 - Alexendoo:uitest-backslash, r=flip1995
Use ui_test's Windows path backslash heuristic changelog: none Instead of unconditionally replacing `\` with `/` we now use [`Match::PathBackslash`](https://docs.rs/ui_test/latest/ui_test/enum.Match.html#variant.PathBackslash) to only replace backslashes in paths that look like windows paths `ui-toml` and `ui-cargo` tests still use the old way because they produce verbatim paths on windows in some tests (`\\?\C:\foo\...`) which was finnicky to get the replacement order correct with Also removes the `ui_test` -> `compiletest` alias and `VarGuard`
2 parents 11efa01 + 77d10ac commit 710db18

21 files changed

+237
-260
lines changed

tests/compile-test.rs

+48-71
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
#![warn(rust_2018_idioms, unused_lifetimes)]
55
#![allow(unused_extern_crates)]
66

7-
use compiletest::{status_emitter, Args, CommandBuilder, OutputConflictHandling};
8-
use ui_test as compiletest;
9-
use ui_test::Mode as TestMode;
7+
use ui_test::{status_emitter, Args, CommandBuilder, Config, Match, Mode, OutputConflictHandling};
108

119
use std::collections::BTreeMap;
12-
use std::env::{self, remove_var, set_var, var_os};
10+
use std::env::{self, set_var, var_os};
1311
use std::ffi::{OsStr, OsString};
1412
use std::fs;
1513
use std::path::{Path, PathBuf};
@@ -29,6 +27,8 @@ extern crate quote;
2927
extern crate syn;
3028
extern crate tokio;
3129

30+
mod test_utils;
31+
3232
/// All crates used in UI tests are listed here
3333
static TEST_DEPENDENCIES: &[&str] = &[
3434
"clippy_lints",
@@ -104,8 +104,6 @@ static EXTERN_FLAGS: LazyLock<Vec<String>> = LazyLock::new(|| {
104104
.collect()
105105
});
106106

107-
mod test_utils;
108-
109107
// whether to run internal tests or not
110108
const RUN_INTERNAL_TESTS: bool = cfg!(feature = "internal");
111109

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

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

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

134-
let mut config = compiletest::Config {
135-
mode: TestMode::Yolo { rustfix: true },
136-
stderr_filters: vec![],
132+
let mut config = Config {
133+
mode: Mode::Yolo { rustfix: true },
134+
stderr_filters: vec![(Match::PathBackslash, b"/")],
137135
stdout_filters: vec![],
138136
output_conflict_handling: if bless {
139137
OutputConflictHandling::Bless
140138
} else {
141139
OutputConflictHandling::Error("cargo uibless".into())
142140
},
143141
target: None,
144-
out_dir: canonicalize(std::env::var_os("CARGO_TARGET_DIR").unwrap_or_else(|| "target".into())).join("ui_test"),
145-
..compiletest::Config::rustc(Path::new("tests").join(test_dir))
142+
out_dir: canonicalize(var_os("CARGO_TARGET_DIR").unwrap_or_else(|| "target".into())).join("ui_test"),
143+
..Config::rustc(Path::new("tests").join(test_dir))
146144
};
147145
let current_exe_path = env::current_exe().unwrap();
148146
let deps_path = current_exe_path.parent().unwrap();
@@ -167,10 +165,6 @@ fn base_config(test_dir: &str) -> (compiletest::Config, Args) {
167165
config.program.args.push(dep.into());
168166
}
169167

170-
// Normalize away slashes in windows paths.
171-
config.stderr_filter(r"\\", "/");
172-
173-
//config.build_base = profile_path.join("test").join(test_dir);
174168
config.program.program = profile_path.join(if cfg!(windows) {
175169
"clippy-driver.exe"
176170
} else {
@@ -180,18 +174,19 @@ fn base_config(test_dir: &str) -> (compiletest::Config, Args) {
180174
}
181175

182176
fn run_ui() {
183-
let (config, args) = base_config("ui");
184-
// use tests/clippy.toml
185-
let _g = VarGuard::set("CARGO_MANIFEST_DIR", canonicalize("tests"));
186-
let _threads = VarGuard::set("RUST_TEST_THREADS", args.threads.to_string());
177+
let (mut config, args) = base_config("ui");
178+
config
179+
.program
180+
.envs
181+
.push(("CLIPPY_CONF_DIR".into(), Some(canonicalize("tests").into())));
187182

188183
let quiet = args.quiet;
189184

190-
compiletest::run_tests_generic(
185+
ui_test::run_tests_generic(
191186
vec![config],
192187
args,
193-
compiletest::default_file_filter,
194-
compiletest::default_per_file_config,
188+
ui_test::default_file_filter,
189+
ui_test::default_per_file_config,
195190
if quiet {
196191
status_emitter::Text::quiet()
197192
} else {
@@ -212,11 +207,11 @@ fn run_internal_tests() {
212207
}
213208
let quiet = args.quiet;
214209

215-
compiletest::run_tests_generic(
210+
ui_test::run_tests_generic(
216211
vec![config],
217212
args,
218-
compiletest::default_file_filter,
219-
compiletest::default_per_file_config,
213+
ui_test::default_file_filter,
214+
ui_test::default_per_file_config,
220215
if quiet {
221216
status_emitter::Text::quiet()
222217
} else {
@@ -229,24 +224,27 @@ fn run_internal_tests() {
229224
fn run_ui_toml() {
230225
let (mut config, args) = base_config("ui-toml");
231226

232-
config.stderr_filter(
233-
&regex::escape(
234-
&canonicalize("tests")
235-
.parent()
236-
.unwrap()
237-
.display()
238-
.to_string()
239-
.replace('\\', "/"),
227+
config.stderr_filters = vec![
228+
(
229+
Match::Exact(
230+
canonicalize("tests")
231+
.parent()
232+
.unwrap()
233+
.to_string_lossy()
234+
.as_bytes()
235+
.to_vec(),
236+
),
237+
b"$DIR",
240238
),
241-
"$$DIR",
242-
);
239+
(Match::Exact(b"\\".to_vec()), b"/"),
240+
];
243241

244242
let quiet = args.quiet;
245243

246244
ui_test::run_tests_generic(
247245
vec![config],
248246
args,
249-
compiletest::default_file_filter,
247+
ui_test::default_file_filter,
250248
|config, path, _file_contents| {
251249
config
252250
.program
@@ -285,17 +283,20 @@ fn run_ui_cargo() {
285283
});
286284
config.edition = None;
287285

288-
config.stderr_filter(
289-
&regex::escape(
290-
&canonicalize("tests")
291-
.parent()
292-
.unwrap()
293-
.display()
294-
.to_string()
295-
.replace('\\', "/"),
286+
config.stderr_filters = vec![
287+
(
288+
Match::Exact(
289+
canonicalize("tests")
290+
.parent()
291+
.unwrap()
292+
.to_string_lossy()
293+
.as_bytes()
294+
.to_vec(),
295+
),
296+
b"$DIR",
296297
),
297-
"$$DIR",
298-
);
298+
(Match::Exact(b"\\".to_vec()), b"/"),
299+
];
299300

300301
let quiet = args.quiet;
301302

@@ -410,27 +411,3 @@ fn ui_cargo_toml_metadata() {
410411
);
411412
}
412413
}
413-
414-
/// Restores an env var on drop
415-
#[must_use]
416-
struct VarGuard {
417-
key: &'static str,
418-
value: Option<OsString>,
419-
}
420-
421-
impl VarGuard {
422-
fn set(key: &'static str, val: impl AsRef<OsStr>) -> Self {
423-
let value = var_os(key);
424-
set_var(key, val);
425-
Self { key, value }
426-
}
427-
}
428-
429-
impl Drop for VarGuard {
430-
fn drop(&mut self) {
431-
match self.value.as_deref() {
432-
None => remove_var(self.key),
433-
Some(value) => set_var(self.key, value),
434-
}
435-
}
436-
}

tests/ui/char_lit_as_u8_suggestions.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ LL | let _ = 'a' as u8;
1010
error: casting a character literal to `u8` truncates
1111
--> $DIR/char_lit_as_u8_suggestions.rs:5:13
1212
|
13-
LL | let _ = '/n' as u8;
14-
| ^^^^^^^^^^ help: use a byte literal instead: `b'/n'`
13+
LL | let _ = '\n' as u8;
14+
| ^^^^^^^^^^ help: use a byte literal instead: `b'\n'`
1515
|
1616
= note: `char` is four bytes wide, but `u8` is a single byte
1717

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

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

tests/ui/crashes/ice-9405.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
warning: multiple lines skipped by escaped newline
22
--> $DIR/ice-9405.rs:6:10
33
|
4-
LL | "/
4+
LL | "\
55
| __________^
66
LL | |
77
LL | | {}",

tests/ui/doc/doc-fixable.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ LL | /// The foo_bar function does _nothing_. See also `foo::bar`. (note the dot
2424
error: item in documentation is missing backticks
2525
--> $DIR/doc-fixable.rs:10:83
2626
|
27-
LL | /// Markdown is _weird_. I mean _really weird_. This /_ is ok. So is `_`. But not Foo::some_fun
27+
LL | /// Markdown is _weird_. I mean _really weird_. This \_ is ok. So is `_`. But not Foo::some_fun
2828
| ^^^^^^^^^^^^^
2929
|
3030
help: try
3131
|
32-
LL | /// Markdown is _weird_. I mean _really weird_. This /_ is ok. So is `_`. But not `Foo::some_fun`
32+
LL | /// Markdown is _weird_. I mean _really weird_. This \_ is ok. So is `_`. But not `Foo::some_fun`
3333
| ~~~~~~~~~~~~~~~
3434

3535
error: item in documentation is missing backticks

tests/ui/eprint_with_newline.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,74 @@
11
error: using `eprint!()` with a format string that ends in a single newline
22
--> $DIR/eprint_with_newline.rs:5:5
33
|
4-
LL | eprint!("Hello/n");
4+
LL | eprint!("Hello\n");
55
| ^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::print-with-newline` implied by `-D warnings`
88
help: use `eprintln!` instead
99
|
10-
LL - eprint!("Hello/n");
10+
LL - eprint!("Hello\n");
1111
LL + eprintln!("Hello");
1212
|
1313

1414
error: using `eprint!()` with a format string that ends in a single newline
1515
--> $DIR/eprint_with_newline.rs:6:5
1616
|
17-
LL | eprint!("Hello {}/n", "world");
17+
LL | eprint!("Hello {}\n", "world");
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1919
|
2020
help: use `eprintln!` instead
2121
|
22-
LL - eprint!("Hello {}/n", "world");
22+
LL - eprint!("Hello {}\n", "world");
2323
LL + eprintln!("Hello {}", "world");
2424
|
2525

2626
error: using `eprint!()` with a format string that ends in a single newline
2727
--> $DIR/eprint_with_newline.rs:7:5
2828
|
29-
LL | eprint!("Hello {} {}/n", "world", "#2");
29+
LL | eprint!("Hello {} {}\n", "world", "#2");
3030
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3131
|
3232
help: use `eprintln!` instead
3333
|
34-
LL - eprint!("Hello {} {}/n", "world", "#2");
34+
LL - eprint!("Hello {} {}\n", "world", "#2");
3535
LL + eprintln!("Hello {} {}", "world", "#2");
3636
|
3737

3838
error: using `eprint!()` with a format string that ends in a single newline
3939
--> $DIR/eprint_with_newline.rs:8:5
4040
|
41-
LL | eprint!("{}/n", 1265);
41+
LL | eprint!("{}\n", 1265);
4242
| ^^^^^^^^^^^^^^^^^^^^^
4343
|
4444
help: use `eprintln!` instead
4545
|
46-
LL - eprint!("{}/n", 1265);
46+
LL - eprint!("{}\n", 1265);
4747
LL + eprintln!("{}", 1265);
4848
|
4949

5050
error: using `eprint!()` with a format string that ends in a single newline
5151
--> $DIR/eprint_with_newline.rs:9:5
5252
|
53-
LL | eprint!("/n");
53+
LL | eprint!("\n");
5454
| ^^^^^^^^^^^^^
5555
|
5656
help: use `eprintln!` instead
5757
|
58-
LL - eprint!("/n");
58+
LL - eprint!("\n");
5959
LL + eprintln!();
6060
|
6161

6262
error: using `eprint!()` with a format string that ends in a single newline
6363
--> $DIR/eprint_with_newline.rs:28:5
6464
|
65-
LL | eprint!("///n"); // should fail
65+
LL | eprint!("\\\n"); // should fail
6666
| ^^^^^^^^^^^^^^^
6767
|
6868
help: use `eprintln!` instead
6969
|
70-
LL - eprint!("///n"); // should fail
71-
LL + eprintln!("//"); // should fail
70+
LL - eprint!("\\\n"); // should fail
71+
LL + eprintln!("\\"); // should fail
7272
|
7373

7474
error: using `eprint!()` with a format string that ends in a single newline
@@ -104,13 +104,13 @@ LL ~
104104
error: using `eprint!()` with a format string that ends in a single newline
105105
--> $DIR/eprint_with_newline.rs:47:5
106106
|
107-
LL | eprint!("//r/n");
107+
LL | eprint!("\\r\n");
108108
| ^^^^^^^^^^^^^^^^
109109
|
110110
help: use `eprintln!` instead
111111
|
112-
LL - eprint!("//r/n");
113-
LL + eprintln!("//r");
112+
LL - eprint!("\\r\n");
113+
LL + eprintln!("\\r");
114114
|
115115

116116
error: aborting due to 9 previous errors

tests/ui/explicit_write.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ LL | std::io::stderr().write_fmt(format_args!("test")).unwrap();
3939
error: use of `writeln!(stdout(), ...).unwrap()`
4040
--> $DIR/explicit_write.rs:31:9
4141
|
42-
LL | writeln!(std::io::stdout(), "test/ntest").unwrap();
43-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `println!("test/ntest")`
42+
LL | writeln!(std::io::stdout(), "test\ntest").unwrap();
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `println!("test\ntest")`
4444

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

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

0 commit comments

Comments
 (0)