Skip to content

Commit 4015b4a

Browse files
committed
std: add FileStream::unlink + more tests
1 parent f6d897d commit 4015b4a

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

src/libstd/rt/io/file.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use super::SeekStyle;
1515
use rt::rtio::{RtioFileDescriptor, IoFactory, IoFactoryObject};
1616
use rt::io::{io_error, read_error, EndOfFile};
1717
use rt::local::Local;
18+
use rt::test::*;
1819
use libc::{O_RDWR, O_RDONLY, O_WRONLY, S_IWUSR, S_IRUSR,
1920
O_CREAT, O_TRUNC, O_APPEND};
2021

@@ -84,6 +85,18 @@ impl FileStream {
8485
}
8586
}
8687
}
88+
fn unlink<P: PathLike>(path: &P) {
89+
let unlink_result = unsafe {
90+
let io = Local::unsafe_borrow::<IoFactoryObject>();
91+
(*io).fs_unlink(path)
92+
};
93+
match unlink_result {
94+
Ok(_) => (),
95+
Err(ioerr) => {
96+
io_error::cond.raise(ioerr);
97+
}
98+
}
99+
}
87100
}
88101

89102
impl Reader for FileStream {
@@ -131,10 +144,9 @@ impl Seek for FileStream {
131144
}
132145

133146
fn file_test_smoke_test_impl() {
134-
use rt::test::*;
135147
do run_in_newsched_task {
136148
let message = "it's alright. have a good time";
137-
let filename = &Path("rt_io_file_test.txt");
149+
let filename = &Path("./rt_io_file_test.txt");
138150
{
139151
let mut write_stream = FileStream::open(filename, Create, ReadWrite).unwrap();
140152
write_stream.write(message.as_bytes());
@@ -149,10 +161,48 @@ fn file_test_smoke_test_impl() {
149161
};
150162
assert!(read_str == message.to_owned());
151163
}
164+
FileStream::unlink(filename);
152165
}
153166
}
154167

155168
#[test]
156169
fn file_test_smoke_test() {
157170
file_test_smoke_test_impl();
158171
}
172+
173+
fn file_test_invalid_path_opened_without_create_should_raise_condition_impl() {
174+
do run_in_newsched_task {
175+
let filename = &Path("./file_that_does_not_exist.txt");
176+
let mut called = false;
177+
do io_error::cond.trap(|_| {
178+
called = true;
179+
}).inside {
180+
let result = FileStream::open(filename, Open, Read);
181+
assert!(result.is_none());
182+
}
183+
assert!(called);
184+
}
185+
}
186+
#[test]
187+
fn file_test_invalid_path_opened_without_create_should_raise_condition() {
188+
file_test_invalid_path_opened_without_create_should_raise_condition_impl();
189+
}
190+
191+
fn file_test_unlinking_invalid_path_should_raise_condition_impl() {
192+
use io;
193+
do run_in_newsched_task {
194+
let filename = &Path("./another_file_that_does_not_exist.txt");
195+
let mut called = false;
196+
do io_error::cond.trap(|e| {
197+
io::println(fmt!("condition kind: %?", e.kind));
198+
called = true;
199+
}).inside {
200+
FileStream::unlink(filename);
201+
}
202+
assert!(called);
203+
}
204+
}
205+
#[test]
206+
fn file_test_unlinking_invalid_path_should_raise_condition() {
207+
file_test_unlinking_invalid_path_should_raise_condition_impl();
208+
}

0 commit comments

Comments
 (0)