Enhanced Windows support, see #70
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Use easier way to convert path components * Fix failing tests on Windows
authored by David Peter · committed by GitHub · parent a84536a · Browse files · 4 files·+24 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+24 added−19 removed
- tests:+5 added−6 removed
- other:+19 added−13 removed
Provenance
- Commit
4731dc6- Tree
4b29904- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
a84536a
4 files changed+24 added−19 removed
modified:src/fshelper/mod.rs+10 added
1 use std::path::{Path, PathBuf};2+use std::io;3 4 /// Get a relative path with respect to a certain base path.5 /// See: https://stackoverflow.com/a/39343127/7048316 pub fn path_relative_from(path: &Path, base: &Path) -> Option<PathBuf> {34 unmodified lines41 }42 Some(comps.iter().map(|c| c.as_os_str()).collect())43 }44 }45+46+pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {47+ let path_buf = path.canonicalize()?;48+49+ #[cfg(windows)]50+ let path_buf = Path::new(path_buf.as_path().to_string_lossy().trim_left_matches(r"\\?\")).to_path_buf();51+52+ Ok(path_buf)53+}modified:src/main.rs+9 added-13 removed
8 unmodified lines9 pub mod lscolors;10 pub mod fshelper;11 mod app;12 13-use std::borrow::Cow;13 use std::env;14 use std::error::Error;15 use std::io::Write;16 use std::ops::Deref;18-#[cfg(target_family = "unix")]17+#[cfg(unix)]18 use std::os::unix::fs::PermissionsExt;20-use std::path::{Component, Path, PathBuf};19+use std::path::{Path, PathBuf};20 use std::process;21 use std::sync::Arc;22 use std::sync::mpsc::channel;23 use std::thread;85 unmodified lines109 Streaming,110 }111 112 /// Root directory113+#[cfg(unix)]114 static ROOT_DIR: &'static str = "/";115 116-/// Parent directory117-static PARENT_DIR: &'static str = "..";116+#[cfg(windows)]117+static ROOT_DIR: &'static str = "";118 119 /// Print a search result to the console.120 fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {121 let path_full = base.join(entry);122 123 let path_str = entry.to_string_lossy();124 125- #[cfg(target_family = "unix")]125+ #[cfg(unix)]126 let is_executable = |p: Option<&std::fs::Metadata>| {127 p.map(|f| f.permissions().mode() & 0o111 != 0)128 .unwrap_or(false)129 };130 131- #[cfg(not(target_family = "unix"))]131+ #[cfg(windows)]132 let is_executable = |_: Option<&std::fs::Metadata>| false;133 134 let stdout = std::io::stdout();135 let mut handle = stdout.lock();8 unmodified lines144 }145 146 // Traverse the path and colorize each component147 for component in entry.components() {148- let comp_str = match component {149- Component::Normal(p) => p.to_string_lossy(),150- Component::ParentDir => Cow::from(PARENT_DIR),151- _ => error("Error: unexpected path component.")152- };148+ let comp_str = component.as_os_str().to_string_lossy();149 150 component_path.push(Path::new(comp_str.deref()));151 152 let metadata = component_path.metadata().ok();215 unmodified lines368 let path = Path::new(rd);369 370 root_dir_is_absolute = path.is_absolute();371 376- path.canonicalize().unwrap_or_else(372+ fshelper::absolute_path(path).unwrap_or_else(373 |_| error(&format!("Error: could not find directory '{}'.", rd))374 )375 } else {376 current_dir_buf.clone()78 unmodified linesmodified:tests/testenv/mod.rs+2 added
45 unmodified lines46 47 #[cfg(unix)]48 unix::fs::symlink(root.join("one/two"), root.join("symlink"))?;49 50+ // Note: creating symlinks on Windows requires the `SeCreateSymbolicLinkPrivilege` which51+ // is by default only granted for administrators.52 #[cfg(windows)]53 windows::fs::symlink_dir(root.join("one/two"), root.join("symlink"))?;54 55 fs::File::create(root.join(".ignore"))?.write_all(b"ignored.foo")?;116 unmodified linesmodified:tests/tests.rs+3 added-6 removed
1 //! Integration tests for the CLI interface of fd.2 3-#![allow(dead_code, unused_imports)]4-3 mod testenv;4 5 use testenv::TestEnv;6 29 unmodified lines36 symlink");37 }38 39 /// Explicit root path42-// TODO: Fails on windows43-#[cfg_attr(windows, ignore)]40 #[test]41 fn test_explicit_root_path() {42 let te = TestEnv::new();43 190 unmodified lines234 symlink");235 }236 237 /// Absolute paths (--absolute-path)242-// TODO: fails on windows243-#[cfg_attr(windows, ignore)]238 #[test]239 fn test_absolute_path() {240 let te = TestEnv::new();241 1 unmodified line243 .canonicalize().expect("absolute path")244 .to_str().expect("string")245 .to_string();246 247+ #[cfg(windows)]248+ let abs_path = abs_path.trim_left_matches(r"\\?\");249+250 te.assert_output(251 &["--absolute-path", "foo"],252 &format!(253 "{abs_path}/a.foo71 unmodified lines