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 @@
11 mod convert {
22 use bstr::ByteSlice;
33 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;
56 #[cfg(not(target_os = "windows"))]
67 use std::os::unix::fs;
78 #[cfg(target_os = "windows")]
@@ -9,6 +10,33 @@ mod convert { · 1 unmodified line
910 use std::path::{Path, PathBuf};
1011 use tempfile::{tempdir, tempdir_in};
1112
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+
1240 #[test]
1341 fn assure_unix_separators() {
1442 assert_eq!(to_unix_separators(b"no-backslash".as_bstr()).as_bstr(), "no-backslash");
@@ -27,12 +55,14 @@ mod convert { · 12 unmodified lines
2755 }
2856
2957 #[test]
30− #[ignore]
3158 fn real_path_tests() {
3259 let cwd = tempdir().unwrap();
3360 let cwd = cwd.path();
34− let tmp_dir = tempdir_in("/private/tmp").unwrap();
35− let tmp_dir = tmp_dir.path();
61+
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");
3666
3767 let empty_path = Path::new("");
3868 assert!(
@@ -124,15 +154,12 @@ mod convert { · 85 unmodified lines
124154
125155 // enum Component.Prefix
126156
127− let a = PathBuf::from("a");
128− let mut a = a.components();
129− let _ = a.next();
130−
131− let mut pb = PathBuf::new();
132− pb.push(a.collect::<PathBuf>());
133−
134− assert_eq!(pb, PathBuf::new());
135− assert_eq!(pb.components().next(), None);
157+ let tmp_dir_in = TempDirIn::new("/tmp/a/b/s");
158+ let p = tmp_dir_in.path;
159+ assert!(p.exists());
160+ assert_eq!(p, Path::new("/tmp/a/b/s"));
161+ drop(tmp_dir_in);
162+ assert!(!p.exists());
136163
137164 // pass iterator input_path.components() instead of input_path
138165 // have real_path mut and not return it