Skip to content

Commit 2d3298b

Browse files
authored
Merge pull request #3148 from matthiaskrgr/rustc_tools_util_tests
rustc_tools_util: add tests
2 parents 055b5eb + a141550 commit 2d3298b

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

ci/base-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ remark -f *.md > /dev/null
88
cargo build --features debugging
99
cargo test --features debugging
1010
cd clippy_lints && cargo test && cd ..
11+
cd rustc_tools_util && cargo test && cd ..
1112
mkdir -p ~/rust/cargo/bin
1213
cp target/debug/cargo-clippy ~/rust/cargo/bin/cargo-clippy
1314
cp target/debug/clippy-driver ~/rust/cargo/bin/clippy-driver

rustc_tools_util/src/lib.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(test)]
12
#![feature(tool_lints)]
23

34
use std::env;
@@ -8,6 +9,7 @@ macro_rules! get_version_info {
89
let major = env!("CARGO_PKG_VERSION_MAJOR").parse::<u8>().unwrap();
910
let minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap();
1011
let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap();
12+
let crate_name = String::from(env!("CARGO_PKG_NAME"));
1113

1214
let host_compiler = $crate::get_channel();
1315
let commit_hash = option_env!("GIT_HASH").map(|s| s.to_string());
@@ -20,6 +22,7 @@ macro_rules! get_version_info {
2022
host_compiler,
2123
commit_hash,
2224
commit_date,
25+
crate_name,
2326
}
2427
}};
2528
}
@@ -32,6 +35,7 @@ pub struct VersionInfo {
3235
pub host_compiler: Option<String>,
3336
pub commit_hash: Option<String>,
3437
pub commit_date: Option<String>,
38+
pub crate_name: String,
3539
}
3640

3741
impl std::fmt::Display for VersionInfo {
@@ -40,7 +44,8 @@ impl std::fmt::Display for VersionInfo {
4044
Some(_) => {
4145
write!(
4246
f,
43-
"clippy {}.{}.{} ({} {})",
47+
"{} {}.{}.{} ({} {})",
48+
self.crate_name,
4449
self.major,
4550
self.minor,
4651
self.patch,
@@ -49,7 +54,7 @@ impl std::fmt::Display for VersionInfo {
4954
)?;
5055
},
5156
None => {
52-
write!(f, "clippy {}.{}.{}", self.major, self.minor, self.patch)?;
57+
write!(f, "{} {}.{}.{}", self.crate_name, self.major, self.minor, self.patch)?;
5358
},
5459
};
5560
Ok(())
@@ -80,3 +85,28 @@ pub fn get_commit_date() -> Option<String> {
8085
.ok()
8186
.and_then(|r| String::from_utf8(r.stdout).ok())
8287
}
88+
89+
#[cfg(test)]
90+
mod test {
91+
use super::*;
92+
93+
#[test]
94+
fn test_struct_local() {
95+
let vi = get_version_info!();
96+
assert_eq!(vi.major, 0);
97+
assert_eq!(vi.minor, 1);
98+
assert_eq!(vi.patch, 0);
99+
assert_eq!(vi.crate_name, "rustc_tools_util");
100+
// hard to make positive tests for these since they will always change
101+
assert!(vi.commit_hash.is_none());
102+
assert!(vi.commit_date.is_none());
103+
}
104+
105+
#[test]
106+
fn test_display_local() {
107+
let vi = get_version_info!();
108+
let fmt = format!("{}", vi);
109+
assert_eq!(fmt, "rustc_tools_util 0.1.0");
110+
}
111+
112+
}

0 commit comments

Comments
 (0)