Skip to content

Commit c51472b

Browse files
committed
Add clippy.toml to project and tests
1 parent 55ccc7a commit c51472b

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
avoid-breaking-exported-api = false

tests/clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# default config for tests, overrides clippy.toml at the project root

tests/compile-test.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
use compiletest_rs as compiletest;
55
use compiletest_rs::common::Mode as TestMode;
66

7-
use std::env::{self, set_var, var};
8-
use std::ffi::OsStr;
7+
use std::env::{self, remove_var, set_var, var_os};
8+
use std::ffi::{OsStr, OsString};
99
use std::fs;
1010
use std::io;
1111
use std::path::{Path, PathBuf};
@@ -88,9 +88,11 @@ fn default_config() -> compiletest::Config {
8888
config
8989
}
9090

91-
fn run_mode(cfg: &mut compiletest::Config) {
91+
fn run_ui(cfg: &mut compiletest::Config) {
9292
cfg.mode = TestMode::Ui;
9393
cfg.src_base = Path::new("tests").join("ui");
94+
// use tests/clippy.toml
95+
let _g = VarGuard::set("CARGO_MANIFEST_DIR", std::fs::canonicalize("tests").unwrap());
9496
compiletest::run_tests(cfg);
9597
}
9698

@@ -114,7 +116,7 @@ fn run_ui_toml(config: &mut compiletest::Config) {
114116
continue;
115117
}
116118
let dir_path = dir.path();
117-
set_var("CARGO_MANIFEST_DIR", &dir_path);
119+
let _g = VarGuard::set("CARGO_MANIFEST_DIR", &dir_path);
118120
for file in fs::read_dir(&dir_path)? {
119121
let file = file?;
120122
let file_path = file.path();
@@ -145,9 +147,7 @@ fn run_ui_toml(config: &mut compiletest::Config) {
145147

146148
let tests = compiletest::make_tests(config);
147149

148-
let manifest_dir = var("CARGO_MANIFEST_DIR").unwrap_or_default();
149150
let res = run_tests(config, tests);
150-
set_var("CARGO_MANIFEST_DIR", &manifest_dir);
151151
match res {
152152
Ok(true) => {},
153153
Ok(false) => panic!("Some tests failed"),
@@ -208,7 +208,7 @@ fn run_ui_cargo(config: &mut compiletest::Config) {
208208
Some("main.rs") => {},
209209
_ => continue,
210210
}
211-
set_var("CLIPPY_CONF_DIR", case.path());
211+
let _g = VarGuard::set("CLIPPY_CONF_DIR", case.path());
212212
let paths = compiletest::common::TestPaths {
213213
file: file_path,
214214
base: config.src_base.clone(),
@@ -236,10 +236,8 @@ fn run_ui_cargo(config: &mut compiletest::Config) {
236236
let tests = compiletest::make_tests(config);
237237

238238
let current_dir = env::current_dir().unwrap();
239-
let conf_dir = var("CLIPPY_CONF_DIR").unwrap_or_default();
240239
let res = run_tests(config, &config.filters, tests);
241240
env::set_current_dir(current_dir).unwrap();
242-
set_var("CLIPPY_CONF_DIR", conf_dir);
243241

244242
match res {
245243
Ok(true) => {},
@@ -260,8 +258,32 @@ fn prepare_env() {
260258
fn compile_test() {
261259
prepare_env();
262260
let mut config = default_config();
263-
run_mode(&mut config);
261+
run_ui(&mut config);
264262
run_ui_toml(&mut config);
265263
run_ui_cargo(&mut config);
266264
run_internal_tests(&mut config);
267265
}
266+
267+
/// Restores an env var on drop
268+
#[must_use]
269+
struct VarGuard {
270+
key: &'static str,
271+
value: Option<OsString>,
272+
}
273+
274+
impl VarGuard {
275+
fn set(key: &'static str, val: impl AsRef<OsStr>) -> Self {
276+
let value = var_os(key);
277+
set_var(key, val);
278+
Self { key, value }
279+
}
280+
}
281+
282+
impl Drop for VarGuard {
283+
fn drop(&mut self) {
284+
match self.value.as_deref() {
285+
None => remove_var(self.key),
286+
Some(value) => set_var(self.key, value),
287+
}
288+
}
289+
}

0 commit comments

Comments
 (0)