Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove String support because Sh breaks assumptions re. UTF-8 #18

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"backquote",
"bindir",
"bstr",
"bstring",
"Clippy",
"dollarsign",
"dtolnay",
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ Or the extension trait [`QuoteExt`] for pushing quoted strings into a buffer:

```rust
use shell_quote::{Bash, QuoteExt};
let mut script: String = "echo ".into();
let mut script: bstr::BString = "echo ".into();
script.push_quoted(Bash, "foo bar");
script.push_str(" > ");
script.extend(b" > ");
script.push_quoted(Bash, "/path/(to)/[output]");
assert_eq!(script, "echo $'foo bar' > $'/path/(to)/[output]'");
```
26 changes: 1 addition & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub type Zsh = bash::Bash;

/// Extension trait for pushing shell quoted byte slices, e.g. `&[u8]`, [`&str`]
/// – anything that's [`Quotable`] – into byte container types like [`Vec<u8>`],
/// [`String`], [`OsString`] on Unix, and [`bstr::BString`] if it's enabled
/// [`String`], [`OsString`] on Unix, and [`bstr::BString`] if it's enabled.
pub trait QuoteExt {
fn push_quoted<'a, Q: Quoter, S: ?Sized + Into<Quotable<'a>>>(&mut self, q: Q, s: S);
}
Expand All @@ -63,17 +63,6 @@ impl QuoteExt for bstr::BString {
}
}

impl QuoteExt for String {
fn push_quoted<'a, Q: Quoter, S: ?Sized + Into<Quotable<'a>>>(&mut self, _q: Q, s: S) {
let quoted = Q::quote(s);
// SAFETY: `quoted` is valid UTF-8 (ASCII, in truth) because it was
// generated by a `quote` implementation from this crate –
// enforced because `Quoter` is sealed.
let quoted = unsafe { std::str::from_utf8_unchecked(&quoted) };
self.push_str(quoted);
}
}

// ----------------------------------------------------------------------------

/// Extension trait for shell quoting many different owned and reference types,
Expand Down Expand Up @@ -116,19 +105,6 @@ where
}
}

impl<'a, S> QuoteRefExt<String> for S
where
S: ?Sized + Into<Quotable<'a>>,
{
fn quoted<Q: Quoter>(self, _q: Q) -> String {
let quoted = Q::quote(self);
// SAFETY: `quoted` is valid UTF-8 (ASCII, in truth) because it was
// generated by a `quote` implementation from this crate –
// enforced because `Quoter` is sealed.
unsafe { String::from_utf8_unchecked(quoted) }
}
}

// ----------------------------------------------------------------------------

pub(crate) mod quoter {
Expand Down
16 changes: 10 additions & 6 deletions tests/test_bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ mod bash_impl {
// -- QuoteExt ----------------------------------------------------------------

mod bash_quote_ext {
use super::util::{find_bins, invoke_shell};
use shell_quote::{Bash, QuoteExt};

#[test]
Expand All @@ -131,23 +130,28 @@ mod bash_quote_ext {
assert_eq!(string, "Hello, $'World, Bob, !@#$%^&*(){}[]'");
}

#[cfg(feature = "bstr")]
#[test]
fn test_string_push_quoted_with_bash() {
let mut string: String = "Hello, ".into();
fn test_bstring_push_quoted_with_bash() {
let mut string: bstr::BString = "Hello, ".into();
string.push_quoted(Bash, "World, Bob, !@#$%^&*(){}[]");
assert_eq!(string, "Hello, $'World, Bob, !@#$%^&*(){}[]'");
}

#[cfg(feature = "bstr")]
#[test]
fn test_string_push_quoted_roundtrip() {
let mut script = "printf %s ".to_owned();
fn test_bstring_push_quoted_roundtrip() {
use super::util::{find_bins, invoke_shell};
use bstr::{BString, ByteSlice};
let mut script: BString = "printf %s ".into();
// It doesn't seem possible to roundtrip NUL, probably because it is the
// string terminator character in C.
let string: Vec<_> = (1u8..=u8::MAX).collect();
script.push_quoted(Bash, &string);
let script = script.to_os_str().unwrap();
// Test with every version of `bash` we find on `PATH`.
for bin in find_bins("bash") {
let output = invoke_shell(&bin, script.as_ref()).unwrap();
let output = invoke_shell(&bin, script).unwrap();
assert_eq!(output.stdout, string);
}
}
Expand Down
16 changes: 10 additions & 6 deletions tests/test_fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ mod fish_impl {
// -- QuoteExt ----------------------------------------------------------------

mod fish_quote_ext {
use super::util::{find_bins, invoke_shell};
use shell_quote::{Fish, QuoteExt};

#[test]
Expand All @@ -128,25 +127,30 @@ mod fish_quote_ext {
assert_eq!(string, "Hello, World,' Bob, !@#$%^&*(){}[]'");
}

#[cfg(feature = "bstr")]
#[test]
fn test_string_push_quoted_with_fish() {
let mut string: String = "Hello, ".into();
fn test_bstring_push_quoted_with_fish() {
let mut string: bstr::BString = "Hello, ".into();
string.push_quoted(Fish, "World, Bob, !@#$%^&*(){}[]");
assert_eq!(string, "Hello, World,' Bob, !@#$%^&*(){}[]'");
}

#[cfg(feature = "bstr")]
#[test]
fn test_string_push_quoted_roundtrip() {
fn test_bstring_push_quoted_roundtrip() {
use super::util::{find_bins, invoke_shell};
use bstr::{BString, ByteSlice};
// Unlike many/most other shells, `echo` is safe here because backslash
// escapes are _not_ interpreted by default.
let mut script = "echo -n -- ".to_owned();
let mut script: BString = "echo -n -- ".into();
// It doesn't seem possible to roundtrip NUL, probably because it is the
// string terminator character in C.
let string: Vec<_> = (1u8..=u8::MAX).collect();
script.push_quoted(Fish, &string);
let script = script.to_os_str().unwrap();
// Test with every version of `fish` we find on `PATH`.
for bin in find_bins("fish") {
let output = invoke_shell(&bin, script.as_ref()).unwrap();
let output = invoke_shell(&bin, script).unwrap();
assert_eq!(output.stdout, string);
}
}
Expand Down
16 changes: 10 additions & 6 deletions tests/test_sh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ mod sh_impl {
// -- QuoteExt ----------------------------------------------------------------

mod sh_quote_ext {
use super::util::{find_bins, invoke_shell};
use shell_quote::{QuoteExt, Sh};

#[test]
Expand All @@ -201,23 +200,28 @@ mod sh_quote_ext {
assert_eq!(string, "Hello, World,' Bob, !@#$%^&*(){}[]'");
}

#[cfg(feature = "bstr")]
#[test]
fn test_string_push_quoted() {
let mut string: String = "Hello, ".into();
fn test_bstring_push_quoted() {
let mut string: bstr::BString = "Hello, ".into();
string.push_quoted(Sh, "World, Bob, !@#$%^&*(){}[]");
assert_eq!(string, "Hello, World,' Bob, !@#$%^&*(){}[]'");
}

#[cfg(feature = "bstr")]
#[test]
fn test_string_push_quoted_roundtrip() {
let mut script = "printf %s ".to_owned();
fn test_bstring_push_quoted_roundtrip() {
use super::util::{find_bins, invoke_shell};
use bstr::{BString, ByteSlice};
let mut script: BString = "printf %s ".into();
// It doesn't seem possible to roundtrip NUL, probably because it is the
// string terminator character in C.
let string: Vec<_> = (1..=u8::MAX).collect();
script.push_quoted(Sh, &string);
let script = script.to_os_str().unwrap();
// Test with every version of `sh` we find on `PATH`.
for bin in find_bins("sh") {
let output = invoke_shell(&bin, script.as_ref()).unwrap();
let output = invoke_shell(&bin, script).unwrap();
assert_eq!(output.stdout, string);
}
}
Expand Down
22 changes: 0 additions & 22 deletions tests/test_ux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ fn test_quote_ref_ext_byte_array() {
assert_eq!(OsString::from_vec(b"$'bytes!'".into()), quoted);
let quoted: BString = source.quoted(Bash);
assert_eq!(BString::from(b"$'bytes!'"), quoted);
let quoted: String = source.quoted(Bash);
assert_eq!(String::from("$'bytes!'"), quoted);
}

#[test]
Expand All @@ -62,8 +60,6 @@ fn test_quote_ref_ext_byte_slice() {
assert_eq!(OsString::from_vec(b"$'bytes!'".into()), quoted);
let quoted: BString = source.quoted(Bash);
assert_eq!(BString::from(b"$'bytes!'"), quoted);
let quoted: String = source.quoted(Bash);
assert_eq!(String::from("$'bytes!'"), quoted);
}

#[test]
Expand All @@ -75,8 +71,6 @@ fn test_quote_ref_ext_vec() {
assert_eq!(OsString::from_vec(b"$'vec!'".into()), quoted);
let quoted: BString = source.quoted(Bash);
assert_eq!(BString::from(b"$'vec!'"), quoted);
let quoted: String = source.quoted(Bash);
assert_eq!(String::from("$'vec!'"), quoted);
}

#[test]
Expand All @@ -88,8 +82,6 @@ fn test_quote_ref_ext_os_string() {
assert_eq!(OsString::from_vec(b"$'os-string!'".into()), quoted);
let quoted: BString = source.quoted(Bash);
assert_eq!(BString::from(b"$'os-string!'"), quoted);
let quoted: String = source.quoted(Bash);
assert_eq!(String::from("$'os-string!'"), quoted);
}

#[test]
Expand All @@ -102,8 +94,6 @@ fn test_quote_ref_ext_os_str() {
assert_eq!(OsString::from_vec(b"$'os-str!'".into()), quoted);
let quoted: BString = source.quoted(Bash);
assert_eq!(BString::from(b"$'os-str!'"), quoted);
let quoted: String = source.quoted(Bash);
assert_eq!(String::from("$'os-str!'"), quoted);
}

#[test]
Expand All @@ -115,8 +105,6 @@ fn test_quote_ref_ext_b_string() {
assert_eq!(OsString::from_vec(b"$'b-string!'".into()), quoted);
let quoted: BString = source.quoted(Bash);
assert_eq!(BString::from(b"$'b-string!'"), quoted);
let quoted: String = source.quoted(Bash);
assert_eq!(String::from("$'b-string!'"), quoted);
}

#[test]
Expand All @@ -129,8 +117,6 @@ fn test_quote_ref_ext_b_str() {
assert_eq!(OsString::from_vec(b"$'b-str!'".into()), quoted);
let quoted: BString = source.quoted(Bash);
assert_eq!(BString::from(b"$'b-str!'"), quoted);
let quoted: String = source.quoted(Bash);
assert_eq!(String::from("$'b-str!'"), quoted);
}

#[test]
Expand All @@ -142,8 +128,6 @@ fn test_quote_ref_ext_path_buf() {
assert_eq!(OsString::from_vec(b"$'path-buf!'".into()), quoted);
let quoted: BString = source.quoted(Bash);
assert_eq!(BString::from(b"$'path-buf!'"), quoted);
let quoted: String = source.quoted(Bash);
assert_eq!(String::from("$'path-buf!'"), quoted);
}

#[test]
Expand All @@ -156,8 +140,6 @@ fn test_quote_ref_ext_path() {
assert_eq!(OsString::from_vec(b"$'path!'".into()), quoted);
let quoted: BString = source.quoted(Bash);
assert_eq!(BString::from(b"$'path!'"), quoted);
let quoted: String = source.quoted(Bash);
assert_eq!(String::from("$'path!'"), quoted);
}

#[test]
Expand All @@ -169,8 +151,6 @@ fn test_quote_ref_ext_string() {
assert_eq!(OsString::from_vec(b"$'string!'".into()), quoted);
let quoted: BString = source.quoted(Bash);
assert_eq!(BString::from(b"$'string!'"), quoted);
let quoted: String = source.quoted(Bash);
assert_eq!(String::from("$'string!'"), quoted);
}

#[test]
Expand All @@ -182,6 +162,4 @@ fn test_quote_ref_ext_str() {
assert_eq!(OsString::from_vec(b"$'str!'".into()), quoted);
let quoted: BString = source.quoted(Bash);
assert_eq!(BString::from(b"$'str!'"), quoted);
let quoted: String = source.quoted(Bash);
assert_eq!(String::from("$'str!'"), quoted);
}