|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// xfail-fast windows doesn't like 'extern mod extra' |
| 12 | + |
| 13 | +// These tests are here to exercise the functionality of the `tempfile` module. |
| 14 | +// One might expect these tests to be located in that module, but sadly they |
| 15 | +// cannot. The tests need to invoke `os::change_dir` which cannot be done in the |
| 16 | +// normal test infrastructure. If the tests change the current working |
| 17 | +// directory, then *all* tests which require relative paths suddenly break b/c |
| 18 | +// they're in a different location than before. Hence, these tests are all run |
| 19 | +// serially here. |
| 20 | + |
| 21 | +extern mod extra; |
| 22 | + |
| 23 | +use extra::tempfile::mkdtemp; |
| 24 | +use std::os; |
| 25 | +use std::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR}; |
| 26 | + |
| 27 | +fn test_mkdtemp() { |
| 28 | + let p = mkdtemp(&Path("."), "foobar").unwrap(); |
| 29 | + os::remove_dir(&p); |
| 30 | + assert!(p.to_str().ends_with("foobar")); |
| 31 | +} |
| 32 | + |
| 33 | +// Ideally these would be in std::os but then core would need |
| 34 | +// to depend on std |
| 35 | +fn recursive_mkdir_rel() { |
| 36 | + let path = Path("frob"); |
| 37 | + debug!("recursive_mkdir_rel: Making: %s in cwd %s [%?]", path.to_str(), |
| 38 | + os::getcwd().to_str(), |
| 39 | + os::path_exists(&path)); |
| 40 | + assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32)); |
| 41 | + assert!(os::path_is_dir(&path)); |
| 42 | + assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32)); |
| 43 | + assert!(os::path_is_dir(&path)); |
| 44 | +} |
| 45 | + |
| 46 | +fn recursive_mkdir_dot() { |
| 47 | + let dot = Path("."); |
| 48 | + assert!(os::mkdir_recursive(&dot, (S_IRUSR | S_IWUSR | S_IXUSR) as i32)); |
| 49 | + let dotdot = Path(".."); |
| 50 | + assert!(os::mkdir_recursive(&dotdot, (S_IRUSR | S_IWUSR | S_IXUSR) as i32)); |
| 51 | +} |
| 52 | + |
| 53 | +fn recursive_mkdir_rel_2() { |
| 54 | + let path = Path("./frob/baz"); |
| 55 | + debug!("recursive_mkdir_rel_2: Making: %s in cwd %s [%?]", path.to_str(), |
| 56 | + os::getcwd().to_str(), os::path_exists(&path)); |
| 57 | + assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32)); |
| 58 | + assert!(os::path_is_dir(&path)); |
| 59 | + assert!(os::path_is_dir(&path.pop())); |
| 60 | + let path2 = Path("quux/blat"); |
| 61 | + debug!("recursive_mkdir_rel_2: Making: %s in cwd %s", path2.to_str(), |
| 62 | + os::getcwd().to_str()); |
| 63 | + assert!(os::mkdir_recursive(&path2, (S_IRUSR | S_IWUSR | S_IXUSR) as i32)); |
| 64 | + assert!(os::path_is_dir(&path2)); |
| 65 | + assert!(os::path_is_dir(&path2.pop())); |
| 66 | +} |
| 67 | + |
| 68 | +// Ideally this would be in core, but needs mkdtemp |
| 69 | +pub fn test_rmdir_recursive_ok() { |
| 70 | + let rwx = (S_IRUSR | S_IWUSR | S_IXUSR) as i32; |
| 71 | + |
| 72 | + let tmpdir = mkdtemp(&os::tmpdir(), "test").expect("test_rmdir_recursive_ok: \ |
| 73 | + couldn't create temp dir"); |
| 74 | + let root = tmpdir.push("foo"); |
| 75 | + |
| 76 | + debug!("making %s", root.to_str()); |
| 77 | + assert!(os::make_dir(&root, rwx)); |
| 78 | + assert!(os::make_dir(&root.push("foo"), rwx)); |
| 79 | + assert!(os::make_dir(&root.push("foo").push("bar"), rwx)); |
| 80 | + assert!(os::make_dir(&root.push("foo").push("bar").push("blat"), rwx)); |
| 81 | + assert!(os::remove_dir_recursive(&root)); |
| 82 | + assert!(!os::path_exists(&root)); |
| 83 | + assert!(!os::path_exists(&root.push("bar"))); |
| 84 | + assert!(!os::path_exists(&root.push("bar").push("blat"))); |
| 85 | +} |
| 86 | + |
| 87 | +fn in_tmpdir(f: &fn()) { |
| 88 | + let tmpdir = mkdtemp(&os::tmpdir(), "test").expect("can't make tmpdir"); |
| 89 | + assert!(os::change_dir(&tmpdir)); |
| 90 | + |
| 91 | + f(); |
| 92 | +} |
| 93 | + |
| 94 | +fn main() { |
| 95 | + in_tmpdir(test_mkdtemp); |
| 96 | + in_tmpdir(recursive_mkdir_rel); |
| 97 | + in_tmpdir(recursive_mkdir_dot); |
| 98 | + in_tmpdir(recursive_mkdir_rel_2); |
| 99 | + in_tmpdir(test_rmdir_recursive_ok); |
| 100 | +} |
0 commit comments