Auto merge of #5688 - ebroto:fix_cargo_tests_in_rustc, r=flip1995

Fix cargo tests when running inside the rustlang/rust repo

It seems we hit https://github.com/rust-lang/cargo/issues/5418, so I've applied the suggested solution. Also added some more info when cargo-metadata fails to execute.

(there was no open issue for this)

changelog: none
This commit is contained in:
bors 2020-06-05 21:59:37 +00:00
commit ea7066a01d
13 changed files with 39 additions and 29 deletions

View file

@ -147,6 +147,8 @@ fn get_manifest_contents(lint_name: &str, hint: &str) -> String {
name = "{}"
version = "0.1.0"
publish = false
[workspace]
"#,
hint, lint_name
)

View file

@ -36,13 +36,9 @@ declare_clippy_lint! {
"common metadata is defined in `Cargo.toml`"
}
fn warning(cx: &LateContext<'_, '_>, message: &str) {
span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, message);
}
fn missing_warning(cx: &LateContext<'_, '_>, package: &cargo_metadata::Package, field: &str) {
let message = format!("package `{}` is missing `{}` metadata", package.name, field);
warning(cx, &message);
span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, &message);
}
fn is_empty_str(value: &Option<String>) -> bool {
@ -66,12 +62,7 @@ impl LateLintPass<'_, '_> for CargoCommonMetadata {
return;
}
let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
metadata
} else {
warning(cx, "could not read cargo metadata");
return;
};
let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);
for package in metadata.packages {
if is_empty_vec(&package.authors) {

View file

@ -7,7 +7,7 @@ use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::DUMMY_SP;
use cargo_metadata::{DependencyKind, MetadataCommand, Node, Package, PackageId};
use cargo_metadata::{DependencyKind, Node, Package, PackageId};
use if_chain::if_chain;
use itertools::Itertools;
@ -42,13 +42,7 @@ impl LateLintPass<'_, '_> for MultipleCrateVersions {
return;
}
let metadata = if let Ok(metadata) = MetadataCommand::new().exec() {
metadata
} else {
span_lint(cx, MULTIPLE_CRATE_VERSIONS, DUMMY_SP, "could not read cargo metadata");
return;
};
let metadata = unwrap_cargo_metadata!(cx, MULTIPLE_CRATE_VERSIONS, true);
let local_name = cx.tcx.crate_name(LOCAL_CRATE).as_str();
let mut packages = metadata.packages;
packages.sort_by(|a, b| a.name.cmp(&b.name));

View file

@ -1405,6 +1405,24 @@ pub fn run_lints(cx: &LateContext<'_, '_>, lints: &[&'static Lint], id: HirId) -
})
}
#[macro_export]
macro_rules! unwrap_cargo_metadata {
($cx: ident, $lint: ident, $deps: expr) => {{
let mut command = cargo_metadata::MetadataCommand::new();
if !$deps {
command.no_deps();
}
match command.exec() {
Ok(metadata) => metadata,
Err(err) => {
span_lint($cx, $lint, DUMMY_SP, &format!("could not read cargo metadata: {}", err));
return;
},
}
}};
}
#[cfg(test)]
mod test {
use super::{trim_multiline, without_block_comments};

View file

@ -34,12 +34,7 @@ impl LateLintPass<'_, '_> for WildcardDependencies {
return;
}
let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
metadata
} else {
span_lint(cx, WILDCARD_DEPENDENCIES, DUMMY_SP, "could not read cargo metadata");
return;
};
let metadata = unwrap_cargo_metadata!(cx, WILDCARD_DEPENDENCIES, false);
for dep in &metadata.packages[0].dependencies {
// VersionReq::any() does not work

View file

@ -220,10 +220,6 @@ fn run_ui_cargo(config: &mut compiletest::Config) {
Ok(result)
}
if cargo::is_rustc_test_suite() {
return;
}
config.mode = TestMode::Ui;
config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap();

View file

@ -2,3 +2,5 @@
name = "cargo_common_metadata"
version = "0.1.0"
publish = false
[workspace]

View file

@ -9,3 +9,5 @@ readme = "README.md"
license = "MIT OR Apache-2.0"
keywords = ["metadata", "lint", "clippy"]
categories = ["development-tools::testing"]
[workspace]

View file

@ -5,6 +5,8 @@ name = "multiple_crate_versions"
version = "0.1.0"
publish = false
[workspace]
# One of the versions of winapi is only a dev dependency: allowed
[dependencies]
ctrlc = "=3.1.0"

View file

@ -3,6 +3,8 @@ name = "multiple_crate_versions"
version = "0.1.0"
publish = false
[workspace]
[dependencies]
ctrlc = "=3.1.0"
ansi_term = "=0.11.0"

View file

@ -3,6 +3,8 @@ name = "cargo_common_metadata"
version = "0.1.0"
publish = false
[workspace]
[dependencies]
regex = "1.3.7"
serde = "1.0.110"

View file

@ -3,5 +3,7 @@ name = "wildcard_dependencies"
version = "0.1.0"
publish = false
[workspace]
[dependencies]
regex = "*"

View file

@ -3,5 +3,7 @@ name = "wildcard_dependencies"
version = "0.1.0"
publish = false
[workspace]
[dependencies]
regex = "1"