From 9d0afade98eaf5b4e407e585e779050a9cd9b0bf 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] iQIcBAABCgAGBQJWnR7yAAoJEN1O030MrHbien4P/2ixFj7HRpWLZDARUTOy1f0I E/WY5FhCD5Nx2EAu1urJhZORwAeOyJurQVjoSC8FfcayK87Co3Hw9bt9CDB5uIxt aQlkA5YkamSlL1FQlOmcEqBUBISGmRtXB89zWeDWscRTrHb9pAPFZmrKpnklfa5r Lye3dAT7yrpTsPp2HxY2ojw5qHRb5Ojp57SKvAX27bYyEjk8PIREC2ZIgk08Mw1h kmx2QsIvWKGGTkOoBj/Lw+rL1aW+b6E6VdVngeZ04l9qU+3JOtHUkFyfvaiJvf7V 0HDiQIEHDjLCeZwvjVgzxBxQE/pANpMtno6y3LF99FdgfQtQy6hswaUpMilk2N9X Mq+kZ1bWaz6Q2Oo3GSFhWeAOgM/en1MHuxRQhjUMZVxp1tsqLo3piq5Ywi1kg/yR Dsxvw/AmQbkV8S77RhbIN+aRbSg6kBs6QmaAy4fVB4f+fnp6WKHIZ4yselZMtwUI AHVsXCvlQwmDm4BG4khJfKEd36x9hJAWnpKkNCprGhedN8QRgV7GmHZ4b3LBtODB UI338SX6ZyZmnYQV3mNgJ5mvzffnjZF0xup5rMLOg3SyNjKoirjYdlbb2SH7tNLp +EwBIJGCaFbiVFRoh1iHTvxtbKj218NPjP6ZU+UowBM5mlysRpVtCAYVGPprl+u7 aOtLp8WQDjnf8ysBL5pk =ZLhc -----END PGP SIGNATURE----- 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;