]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/rename-directory.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[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::ffi::CString;
17 use std::old_io::TempDir;
18 use std::old_io::fs::PathExtensions;
19 use std::old_io::fs;
20 use std::old_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, old_io::USER_RWX);
31         let test_file = &old_path.join("temp.txt");
32
33         /* Write the temp input file */
34         let fromp = CString::from_slice(test_file.as_vec());
35         let modebuf = CString::from_slice(b"w+b");
36         let ostream = libc::fopen(fromp.as_ptr(), modebuf.as_ptr());
37         assert!((ostream as uint != 0_usize));
38         let s = "hello".to_string();
39         let buf = CString::from_slice(b"hello");
40         let write_len = libc::fwrite(buf.as_ptr() as *mut _,
41                                      1_usize as libc::size_t,
42                                      (s.len() + 1_usize) as libc::size_t,
43                                      ostream);
44         assert_eq!(write_len, (s.len() + 1) as libc::size_t);
45         assert_eq!(libc::fclose(ostream), (0_usize as libc::c_int));
46
47         let new_path = tmpdir.join_many(&["quux", "blat"]);
48         fs::mkdir_recursive(&new_path, old_io::USER_RWX);
49         fs::rename(&old_path, &new_path.join("newdir"));
50         assert!(new_path.join("newdir").is_dir());
51         assert!(new_path.join_many(&["newdir", "temp.txt"]).exists());
52     }
53 }
54
55 pub fn main() { rename_directory() }