]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/versioncheck.rs
Rollup merge of #91641 - dtolnay:cchar-if, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / tests / versioncheck.rs
1 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
2 #![warn(rust_2018_idioms, unused_lifetimes)]
3 #![allow(clippy::single_match_else)]
4
5 use rustc_tools_util::VersionInfo;
6
7 #[test]
8 fn check_that_clippy_lints_and_clippy_utils_have_the_same_version_as_clippy() {
9     // do not run this test inside the upstream rustc repo:
10     // https://github.com/rust-lang/rust-clippy/issues/6683
11     if option_env!("RUSTC_TEST_SUITE").is_some() {
12         return;
13     }
14
15     let clippy_meta = cargo_metadata::MetadataCommand::new()
16         .no_deps()
17         .exec()
18         .expect("could not obtain cargo metadata");
19
20     for krate in &["clippy_lints", "clippy_utils"] {
21         let krate_meta = cargo_metadata::MetadataCommand::new()
22             .current_dir(std::env::current_dir().unwrap().join(krate))
23             .no_deps()
24             .exec()
25             .expect("could not obtain cargo metadata");
26         assert_eq!(krate_meta.packages[0].version, clippy_meta.packages[0].version);
27         for package in &clippy_meta.packages[0].dependencies {
28             if package.name == *krate {
29                 assert!(package.req.matches(&krate_meta.packages[0].version));
30                 break;
31             }
32         }
33     }
34 }
35
36 #[test]
37 fn check_that_clippy_has_the_same_major_version_as_rustc() {
38     // do not run this test inside the upstream rustc repo:
39     // https://github.com/rust-lang/rust-clippy/issues/6683
40     if option_env!("RUSTC_TEST_SUITE").is_some() {
41         return;
42     }
43
44     let clippy_version = rustc_tools_util::get_version_info!();
45     let clippy_major = clippy_version.major;
46     let clippy_minor = clippy_version.minor;
47     let clippy_patch = clippy_version.patch;
48
49     // get the rustc version either from the rustc installed with the toolchain file or from
50     // `RUSTC_REAL` if Clippy is build in the Rust repo with `./x.py`.
51     let rustc = std::env::var("RUSTC_REAL").unwrap_or_else(|_| "rustc".to_string());
52     let rustc_version = String::from_utf8(
53         std::process::Command::new(&rustc)
54             .arg("--version")
55             .output()
56             .expect("failed to run `rustc --version`")
57             .stdout,
58     )
59     .unwrap();
60     // extract "1 XX 0" from "rustc 1.XX.0-nightly (<commit> <date>)"
61     let vsplit: Vec<&str> = rustc_version
62         .split(' ')
63         .nth(1)
64         .unwrap()
65         .split('-')
66         .next()
67         .unwrap()
68         .split('.')
69         .collect();
70     match vsplit.as_slice() {
71         [rustc_major, rustc_minor, _rustc_patch] => {
72             // clippy 0.1.XX should correspond to rustc 1.XX.0
73             assert_eq!(clippy_major, 0); // this will probably stay the same for a long time
74             assert_eq!(
75                 clippy_minor.to_string(),
76                 *rustc_major,
77                 "clippy minor version does not equal rustc major version"
78             );
79             assert_eq!(
80                 clippy_patch.to_string(),
81                 *rustc_minor,
82                 "clippy patch version does not equal rustc minor version"
83             );
84             // do not check rustc_patch because when a stable-patch-release is made (like 1.50.2),
85             // we don't want our tests failing suddenly
86         },
87         _ => {
88             panic!("Failed to parse rustc version: {:?}", vsplit);
89         },
90     };
91 }