Merge pull request #21 from dbrgn/testing

Testing
This commit is contained in:
Danilo Bargen 2016-04-04 00:08:02 +02:00
commit d5f6814298
6 changed files with 119 additions and 22 deletions

7
Cargo.lock generated
View file

@ -11,6 +11,7 @@ dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
"tar 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -20,7 +21,7 @@ name = "aho-corasick"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -154,7 +155,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "0.1.10"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
@ -218,7 +219,7 @@ version = "0.1.62"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"mempool 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -24,3 +24,6 @@ time = "^0.1"
walkdir = "^0.1"
env_logger = { version = "^0.3", optional = true }
clippy = {version = "0.0.37", optional = true}
[dev-dependencies]
tempdir = "^0.3"

View file

@ -32,7 +32,24 @@ impl Cache {
/// Return the path to the cache directory.
fn get_cache_dir(&self) -> Result<PathBuf, TldrError> {
let home_dir = try!(env::home_dir().ok_or(CacheError("Could not determine home directory".into())));
// Allow overriding the cache directory by setting the
// $TLDR_RS_CACHE_DIR env variable.
if let Ok(value) = env::var("TLDR_RS_CACHE_DIR") {
let path = PathBuf::from(value);
if path.exists() && path.is_dir() {
return Ok(path)
} else {
return Err(CacheError(
"Path specified by $TLDR_RS_CACHE_DIR \
does not exist or is not a directory.".into()
));
}
};
// Otherwise, fall back to ~/.cache/tldr-rs
let home_dir = try!(env::home_dir().ok_or(
CacheError("Could not determine home directory".into())
));
Ok(home_dir.join(".cache").join("tldr-rs"))
}

View file

@ -262,21 +262,3 @@ fn main() {
process::exit(1);
}
}
#[cfg(test)]
mod test {
use types::LineType;
#[test]
fn test_linetype_from_str() {
assert_eq!(LineType::from(""), LineType::Empty);
assert_eq!(LineType::from(" \n \r"), LineType::Empty);
assert_eq!(LineType::from("# Hello there"), LineType::Title("Hello there".into()));
assert_eq!(LineType::from("> tis a description \n"), LineType::Description("tis a description".into()));
assert_eq!(LineType::from("- some command"), LineType::ExampleText("some command".into()));
assert_eq!(LineType::from("`$ cargo run`"), LineType::ExampleCode("$ cargo run".into()));
assert_eq!(LineType::from("`$ cargo run"), LineType::Other("`$ cargo run".into()));
assert_eq!(LineType::from("jkl\u{f6}"), LineType::Other("jkl\u{f6}".into()));
}
}

View file

@ -65,6 +65,7 @@ mod test {
extern crate docopt;
use super::OsType::{self, Linux, OsX, SunOs, Other};
use super::LineType;
use rustc_serialize::json;
#[test]
@ -85,4 +86,16 @@ mod test {
fn test_os_type_decoding_unknown() {
assert!(json::decode::<OsType>("\"lindows\"").is_err());
}
#[test]
fn test_linetype_from_str() {
assert_eq!(LineType::from(""), LineType::Empty);
assert_eq!(LineType::from(" \n \r"), LineType::Empty);
assert_eq!(LineType::from("# Hello there"), LineType::Title("Hello there".into()));
assert_eq!(LineType::from("> tis a description \n"), LineType::Description("tis a description".into()));
assert_eq!(LineType::from("- some command"), LineType::ExampleText("some command".into()));
assert_eq!(LineType::from("`$ cargo run`"), LineType::ExampleCode("$ cargo run".into()));
assert_eq!(LineType::from("`$ cargo run"), LineType::Other("`$ cargo run".into()));
assert_eq!(LineType::from("jkl\u{f6}"), LineType::Other("jkl\u{f6}".into()));
}
}

81
tests/lib.rs Normal file
View file

@ -0,0 +1,81 @@
//! Integration tests.
extern crate tempdir;
use std::env;
use std::path::PathBuf;
use std::process::Command;
use tempdir::TempDir;
struct TestEnv {
cache_dir: TempDir,
bin_path: PathBuf,
}
impl TestEnv {
fn new() -> Self {
// Initialize tempdir for cache
let dir = TempDir::new(".tldr.test").unwrap();
// Determine binary path
let lib_path = env::current_exe().unwrap();
let bin_dir = lib_path.parent().unwrap();
let bin_path = bin_dir.join("tldr");
TestEnv {
cache_dir: dir,
bin_path: bin_path,
}
}
/// Return a new Command instance with the base binary and env vars set.
fn cmd(&self) -> Command {
let mut cmd = Command::new(&self.bin_path);
cmd.env("TLDR_RS_CACHE_DIR", self.cache_dir.path());
cmd
}
}
#[test]
fn test_missing_cache() {
let testenv = TestEnv::new();
let out = testenv.cmd()
.arg("sl")
.output()
.expect(&format!("Could not launch tldr binary ({:?})", &testenv.bin_path));
assert_eq!(out.status.success(), false);
let stdout = String::from_utf8(out.stdout).unwrap();
assert_eq!(stdout, "Cache not found. Please run `tldr --update`.\n");
}
#[test]
fn test_update_cache() {
let testenv = TestEnv::new();
let out1 = testenv.cmd()
.arg("sl")
.output()
.expect(&format!("Could not launch tldr binary ({:?})", &testenv.bin_path));
assert_eq!(out1.status.success(), false);
let out2 = testenv.cmd()
.arg("--update")
.output()
.expect(&format!("Could not launch tldr binary ({:?})", &testenv.bin_path));
let stdout = String::from_utf8(out2.stdout).unwrap();
println!("{}", stdout);
assert_eq!(out2.status.success(), true);
assert_eq!(stdout, "Successfully updated cache.\n");
let out3 = testenv.cmd()
.arg("sl")
.output()
.expect(&format!("Could not launch tldr binary ({:?})", &testenv.bin_path));
assert_eq!(out3.status.success(), true);
}