Debian Codegraph @

helix-stdx/tests/path.rs

raw symbols SCIP
1 #![cfg(windows)]
2
3 use std::{env::set_current_dir, error::Error, path::Component};
4
5 use helix_stdx::path;
6 use tempfile::Builder;
7
8 // Paths on Windows are almost always case-insensitive.
9 // Normalization should return the original path.
10 // E.g. mkdir `CaSe`, normalize(`case`) = `CaSe`.
11 #[test]
12 fn test_case_folding_windows() -> Result<(), Box<dyn Error>> {
13 // tmp/root/case
14 let tmp_prefix = std::env::temp_dir();
15 set_current_dir(&tmp_prefix)?;
16
17 let root = Builder::new().prefix("root-").tempdir()?;
18 let case = Builder::new().prefix("CaSe-").tempdir_in(&root)?;
19
20 let root_without_prefix = root.path().strip_prefix(&tmp_prefix)?;
21
22 let lowercase_case = format!(
23 "case-{}",
24 case.path()
25 .file_name()
26 .unwrap()
27 .to_string_lossy()
28 .split_at(5)
29 .1
30 );
31 let test_path = root_without_prefix.join(lowercase_case);
32 assert_eq!(
33 path::normalize(&test_path),
34 case.path().strip_prefix(&tmp_prefix)?
35 );
36
37 Ok(())
38 }
39
40 #[test]
41 fn test_normalize_path() -> Result<(), Box<dyn Error>> {
42 /*
43 tmp/root/
44 ├── link -> dir1/orig_file
45 ├── dir1/
46 │ └── orig_file
47 └── dir2/
48 └── dir_link -> ../dir1/
49 */
50
51 let tmp_prefix = std::env::temp_dir();
52 set_current_dir(&tmp_prefix)?;
53
54 // Create a tree structure as shown above
55 let root = Builder::new().prefix("root-").tempdir()?;
56 let dir1 = Builder::new().prefix("dir1-").tempdir_in(&root)?;
57 let orig_file = Builder::new().prefix("orig_file-").tempfile_in(&dir1)?;
58 let dir2 = Builder::new().prefix("dir2-").tempdir_in(&root)?;
59
60 // Create path and delete existing file
61 let dir_link = Builder::new()
62 .prefix("dir_link-")
63 .tempfile_in(&dir2)?
64 .path()
65 .to_owned();
66 let link = Builder::new()
67 .prefix("link-")
68 .tempfile_in(&root)?
69 .path()
70 .to_owned();
71
72 use std::os::windows;
73 windows::fs::symlink_dir(&dir1, &dir_link)?;
74 windows::fs::symlink_file(&orig_file, &link)?;
75
76 // root/link
77 let path = link.strip_prefix(&tmp_prefix)?;
78 assert_eq!(
79 path::normalize(path),
80 path,
81 "input {:?} and symlink last component shouldn't be resolved",
82 path
83 );
84
85 // root/dir2/dir_link/orig_file/../..
86 let path = dir_link
87 .strip_prefix(&tmp_prefix)
88 .unwrap()
89 .join(orig_file.path().file_name().unwrap())
90 .join(Component::ParentDir)
91 .join(Component::ParentDir);
92 let expected = dir_link
93 .strip_prefix(&tmp_prefix)
94 .unwrap()
95 .join(Component::ParentDir);
96 assert_eq!(
97 path::normalize(&path),
98 expected,
99 "input {:?} and \"..\" should not erase the symlink that goes ahead",
100 &path
101 );
102
103 // root/link/.././../dir2/../
104 let path = link
105 .strip_prefix(&tmp_prefix)
106 .unwrap()
107 .join(Component::ParentDir)
108 .join(Component::CurDir)
109 .join(Component::ParentDir)
110 .join(dir2.path().file_name().unwrap())
111 .join(Component::ParentDir);
112 let expected = link
113 .strip_prefix(&tmp_prefix)
114 .unwrap()
115 .join(Component::ParentDir)
116 .join(Component::ParentDir);
117 assert_eq!(path::normalize(&path), expected, "input {:?}", &path);
118
119 Ok(())
120 }