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-08-11 18:24:19 -05:00
|
|
|
// ignore-windows 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-05-22 13:28:01 -05:00
|
|
|
extern crate debug;
|
|
|
|
|
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();
|
2014-06-18 13:25:36 -05:00
|
|
|
assert!(p.as_vec().ends_with(b"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();
|
2014-04-07 15:30:48 -05:00
|
|
|
let f: proc():Send = 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();
|
2014-04-07 15:30:48 -05:00
|
|
|
let f: proc():Send = 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;
|
|
|
|
{
|
2014-03-08 20:21:49 -06:00
|
|
|
let f = 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
|
|
|
}
|
|
|
|
|
2014-05-14 19:49:14 -05:00
|
|
|
fn test_rm_tempdir_close() {
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
let f: proc():Send = proc() {
|
|
|
|
let tmp = TempDir::new("test_rm_tempdir").unwrap();
|
|
|
|
tx.send(tmp.path().clone());
|
|
|
|
tmp.close();
|
|
|
|
fail!("fail to unwind past `tmp`");
|
|
|
|
};
|
|
|
|
task::try(f);
|
|
|
|
let path = rx.recv();
|
|
|
|
assert!(!path.exists());
|
|
|
|
|
|
|
|
let tmp = TempDir::new("test_rm_tempdir").unwrap();
|
|
|
|
let path = tmp.path().clone();
|
|
|
|
let f: proc():Send = proc() {
|
|
|
|
let tmp = tmp;
|
|
|
|
tmp.close();
|
|
|
|
fail!("fail to unwind past `tmp`");
|
|
|
|
};
|
|
|
|
task::try(f);
|
|
|
|
assert!(!path.exists());
|
|
|
|
|
|
|
|
let path;
|
|
|
|
{
|
|
|
|
let f = proc() {
|
|
|
|
TempDir::new("test_rm_tempdir").unwrap()
|
|
|
|
};
|
|
|
|
let tmp = task::try(f).ok().expect("test_rm_tmdir");
|
|
|
|
path = tmp.path().clone();
|
|
|
|
assert!(path.exists());
|
|
|
|
tmp.close();
|
|
|
|
}
|
|
|
|
assert!(!path.exists());
|
|
|
|
|
|
|
|
let path;
|
|
|
|
{
|
|
|
|
let tmp = TempDir::new("test_rm_tempdir").unwrap();
|
|
|
|
path = tmp.unwrap();
|
|
|
|
}
|
|
|
|
assert!(path.exists());
|
|
|
|
fs::rmdir_recursive(&path);
|
|
|
|
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();
|
log: Introduce liblog, the old std::logging
This commit moves all logging out of the standard library into an external
crate. This crate is the new crate which is responsible for all logging macros
and logging implementation. A few reasons for this change are:
* The crate map has always been a bit of a code smell among rust programs. It
has difficulty being loaded on almost all platforms, and it's used almost
exclusively for logging and only logging. Removing the crate map is one of the
end goals of this movement.
* The compiler has a fair bit of special support for logging. It has the
__log_level() expression as well as generating a global word per module
specifying the log level. This is unfairly favoring the built-in logging
system, and is much better done purely in libraries instead of the compiler
itself.
* Initialization of logging is much easier to do if there is no reliance on a
magical crate map being available to set module log levels.
* If the logging library can be written outside of the standard library, there's
no reason that it shouldn't be. It's likely that we're not going to build the
highest quality logging library of all time, so third-party libraries should
be able to provide just as high-quality logging systems as the default one
provided in the rust distribution.
With a migration such as this, the change does not come for free. There are some
subtle changes in the behavior of liblog vs the previous logging macros:
* The core change of this migration is that there is no longer a physical
log-level per module. This concept is still emulated (it is quite useful), but
there is now only a global log level, not a local one. This global log level
is a reflection of the maximum of all log levels specified. The previously
generated logging code looked like:
if specified_level <= __module_log_level() {
println!(...)
}
The newly generated code looks like:
if specified_level <= ::log::LOG_LEVEL {
if ::log::module_enabled(module_path!()) {
println!(...)
}
}
Notably, the first layer of checking is still intended to be "super fast" in
that it's just a load of a global word and a compare. The second layer of
checking is executed to determine if the current module does indeed have
logging turned on.
This means that if any module has a debug log level turned on, all modules
with debug log levels get a little bit slower (they all do more expensive
dynamic checks to determine if they're turned on or not).
Semantically, this migration brings no change in this respect, but
runtime-wise, this will have a perf impact on some code.
* A `RUST_LOG=::help` directive will no longer print out a list of all modules
that can be logged. This is because the crate map will no longer specify the
log levels of all modules, so the list of modules is not known. Additionally,
warnings can no longer be provided if a malformed logging directive was
supplied.
The new "hello world" for logging looks like:
#[phase(syntax, link)]
extern crate log;
fn main() {
debug!("Hello, world!");
}
2014-03-09 00:11:44 -06:00
|
|
|
println!("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();
|
log: Introduce liblog, the old std::logging
This commit moves all logging out of the standard library into an external
crate. This crate is the new crate which is responsible for all logging macros
and logging implementation. A few reasons for this change are:
* The crate map has always been a bit of a code smell among rust programs. It
has difficulty being loaded on almost all platforms, and it's used almost
exclusively for logging and only logging. Removing the crate map is one of the
end goals of this movement.
* The compiler has a fair bit of special support for logging. It has the
__log_level() expression as well as generating a global word per module
specifying the log level. This is unfairly favoring the built-in logging
system, and is much better done purely in libraries instead of the compiler
itself.
* Initialization of logging is much easier to do if there is no reliance on a
magical crate map being available to set module log levels.
* If the logging library can be written outside of the standard library, there's
no reason that it shouldn't be. It's likely that we're not going to build the
highest quality logging library of all time, so third-party libraries should
be able to provide just as high-quality logging systems as the default one
provided in the rust distribution.
With a migration such as this, the change does not come for free. There are some
subtle changes in the behavior of liblog vs the previous logging macros:
* The core change of this migration is that there is no longer a physical
log-level per module. This concept is still emulated (it is quite useful), but
there is now only a global log level, not a local one. This global log level
is a reflection of the maximum of all log levels specified. The previously
generated logging code looked like:
if specified_level <= __module_log_level() {
println!(...)
}
The newly generated code looks like:
if specified_level <= ::log::LOG_LEVEL {
if ::log::module_enabled(module_path!()) {
println!(...)
}
}
Notably, the first layer of checking is still intended to be "super fast" in
that it's just a load of a global word and a compare. The second layer of
checking is executed to determine if the current module does indeed have
logging turned on.
This means that if any module has a debug log level turned on, all modules
with debug log levels get a little bit slower (they all do more expensive
dynamic checks to determine if they're turned on or not).
Semantically, this migration brings no change in this respect, but
runtime-wise, this will have a perf impact on some code.
* A `RUST_LOG=::help` directive will no longer print out a list of all modules
that can be logged. This is because the crate map will no longer specify the
log levels of all modules, so the list of modules is not known. Additionally,
warnings can no longer be provided if a malformed logging directive was
supplied.
The new "hello world" for logging looks like:
#[phase(syntax, link)]
extern crate log;
fn main() {
debug!("Hello, world!");
}
2014-03-09 00:11:44 -06:00
|
|
|
println!("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");
|
log: Introduce liblog, the old std::logging
This commit moves all logging out of the standard library into an external
crate. This crate is the new crate which is responsible for all logging macros
and logging implementation. A few reasons for this change are:
* The crate map has always been a bit of a code smell among rust programs. It
has difficulty being loaded on almost all platforms, and it's used almost
exclusively for logging and only logging. Removing the crate map is one of the
end goals of this movement.
* The compiler has a fair bit of special support for logging. It has the
__log_level() expression as well as generating a global word per module
specifying the log level. This is unfairly favoring the built-in logging
system, and is much better done purely in libraries instead of the compiler
itself.
* Initialization of logging is much easier to do if there is no reliance on a
magical crate map being available to set module log levels.
* If the logging library can be written outside of the standard library, there's
no reason that it shouldn't be. It's likely that we're not going to build the
highest quality logging library of all time, so third-party libraries should
be able to provide just as high-quality logging systems as the default one
provided in the rust distribution.
With a migration such as this, the change does not come for free. There are some
subtle changes in the behavior of liblog vs the previous logging macros:
* The core change of this migration is that there is no longer a physical
log-level per module. This concept is still emulated (it is quite useful), but
there is now only a global log level, not a local one. This global log level
is a reflection of the maximum of all log levels specified. The previously
generated logging code looked like:
if specified_level <= __module_log_level() {
println!(...)
}
The newly generated code looks like:
if specified_level <= ::log::LOG_LEVEL {
if ::log::module_enabled(module_path!()) {
println!(...)
}
}
Notably, the first layer of checking is still intended to be "super fast" in
that it's just a load of a global word and a compare. The second layer of
checking is executed to determine if the current module does indeed have
logging turned on.
This means that if any module has a debug log level turned on, all modules
with debug log levels get a little bit slower (they all do more expensive
dynamic checks to determine if they're turned on or not).
Semantically, this migration brings no change in this respect, but
runtime-wise, this will have a perf impact on some code.
* A `RUST_LOG=::help` directive will no longer print out a list of all modules
that can be logged. This is because the crate map will no longer specify the
log levels of all modules, so the list of modules is not known. Additionally,
warnings can no longer be provided if a malformed logging directive was
supplied.
The new "hello world" for logging looks like:
#[phase(syntax, link)]
extern crate log;
fn main() {
debug!("Hello, world!");
}
2014-03-09 00:11:44 -06:00
|
|
|
println!("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
|
|
|
|
log: Introduce liblog, the old std::logging
This commit moves all logging out of the standard library into an external
crate. This crate is the new crate which is responsible for all logging macros
and logging implementation. A few reasons for this change are:
* The crate map has always been a bit of a code smell among rust programs. It
has difficulty being loaded on almost all platforms, and it's used almost
exclusively for logging and only logging. Removing the crate map is one of the
end goals of this movement.
* The compiler has a fair bit of special support for logging. It has the
__log_level() expression as well as generating a global word per module
specifying the log level. This is unfairly favoring the built-in logging
system, and is much better done purely in libraries instead of the compiler
itself.
* Initialization of logging is much easier to do if there is no reliance on a
magical crate map being available to set module log levels.
* If the logging library can be written outside of the standard library, there's
no reason that it shouldn't be. It's likely that we're not going to build the
highest quality logging library of all time, so third-party libraries should
be able to provide just as high-quality logging systems as the default one
provided in the rust distribution.
With a migration such as this, the change does not come for free. There are some
subtle changes in the behavior of liblog vs the previous logging macros:
* The core change of this migration is that there is no longer a physical
log-level per module. This concept is still emulated (it is quite useful), but
there is now only a global log level, not a local one. This global log level
is a reflection of the maximum of all log levels specified. The previously
generated logging code looked like:
if specified_level <= __module_log_level() {
println!(...)
}
The newly generated code looks like:
if specified_level <= ::log::LOG_LEVEL {
if ::log::module_enabled(module_path!()) {
println!(...)
}
}
Notably, the first layer of checking is still intended to be "super fast" in
that it's just a load of a global word and a compare. The second layer of
checking is executed to determine if the current module does indeed have
logging turned on.
This means that if any module has a debug log level turned on, all modules
with debug log levels get a little bit slower (they all do more expensive
dynamic checks to determine if they're turned on or not).
Semantically, this migration brings no change in this respect, but
runtime-wise, this will have a perf impact on some code.
* A `RUST_LOG=::help` directive will no longer print out a list of all modules
that can be logged. This is because the crate map will no longer specify the
log levels of all modules, so the list of modules is not known. Additionally,
warnings can no longer be provided if a malformed logging directive was
supplied.
The new "hello world" for logging looks like:
#[phase(syntax, link)]
extern crate log;
fn main() {
debug!("Hello, world!");
}
2014-03-09 00:11:44 -06:00
|
|
|
println!("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
|
|
|
}
|
|
|
|
|
2014-05-14 19:49:14 -05:00
|
|
|
pub fn dont_double_fail() {
|
|
|
|
let r: Result<(), _> = task::try(proc() {
|
|
|
|
let tmpdir = TempDir::new("test").unwrap();
|
|
|
|
// Remove the temporary directory so that TempDir sees
|
|
|
|
// an error on drop
|
|
|
|
fs::rmdir(tmpdir.path());
|
|
|
|
// Trigger failure. If TempDir fails *again* due to the rmdir
|
|
|
|
// error then the process will abort.
|
|
|
|
fail!();
|
|
|
|
});
|
|
|
|
assert!(r.is_err());
|
|
|
|
}
|
|
|
|
|
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);
|
2014-05-14 19:49:14 -05:00
|
|
|
in_tmpdir(test_rm_tempdir_close);
|
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);
|
2014-05-14 19:49:14 -05:00
|
|
|
in_tmpdir(dont_double_fail);
|
2013-09-13 23:41:28 -05:00
|
|
|
}
|