]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/rename-directory.rs
Merge pull request #20442 from csouth3/vim-syntax
[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::c_str::ToCStr;
18 use std::io::fs::PathExtensions;
19 use std::io::fs;
20 use std::io;
21 use std::os;
22
23 fn rename_directory() {
24     unsafe {
25         static U_RWX: i32 = (libc::S_IRUSR | libc::S_IWUSR | libc::S_IXUSR) as i32;
26
27         let tmpdir = TempDir::new("rename_directory").ok().expect("rename_directory failed");
28         let tmpdir = tmpdir.path();
29         let old_path = tmpdir.join_many(&["foo", "bar", "baz"]);
30         fs::mkdir_recursive(&old_path, io::USER_RWX);
31         let test_file = &old_path.join("temp.txt");
32
33         /* Write the temp input file */
34         let ostream = test_file.with_c_str(|fromp| {
35             "w+b".with_c_str(|modebuf| {
36                 libc::fopen(fromp, modebuf)
37             })
38         });
39         assert!((ostream as uint != 0u));
40         let s = "hello".to_string();
41         "hello".with_c_str(|buf| {
42             let write_len = libc::fwrite(buf as *const libc::c_void,
43                                          1u as libc::size_t,
44                                          (s.len() + 1u) as libc::size_t,
45                                          ostream);
46             assert_eq!(write_len, (s.len() + 1) as libc::size_t)
47         });
48         assert_eq!(libc::fclose(ostream), (0u as libc::c_int));
49
50         let new_path = tmpdir.join_many(&["quux", "blat"]);
51         fs::mkdir_recursive(&new_path, io::USER_RWX);
52         fs::rename(&old_path, &new_path.join("newdir"));
53         assert!(new_path.join("newdir").is_dir());
54         assert!(new_path.join_many(&["newdir", "temp.txt"]).exists());
55     }
56 }
57
58 pub fn main() { rename_directory() }