Skip to content

Commit

Permalink
Be more specific about UNIX-only tests
Browse files Browse the repository at this point in the history
  • Loading branch information
allenap committed Jul 10, 2024
1 parent a8c4da8 commit c2cb735
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
10 changes: 8 additions & 2 deletions tests/test_bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ mod util;

// -- impl Bash ---------------------------------------------------------------

#[cfg(unix)]
mod bash_impl {
use std::ffi::OsString;
use std::os::unix::ffi::{OsStrExt, OsStringExt};

use super::{
resources,
Expand Down Expand Up @@ -82,11 +80,13 @@ mod bash_impl {
assert_eq!(buffer, b"$'-_=/,.+'");
}

#[cfg(unix)]
#[test_matrix(
(script_bytes, script_text),
("bash", "zsh")
)]
fn test_roundtrip(prepare: fn() -> (OsString, OsString), shell: &str) {
use std::os::unix::ffi::OsStringExt;
let (input, script) = prepare();
for bin in find_bins(shell) {
let output = invoke_shell(&bin, &script).unwrap();
Expand All @@ -95,7 +95,9 @@ mod bash_impl {
}
}

#[cfg(unix)]
fn script_bytes() -> (OsString, OsString) {
use std::os::unix::ffi::{OsStrExt, OsStringExt};
// It doesn't seem possible to roundtrip NUL, probably because it is the
// string terminator character in C.
let input: OsString = OsString::from_vec((1..=u8::MAX).collect());
Expand All @@ -110,7 +112,9 @@ mod bash_impl {
(input, script)
}

#[cfg(unix)]
fn script_text() -> (OsString, OsString) {
use std::os::unix::ffi::OsStringExt;
// NOTE: Do NOT use `echo` here; in most/all shells it interprets
// escapes with no way to disable that behaviour (unlike the `echo`
// builtin in Bash, for example, which accepts a `-E` flag). Using
Expand All @@ -122,8 +126,10 @@ mod bash_impl {
(resources::UTF8_SAMPLE.into(), script)
}

#[cfg(unix)]
#[test_matrix(("bash", "zsh"))]
fn test_roundtrip_utf8_full(shell: &str) {
use std::os::unix::ffi::OsStringExt;
let utf8: Vec<_> = ('\x01'..=char::MAX).collect(); // Not including NUL.
for bin in find_bins(shell) {
// Chunk to avoid over-length arguments (see`getconf ARG_MAX`).
Expand Down
12 changes: 10 additions & 2 deletions tests/test_fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ static FISH_VERSION_UNICODE_FIXED: semver::Version = semver::Version::new(3, 6,

// -- impl Fish ---------------------------------------------------------------

#[cfg(unix)]
mod fish_impl {
use std::ffi::OsString;
use std::os::unix::ffi::{OsStrExt, OsStringExt};

use super::{
resources,
Expand Down Expand Up @@ -129,8 +127,10 @@ mod fish_impl {
assert_eq!(buffer, b"-_'=/,.+'");
}

#[cfg(unix)]
#[test_matrix((script_bytes, script_text))]
fn test_roundtrip(prepare: fn() -> (OsString, OsString)) {
use std::os::unix::ffi::OsStringExt;
let (input, script) = prepare();
// Test with every version of `fish` we find on `PATH`.
for bin in find_bins("fish") {
Expand All @@ -140,7 +140,9 @@ mod fish_impl {
}
}

#[cfg(unix)]
fn script_bytes() -> (OsString, OsString) {
use std::os::unix::ffi::{OsStrExt, OsStringExt};
// It doesn't seem possible to roundtrip NUL, probably because it is the
// string terminator character in C.
let input: OsString = OsString::from_vec((1..=u8::MAX).collect());
Expand All @@ -152,7 +154,9 @@ mod fish_impl {
(input, script)
}

#[cfg(unix)]
fn script_text() -> (OsString, OsString) {
use std::os::unix::ffi::OsStringExt;
// Unlike many/most other shells, `echo` is safe here because backslash
// escapes are _not_ interpreted by default.
let mut script = b"echo -n -- ".to_vec();
Expand All @@ -161,8 +165,10 @@ mod fish_impl {
(resources::UTF8_SAMPLE.into(), script)
}

#[cfg(unix)]
#[test]
fn test_roundtrip_utf8_full() {
use std::os::unix::ffi::OsStringExt;
let utf8: Vec<_> = ('\x01'..=char::MAX).collect(); // Not including NUL.
for bin in find_bins("fish") {
let version = super::fish_version(&bin);
Expand All @@ -183,10 +189,12 @@ mod fish_impl {
}
}

#[cfg(unix)]
#[test]
/// IIRC, this caught bugs not found by `test_roundtrip_utf8_full`, and it
/// was much easier to figure out what the failures meant. For now it stays!
fn test_roundtrip_utf8_full_char_by_char() {
use std::os::unix::ffi::OsStringExt;
let utf8: Vec<_> = ('\x01'..=char::MAX).collect(); // Not including NUL.
for bin in find_bins("fish") {
let version = super::fish_version(&bin);
Expand Down
13 changes: 10 additions & 3 deletions tests/test_sh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub(crate) fn invoke_zsh_as_sh(bin: &Path, script: &OsStr) -> io::Result<Output>

mod sh_impl {
use std::ffi::{OsStr, OsString};
use std::os::unix::ffi::{OsStrExt, OsStringExt};
use std::{io::Result, path::Path, process::Output};

use super::util::{find_bins, invoke_shell};
Expand Down Expand Up @@ -105,6 +104,7 @@ mod sh_impl {

type InvokeShell = fn(&Path, &OsStr) -> Result<Output>;

#[cfg(unix)]
#[test_matrix(
(script_bytes,
script_text),
Expand All @@ -116,6 +116,7 @@ mod sh_impl {
("zsh", invoke_zsh_as_sh))
)]
fn test_roundtrip(prepare: fn() -> (OsString, OsString), (shell, invoke): (&str, InvokeShell)) {
use std::os::unix::ffi::OsStringExt;
let (input, script) = prepare();
for bin in find_bins(shell) {
let output = invoke(&bin, &script).unwrap();
Expand All @@ -124,7 +125,9 @@ mod sh_impl {
}
}

#[cfg(unix)]
fn script_bytes() -> (OsString, OsString) {
use std::os::unix::ffi::{OsStrExt, OsStringExt};
// It doesn't seem possible to roundtrip NUL, probably because it is the
// string terminator character in C.
let input: OsString = OsString::from_vec((1..=u8::MAX).collect());
Expand All @@ -139,7 +142,9 @@ mod sh_impl {
(input, script)
}

#[cfg(unix)]
fn script_text() -> (OsString, OsString) {
use std::os::unix::ffi::OsStringExt;
// NOTE: Do NOT use `echo` here; in most/all shells it interprets
// escapes with no way to disable that behaviour (unlike the `echo`
// builtin in Bash, for example, which accepts a `-E` flag). Using
Expand All @@ -152,6 +157,7 @@ mod sh_impl {
(input, script)
}

#[cfg(unix)]
#[test_matrix(
(("sh", invoke_shell),
("dash", invoke_shell),
Expand All @@ -161,6 +167,7 @@ mod sh_impl {
("zsh", invoke_zsh_as_sh))
)]
fn test_roundtrip_utf8_full((shell, invoke): (&str, InvokeShell)) {
use std::os::unix::ffi::OsStringExt;
let utf8: Vec<_> = ('\x01'..=char::MAX).collect(); // Not including NUL.
for bin in find_bins(shell) {
// Chunk to avoid over-length arguments (see`getconf ARG_MAX`).
Expand All @@ -180,6 +187,8 @@ mod sh_impl {
// -- QuoteExt ----------------------------------------------------------------

mod sh_quote_ext {
use std::ffi::OsString;

use shell_quote::{QuoteExt, Sh};

#[test]
Expand All @@ -193,8 +202,6 @@ mod sh_quote_ext {
#[cfg(unix)]
#[test]
fn test_os_string_push_quoted() {
use std::ffi::OsString;

let mut buffer: OsString = "Hello, ".into();
buffer.push_quoted(Sh, "World, Bob, !@#$%^&*(){}[]");
let string = buffer.into_string().unwrap(); // -> test failures are more readable.
Expand Down

0 comments on commit c2cb735

Please sign in to comment.