Skip to content

Commit

Permalink
Auto merge of #5688 - ebroto:fix_cargo_tests_in_rustc, r=flip1995
Browse files Browse the repository at this point in the history
Fix cargo tests when running inside the rustlang/rust repo

It seems we hit rust-lang/cargo#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
  • Loading branch information
bors committed Jun 5, 2020
2 parents b16d101 + c325c12 commit ea7066a
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 29 deletions.
2 changes: 2 additions & 0 deletions clippy_dev/src/new_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
13 changes: 2 additions & 11 deletions clippy_lints/src/cargo_common_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
10 changes: 2 additions & 8 deletions clippy_lints/src/multiple_crate_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand Down
18 changes: 18 additions & 0 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
7 changes: 1 addition & 6 deletions clippy_lints/src/wildcard_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions tests/compile-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 2 additions & 0 deletions tests/ui-cargo/cargo_common_metadata/fail/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
name = "cargo_common_metadata"
version = "0.1.0"
publish = false

[workspace]
2 changes: 2 additions & 0 deletions tests/ui-cargo/cargo_common_metadata/pass/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ readme = "README.md"
license = "MIT OR Apache-2.0"
keywords = ["metadata", "lint", "clippy"]
categories = ["development-tools::testing"]

[workspace]
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions tests/ui-cargo/multiple_crate_versions/fail/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 2 additions & 0 deletions tests/ui-cargo/multiple_crate_versions/pass/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name = "cargo_common_metadata"
version = "0.1.0"
publish = false

[workspace]

[dependencies]
regex = "1.3.7"
serde = "1.0.110"
2 changes: 2 additions & 0 deletions tests/ui-cargo/wildcard_dependencies/fail/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ name = "wildcard_dependencies"
version = "0.1.0"
publish = false

[workspace]

[dependencies]
regex = "*"
2 changes: 2 additions & 0 deletions tests/ui-cargo/wildcard_dependencies/pass/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ name = "wildcard_dependencies"
version = "0.1.0"
publish = false

[workspace]

[dependencies]
regex = "1"

0 comments on commit ea7066a

Please sign in to comment.