Skip to content

Commit 387da88

Browse files
committed
Use String paths and replace \ with /, in new tests
This fixes the two failing `*_indirect` tests on Windows. The three failing `*_direct` tests still fail. This is for a different reason related to the meaning of paths like `/bin/sh` on Windows, which would need to be interpreted differently form their drive-relative meaning as Windows paths, in order to work correctly in `#!` lines.
1 parent 0d1ac8e commit 387da88

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

gix-command/tests/command.rs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -561,22 +561,40 @@ mod spawn {
561561
}
562562

563563
mod script {
564-
use std::ffi::{OsStr, OsString};
565-
use std::path::{Path, PathBuf};
566-
567564
use gix_testtools::bstr::ByteSlice;
568-
569-
fn script_path(filename: impl AsRef<Path>) -> crate::Result<PathBuf> {
570-
let mut path = gix_testtools::scripted_fixture_read_only("scripts.sh")?;
571-
path.push(filename);
565+
use std::path::Path;
566+
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")?
580+
.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");
572590
Ok(path)
573591
}
574592

575593
fn script_stdout_lines(
576-
path: impl Into<OsString>,
577-
args: Option<&[&OsStr]>, // Let us test calling vs. not calling `args` (rather than calling with `[]`).
594+
path: &str,
595+
args: Option<&[&str]>, // Let us test calling vs. not calling `args` (rather than calling with `[]`).
578596
indirect: bool,
579-
) -> crate::Result<Vec<OsString>> {
597+
) -> crate::Result<Vec<String>> {
580598
let mut prep = gix_command::prepare(path);
581599
if indirect {
582600
prep.use_shell = true;
@@ -592,15 +610,15 @@ mod spawn {
592610
let lines = out
593611
.stdout
594612
.lines()
595-
.map(|line| line.to_os_str().expect("valid UTF-8"))
613+
.map(|line| line.to_str().expect("valid UTF-8"))
596614
.map(ToOwned::to_owned)
597615
.collect();
598616
Ok(lines)
599617
}
600618

601619
fn do_trivial(indirect: bool) -> crate::Result {
602620
let path = script_path("trivial")?;
603-
let lines = script_stdout_lines(path, None, indirect)?;
621+
let lines = script_stdout_lines(&path, None, indirect)?;
604622
assert_eq!(lines, ["Hello, world!"]);
605623
Ok(())
606624
}
@@ -634,8 +652,8 @@ mod spawn {
634652

635653
fn do_name_with_args(indirect: bool) -> crate::Result {
636654
let path = script_path("name-and-args")?;
637-
let args = ["foo", "bar baz", "quux"].map(OsStr::new);
638-
let expected: Vec<_> = std::iter::once(path.as_os_str()).chain(args).collect();
655+
let args = ["foo", "bar baz", "quux"];
656+
let expected: Vec<_> = std::iter::once(path.as_str()).chain(args).collect();
639657
let lines = script_stdout_lines(&path, Some(&args), indirect)?;
640658
assert_eq!(lines, expected);
641659
Ok(())

0 commit comments

Comments
 (0)