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