Apply changes that were required for running in the rustc test suite

This commit is contained in:
Oliver Schneider 2017-11-14 14:56:00 +01:00
parent 299f1270a6
commit 127c41f700
No known key found for this signature in database
GPG Key ID: A69F8D225B3AD7D9
24 changed files with 102 additions and 141 deletions

View File

@ -40,12 +40,12 @@ path = "src/driver.rs"
clippy_lints = { version = "0.0.170", path = "clippy_lints" }
# end automatic update
cargo_metadata = "0.2"
regex = "0.2"
[dev-dependencies]
compiletest_rs = "0.3"
duct = "0.8.2"
lazy_static = "0.2"
regex = "0.2"
serde_derive = "1.0"
clippy-mini-macro-test = { version = "0.1", path = "mini-macro" }
serde = "1.0"

View File

@ -183,17 +183,28 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
match utils::conf::lookup_conf_file() {
Ok(path) => path,
Err(error) => {
reg.sess.struct_err(&format!("error reading Clippy's configuration file: {}", error)).emit();
reg.sess.struct_err(&format!("error finding Clippy's configuration file: {}", error)).emit();
None
}
}
};
let file_name = file_name.map(|file_name| if file_name.is_relative() {
reg.sess
.local_crate_source_file
.as_ref()
.and_then(|file| std::path::Path::new(&file).parent().map(std::path::Path::to_path_buf))
.unwrap_or_default()
.join(file_name)
} else {
file_name
});
let (conf, errors) = utils::conf::read(file_name.as_ref().map(|p| p.as_ref()));
// all conf errors are non-fatal, we just use the default conf in case of error
for error in errors {
reg.sess.struct_err(&format!("error reading Clippy's configuration file: {}", error)).emit();
reg.sess.struct_err(&format!("error reading Clippy's configuration file `{}`: {}", file_name.as_ref().and_then(|p| p.to_str()).unwrap_or(""), error)).emit();
}
conf

View File

@ -129,33 +129,29 @@ fn show_version() {
pub fn main() {
use std::env;
if env::var("CLIPPY_DOGFOOD").is_ok() {
panic!("yummy");
}
if std::env::args().any(|a| a == "--version" || a == "-V") {
show_version();
return;
}
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
let sys_root = if let (Some(home), Some(toolchain)) = (home, toolchain) {
format!("{}/toolchains/{}", home, toolchain)
} else {
option_env!("SYSROOT")
.map(|s| s.to_owned())
.or_else(|| {
Command::new("rustc")
.arg("--print")
.arg("sysroot")
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.map(|s| s.trim().to_owned())
})
.expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust")
};
let sys_root = option_env!("SYSROOT")
.map(String::from)
.or_else(|| std::env::var("SYSROOT").ok())
.or_else(|| {
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
})
.or_else(|| {
Command::new("rustc")
.arg("--print")
.arg("sysroot")
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.map(|s| s.trim().to_owned())
})
.expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
rustc_driver::in_rustc_thread(|| {
// Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.

View File

@ -49,12 +49,6 @@ fn show_version() {
}
pub fn main() {
use std::env;
if env::var("CLIPPY_DOGFOOD").is_ok() {
panic!("yummy");
}
// Check for version and help flags even when invoked as 'cargo-clippy'
if std::env::args().any(|a| a == "--help" || a == "-h") {
show_help();

View File

@ -1,6 +1,9 @@
extern crate compiletest_rs as compiletest;
#![feature(test)]
use std::path::PathBuf;
extern crate compiletest_rs as compiletest;
extern crate test;
use std::path::{PathBuf, Path};
use std::env::{set_var, var};
fn clippy_driver_path() -> PathBuf {
@ -11,16 +14,37 @@ fn clippy_driver_path() -> PathBuf {
}
}
fn run_mode(dir: &'static str, mode: &'static str) {
fn host_libs() -> PathBuf {
if let Some(path) = option_env!("HOST_LIBS") {
PathBuf::from(path)
} else {
Path::new("target").join(env!("PROFILE"))
}
}
fn rustc_test_suite() -> Option<PathBuf> {
option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
}
fn rustc_lib_path() -> PathBuf {
option_env!("RUSTC_LIB_PATH").unwrap().into()
}
fn config(dir: &'static str, mode: &'static str) -> compiletest::Config {
let mut config = compiletest::Config::default();
let cfg_mode = mode.parse().expect("Invalid mode");
config.target_rustcflags = Some("-L target/debug/ -L target/debug/deps -Dwarnings".to_owned());
if let Ok(name) = var::<&str>("TESTNAME") {
let s: String = name.to_owned();
config.filter = Some(s)
}
if rustc_test_suite().is_some() {
config.run_lib_path = rustc_lib_path();
config.compile_lib_path = rustc_lib_path();
}
config.target_rustcflags = Some(format!("-L {0} -L {0}/deps -Dwarnings", host_libs().display()));
config.mode = cfg_mode;
config.build_base = {
let mut path = std::env::current_dir().unwrap();
@ -29,8 +53,11 @@ fn run_mode(dir: &'static str, mode: &'static str) {
};
config.src_base = PathBuf::from(format!("tests/{}", dir));
config.rustc_path = clippy_driver_path();
config
}
compiletest::run_tests(&config);
fn run_mode(dir: &'static str, mode: &'static str) {
compiletest::run_tests(&config(dir, mode));
}
fn prepare_env() {
@ -45,3 +72,21 @@ fn compile_test() {
run_mode("run-pass", "run-pass");
run_mode("ui", "ui");
}
#[test]
fn dogfood() {
prepare_env();
let files = ["src/main.rs", "src/driver.rs", "src/lib.rs", "clippy_lints/src/lib.rs"];
let mut config = config("dogfood", "ui");
config.target_rustcflags = config.target_rustcflags.map(|flags| format!("{} -Dclippy -Dclippy_pedantic -Dclippy_internal", flags));
for file in &files {
let paths = test::TestPaths {
base: PathBuf::new(),
file: PathBuf::from(file),
relative_dir: PathBuf::new(),
};
compiletest::runtest::run(config.clone(), &paths);
}
}

View File

@ -1,2 +1,2 @@
#![feature(plugin)]
#![plugin(clippy(conf_file = "./tests/auxiliary/conf_whitelisted.toml"))]
#![plugin(clippy(conf_file = "./auxiliary/conf_whitelisted.toml"))]

View File

@ -1,49 +0,0 @@
#![feature(test, plugin)]
#![plugin(clippy)]
#![deny(clippy, clippy_pedantic)]
extern crate compiletest_rs as compiletest;
extern crate test;
use std::env::{set_var, var};
use std::path::PathBuf;
use test::TestPaths;
#[test]
fn dogfood() {
// don't run dogfood on travis, cargo-clippy already runs clippy on itself
if let Ok(travis) = var("TRAVIS") {
if travis == "true" {
return;
}
}
let mut config = compiletest::Config::default();
let cfg_mode = "run-fail".parse().expect("Invalid mode");
let mut s = String::new();
s.push_str(" -L target/debug/");
s.push_str(" -L target/debug/deps");
s.push_str(" -Zextra-plugins=clippy -Ltarget_recur/debug -Dwarnings -Dclippy_pedantic -Dclippy -Dclippy_internal");
config.target_rustcflags = Some(s);
if let Ok(name) = var("TESTNAME") {
config.filter = Some(name.to_owned())
}
config.mode = cfg_mode;
config.verbose = true;
let files = ["src/main.rs", "src/lib.rs", "clippy_lints/src/lib.rs"];
for file in &files {
let paths = TestPaths {
base: PathBuf::new(),
file: PathBuf::from(file),
relative_dir: PathBuf::new(),
};
set_var("CLIPPY_DOGFOOD", "tastes like chicken");
compiletest::runtest::run(config.clone(), &paths);
}
}

View File

@ -1,6 +1,6 @@
// error-pattern: error reading Clippy's configuration file
#![plugin(clippy(conf_file="./tests/ui/conf_bad_toml.toml"))]
#![plugin(clippy(conf_file="../ui/conf_bad_toml.toml"))]
fn main() {}

View File

@ -1,8 +1,8 @@
error: compiler plugins are experimental and possibly buggy (see issue #29597)
--> $DIR/conf_bad_toml.rs:4:1
|
4 | #![plugin(clippy(conf_file="./$DIR/conf_bad_toml.toml"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 | #![plugin(clippy(conf_file="../ui/conf_bad_toml.toml"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(plugin)] to the crate attributes to enable

View File

@ -1,6 +1,6 @@
// error-pattern: error reading Clippy's configuration file: `blacklisted-names` is expected to be a `Vec < String >` but is a `integer`
#![plugin(clippy(conf_file="./tests/ui/conf_bad_type.toml"))]
#![plugin(clippy(conf_file="../ui/conf_bad_type.toml"))]
fn main() {}

View File

@ -1,8 +1,8 @@
error: compiler plugins are experimental and possibly buggy (see issue #29597)
--> $DIR/conf_bad_type.rs:4:1
|
4 | #![plugin(clippy(conf_file="./$DIR/conf_bad_type.toml"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 | #![plugin(clippy(conf_file="../ui/conf_bad_type.toml"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(plugin)] to the crate attributes to enable

View File

@ -1,5 +1,5 @@
#![plugin(clippy(conf_file="./tests/auxiliary/conf_french_blacklisted_name.toml"))]
#![plugin(clippy(conf_file="../auxiliary/conf_french_blacklisted_name.toml"))]
#![allow(dead_code)]
#![allow(single_match)]

View File

@ -1,8 +1,8 @@
error: compiler plugins are experimental and possibly buggy (see issue #29597)
--> $DIR/conf_french_blacklisted_name.rs:2:1
|
2 | #![plugin(clippy(conf_file="./tests/auxiliary/conf_french_blacklisted_name.toml"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 | #![plugin(clippy(conf_file="../auxiliary/conf_french_blacklisted_name.toml"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(plugin)] to the crate attributes to enable

View File

@ -1,6 +1,6 @@
// error-pattern: error reading Clippy's configuration file: unknown key `foobar`
#![plugin(clippy(conf_file="./tests/auxiliary/conf_unknown_key.toml"))]
#![plugin(clippy(conf_file="../auxiliary/conf_unknown_key.toml"))]
fn main() {}

View File

@ -1,8 +1,8 @@
error: compiler plugins are experimental and possibly buggy (see issue #29597)
--> $DIR/conf_unknown_key.rs:4:1
|
4 | #![plugin(clippy(conf_file="./tests/auxiliary/conf_unknown_key.toml"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 | #![plugin(clippy(conf_file="../auxiliary/conf_unknown_key.toml"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(plugin)] to the crate attributes to enable

View File

@ -6,15 +6,3 @@ error: useless use of `format!`
|
= note: `-D useless-format` implied by `-D warnings`
error: useless use of `format!`
--> $DIR/format.rs:8:5
|
8 | format!("{}", "foo");
| ^^^^^^^^^^^^^^^^^^^^^
error: useless use of `format!`
--> $DIR/format.rs:15:5
|
15 | format!("{}", arg);
| ^^^^^^^^^^^^^^^^^^^

View File

@ -1,5 +1,5 @@
#![allow(unused)]
//#![feature(plugin)]#![plugin(clippy)]
use std::collections::{HashMap, HashSet};
use std::cmp::Eq;
use std::hash::{Hash, BuildHasher};

View File

@ -1,5 +1,5 @@
#![feature(plugin)]
#![plugin(clippy)]
#[allow(no_effect, unnecessary_operation)]
#[warn(int_plus_one)]

View File

@ -1,5 +1,3 @@
warning: running cargo clippy on a crate that also imports the clippy plugin
error: Unnecessary `>= y + 1` or `x - 1 >=`
--> $DIR/int_plus_one.rs:10:5
|

View File

@ -1,5 +1,5 @@
#![feature(plugin)]
#![plugin(clippy)]
#![allow(unused)]
#![feature(core_intrinsics)]

View File

@ -1,5 +1,3 @@
warning: running cargo clippy on a crate that also imports the clippy plugin
error: reference to zeroed memory
--> $DIR/invalid_ref.rs:27:24
|

View File

@ -1,5 +1,5 @@
#![feature(plugin)]
#![plugin(clippy)]
#![allow(unused)]

View File

@ -1,5 +1,3 @@
warning: running cargo clippy on a crate that also imports the clippy plugin
error: attempt to mutate range bound within loop; note that the range of the loop is unchanged
--> $DIR/mut_range_bound.rs:18:21
|

View File

@ -6,21 +6,3 @@ error: using `print!()` with a format string that ends in a newline, consider us
|
= note: `-D print-with-newline` implied by `-D warnings`
error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead
--> $DIR/print_with_newline.rs:7:5
|
7 | print!("Hello {}/n", "world");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead
--> $DIR/print_with_newline.rs:8:5
|
8 | print!("Hello {} {}/n/n", "world", "#2");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead
--> $DIR/print_with_newline.rs:9:5
|
9 | print!("{}/n", 1265);
| ^^^^^^^^^^^^^^^^^^^^^