diff options
| author | Benjamin Herr <ben@0x539.de> | 2013-10-11 15:55:37 +0200 |
|---|---|---|
| committer | Benjamin Herr <ben@0x539.de> | 2013-10-11 15:55:37 +0200 |
| commit | 63e9e496f65cac6b9f22abda7f2f0ffa1af0cf94 (patch) | |
| tree | 25ba34476090f29fdd14838e84c94ee0b61f40d8 /src/libextra | |
| parent | 5bddcc1eadeafd2461f7026d7c1f5c70980c1f06 (diff) | |
| download | rust-63e9e496f65cac6b9f22abda7f2f0ffa1af0cf94.tar.gz rust-63e9e496f65cac6b9f22abda7f2f0ffa1af0cf94.zip | |
extra::tempfile: replace mkdtemp with an RAII wrapper
this incidentally stops `make check` from leaving directories in `/tmp`
Diffstat (limited to 'src/libextra')
| -rw-r--r-- | src/libextra/tempfile.rs | 64 | ||||
| -rw-r--r-- | src/libextra/test.rs | 10 |
2 files changed, 58 insertions, 16 deletions
diff --git a/src/libextra/tempfile.rs b/src/libextra/tempfile.rs index 13e0c47433f..60084faad98 100644 --- a/src/libextra/tempfile.rs +++ b/src/libextra/tempfile.rs @@ -15,17 +15,63 @@ use std::os; use std::rand::Rng; use std::rand; -/// Attempts to make a temporary directory inside of `tmpdir` whose name will -/// have the suffix `suffix`. If no directory can be created, None is returned. -pub fn mkdtemp(tmpdir: &Path, suffix: &str) -> Option<Path> { - let mut r = rand::rng(); - for _ in range(0u, 1000) { - let p = tmpdir.push(r.gen_ascii_str(16) + suffix); - if os::make_dir(&p, 0x1c0) { // 700 - return Some(p); +/// A wrapper for a path to temporary directory implementing automatic +/// scope-pased deletion. +pub struct TempDir { + priv path: Option<Path> +} + +impl TempDir { + /// Attempts to make a temporary directory inside of `tmpdir` whose name + /// will have the suffix `suffix`. The directory will be automatically + /// deleted once the returned wrapper is destroyed. + /// + /// If no directory can be created, None is returned. + pub fn new_in(tmpdir: &Path, suffix: &str) -> Option<TempDir> { + if !tmpdir.is_absolute() { + let abs_tmpdir = os::make_absolute(tmpdir); + return TempDir::new_in(&abs_tmpdir, suffix); + } + + let mut r = rand::rng(); + for _ in range(0u, 1000) { + let p = tmpdir.push(r.gen_ascii_str(16) + suffix); + if os::make_dir(&p, 0x1c0) { // 700 + return Some(TempDir { path: Some(p) }); + } + } + None + } + + /// Attempts to make a temporary directory inside of `os::tmpdir()` whose + /// name will have the suffix `suffix`. The directory will be automatically + /// deleted once the returned wrapper is destroyed. + /// + /// If no directory can be created, None is returned. + pub fn new(suffix: &str) -> Option<TempDir> { + TempDir::new_in(&os::tmpdir(), suffix) + } + + /// Unwrap the wrapped `std::path::Path` from the `TempDir` wrapper. + /// This discards the wrapper so that the automatic deletion of the + /// temporary directory is prevented. + pub fn unwrap(self) -> Path { + let mut tmpdir = self; + tmpdir.path.take_unwrap() + } + + /// Access the wrapped `std::path::Path` to the temporary directory. + pub fn path<'a>(&'a self) -> &'a Path { + self.path.get_ref() + } +} + +impl Drop for TempDir { + fn drop(&mut self) { + for path in self.path.iter() { + os::remove_dir_recursive(path); } } - None } // the tests for this module need to change the path using change_dir, diff --git a/src/libextra/test.rs b/src/libextra/test.rs index 09e1fae14af..c01c619b0c2 100644 --- a/src/libextra/test.rs +++ b/src/libextra/test.rs @@ -1148,8 +1148,7 @@ mod tests { use test::{TestOpts, run_test}; use std::comm::{stream, SharedChan}; - use tempfile; - use std::os; + use tempfile::TempDir; #[test] pub fn do_not_run_ignored_tests() { @@ -1392,9 +1391,8 @@ mod tests { pub fn ratchet_test() { - let dpth = tempfile::mkdtemp(&os::tmpdir(), - "test-ratchet").expect("missing test for ratchet"); - let pth = dpth.push("ratchet.json"); + let dpth = TempDir::new("test-ratchet").expect("missing test for ratchet"); + let pth = dpth.path().push("ratchet.json"); let mut m1 = MetricMap::new(); m1.insert_metric("runtime", 1000.0, 2.0); @@ -1432,7 +1430,5 @@ mod tests { assert_eq!(m4.len(), 2); assert_eq!(*(m4.find(&~"runtime").unwrap()), Metric { value: 1100.0, noise: 2.0 }); assert_eq!(*(m4.find(&~"throughput").unwrap()), Metric { value: 50.0, noise: 2.0 }); - - os::remove_dir_recursive(&dpth); } } |
