Skip to content

rustc_tools_util: add tests #3148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/base-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ remark -f *.md > /dev/null
cargo build --features debugging
cargo test --features debugging
cd clippy_lints && cargo test && cd ..
cd rustc_tools_util && cargo test && cd ..
mkdir -p ~/rust/cargo/bin
cp target/debug/cargo-clippy ~/rust/cargo/bin/cargo-clippy
cp target/debug/clippy-driver ~/rust/cargo/bin/clippy-driver
Expand Down
34 changes: 32 additions & 2 deletions rustc_tools_util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(test)]
#![feature(tool_lints)]

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

let host_compiler = $crate::get_channel();
let commit_hash = option_env!("GIT_HASH").map(|s| s.to_string());
Expand All @@ -20,6 +22,7 @@ macro_rules! get_version_info {
host_compiler,
commit_hash,
commit_date,
crate_name,
}
}};
}
Expand All @@ -32,6 +35,7 @@ pub struct VersionInfo {
pub host_compiler: Option<String>,
pub commit_hash: Option<String>,
pub commit_date: Option<String>,
pub crate_name: String,
}

impl std::fmt::Display for VersionInfo {
Expand All @@ -40,7 +44,8 @@ impl std::fmt::Display for VersionInfo {
Some(_) => {
write!(
f,
"clippy {}.{}.{} ({} {})",
"{} {}.{}.{} ({} {})",
self.crate_name,
self.major,
self.minor,
self.patch,
Expand All @@ -49,7 +54,7 @@ impl std::fmt::Display for VersionInfo {
)?;
},
None => {
write!(f, "clippy {}.{}.{}", self.major, self.minor, self.patch)?;
write!(f, "{} {}.{}.{}", self.crate_name, self.major, self.minor, self.patch)?;
},
};
Ok(())
Expand Down Expand Up @@ -80,3 +85,28 @@ pub fn get_commit_date() -> Option<String> {
.ok()
.and_then(|r| String::from_utf8(r.stdout).ok())
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_struct_local() {
let vi = get_version_info!();
assert_eq!(vi.major, 0);
assert_eq!(vi.minor, 1);
assert_eq!(vi.patch, 0);
assert_eq!(vi.crate_name, "rustc_tools_util");
// hard to make positive tests for these since they will always change
assert!(vi.commit_hash.is_none());
assert!(vi.commit_date.is_none());
}

#[test]
fn test_display_local() {
let vi = get_version_info!();
let fmt = format!("{}", vi);
assert_eq!(fmt, "rustc_tools_util 0.1.0");
}

}