2014-02-07 13:08:32 -06:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-09-13 23:41:28 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-02-14 12:10:06 -06:00
|
|
|
// ignore-fast check-fast doesn't like 'extern crate'
|
2014-02-07 13:08:32 -06:00
|
|
|
// ignore-win32 TempDir may cause IoError on windows: #10463
|
2013-09-13 23:41:28 -05:00
|
|
|
|
|
|
|
// These tests are here to exercise the functionality of the `tempfile` module.
|
|
|
|
// One might expect these tests to be located in that module, but sadly they
|
|
|
|
// cannot. The tests need to invoke `os::change_dir` which cannot be done in the
|
|
|
|
// normal test infrastructure. If the tests change the current working
|
|
|
|
// directory, then *all* tests which require relative paths suddenly break b/c
|
|
|
|
// they're in a different location than before. Hence, these tests are all run
|
|
|
|
// serially here.
|
|
|
|
|
2014-03-14 13:16:10 -05:00
|
|
|
use std::io::{fs, TempDir};
|
2013-12-03 18:44:16 -06:00
|
|
|
use std::io;
|
2013-09-13 23:41:28 -05:00
|
|
|
use std::os;
|
2013-10-11 08:55:37 -05:00
|
|
|
use std::task;
|
2013-09-13 23:41:28 -05:00
|
|
|
|
2013-10-11 08:55:37 -05:00
|
|
|
fn test_tempdir() {
|
|
|
|
let path = {
|
2013-12-03 21:15:12 -06:00
|
|
|
let p = TempDir::new_in(&Path::new("."), "foobar").unwrap();
|
2013-10-11 08:55:37 -05:00
|
|
|
let p = p.path();
|
2013-10-17 00:01:20 -05:00
|
|
|
assert!(p.as_vec().ends_with(bytes!("foobar")));
|
2013-10-11 08:55:37 -05:00
|
|
|
p.clone()
|
|
|
|
};
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(!path.exists());
|
2013-10-11 08:55:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_rm_tempdir() {
|
2014-03-09 16:58:32 -05:00
|
|
|
let (tx, rx) = channel();
|
2013-11-22 01:36:52 -06:00
|
|
|
let f: proc() = proc() {
|
2013-10-11 08:55:37 -05:00
|
|
|
let tmp = TempDir::new("test_rm_tempdir").unwrap();
|
2014-03-09 16:58:32 -05:00
|
|
|
tx.send(tmp.path().clone());
|
2013-10-21 15:08:31 -05:00
|
|
|
fail!("fail to unwind past `tmp`");
|
2013-10-11 08:55:37 -05:00
|
|
|
};
|
|
|
|
task::try(f);
|
2014-03-09 16:58:32 -05:00
|
|
|
let path = rx.recv();
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(!path.exists());
|
2013-10-11 08:55:37 -05:00
|
|
|
|
|
|
|
let tmp = TempDir::new("test_rm_tempdir").unwrap();
|
|
|
|
let path = tmp.path().clone();
|
2013-11-22 01:36:52 -06:00
|
|
|
let f: proc() = proc() {
|
2013-12-03 18:44:16 -06:00
|
|
|
let _tmp = tmp;
|
2013-10-21 15:08:31 -05:00
|
|
|
fail!("fail to unwind past `tmp`");
|
2013-10-11 08:55:37 -05:00
|
|
|
};
|
|
|
|
task::try(f);
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(!path.exists());
|
2013-10-11 08:55:37 -05:00
|
|
|
|
|
|
|
let path;
|
|
|
|
{
|
2013-11-22 01:36:52 -06:00
|
|
|
let f: proc() -> TempDir = proc() {
|
2013-10-11 08:55:37 -05:00
|
|
|
TempDir::new("test_rm_tempdir").unwrap()
|
|
|
|
};
|
2013-12-06 15:23:23 -06:00
|
|
|
let tmp = task::try(f).ok().expect("test_rm_tmdir");
|
2013-10-11 08:55:37 -05:00
|
|
|
path = tmp.path().clone();
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(path.exists());
|
2013-10-11 08:55:37 -05:00
|
|
|
}
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(!path.exists());
|
2013-10-11 08:55:37 -05:00
|
|
|
|
|
|
|
let path;
|
|
|
|
{
|
|
|
|
let tmp = TempDir::new("test_rm_tempdir").unwrap();
|
|
|
|
path = tmp.unwrap();
|
|
|
|
}
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(path.exists());
|
2013-10-31 17:15:30 -05:00
|
|
|
fs::rmdir_recursive(&path);
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(!path.exists());
|
2013-09-13 23:41:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally these would be in std::os but then core would need
|
|
|
|
// to depend on std
|
|
|
|
fn recursive_mkdir_rel() {
|
2013-12-03 21:15:12 -06:00
|
|
|
let path = Path::new("frob");
|
2013-09-26 19:21:59 -05:00
|
|
|
let cwd = os::getcwd();
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("recursive_mkdir_rel: Making: {} in cwd {} [{:?}]", path.display(),
|
2013-10-25 19:04:37 -05:00
|
|
|
cwd.display(), path.exists());
|
2013-10-31 17:15:30 -05:00
|
|
|
fs::mkdir_recursive(&path, io::UserRWX);
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(path.is_dir());
|
2013-10-31 17:15:30 -05:00
|
|
|
fs::mkdir_recursive(&path, io::UserRWX);
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(path.is_dir());
|
2013-09-13 23:41:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn recursive_mkdir_dot() {
|
2013-12-03 21:15:12 -06:00
|
|
|
let dot = Path::new(".");
|
2013-10-31 17:15:30 -05:00
|
|
|
fs::mkdir_recursive(&dot, io::UserRWX);
|
2013-12-03 21:15:12 -06:00
|
|
|
let dotdot = Path::new("..");
|
2013-10-31 17:15:30 -05:00
|
|
|
fs::mkdir_recursive(&dotdot, io::UserRWX);
|
2013-09-13 23:41:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn recursive_mkdir_rel_2() {
|
2013-12-03 21:15:12 -06:00
|
|
|
let path = Path::new("./frob/baz");
|
2013-09-26 19:21:59 -05:00
|
|
|
let cwd = os::getcwd();
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("recursive_mkdir_rel_2: Making: {} in cwd {} [{:?}]", path.display(),
|
2013-10-25 19:04:37 -05:00
|
|
|
cwd.display(), path.exists());
|
2013-10-31 17:15:30 -05:00
|
|
|
fs::mkdir_recursive(&path, io::UserRWX);
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(path.is_dir());
|
|
|
|
assert!(path.dir_path().is_dir());
|
2013-12-03 21:15:12 -06:00
|
|
|
let path2 = Path::new("quux/blat");
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("recursive_mkdir_rel_2: Making: {} in cwd {}", path2.display(),
|
2013-09-26 19:21:59 -05:00
|
|
|
cwd.display());
|
2013-10-31 17:15:30 -05:00
|
|
|
fs::mkdir_recursive(&path2, io::UserRWX);
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(path2.is_dir());
|
|
|
|
assert!(path2.dir_path().is_dir());
|
2013-09-13 23:41:28 -05:00
|
|
|
}
|
|
|
|
|
2013-10-11 08:55:37 -05:00
|
|
|
// Ideally this would be in core, but needs TempFile
|
2013-09-13 23:41:28 -05:00
|
|
|
pub fn test_rmdir_recursive_ok() {
|
2013-10-25 19:04:37 -05:00
|
|
|
let rwx = io::UserRWX;
|
2013-09-13 23:41:28 -05:00
|
|
|
|
2013-10-11 08:55:37 -05:00
|
|
|
let tmpdir = TempDir::new("test").expect("test_rmdir_recursive_ok: \
|
|
|
|
couldn't create temp dir");
|
|
|
|
let tmpdir = tmpdir.path();
|
2013-10-05 21:49:32 -05:00
|
|
|
let root = tmpdir.join("foo");
|
2013-09-13 23:41:28 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("making {}", root.display());
|
2013-10-31 17:15:30 -05:00
|
|
|
fs::mkdir(&root, rwx);
|
|
|
|
fs::mkdir(&root.join("foo"), rwx);
|
|
|
|
fs::mkdir(&root.join("foo").join("bar"), rwx);
|
|
|
|
fs::mkdir(&root.join("foo").join("bar").join("blat"), rwx);
|
|
|
|
fs::rmdir_recursive(&root);
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(!root.exists());
|
|
|
|
assert!(!root.join("bar").exists());
|
|
|
|
assert!(!root.join("bar").join("blat").exists());
|
2013-09-13 23:41:28 -05:00
|
|
|
}
|
|
|
|
|
2013-11-19 18:34:19 -06:00
|
|
|
fn in_tmpdir(f: ||) {
|
2013-10-11 08:55:37 -05:00
|
|
|
let tmpdir = TempDir::new("test").expect("can't make tmpdir");
|
|
|
|
assert!(os::change_dir(tmpdir.path()));
|
2013-09-13 23:41:28 -05:00
|
|
|
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
2014-01-03 17:30:54 -06:00
|
|
|
pub fn main() {
|
2013-10-11 08:55:37 -05:00
|
|
|
in_tmpdir(test_tempdir);
|
|
|
|
in_tmpdir(test_rm_tempdir);
|
2013-09-13 23:41:28 -05:00
|
|
|
in_tmpdir(recursive_mkdir_rel);
|
|
|
|
in_tmpdir(recursive_mkdir_dot);
|
|
|
|
in_tmpdir(recursive_mkdir_rel_2);
|
|
|
|
in_tmpdir(test_rmdir_recursive_ok);
|
|
|
|
}
|