diff options
| author | Jeff Olson <olson.jeffery@gmail.com> | 2013-08-20 00:34:50 -0700 |
|---|---|---|
| committer | Jeff Olson <olson.jeffery@gmail.com> | 2013-08-22 16:31:57 -0700 |
| commit | 4015b4a9b4ec90b5b5d6542474e396181c7df8b5 (patch) | |
| tree | 84925191a79a639e422e2f7d78c9825bb9178bc3 | |
| parent | f6d897d7d97c6f75126d487da196084aaafde659 (diff) | |
| download | rust-4015b4a9b4ec90b5b5d6542474e396181c7df8b5.tar.gz rust-4015b4a9b4ec90b5b5d6542474e396181c7df8b5.zip | |
std: add FileStream::unlink + more tests
| -rw-r--r-- | src/libstd/rt/io/file.rs | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/src/libstd/rt/io/file.rs b/src/libstd/rt/io/file.rs index 0e972206ea7..6973e117131 100644 --- a/src/libstd/rt/io/file.rs +++ b/src/libstd/rt/io/file.rs @@ -15,6 +15,7 @@ use super::SeekStyle; use rt::rtio::{RtioFileDescriptor, IoFactory, IoFactoryObject}; use rt::io::{io_error, read_error, EndOfFile}; use rt::local::Local; +use rt::test::*; use libc::{O_RDWR, O_RDONLY, O_WRONLY, S_IWUSR, S_IRUSR, O_CREAT, O_TRUNC, O_APPEND}; @@ -84,6 +85,18 @@ impl FileStream { } } } + fn unlink<P: PathLike>(path: &P) { + let unlink_result = unsafe { + let io = Local::unsafe_borrow::<IoFactoryObject>(); + (*io).fs_unlink(path) + }; + match unlink_result { + Ok(_) => (), + Err(ioerr) => { + io_error::cond.raise(ioerr); + } + } + } } impl Reader for FileStream { @@ -131,10 +144,9 @@ impl Seek for FileStream { } fn file_test_smoke_test_impl() { - use rt::test::*; do run_in_newsched_task { let message = "it's alright. have a good time"; - let filename = &Path("rt_io_file_test.txt"); + let filename = &Path("./rt_io_file_test.txt"); { let mut write_stream = FileStream::open(filename, Create, ReadWrite).unwrap(); write_stream.write(message.as_bytes()); @@ -149,6 +161,7 @@ fn file_test_smoke_test_impl() { }; assert!(read_str == message.to_owned()); } + FileStream::unlink(filename); } } @@ -156,3 +169,40 @@ fn file_test_smoke_test_impl() { fn file_test_smoke_test() { file_test_smoke_test_impl(); } + +fn file_test_invalid_path_opened_without_create_should_raise_condition_impl() { + do run_in_newsched_task { + let filename = &Path("./file_that_does_not_exist.txt"); + let mut called = false; + do io_error::cond.trap(|_| { + called = true; + }).inside { + let result = FileStream::open(filename, Open, Read); + assert!(result.is_none()); + } + assert!(called); + } +} +#[test] +fn file_test_invalid_path_opened_without_create_should_raise_condition() { + file_test_invalid_path_opened_without_create_should_raise_condition_impl(); +} + +fn file_test_unlinking_invalid_path_should_raise_condition_impl() { + use io; + do run_in_newsched_task { + let filename = &Path("./another_file_that_does_not_exist.txt"); + let mut called = false; + do io_error::cond.trap(|e| { + io::println(fmt!("condition kind: %?", e.kind)); + called = true; + }).inside { + FileStream::unlink(filename); + } + assert!(called); + } +} +#[test] +fn file_test_unlinking_invalid_path_should_raise_condition() { + file_test_unlinking_invalid_path_should_raise_condition_impl(); +} |
