]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/versioncheck.rs
Rollup merge of #98111 - eggyal:issue-97982, r=GuillaumeGomez
[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 use std::fs;
7
8 #[test]
9 fn check_that_clippy_lints_and_clippy_utils_have_the_same_version_as_clippy() {
10     fn read_version(path: &str) -> String {
11         let contents = fs::read_to_string(path).unwrap_or_else(|e| panic!("error reading `{}`: {:?}", path, e));
12         contents
13             .lines()
14             .filter_map(|l| l.split_once('='))
15             .find_map(|(k, v)| (k.trim() == "version").then(|| v.trim()))
16             .unwrap_or_else(|| panic!("error finding version in `{}`", path))
17             .to_string()
18     }
19
20     // do not run this test inside the upstream rustc repo:
21     // https://github.com/rust-lang/rust-clippy/issues/6683
22     if option_env!("RUSTC_TEST_SUITE").is_some() {
23         return;
24     }
25
26     let clippy_version = read_version("Cargo.toml");
27     let clippy_lints_version = read_version("clippy_lints/Cargo.toml");
28     let clippy_utils_version = read_version("clippy_utils/Cargo.toml");
29
30     assert_eq!(clippy_version, clippy_lints_version);
31     assert_eq!(clippy_version, clippy_utils_version);
32 }
33
34 #[test]
35 fn check_that_clippy_has_the_same_major_version_as_rustc() {
36     // do not run this test inside the upstream rustc repo:
37     // https://github.com/rust-lang/rust-clippy/issues/6683
38     if option_env!("RUSTC_TEST_SUITE").is_some() {
39         return;
40     }
41
42     let clippy_version = rustc_tools_util::get_version_info!();
43     let clippy_major = clippy_version.major;
44     let clippy_minor = clippy_version.minor;
45     let clippy_patch = clippy_version.patch;
46
47     // get the rustc version either from the rustc installed with the toolchain file or from
48     // `RUSTC_REAL` if Clippy is build in the Rust repo with `./x.py`.
49     let rustc = std::env::var("RUSTC_REAL").unwrap_or_else(|_| "rustc".to_string());
50     let rustc_version = String::from_utf8(
51         std::process::Command::new(&rustc)
52             .arg("--version")
53             .output()
54             .expect("failed to run `rustc --version`")
55             .stdout,
56     )
57     .unwrap();
58     // extract "1 XX 0" from "rustc 1.XX.0-nightly (<commit> <date>)"
59     let vsplit: Vec<&str> = rustc_version
60         .split(' ')
61         .nth(1)
62         .unwrap()
63         .split('-')
64         .next()
65         .unwrap()
66         .split('.')
67         .collect();
68     match vsplit.as_slice() {
69         [rustc_major, rustc_minor, _rustc_patch] => {
70             // clippy 0.1.XX should correspond to rustc 1.XX.0
71             assert_eq!(clippy_major, 0); // this will probably stay the same for a long time
72             assert_eq!(
73                 clippy_minor.to_string(),
74                 *rustc_major,
75                 "clippy minor version does not equal rustc major version"
76             );
77             assert_eq!(
78                 clippy_patch.to_string(),
79                 *rustc_minor,
80                 "clippy patch version does not equal rustc minor version"
81             );
82             // do not check rustc_patch because when a stable-patch-release is made (like 1.50.2),
83             // we don't want our tests failing suddenly
84         },
85         _ => {
86             panic!("Failed to parse rustc version: {:?}", vsplit);
87         },
88     };
89 }