1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-16 03:36:18 +00:00

uucore::display: Fix tests

This commit is contained in:
Jan Verbeek 2021-09-01 00:37:21 +02:00
parent 7ca2f4989f
commit 7bf85751b0

View file

@ -60,16 +60,15 @@ pub trait Quotable {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #[macro_use]
/// extern crate uucore;
/// use std::path::Path; /// use std::path::Path;
/// use uucore::display::Quotable; /// use uucore::display::Quotable;
/// use uucore::show_error;
/// ///
/// let foo = Path::new("foo/bar.baz"); /// let foo = Path::new("foo/bar.baz");
/// let bar = Path::new("foo bar"); /// let bar = Path::new("foo bar");
/// ///
/// show_error!("{}: Not found", foo); // Prints "util: foo/bar.baz: Not found" /// show_error!("{}: Not found", foo.maybe_quote()); // Prints "util: foo/bar.baz: Not found"
/// show_error!("{}: Not found", bar); // Prints "util: 'foo bar': Not found" /// show_error!("{}: Not found", bar.maybe_quote()); // Prints "util: 'foo bar': Not found"
/// ``` /// ```
fn maybe_quote(&self) -> Quoted<'_> { fn maybe_quote(&self) -> Quoted<'_> {
let mut quoted = self.quote(); let mut quoted = self.quote();
@ -156,11 +155,9 @@ impl Display for Quoted<'_> {
#[cfg(any(unix, target_os = "wasi"))] #[cfg(any(unix, target_os = "wasi"))]
const SPECIAL_SHELL_CHARS_START: &[char] = &['~', '#', '!']; const SPECIAL_SHELL_CHARS_START: &[char] = &['~', '#', '!'];
// Same deal as before, this is possibly incomplete. // Same deal as before, this is possibly incomplete.
// '-' is included because unlike in Unix, quoting an argument may stop it
// from being recognized as an option. I like that very much.
// A single stand-alone exclamation mark seems to have some special meaning. // A single stand-alone exclamation mark seems to have some special meaning.
#[cfg(windows)] #[cfg(windows)]
const SPECIAL_SHELL_CHARS_START: &[char] = &['~', '#', '@', '-', '!']; const SPECIAL_SHELL_CHARS_START: &[char] = &['~', '#', '@', '!'];
/// Characters that are interpreted specially in a double-quoted string. /// Characters that are interpreted specially in a double-quoted string.
#[cfg(any(unix, target_os = "wasi"))] #[cfg(any(unix, target_os = "wasi"))]
@ -181,6 +178,14 @@ impl Display for Quoted<'_> {
if SPECIAL_SHELL_CHARS_START.contains(&first) { if SPECIAL_SHELL_CHARS_START.contains(&first) {
requires_quote = true; requires_quote = true;
} }
// Unlike in Unix, quoting an argument may stop it
// from being recognized as an option. I like that very much.
// But we don't want to quote "-" because that's a common
// special argument and PowerShell doesn't mind it.
#[cfg(windows)]
if first == '-' && text.len() > 1 {
requires_quote = true;
}
} else { } else {
// Empty string // Empty string
requires_quote = true; requires_quote = true;
@ -431,6 +436,7 @@ mod tests {
("", "''"), ("", "''"),
("foo bar", "'foo bar'"), ("foo bar", "'foo bar'"),
("$foo", "'$foo'"), ("$foo", "'$foo'"),
("-", "-"),
]); ]);
} }