Skip to content

Commit bb36266

Browse files
committed
Use OsString again while still changing \ to /
This use `bstr` facilities and `OsString`. The same tests fail on Windows as in the previous commit (the `*_direct` tests), but this approach allows future tests involving unpaired surrogates in the arguments, and unpaired surrogates or other invalid Unicode in the output, to be added later more easily.
1 parent 34a9dab commit bb36266

File tree

1 file changed

+33
-43
lines changed

1 file changed

+33
-43
lines changed

gix-command/tests/command.rs

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -561,40 +561,29 @@ mod spawn {
561561
}
562562

563563
mod script {
564-
use gix_testtools::bstr::ByteSlice;
564+
use std::ffi::{OsStr, OsString};
565565
use std::path::Path;
566566

567-
/// Get the path to a script created by the `scripts.sh` fixture.
568-
///
569-
/// The path uses `/` as a separator. On Windows, it achieves this by replacing each
570-
/// occurrence of `\` with `/`. This does not always preserve usability, and sometimes
571-
/// does not even preserve meaning. In particular, `\\?\` paths would often become less
572-
/// usable (though `std` recognizes `//?/`) and occasionally incorrect (if a component
573-
/// contained a literal `/` on a strange filesystem), and NT object manager paths such as
574-
/// those that begin with `\??\` (which `std` recognizes, when they refer to entries on a
575-
/// filesystem) are always broken. But they might be good enough to use in the test suite.
576-
///
577-
/// This fails if the path is not valid Unicode (as `String` is a bit easier to use here).
578-
fn script_path(filename: impl AsRef<Path>) -> crate::Result<String> {
579-
let path = gix_testtools::scripted_fixture_read_only("scripts.sh")?
567+
use gix_testtools::bstr::{BString, ByteSlice, ByteVec};
568+
569+
fn script_path(filename: impl AsRef<Path>) -> crate::Result<OsString> {
570+
let native_path = gix_testtools::scripted_fixture_read_only("scripts.sh")?
580571
.join(filename)
581-
.to_str()
582-
.map(|p| {
583-
if cfg!(windows) {
584-
p.replace('\\', "/")
585-
} else {
586-
p.to_owned()
587-
}
588-
})
589-
.expect("valid UTF-8");
590-
Ok(path)
572+
.as_os_str()
573+
.as_encoded_bytes()
574+
.as_bstr()
575+
.to_owned();
576+
let unix_path = gix_path::to_unix_separators_on_windows(native_path)
577+
.to_os_str()?
578+
.to_owned();
579+
Ok(unix_path)
591580
}
592581

593-
fn script_stdout_lines(
594-
path: &str,
595-
args: Option<&[&str]>, // Let us test calling vs. not calling `args` (rather than calling with `[]`).
582+
fn script_stdout(
583+
path: impl Into<OsString>,
584+
args: Option<&[&OsStr]>, // Let us test calling vs. not calling `args` (rather than calling with `[]`).
596585
indirect: bool,
597-
) -> crate::Result<Vec<String>> {
586+
) -> crate::Result<BString> {
598587
let mut prep = gix_command::prepare(path);
599588
if indirect {
600589
prep.use_shell = true;
@@ -607,19 +596,19 @@ mod spawn {
607596
let out = prep.spawn()?.wait_with_output()?;
608597
assert!(out.status.success());
609598
assert!(out.stderr.is_empty());
610-
let lines = out
611-
.stdout
612-
.lines()
613-
.map(|line| line.to_str().expect("valid UTF-8"))
614-
.map(ToOwned::to_owned)
615-
.collect();
616-
Ok(lines)
599+
Ok(out.stdout.into())
600+
}
601+
602+
fn concatenate(prefix: &OsString, suffix: &str) -> BString {
603+
let mut buffer = prefix.as_encoded_bytes().as_bstr().to_owned();
604+
buffer.push_str(suffix);
605+
buffer
617606
}
618607

619608
fn do_trivial(indirect: bool) -> crate::Result {
620609
let path = script_path("trivial")?;
621-
let lines = script_stdout_lines(&path, None, indirect)?;
622-
assert_eq!(lines, ["Hello, world!"]);
610+
let stdout = script_stdout(&path, None, indirect)?;
611+
assert_eq!(stdout, "Hello, world!\n");
623612
Ok(())
624613
}
625614

@@ -635,8 +624,9 @@ mod spawn {
635624

636625
fn do_name_no_args(indirect: bool) -> crate::Result {
637626
let path = script_path("name-and-args")?;
638-
let lines = script_stdout_lines(&path, None, indirect)?;
639-
assert_eq!(lines, [path]);
627+
let stdout = script_stdout(&path, None, indirect)?;
628+
let expected = concatenate(&path, "\n");
629+
assert_eq!(stdout, expected);
640630
Ok(())
641631
}
642632

@@ -652,10 +642,10 @@ mod spawn {
652642

653643
fn do_name_with_args(indirect: bool) -> crate::Result {
654644
let path = script_path("name-and-args")?;
655-
let args = ["foo", "bar baz", "quux"];
656-
let expected: Vec<_> = std::iter::once(path.as_str()).chain(args).collect();
657-
let lines = script_stdout_lines(&path, Some(&args), indirect)?;
658-
assert_eq!(lines, expected);
645+
let args = ["foo", "bar baz", "quux"].map(OsStr::new);
646+
let stdout = script_stdout(&path, Some(&args), indirect)?;
647+
let expected = concatenate(&path, "\nfoo\nbar baz\nquux\n");
648+
assert_eq!(stdout, expected);
659649
Ok(())
660650
}
661651

0 commit comments

Comments
 (0)