Stop transmuting std internals (copy them instead)
[symlink] / tests / test.rs
1 use std::env::temp_dir;
2 use std::fs::{self, File};
3 use std::io;
4 use std::io::prelude::*;
5 use std::path::Path;
6
7 extern crate symlink;
8 use symlink::{symlink_auto, symlink_file, symlink_dir, remove_symlink_file, remove_symlink_dir};
9
10 const TEST_FILE_CONTENTS: &'static [u8] =
11 b"This file was created for the purpose of testing the symlink crate.";
12
13 #[test]
14 fn test_symlink_file() {
15 let temp = temp_dir();
16 let file_path = temp.join("symlink-crate-test-file");
17 let symlink_path = temp.join("symlink-crate-test-file-symlink");
18 test_file_symlink(&file_path, &symlink_path, |src, dst| symlink_file(src, dst));
19 }
20
21 #[test]
22 fn test_symlink_auto_file() {
23 let temp = temp_dir();
24 let file_path = temp.join("symlink-crate-test-auto-file");
25 let symlink_path = temp.join("symlink-crate-test-auto-file-symlink");
26 test_file_symlink(&file_path, &symlink_path, |src, dst| symlink_auto(src, dst));
27 }
28
29 #[test]
30 fn test_symlink_dir() {
31 let temp = temp_dir();
32 let dir_path = temp.join("symlink-crate-test-dir");
33 let symlink_path = temp.join("symlink-crate-test-dir-symlink");
34 test_dir_symlink(&dir_path, &symlink_path, |src, dst| symlink_dir(src, dst));
35 }
36
37 #[test]
38 fn test_symlink_auto_dir() {
39 let temp = temp_dir();
40 let dir_path = temp.join("symlink-crate-test-auto-dir");
41 let symlink_path = temp.join("symlink-crate-test-auto-dir-symlink");
42 test_dir_symlink(&dir_path, &symlink_path, |src, dst| symlink_auto(src, dst));
43 }
44
45 fn test_file_symlink<F>(file_path: &Path, symlink_path: &Path, create_symlink: F)
46 where F: for<'a> FnOnce(&'a Path, &'a Path) -> io::Result<()> {
47 let mut file = File::create(file_path).unwrap();
48 file.write_all(TEST_FILE_CONTENTS).unwrap();
49 // Ensure it’s all written to disk properly.
50 drop(file);
51
52 // Note: the destination is *deliberately* a relative path. TODO: this would probably be a bad
53 // idea. On Windows, the paths are relative to the working directory (including treating X:foo
54 // as foo in the X: working directory); on Linux, I don’t know? If it’s anything like ln, it’s
55 // a path relative to the symlink.
56 //create_symlink("symlink-crate-test-file", symlink_path).unwrap();
57 create_symlink(file_path, symlink_path).unwrap();
58
59 assert!(symlink_path.symlink_metadata().unwrap().file_type().is_symlink());
60
61 file = File::open(symlink_path).unwrap();
62 let mut contents = vec![];
63 file.read_to_end(&mut contents).unwrap();
64 assert_eq!(contents, TEST_FILE_CONTENTS);
65 drop(file);
66
67 // TODO: use some kind of temp file wrapper which makes sure that the files gets deleted if
68 // they get created.
69 remove_symlink_file(symlink_path).unwrap();
70 fs::remove_file(file_path).unwrap();
71
72 assert!(!symlink_path.exists());
73 assert!(!file_path.exists());
74 }
75
76 fn test_dir_symlink<F>(dir_path: &Path, symlink_path: &Path, create_symlink: F)
77 where F: for<'a> FnOnce(&'a Path, &'a Path) -> io::Result<()> {
78 fs::create_dir(dir_path).unwrap();
79
80 let file_path = dir_path.join("test-file");
81 let mut file = File::create(&file_path).unwrap();
82 file.write_all(TEST_FILE_CONTENTS).unwrap();
83 drop(file);
84
85 create_symlink(dir_path, symlink_path).unwrap();
86
87 assert!(symlink_path.symlink_metadata().unwrap().file_type().is_symlink());
88
89 file = File::open(symlink_path.join("test-file")).unwrap();
90 let mut contents = vec![];
91 file.read_to_end(&mut contents).unwrap();
92 assert_eq!(contents, TEST_FILE_CONTENTS);
93 drop(file);
94
95 // TODO: use some kind of temp file wrapper which makes sure that the files gets deleted if
96 // they get created.
97 remove_symlink_dir(symlink_path).unwrap();
98 fs::remove_file(&file_path).unwrap();
99 fs::remove_dir(dir_path).unwrap();
100
101 assert!(!symlink_path.exists());
102 assert!(!file_path.exists());
103 assert!(!dir_path.exists());
104 }