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