From c122c3cf370e2d07874e1393d852e79bfbd00043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Mon, 18 Jan 2016 18:20:50 +0100 Subject: [PATCH] Use tempdir in tests --- libimagstore/Cargo.lock | 28 ++++++++++++++++++++++++++++ libimagstore/src/lazyfile.rs | 28 ++++++++++++++++++++-------- libimagstore/src/lib.rs | 1 + 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/libimagstore/Cargo.lock b/libimagstore/Cargo.lock index 1d5c3264..6eb4a715 100644 --- a/libimagstore/Cargo.lock +++ b/libimagstore/Cargo.lock @@ -3,9 +3,19 @@ name = "libimagstore" version = "0.1.0" dependencies = [ "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)", ] +[[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]] name = "fs2" version = "0.2.2" @@ -30,11 +40,29 @@ name = "libc" version = "0.2.4" 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]] name = "rustc-serialize" version = "0.3.16" 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]] name = "toml" version = "0.1.25" diff --git a/libimagstore/src/lazyfile.rs b/libimagstore/src/lazyfile.rs index 29a598b6..777b7aff 100644 --- a/libimagstore/src/lazyfile.rs +++ b/libimagstore/src/lazyfile.rs @@ -83,25 +83,37 @@ mod test { use std::io::{Read, Write}; use std::path::PathBuf; use std::fs::File; + use tempdir::TempDir; + + fn get_dir() -> TempDir { + TempDir::new("test-image").unwrap() + } #[test] 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); write!(lf.create_file().unwrap(), "Hello World").unwrap(); + dir.close().unwrap(); } #[test] fn lazy_file_with_file() { - let path = PathBuf::from("/tmp/test2"); - let mut lf = LazyFile::new_with_file(File::create(path).unwrap()); - let mut file = lf.get_file_mut().unwrap(); + let dir = get_dir(); + let mut path = PathBuf::from(dir.path()); + 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(); - let mut s = String::new(); - file.read_to_string(&mut s).unwrap(); - assert_eq!(s, "Hello World"); + let mut s = Vec::new(); + file.read_to_end(&mut s).unwrap(); + assert_eq!(s, "Hello World".to_string().into_bytes()); + + dir.close().unwrap(); } } diff --git a/libimagstore/src/lib.rs b/libimagstore/src/lib.rs index 1296d6dd..30a7f999 100644 --- a/libimagstore/src/lib.rs +++ b/libimagstore/src/lib.rs @@ -1,5 +1,6 @@ extern crate fs2; extern crate toml; +extern crate tempdir; pub mod content; pub mod entry;