Skip to content

Commit

Permalink
Fix tests & switch debug_assert_eq! to assert_eq!
Browse files Browse the repository at this point in the history
The reason for the switch to `assert_eq!` is because in both cases there was an
important mutation:

```rust
debug_assert_eq!(sout.pop(), Some(b'\''));
```

That `pop()` was not being called in release builds (try running `cargo test
--release`) and it was affecting correctness.
  • Loading branch information
allenap committed Jun 27, 2024
1 parent 08614d3 commit 3354e17
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Inspired by the Haskell [shell-escape][] package, which is the most
comprehensive implementation of shell escaping I've yet seen.

For now this package implements escaping for `/bin/sh`-like shells, [GNU
Bash][gnu-bash] and [fish][].
Bash][gnu-bash] and [fish][].
Please read the documentation for each module to learn about some limitations
and caveats.

Expand All @@ -30,14 +30,14 @@ assert_eq!(Sh::quote("foobar"), b"foobar");
assert_eq!(Fish::quote("foobar"), b"foobar");
assert_eq!(Bash::quote("foo bar"), b"$'foo bar'");
assert_eq!(Sh::quote("foo bar"), b"'foo bar'");
assert_eq!(Fish::quote("foo bar"), b"'foobar'");
assert_eq!(Fish::quote("foo bar"), b"'foo bar'");
```

It's also possible to use the extension trait [`QuoteRefExt`] which provides a
[`quoted`] function:

```rust
use shell_quote::{Bash, Sh, QuoteRefExt};
use shell_quote::{Bash, Sh, Fish, QuoteRefExt};
let quoted: Vec<u8> = "foo bar".quoted(Bash);
assert_eq!(quoted, b"$'foo bar'");
let quoted: Vec<u8> = "foo bar".quoted(Sh);
Expand Down
8 changes: 4 additions & 4 deletions src/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Fish {
/// ```
/// # use shell_quote::{Fish, Quoter};
/// assert_eq!(Fish::quote("foobar"), b"foobar");
/// assert_eq!(Fish::quote("foo 'bar"), b"'foo \'bar'");
/// assert_eq!(Fish::quote("foo 'bar"), b"'foo \\'bar'");
/// ```
pub fn quote<'a, S: ?Sized + Into<Quotable<'a>>>(s: S) -> Vec<u8> {
let sin: Quotable<'a> = s.into();
Expand All @@ -60,7 +60,7 @@ impl Fish {
/// Fish::quote_into("foobar", &mut buf);
/// buf.push(b' '); // Add a space.
/// Fish::quote_into("foo 'bar", &mut buf);
/// assert_eq!(buf, b"foobar 'foo \'bar'");
/// assert_eq!(buf, b"foobar 'foo \\'bar'");
/// ```
///
pub fn quote_into<'a, S: ?Sized + Into<Quotable<'a>>>(s: S, sout: &mut Vec<u8>) {
Expand Down Expand Up @@ -114,7 +114,7 @@ fn escape_chars(esc: Vec<Char>, sout: &mut Vec<u8>) {
is_there_char_after_last_single_quote = false;
} else {
// Pop the useless single quote
debug_assert_eq!(sout.pop(), Some(b'\''));
assert_eq!(sout.pop(), Some(b'\''));
sout.extend(literal);
sout.push(b'\'');
is_there_char_after_last_single_quote = false;
Expand Down Expand Up @@ -153,6 +153,6 @@ fn escape_chars(esc: Vec<Char>, sout: &mut Vec<u8>) {
sout.push(b'\'');
} else {
// Pop the useless single quote
debug_assert_eq!(sout.pop(), Some(b'\''));
assert_eq!(sout.pop(), Some(b'\''));
}
}

0 comments on commit 3354e17

Please sign in to comment.