From 9dd4789d80b1313bb39daa8c8973e63a60b07b94 Mon Sep 17 00:00:00 2001 From: Elly Jones Date: Tue, 22 Nov 2011 17:09:35 -0500 Subject: [PATCH] Add fs::rmdir() and tempfile/gen_str() tests. --- src/lib/fs.rs | 22 +++++++++++++++++++++- src/lib/linux_os.rs | 1 + src/lib/macos_os.rs | 1 + src/lib/win32_os.rs | 1 + src/test/stdtest/rand.rs | 11 +++++++++++ src/test/stdtest/stdtest.rc | 1 + src/test/stdtest/tempfile.rs | 17 +++++++++++++++++ 7 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/test/stdtest/tempfile.rs diff --git a/src/lib/fs.rs b/src/lib/fs.rs index 915dbd9cd3b..5d4db459b5b 100644 --- a/src/lib/fs.rs +++ b/src/lib/fs.rs @@ -119,7 +119,7 @@ fn file_is_dir(p: path) -> bool { /* Function: make_dir -Creates a directory at the specific path. +Creates a directory at the specified path. */ fn make_dir(p: path, mode: int) -> bool { ret mkdir(p, mode); @@ -157,6 +157,26 @@ fn list_dir(p: path) -> [str] { ret full_paths; } +/* +Function: remove_dir + +Removes a directory at the specified path. +*/ +fn remove_dir(p: path) -> bool { + ret rmdir(p); + + #[cfg(target_os = "win32")] + fn rmdir(_p: path) -> bool { + ret str::as_buf(_p, {|buf| os::kernel32::RemoveDirectory(buf)}); + } + + #[cfg(target_os = "linux")] + #[cfg(target_os = "macos")] + fn rmdir(_p: path) -> bool { + ret str::as_buf(_p, {|buf| os::libc::rmdir(buf) == 0 }); + } +} + /* Function: path_is_absolute diff --git a/src/lib/linux_os.rs b/src/lib/linux_os.rs index 093fe542ca9..255cbb97410 100644 --- a/src/lib/linux_os.rs +++ b/src/lib/linux_os.rs @@ -52,6 +52,7 @@ fn waitpid(pid: pid_t, &status: c_int, options: c_int) -> pid_t; fn readlink(path: str::sbuf, buf: str::sbuf, bufsize: size_t) -> ssize_t; fn mkdir(path: str::sbuf, mode: int) -> int; + fn rmdir(path: str::sbuf) -> int; } mod libc_constants { diff --git a/src/lib/macos_os.rs b/src/lib/macos_os.rs index e2209f15d2e..7424668e509 100644 --- a/src/lib/macos_os.rs +++ b/src/lib/macos_os.rs @@ -45,6 +45,7 @@ fn pipe(buf: *mutable int) -> int; fn waitpid(pid: int, &status: int, options: int) -> int; fn mkdir(s: str::sbuf, mode: int) -> int; + fn rmdir(s: str::sbuf) -> int; } mod libc_constants { diff --git a/src/lib/win32_os.rs b/src/lib/win32_os.rs index 6befe626817..db9568c39a4 100644 --- a/src/lib/win32_os.rs +++ b/src/lib/win32_os.rs @@ -54,6 +54,7 @@ fn GetModuleFileNameA(hModule: HMODULE, nSize: DWORD) -> DWORD; fn CreateDirectory(lpPathName: LPCTSTR, lpSecurityAttributes: LPSECURITY_ATTRIBUTES) -> bool; + fn RemoveDirectory(lpPathName: LPCTSTR) -> bool; } // FIXME turn into constants diff --git a/src/test/stdtest/rand.rs b/src/test/stdtest/rand.rs index a8f13ed6704..b0d71d0e431 100644 --- a/src/test/stdtest/rand.rs +++ b/src/test/stdtest/rand.rs @@ -3,6 +3,7 @@ // -*- rust -*- use std; import std::rand; +import std::str; #[test] fn test() { @@ -27,3 +28,13 @@ fn test() { log r1.next(); log r1.next(); } + +#[test] +fn genstr() { + let r: rand::rng = rand::mk_rng(); + log r.gen_str(10u); + log r.gen_str(10u); + log r.gen_str(10u); + assert(str::char_len(r.gen_str(10u)) == 10u); + assert(str::char_len(r.gen_str(16u)) == 16u); +} diff --git a/src/test/stdtest/stdtest.rc b/src/test/stdtest/stdtest.rc index 289c1221c92..a31f2430629 100644 --- a/src/test/stdtest/stdtest.rc +++ b/src/test/stdtest/stdtest.rc @@ -33,6 +33,7 @@ mod sort; mod str; mod sys; mod task; +mod tempfile; mod test; mod tri; mod treemap; diff --git a/src/test/stdtest/tempfile.rs b/src/test/stdtest/tempfile.rs new file mode 100644 index 00000000000..2f78d3e8a92 --- /dev/null +++ b/src/test/stdtest/tempfile.rs @@ -0,0 +1,17 @@ +use std; +import std::fs; +import std::option::some; +import std::str; +import std::tempfile; + +#[test] +fn mkdtemp() { + let r = tempfile::mkdtemp("./", "foobar"); + alt r { + some(p) { + fs::remove_dir(p); + assert(str::ends_with(p, "foobar")); + } + _ { assert(false); } + } +}