tempfile.rs 6.4 KB
Newer Older
1
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 3 4 5 6 7 8 9 10
// 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.

11
// ignore-windows TempDir may cause IoError on windows: #10463
12 13 14 15 16 17 18 19 20

// 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.

21
use std::io::fs::PathExtensions;
22
use std::io::{fs, TempDir};
23
use std::io;
24
use std::os;
25
use std::sync::mpsc::channel;
A
Alex Crichton 已提交
26
use std::thread::Thread;
27

28 29
fn test_tempdir() {
    let path = {
30
        let p = TempDir::new_in(&Path::new("."), "foobar").unwrap();
31
        let p = p.path();
S
Simon Sapin 已提交
32
        assert!(p.as_vec().ends_with(b"foobar"));
33 34
        p.clone()
    };
35
    assert!(!path.exists());
36 37 38
}

fn test_rm_tempdir() {
39
    let (tx, rx) = channel();
40
    let f = move|:| -> () {
41
        let tmp = TempDir::new("test_rm_tempdir").unwrap();
42
        tx.send(tmp.path().clone()).unwrap();
S
Steve Klabnik 已提交
43
        panic!("panic to unwind past `tmp`");
44
    };
A
Alex Crichton 已提交
45
    let _ = Thread::spawn(f).join();
46
    let path = rx.recv().unwrap();
47
    assert!(!path.exists());
48 49 50

    let tmp = TempDir::new("test_rm_tempdir").unwrap();
    let path = tmp.path().clone();
51
    let f = move|:| -> () {
52
        let _tmp = tmp;
S
Steve Klabnik 已提交
53
        panic!("panic to unwind past `tmp`");
54
    };
A
Alex Crichton 已提交
55
    let _ = Thread::spawn(f).join();
56
    assert!(!path.exists());
57 58 59

    let path;
    {
60
        let f = move|:| {
61 62
            TempDir::new("test_rm_tempdir").unwrap()
        };
A
Alex Crichton 已提交
63
        let tmp = Thread::spawn(f).join().ok().expect("test_rm_tmdir");
64
        path = tmp.path().clone();
65
        assert!(path.exists());
66
    }
67
    assert!(!path.exists());
68 69 70 71

    let path;
    {
        let tmp = TempDir::new("test_rm_tempdir").unwrap();
A
Alex Crichton 已提交
72
        path = tmp.into_inner();
73
    }
74
    assert!(path.exists());
75
    fs::rmdir_recursive(&path);
76
    assert!(!path.exists());
77 78
}

79 80
fn test_rm_tempdir_close() {
    let (tx, rx) = channel();
81
    let f = move|:| -> () {
82
        let tmp = TempDir::new("test_rm_tempdir").unwrap();
83
        tx.send(tmp.path().clone()).unwrap();
84
        tmp.close();
S
Steve Klabnik 已提交
85
        panic!("panic when unwinding past `tmp`");
86
    };
A
Alex Crichton 已提交
87
    let _ = Thread::spawn(f).join();
88
    let path = rx.recv().unwrap();
89 90 91 92
    assert!(!path.exists());

    let tmp = TempDir::new("test_rm_tempdir").unwrap();
    let path = tmp.path().clone();
93
    let f = move|:| -> () {
94 95
        let tmp = tmp;
        tmp.close();
S
Steve Klabnik 已提交
96
        panic!("panic when unwinding past `tmp`");
97
    };
A
Alex Crichton 已提交
98
    let _ = Thread::spawn(f).join();
99 100 101 102
    assert!(!path.exists());

    let path;
    {
103
        let f = move|:| {
104 105
            TempDir::new("test_rm_tempdir").unwrap()
        };
A
Alex Crichton 已提交
106
        let tmp = Thread::spawn(f).join().ok().expect("test_rm_tmdir");
107 108 109 110 111 112 113 114 115
        path = tmp.path().clone();
        assert!(path.exists());
        tmp.close();
    }
    assert!(!path.exists());

    let path;
    {
        let tmp = TempDir::new("test_rm_tempdir").unwrap();
A
Alex Crichton 已提交
116
        path = tmp.into_inner();
117 118 119 120 121 122
    }
    assert!(path.exists());
    fs::rmdir_recursive(&path);
    assert!(!path.exists());
}

123 124 125
// Ideally these would be in std::os but then core would need
// to depend on std
fn recursive_mkdir_rel() {
126
    let path = Path::new("frob");
127
    let cwd = os::getcwd().unwrap();
L
Luqman Aden 已提交
128
    println!("recursive_mkdir_rel: Making: {} in cwd {} [{}]", path.display(),
129
           cwd.display(), path.exists());
130
    fs::mkdir_recursive(&path, io::USER_RWX);
131
    assert!(path.is_dir());
132
    fs::mkdir_recursive(&path, io::USER_RWX);
133
    assert!(path.is_dir());
134 135 136
}

fn recursive_mkdir_dot() {
137
    let dot = Path::new(".");
138
    fs::mkdir_recursive(&dot, io::USER_RWX);
139
    let dotdot = Path::new("..");
140
    fs::mkdir_recursive(&dotdot, io::USER_RWX);
141 142 143
}

fn recursive_mkdir_rel_2() {
144
    let path = Path::new("./frob/baz");
145
    let cwd = os::getcwd().unwrap();
L
Luqman Aden 已提交
146
    println!("recursive_mkdir_rel_2: Making: {} in cwd {} [{}]", path.display(),
147
           cwd.display(), path.exists());
148
    fs::mkdir_recursive(&path, io::USER_RWX);
149 150
    assert!(path.is_dir());
    assert!(path.dir_path().is_dir());
151
    let path2 = Path::new("quux/blat");
152
    println!("recursive_mkdir_rel_2: Making: {} in cwd {}", path2.display(),
153
           cwd.display());
154
    fs::mkdir_recursive(&path2, io::USER_RWX);
155 156
    assert!(path2.is_dir());
    assert!(path2.dir_path().is_dir());
157 158
}

159
// Ideally this would be in core, but needs TempFile
160
pub fn test_rmdir_recursive_ok() {
161
    let rwx = io::USER_RWX;
162

163 164
    let tmpdir = TempDir::new("test").ok().expect("test_rmdir_recursive_ok: \
                                                   couldn't create temp dir");
165
    let tmpdir = tmpdir.path();
166
    let root = tmpdir.join("foo");
167

168
    println!("making {}", root.display());
169 170 171 172 173
    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);
174 175 176
    assert!(!root.exists());
    assert!(!root.join("bar").exists());
    assert!(!root.join("bar").join("blat").exists());
177 178
}

S
Steve Klabnik 已提交
179
pub fn dont_double_panic() {
A
Alex Crichton 已提交
180
    let r: Result<(), _> = Thread::spawn(move|| {
181 182 183 184
        let tmpdir = TempDir::new("test").unwrap();
        // Remove the temporary directory so that TempDir sees
        // an error on drop
        fs::rmdir(tmpdir.path());
S
Steve Klabnik 已提交
185
        // Panic. If TempDir panics *again* due to the rmdir
186
        // error then the process will abort.
S
Steve Klabnik 已提交
187
        panic!();
A
Alex Crichton 已提交
188
    }).join();
189 190 191
    assert!(r.is_err());
}

J
Jorge Aparicio 已提交
192
fn in_tmpdir<F>(f: F) where F: FnOnce() {
193
    let tmpdir = TempDir::new("test").ok().expect("can't make tmpdir");
194
    assert!(os::change_dir(tmpdir.path()).is_ok());
195 196 197 198

    f();
}

199
pub fn main() {
200 201
    in_tmpdir(test_tempdir);
    in_tmpdir(test_rm_tempdir);
202
    in_tmpdir(test_rm_tempdir_close);
203 204 205 206
    in_tmpdir(recursive_mkdir_rel);
    in_tmpdir(recursive_mkdir_dot);
    in_tmpdir(recursive_mkdir_rel_2);
    in_tmpdir(test_rmdir_recursive_ok);
S
Steve Klabnik 已提交
207
    in_tmpdir(dont_double_panic);
208
}