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