Use tempdir in tests

This commit is contained in:
Marcel Müller 2016-01-18 18:20:50 +01:00
parent be0bb6d0ad
commit c122c3cf37
No known key found for this signature in database
GPG key ID: DD4ED37D0CAC76E2
3 changed files with 49 additions and 8 deletions

View file

@ -3,9 +3,19 @@ name = "libimagstore"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"fs2 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "fs2 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "advapi32-sys"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "fs2" name = "fs2"
version = "0.2.2" version = "0.2.2"
@ -30,11 +40,29 @@ name = "libc"
version = "0.2.4" version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "rustc-serialize" name = "rustc-serialize"
version = "0.3.16" version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "tempdir"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rand 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "toml" name = "toml"
version = "0.1.25" version = "0.1.25"

View file

@ -83,25 +83,37 @@ mod test {
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::fs::File; use std::fs::File;
use tempdir::TempDir;
fn get_dir() -> TempDir {
TempDir::new("test-image").unwrap()
}
#[test] #[test]
fn lazy_file() { fn lazy_file() {
let path = PathBuf::from("/tmp/test"); let dir = get_dir();
let mut path = PathBuf::from(dir.path());
path.set_file_name("test1");
let mut lf = LazyFile::new(path); let mut lf = LazyFile::new(path);
write!(lf.create_file().unwrap(), "Hello World").unwrap(); write!(lf.create_file().unwrap(), "Hello World").unwrap();
dir.close().unwrap();
} }
#[test] #[test]
fn lazy_file_with_file() { fn lazy_file_with_file() {
let path = PathBuf::from("/tmp/test2"); let dir = get_dir();
let mut lf = LazyFile::new_with_file(File::create(path).unwrap()); let mut path = PathBuf::from(dir.path());
let mut file = lf.get_file_mut().unwrap(); path.set_file_name("test2");
let mut lf = LazyFile::new(path);
let mut file = lf.create_file().unwrap();
write!(file, "Hello World").unwrap(); file.write(b"Hello World").unwrap();
file.sync_all().unwrap(); file.sync_all().unwrap();
let mut s = String::new(); let mut s = Vec::new();
file.read_to_string(&mut s).unwrap(); file.read_to_end(&mut s).unwrap();
assert_eq!(s, "Hello World"); assert_eq!(s, "Hello World".to_string().into_bytes());
dir.close().unwrap();
} }
} }

View file

@ -1,5 +1,6 @@
extern crate fs2; extern crate fs2;
extern crate toml; extern crate toml;
extern crate tempdir;
pub mod content; pub mod content;
pub mod entry; pub mod entry;