Skip to content

Commit fe00717

Browse files
committed
Add cargo-clippy sysroot test
Whne SYSROOT is defined, clippy-driver will insert a --sysroot argument when calling rustc. However, when a sysroot argument is already defined, e.g. through RUSTFLAGS=--sysroot=... the `cargo clippy` call would error. This tests that the sysroot argument is only passed once and that SYSROOT is ignored in this case.
1 parent 321c530 commit fe00717

File tree

2 files changed

+36
-69
lines changed

2 files changed

+36
-69
lines changed

.github/driver.sh

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ test "$sysroot" = $desired_sysroot
1717
sysroot=$(SYSROOT=$desired_sysroot ./target/debug/clippy-driver --print sysroot)
1818
test "$sysroot" = $desired_sysroot
1919

20+
# Check that the --sysroot argument is only passed once (SYSROOT is ignored)
21+
(
22+
cd rustc_tools_util
23+
touch src/lib.rs
24+
SYSROOT=/tmp RUSTFLAGS="--sysroot=$(rustc --print sysroot)" ../target/debug/cargo-clippy clippy --verbose
25+
)
26+
2027
# Make sure this isn't set - clippy-driver should cope without it
2128
unset CARGO_MANIFEST_DIR
2229

tests/integration.rs

+29-69
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,28 @@
1-
//! To run this test, use
1+
//! This test is meant to only be run in CI. To run it locally use:
2+
//!
23
//! `env INTEGRATION=rust-lang/log cargo test --test integration --features=integration`
34
//!
45
//! You can use a different `INTEGRATION` value to test different repositories.
6+
//!
7+
//! This test will clone the specified repository and run Clippy on it. The test succeeds, if
8+
//! Clippy doesn't produce an ICE. Lint warnings are ignored by this test.
59
610
#![cfg(feature = "integration")]
711
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
812
#![warn(rust_2018_idioms, unused_lifetimes)]
913

14+
use std::env;
1015
use std::ffi::OsStr;
11-
use std::path::{Path, PathBuf};
1216
use std::process::Command;
13-
use std::{env, eprintln};
1417

1518
#[cfg(not(windows))]
1619
const CARGO_CLIPPY: &str = "cargo-clippy";
1720
#[cfg(windows)]
1821
const CARGO_CLIPPY: &str = "cargo-clippy.exe";
1922

20-
// NOTE: arguments passed to the returned command will be `clippy-driver` args, not `cargo-clippy`
21-
// args. Use `cargo_args` to pass arguments to cargo-clippy.
22-
fn clippy_command(repo_dir: &Path, cargo_args: &[&str]) -> Command {
23-
let root_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
24-
let target_dir = option_env!("CARGO_TARGET_DIR").map_or_else(|| root_dir.join("target"), PathBuf::from);
25-
let clippy_binary = target_dir.join(env!("PROFILE")).join(CARGO_CLIPPY);
26-
27-
let mut cargo_clippy = Command::new(clippy_binary);
28-
cargo_clippy
29-
.current_dir(repo_dir)
30-
.env("RUST_BACKTRACE", "full")
31-
.env("CARGO_TARGET_DIR", root_dir.join("target"))
32-
.args(["clippy", "--all-targets", "--all-features"])
33-
.args(cargo_args)
34-
.args(["--", "--cap-lints", "warn", "-Wclippy::pedantic", "-Wclippy::nursery"]);
35-
cargo_clippy
36-
}
37-
38-
/// Return a directory with a checkout of the repository in `INTEGRATION`.
39-
fn repo_dir(repo_name: &str) -> PathBuf {
23+
#[cfg_attr(feature = "integration", test)]
24+
fn integration_test() {
25+
let repo_name = env::var("INTEGRATION").expect("`INTEGRATION` var not set");
4026
let repo_url = format!("https://github.com/{repo_name}");
4127
let crate_name = repo_name
4228
.split('/')
@@ -57,19 +43,28 @@ fn repo_dir(repo_name: &str) -> PathBuf {
5743
.expect("unable to run git");
5844
assert!(st.success());
5945

60-
repo_dir
61-
}
46+
let root_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
47+
let target_dir = std::path::Path::new(&root_dir).join("target");
48+
let clippy_binary = target_dir.join(env!("PROFILE")).join(CARGO_CLIPPY);
6249

63-
#[cfg_attr(feature = "integration", test)]
64-
fn integration_test() {
65-
let repo_name = env::var("INTEGRATION").expect("`INTEGRATION` var not set");
66-
let repo_dir = repo_dir(&repo_name);
67-
let output = clippy_command(&repo_dir, &[]).output().expect("failed to run clippy");
68-
let stderr = String::from_utf8_lossy(&output.stderr);
69-
if !stderr.is_empty() {
70-
eprintln!("{stderr}");
71-
}
50+
let output = Command::new(clippy_binary)
51+
.current_dir(repo_dir)
52+
.env("RUST_BACKTRACE", "full")
53+
.env("CARGO_TARGET_DIR", target_dir)
54+
.args([
55+
"clippy",
56+
"--all-targets",
57+
"--all-features",
58+
"--",
59+
"--cap-lints",
60+
"warn",
61+
"-Wclippy::pedantic",
62+
"-Wclippy::nursery",
63+
])
64+
.output()
65+
.expect("unable to run clippy");
7266

67+
let stderr = String::from_utf8_lossy(&output.stderr);
7368
if let Some(backtrace_start) = stderr.find("error: internal compiler error") {
7469
static BACKTRACE_END_MSG: &str = "end of query stack";
7570
let backtrace_end = stderr[backtrace_start..]
@@ -104,38 +99,3 @@ fn integration_test() {
10499
None => panic!("Process terminated by signal"),
105100
}
106101
}
107-
108-
#[cfg_attr(feature = "integration", test)]
109-
fn test_sysroot() {
110-
#[track_caller]
111-
fn verify_cmd(cmd: &mut Command) {
112-
// Test that SYSROOT is ignored if `--sysroot` is passed explicitly.
113-
cmd.env("SYSROOT", "/dummy/value/does/not/exist");
114-
// We don't actually care about emitting lints, we only want to verify clippy doesn't give a hard
115-
// error.
116-
cmd.arg("-Awarnings");
117-
let output = cmd.output().expect("failed to run clippy");
118-
let stderr = String::from_utf8_lossy(&output.stderr);
119-
assert!(stderr.is_empty(), "clippy printed an error: {stderr}");
120-
assert!(output.status.success(), "clippy exited with an error");
121-
}
122-
123-
let rustc = std::env::var("RUSTC").unwrap_or("rustc".to_string());
124-
let rustc_output = Command::new(rustc)
125-
.args(["--print", "sysroot"])
126-
.output()
127-
.expect("unable to run rustc");
128-
assert!(rustc_output.status.success());
129-
let sysroot = String::from_utf8(rustc_output.stdout).unwrap();
130-
let sysroot = sysroot.trim_end();
131-
132-
// This is a fairly small repo; we want to avoid checking out anything heavy twice, so just
133-
// hard-code it.
134-
let repo_name = "rust-lang/log";
135-
let repo_dir = repo_dir(repo_name);
136-
// Pass the sysroot through RUSTFLAGS.
137-
verify_cmd(clippy_command(&repo_dir, &["--quiet"]).env("RUSTFLAGS", format!("--sysroot={sysroot}")));
138-
// NOTE: we don't test passing the arguments directly to clippy-driver (with `-- --sysroot`)
139-
// because it breaks for some reason. I (@jyn514) haven't taken time to track down the bug
140-
// because rust-lang/rust uses RUSTFLAGS and nearly no one else uses --sysroot.
141-
}

0 commit comments

Comments
 (0)