Skip to content

Commit 545c5d3

Browse files
committed
Do not overwrite original config.toml in opt-dist
So that follow-up CI commands can proceed normally. It will also avoid overwriting `config.toml` when running opt-dist tests locally.
1 parent a1e034a commit 545c5d3

File tree

1 file changed

+52
-29
lines changed

1 file changed

+52
-29
lines changed

src/tools/opt-dist/src/tests.rs

+52-29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::Path;
2+
13
use anyhow::Context;
24
use camino::{Utf8Path, Utf8PathBuf};
35

@@ -86,36 +88,57 @@ llvm-config = "{llvm_config}"
8688
log::info!("Using following `config.toml` for running tests:\n{config_content}");
8789

8890
// Simulate a stage 0 compiler with the extracted optimized dist artifacts.
89-
std::fs::write("config.toml", config_content)?;
90-
91-
let x_py = env.checkout_path().join("x.py");
92-
let mut args = vec![
93-
env.python_binary(),
94-
x_py.as_str(),
95-
"test",
96-
"--build",
97-
env.host_tuple(),
98-
"--stage",
99-
"0",
100-
"tests/assembly",
101-
"tests/codegen",
102-
"tests/codegen-units",
103-
"tests/incremental",
104-
"tests/mir-opt",
105-
"tests/pretty",
106-
"tests/run-make/glibc-symbols-x86_64-unknown-linux-gnu",
107-
"tests/ui",
108-
"tests/crashes",
109-
];
110-
for test_path in env.skipped_tests() {
111-
args.extend(["--skip", test_path]);
91+
with_backed_up_file(Path::new("config.toml"), &config_content, || {
92+
let x_py = env.checkout_path().join("x.py");
93+
let mut args = vec![
94+
env.python_binary(),
95+
x_py.as_str(),
96+
"test",
97+
"--build",
98+
env.host_tuple(),
99+
"--stage",
100+
"0",
101+
"tests/assembly",
102+
"tests/codegen",
103+
"tests/codegen-units",
104+
"tests/incremental",
105+
"tests/mir-opt",
106+
"tests/pretty",
107+
"tests/run-make/glibc-symbols-x86_64-unknown-linux-gnu",
108+
"tests/ui",
109+
"tests/crashes",
110+
];
111+
for test_path in env.skipped_tests() {
112+
args.extend(["--skip", test_path]);
113+
}
114+
cmd(&args)
115+
.env("COMPILETEST_FORCE_STAGE0", "1")
116+
// Also run dist-only tests
117+
.env("COMPILETEST_ENABLE_DIST_TESTS", "1")
118+
.run()
119+
.context("Cannot execute tests")
120+
})
121+
}
122+
123+
/// Backup `path` (if it exists), then write `contents` into it, and then restore the original
124+
/// contents of the file.
125+
fn with_backed_up_file<F>(path: &Path, contents: &str, func: F) -> anyhow::Result<()>
126+
where
127+
F: FnOnce() -> anyhow::Result<()>,
128+
{
129+
let original_contents =
130+
if path.is_file() { Some(std::fs::read_to_string(path)?) } else { None };
131+
132+
// Overwrite it with new contents
133+
std::fs::write(path, contents)?;
134+
135+
let ret = func();
136+
137+
if let Some(original_contents) = original_contents {
138+
std::fs::write(path, original_contents)?;
112139
}
113-
cmd(&args)
114-
.env("COMPILETEST_FORCE_STAGE0", "1")
115-
// Also run dist-only tests
116-
.env("COMPILETEST_ENABLE_DIST_TESTS", "1")
117-
.run()
118-
.context("Cannot execute tests")
140+
141+
ret
119142
}
120143

121144
/// Tries to find the version of the dist artifacts (either nightly, beta, or 1.XY.Z).

0 commit comments

Comments
 (0)