]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/rename-directory.rs
rollup merge of #19020: Gankro/better-warn
[rust.git] / src / test / run-pass / rename-directory.rs
1 // Copyright 2013-2014 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 // This test can't be a unit test in std,
12 // because it needs TempDir, which is in extra
13
14 extern crate libc;
15
16 use std::io::TempDir;
17 use std::io::fs::PathExtensions;
18 use std::io::fs;
19 use std::io;
20 use std::os;
21
22 fn rename_directory() {
23     unsafe {
24         static U_RWX: i32 = (libc::S_IRUSR | libc::S_IWUSR | libc::S_IXUSR) as i32;
25
26         let tmpdir = TempDir::new("rename_directory").ok().expect("rename_directory failed");
27         let tmpdir = tmpdir.path();
28         let old_path = tmpdir.join_many(&["foo", "bar", "baz"]);
29         fs::mkdir_recursive(&old_path, io::USER_RWX);
30         let test_file = &old_path.join("temp.txt");
31
32         /* Write the temp input file */
33         let ostream = test_file.with_c_str(|fromp| {
34             "w+b".with_c_str(|modebuf| {
35                 libc::fopen(fromp, modebuf)
36             })
37         });
38         assert!((ostream as uint != 0u));
39         let s = "hello".to_string();
40         "hello".with_c_str(|buf| {
41             let write_len = libc::fwrite(buf as *const libc::c_void,
42                                          1u as libc::size_t,
43                                          (s.len() + 1u) as libc::size_t,
44                                          ostream);
45             assert_eq!(write_len, (s.len() + 1) as libc::size_t)
46         });
47         assert_eq!(libc::fclose(ostream), (0u as libc::c_int));
48
49         let new_path = tmpdir.join_many(&["quux", "blat"]);
50         fs::mkdir_recursive(&new_path, io::USER_RWX);
51         fs::rename(&old_path, &new_path.join("newdir"));
52         assert!(new_path.join("newdir").is_dir());
53         assert!(new_path.join_many(&["newdir", "temp.txt"]).exists());
54     }
55 }
56
57 pub fn main() { rename_directory() }