Skip to content

Commit 80e81d3

Browse files
committed
add version check to the unit tests
1 parent 3ad0a49 commit 80e81d3

File tree

6 files changed

+54
-9
lines changed

6 files changed

+54
-9
lines changed

clippy_lints/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ semver = "0.2.1"
2121
toml = "0.1"
2222
unicode-normalization = "0.1"
2323
quine-mc_cluskey = "0.2.2"
24+
rustc-serialize = "0.3"
2425

2526
[features]
2627
debugging = []

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ extern crate regex_syntax;
3636
// for finding minimal boolean expressions
3737
extern crate quine_mc_cluskey;
3838

39+
extern crate rustc_serialize;
40+
3941
extern crate rustc_plugin;
4042
extern crate rustc_const_eval;
4143
extern crate rustc_const_math;

src/cargo.rs renamed to clippy_lints/src/utils/cargo.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
use std::collections::HashMap;
2+
use std::process::Command;
3+
use std::str::{from_utf8, Utf8Error};
4+
use std::io;
5+
use rustc_serialize::json;
26

37
#[derive(RustcDecodable, Debug)]
48
pub struct Metadata {
@@ -9,21 +13,21 @@ pub struct Metadata {
913

1014
#[derive(RustcDecodable, Debug)]
1115
pub struct Package {
12-
name: String,
13-
version: String,
16+
pub name: String,
17+
pub version: String,
1418
id: String,
1519
source: Option<()>,
16-
dependencies: Vec<Dependency>,
20+
pub dependencies: Vec<Dependency>,
1721
pub targets: Vec<Target>,
1822
features: HashMap<String, Vec<String>>,
1923
manifest_path: String,
2024
}
2125

2226
#[derive(RustcDecodable, Debug)]
2327
pub struct Dependency {
24-
name: String,
28+
pub name: String,
2529
source: Option<String>,
26-
req: String,
30+
pub req: String,
2731
kind: Option<String>,
2832
optional: bool,
2933
uses_default_features: bool,
@@ -46,3 +50,26 @@ pub struct Target {
4650
pub kind: Vec<Kind>,
4751
src_path: String,
4852
}
53+
54+
#[derive(Debug)]
55+
pub enum Error {
56+
Io(io::Error),
57+
Utf8(Utf8Error),
58+
Json(json::DecoderError),
59+
}
60+
61+
impl From<io::Error> for Error {
62+
fn from(err: io::Error) -> Self { Error::Io(err) }
63+
}
64+
impl From<Utf8Error> for Error {
65+
fn from(err: Utf8Error) -> Self { Error::Utf8(err) }
66+
}
67+
impl From<json::DecoderError> for Error {
68+
fn from(err: json::DecoderError) -> Self { Error::Json(err) }
69+
}
70+
71+
pub fn metadata() -> Result<Metadata, Error> {
72+
let output = Command::new("cargo").args(&["metadata", "--no-deps"]).output()?;
73+
let stdout = from_utf8(&output.stdout)?;
74+
Ok(json::decode(stdout)?)
75+
}

clippy_lints/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod conf;
2323
mod hir;
2424
pub mod paths;
2525
pub use self::hir::{SpanlessEq, SpanlessHash};
26+
pub mod cargo;
2627

2728
pub type MethodArgs = HirVec<P<Expr>>;
2829

src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use syntax::diagnostics;
1818
use std::path::PathBuf;
1919
use std::process::Command;
2020

21-
mod cargo;
21+
use clippy_lints::utils::cargo;
2222

2323
struct ClippyCompilerCalls(RustcDefaultCalls);
2424

@@ -122,9 +122,7 @@ pub fn main() {
122122
};
123123

124124
if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
125-
let output = std::process::Command::new("cargo").args(&["metadata", "--no-deps"]).output().expect("could not run `cargo metadata`");
126-
let stdout = std::str::from_utf8(&output.stdout).expect("`cargo metadata` output is not utf8");
127-
let mut metadata: cargo::Metadata = rustc_serialize::json::decode(stdout).expect("`cargo metadata` output is not valid json");
125+
let mut metadata = cargo::metadata().expect("could not obtain cargo metadata");
128126
assert_eq!(metadata.version, 1);
129127
for target in metadata.packages.remove(0).targets {
130128
let args = std::env::args().skip(2);

tests/versioncheck.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
extern crate clippy_lints;
2+
use clippy_lints::utils::cargo;
3+
4+
#[test]
5+
fn check_that_clippy_lints_has_the_same_version_as_clippy() {
6+
let clippy_meta = cargo::metadata().expect("could not obtain cargo metadata");
7+
std::env::set_current_dir(std::env::current_dir().unwrap().join("clippy_lints")).unwrap();
8+
let clippy_lints_meta = cargo::metadata().expect("could not obtain cargo metadata");
9+
assert_eq!(clippy_lints_meta.packages[0].version, clippy_meta.packages[0].version);
10+
for package in &clippy_meta.packages[0].dependencies {
11+
if package.name == "clippy_lints" {
12+
assert_eq!(clippy_lints_meta.packages[0].version, package.req[1..]);
13+
return;
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)