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