Skip to content

Commit

Permalink
add support for camino Utf8Path and Utf8PathBuf
Browse files Browse the repository at this point in the history
Add support behind a non-default `"camino"` feature.

Closes #90.
  • Loading branch information
sunshowers committed Mar 19, 2021
1 parent e4af344 commit c5c07e9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ edition = "2018"
os_pipe = "0.9.0"
shared_child = "0.3.4"
once_cell = "1.0.1"
camino = { version = "1.0.4", optional = true }

[target.'cfg(unix)'.dependencies]
libc = "0.2.43"

[dev-dependencies]
tempdir = "0.3.7"

[package.metadata.docs.rs]
all-features = true
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
//! - [GitHub repo](https://github.com/oconnor663/duct.rs)
//! - [the same library, in Python](https://github.com/oconnor663/duct.py)
//!
//! Optional features
//! -----------------
//!
//! `camino`: Adds support for [`camino`] paths as the first argument to `cmd!` and `cmd`.
//!
//! [`camino`]: https://docs.rs/camino/1
//!
//! Examples
//! --------
//!
Expand Down Expand Up @@ -1755,6 +1762,30 @@ impl<'a> IntoExecutablePath for &'a PathBuf {
}
}

#[cfg(feature = "camino")]
mod camino_impls {
use super::*;
use camino::{Utf8Path, Utf8PathBuf};

impl<'a> IntoExecutablePath for &'a Utf8Path {
fn to_executable(self) -> OsString {
dotify_relative_exe_path(self.as_ref()).into()
}
}

impl IntoExecutablePath for Utf8PathBuf {
fn to_executable(self) -> OsString {
dotify_relative_exe_path(self.as_ref()).into()
}
}

impl<'a> IntoExecutablePath for &'a Utf8PathBuf {
fn to_executable(self) -> OsString {
dotify_relative_exe_path(self.as_ref()).into()
}
}
}

impl<'a> IntoExecutablePath for &'a str {
fn to_executable(self) -> OsString {
self.into()
Expand Down
44 changes: 43 additions & 1 deletion src/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{cmd, Expression};
use super::{cmd, Expression, ExpressionInner};
use std;
use std::collections::HashMap;
use std::env;
Expand Down Expand Up @@ -610,3 +610,45 @@ fn test_pids() -> io::Result<()> {

Ok(())
}

#[test]
fn test_into_executable_path() {
let expected_undotted: Vec<OsString> = vec!["does-not-exist".into(), "arg".into()];
let expected_dotted: Vec<OsString> = vec![
[".", "does-not-exist"].iter().collect::<PathBuf>().into(),
"arg".into(),
];

// Bare strings are undotted.
let cmd = cmd!("does-not-exist", "arg");
assert!(matches!(&*cmd.0, ExpressionInner::Cmd(x) if x == &expected_undotted));
let cmd = cmd!("does-not-exist".to_string(), "arg");
assert!(matches!(&*cmd.0, ExpressionInner::Cmd(x) if x == &expected_undotted));

// Path and PathBuf are dotted.
let cmd = cmd!(Path::new("does-not-exist"), "arg");
assert!(matches!(&*cmd.0, ExpressionInner::Cmd(x) if x == &expected_dotted));
let cmd = cmd!(PathBuf::from("does-not-exist"), "arg");
assert!(matches!(&*cmd.0, ExpressionInner::Cmd(x) if x == &expected_dotted));
let cmd = cmd!(&PathBuf::from("does-not-exist"), "arg");
assert!(matches!(&*cmd.0, ExpressionInner::Cmd(x) if x == &expected_dotted));
}

#[cfg(feature = "camino")]
#[test]
fn test_into_executable_utf8_path() {
use camino::{Utf8Path, Utf8PathBuf};

let expected_dotted: Vec<OsString> = vec![
[".", "does-not-exist"].iter().collect::<PathBuf>().into(),
"arg".into(),
];

// Utf8Path and Utf8PathBuf are dotted.
let cmd = cmd!(Utf8Path::new("does-not-exist"), "arg");
assert!(matches!(&*cmd.0, ExpressionInner::Cmd(x) if x == &expected_dotted));
let cmd = cmd!(Utf8PathBuf::from("does-not-exist"), "arg");
assert!(matches!(&*cmd.0, ExpressionInner::Cmd(x) if x == &expected_dotted));
let cmd = cmd!(&Utf8PathBuf::from("does-not-exist"), "arg");
assert!(matches!(&*cmd.0, ExpressionInner::Cmd(x) if x == &expected_dotted));
}

0 comments on commit c5c07e9

Please sign in to comment.