Skip to content

Commit 1088507

Browse files
committed
Canonicalize paths in a single location
1 parent 00919a4 commit 1088507

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

tests/compile-test.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ mod test_utils;
110110
// whether to run internal tests or not
111111
const RUN_INTERNAL_TESTS: bool = cfg!(feature = "internal");
112112

113+
fn canonicalize(path: impl AsRef<Path>) -> PathBuf {
114+
let path = path.as_ref();
115+
fs::create_dir_all(path).unwrap();
116+
fs::canonicalize(path).unwrap_or_else(|err| panic!("{} cannot be canonicalized: {err}", path.display()))
117+
}
118+
113119
fn base_config(test_dir: &str) -> (compiletest::Config, Args) {
114120
let args = Args::test();
115121
let mut config = compiletest::Config {
@@ -124,10 +130,11 @@ fn base_config(test_dir: &str) -> (compiletest::Config, Args) {
124130
OutputConflictHandling::Error("cargo uibless".into())
125131
},
126132
target: None,
127-
out_dir: PathBuf::from(std::env::var_os("CARGO_TARGET_DIR").unwrap_or("target".into()))
128-
.join("ui_test")
129-
.canonicalize()
130-
.unwrap(),
133+
out_dir: canonicalize(
134+
std::env::var_os("CARGO_TARGET_DIR")
135+
.map_or_else(|| std::env::current_dir().unwrap().join("target"), PathBuf::from),
136+
)
137+
.join("ui_test"),
131138
..compiletest::Config::rustc(Path::new("tests").join(test_dir))
132139
};
133140
let current_exe_path = env::current_exe().unwrap();
@@ -178,7 +185,7 @@ fn run_ui() {
178185
let (config, args) = base_config("ui");
179186
//config.rustfix_coverage = true;
180187
// use tests/clippy.toml
181-
let _g = VarGuard::set("CARGO_MANIFEST_DIR", fs::canonicalize("tests").unwrap());
188+
let _g = VarGuard::set("CARGO_MANIFEST_DIR", canonicalize("tests"));
182189
let _threads = VarGuard::set(
183190
"RUST_TEST_THREADS",
184191
// if RUST_TEST_THREADS is set, adhere to it, otherwise override it
@@ -238,8 +245,7 @@ fn run_ui_toml() {
238245

239246
config.stderr_filter(
240247
&regex::escape(
241-
&fs::canonicalize("tests")
242-
.unwrap()
248+
&canonicalize("tests")
243249
.parent()
244250
.unwrap()
245251
.display()
@@ -298,8 +304,7 @@ fn run_ui_cargo() {
298304

299305
config.stderr_filter(
300306
&regex::escape(
301-
&fs::canonicalize("tests")
302-
.unwrap()
307+
&canonicalize("tests")
303308
.parent()
304309
.unwrap()
305310
.display()
@@ -318,7 +323,13 @@ fn run_ui_cargo() {
318323
|path, _args| test_filter(path) && path.ends_with("Cargo.toml"),
319324
|config, path| {
320325
let mut config = config.clone();
321-
config.out_dir = PathBuf::from("target/ui_test_cargo/").join(path.parent().unwrap());
326+
config.out_dir = canonicalize(
327+
std::env::current_dir()
328+
.unwrap()
329+
.join("target")
330+
.join("ui_test_cargo/")
331+
.join(path.parent().unwrap()),
332+
);
322333
Some(config)
323334
},
324335
if quiet {

0 commit comments

Comments
 (0)