1 use std
::env
::temp_dir
;
2 use std
::fs
::{self, File
};
4 use std
::io
::prelude
::*;
8 use symlink
::{symlink_auto
, symlink_file
, symlink_dir
, remove_symlink_file
, remove_symlink_dir
};
10 const TEST_FILE_CONTENTS
: &'
static [u8] =
11 b
"This file was created for the purpose of testing the symlink crate.";
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
));
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
));
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
));
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
));
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.
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();
59 assert!(symlink_path
.symlink_metadata().unwrap().file_type().is_symlink());
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
);
67 // TODO: use some kind of temp file wrapper which makes sure that the files gets deleted if
69 remove_symlink_file(symlink_path
).unwrap();
70 fs
::remove_file(file_path
).unwrap();
72 assert!(!symlink_path
.exists());
73 assert!(!file_path
.exists());
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();
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();
85 create_symlink(dir_path
, symlink_path
).unwrap();
87 assert!(symlink_path
.symlink_metadata().unwrap().file_type().is_symlink());
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
);
95 // TODO: use some kind of temp file wrapper which makes sure that the files gets deleted if
97 remove_symlink_dir(symlink_path
).unwrap();
98 fs
::remove_file(&file_path
).unwrap();
99 fs
::remove_dir(dir_path
).unwrap();
101 assert!(!symlink_path
.exists());
102 assert!(!file_path
.exists());
103 assert!(!dir_path
.exists());