Canonicalizes path given by expanding the path given
(os: &Os, path_as_str: &str)
| 152 | |
| 153 | /// Canonicalizes path given by expanding the path given |
| 154 | pub fn canonicalizes_path(os: &Os, path_as_str: &str) -> Result<String> { |
| 155 | let context = |input: &str| Ok(os.env.get(input).ok()); |
| 156 | let home_dir_fn = || os.env.home().map(|p| p.to_string_lossy().to_string()); |
| 157 | |
| 158 | let expanded = shellexpand::full_with_context(path_as_str, home_dir_fn, context)?; |
| 159 | let path_buf = if !expanded.starts_with("/") { |
| 160 | let current_dir = os.env.current_dir()?; |
| 161 | current_dir.join(expanded.as_ref() as &str) |
| 162 | } else { |
| 163 | PathBuf::from(expanded.as_ref() as &str) |
| 164 | }; |
| 165 | |
| 166 | match path_buf.canonicalize() { |
| 167 | Ok(normalized) => Ok(normalized.as_path().to_string_lossy().to_string()), |
| 168 | Err(_) => { |
| 169 | let normalized = normalize_path(&path_buf); |
| 170 | Ok(normalized.to_string_lossy().to_string()) |
| 171 | }, |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /// Manually normalize a path by resolving . and .. components |
| 176 | fn normalize_path(path: &std::path::Path) -> std::path::PathBuf { |
no test coverage detected