|
| 1 | +use std::path::Path; |
| 2 | + |
1 | 3 | use anyhow::Context; |
2 | 4 | use camino::{Utf8Path, Utf8PathBuf}; |
3 | 5 |
|
@@ -86,36 +88,57 @@ llvm-config = "{llvm_config}" |
86 | 88 | log::info!("Using following `config.toml` for running tests:\n{config_content}"); |
87 | 89 |
|
88 | 90 | // 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)?; |
112 | 139 | } |
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 |
119 | 142 | } |
120 | 143 |
|
121 | 144 | /// Tries to find the version of the dist artifacts (either nightly, beta, or 1.XY.Z). |
|
0 commit comments