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