History

Add custom tempdir in.

Svetlin Stefanov committed 4 years ago

Commit 8bfd52a1 parent 27f4bfc1 file · +40 −13Browse files

Unified · Split

git-path/tests/path.rs+40−13
@@ -1,7 +1,8 @@
1mod convert {1mod convert {
2 use bstr::ByteSlice;2 use bstr::ByteSlice;
3 use git_path::{real_path, to_unix_separators, to_windows_separators, RealPathError};3 use git_path::{real_path, to_unix_separators, to_windows_separators, RealPathError};
4 use std::fs::create_dir_all;4 use std::fs::{create_dir_all, remove_dir_all};
5 use std::ops::Deref;
5 #[cfg(not(target_os = "windows"))]6 #[cfg(not(target_os = "windows"))]
6 use std::os::unix::fs;7 use std::os::unix::fs;
7 #[cfg(target_os = "windows")]8 #[cfg(target_os = "windows")]
@@ -9,6 +10,33 @@ mod convert { · 1 unmodified line
9 use std::path::{Path, PathBuf};10 use std::path::{Path, PathBuf};
10 use tempfile::{tempdir, tempdir_in};11 use tempfile::{tempdir, tempdir_in};
11 12
13 struct TempDirIn<'a> {
14 pub path: &'a Path,
15 }
16
17 impl<'a> TempDirIn<'a> {
18 fn new(path: &'a str) -> Self {
19 let path = Path::new(path);
20 create_dir_all(path).unwrap();
21 tempdir_in(path).unwrap();
22 Self { path }
23 }
24 }
25
26 impl Deref for TempDirIn<'_> {
27 type Target = Path;
28
29 fn deref(&self) -> &Self::Target {
30 self.path
31 }
32 }
33
34 impl Drop for TempDirIn<'_> {
35 fn drop(&mut self) {
36 remove_dir_all(&self.path).unwrap();
37 }
38 }
39
12 #[test]40 #[test]
13 fn assure_unix_separators() {41 fn assure_unix_separators() {
14 assert_eq!(to_unix_separators(b"no-backslash".as_bstr()).as_bstr(), "no-backslash");42 assert_eq!(to_unix_separators(b"no-backslash".as_bstr()).as_bstr(), "no-backslash");
@@ -27,12 +55,14 @@ mod convert { · 12 unmodified lines
27 }55 }
28 56
29 #[test]57 #[test]
30 #[ignore]
31 fn real_path_tests() {58 fn real_path_tests() {
32 let cwd = tempdir().unwrap();59 let cwd = tempdir().unwrap();
33 let cwd = cwd.path();60 let cwd = cwd.path();
34 let tmp_dir = tempdir_in("/private/tmp").unwrap();61
35 let tmp_dir = tmp_dir.path();62 #[cfg(not(target_os = "windows"))]
63 let tmp_dir = TempDirIn::new("/private/tmp/t");
64 #[cfg(target_os = "windows")]
65 let tmp_dir = TempDirIn::new("%localappdata%\\Temp\\t");
36 66
37 let empty_path = Path::new("");67 let empty_path = Path::new("");
38 assert!(68 assert!(
@@ -124,15 +154,12 @@ mod convert { · 85 unmodified lines
124 154
125 // enum Component.Prefix155 // enum Component.Prefix
126 156
127 let a = PathBuf::from("a");157 let tmp_dir_in = TempDirIn::new("/tmp/a/b/s");
128 let mut a = a.components();158 let p = tmp_dir_in.path;
129 let _ = a.next();159 assert!(p.exists());
130 160 assert_eq!(p, Path::new("/tmp/a/b/s"));
131 let mut pb = PathBuf::new();161 drop(tmp_dir_in);
132 pb.push(a.collect::<PathBuf>());162 assert!(!p.exists());
133
134 assert_eq!(pb, PathBuf::new());
135 assert_eq!(pb.components().next(), None);
136 163
137 // pass iterator input_path.components() instead of input_path164 // pass iterator input_path.components() instead of input_path
138 // have real_path mut and not return it165 // have real_path mut and not return it