Skip to content

Commit 56c334a

Browse files
committed
allow checkouts on Windows
1 parent 7feed70 commit 56c334a

File tree

6 files changed

+171
-15
lines changed

6 files changed

+171
-15
lines changed

Cargo.lock

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ fancy-regex = "0.13.0"
1717
libtest-mimic = "0.7.2"
1818
walkdir = "2.5.0"
1919

20+
[target.'cfg(unix)'.dev-dependencies]
21+
camino-tempfile = "1.1.1"
22+
fs_extra = "1.3.0"
23+
2024
[[test]]
2125
name = "example"
2226
harness = false

src/runner.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ use std::{path::Path, process::ExitCode};
88

99
#[doc(hidden)]
1010
pub fn runner(requirements: &[Requirements]) -> ExitCode {
11+
if let Some(cwd) = custom_cwd() {
12+
std::env::set_current_dir(&cwd).expect("set custom working directory");
13+
}
14+
1115
let args = Arguments::from_args();
1216

1317
let tests = find_tests(&args, requirements);
@@ -23,6 +27,12 @@ pub fn runner(requirements: &[Requirements]) -> ExitCode {
2327
conclusion.exit_code()
2428
}
2529

30+
/// One of our tests requires that a custom working directory be set. This function is used to do
31+
/// that.
32+
fn custom_cwd() -> Option<Utf8PathBuf> {
33+
std::env::var("__DATATEST_CWD").ok().map(Utf8PathBuf::from)
34+
}
35+
2636
fn find_tests(args: &Arguments, requirements: &[Requirements]) -> Vec<Trial> {
2737
let tests: Vec<_> = if let Some(exact_filter) = exact_filter(args) {
2838
let exact_tests: Vec<_> = requirements

tests/files/::colon::dir/::.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/files/::colon::dir/a.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/run_example.rs

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// Copyright (c) The datatest-stable Contributors
22
// SPDX-License-Identifier: MIT OR Apache-2.0
33

4+
static EXPECTED_LINES: &[&str] = &[
5+
"datatest-stable::example test_artifact::a.txt",
6+
"datatest-stable::example test_artifact::b.txt",
7+
"datatest-stable::example test_artifact_utf8::a.txt",
8+
"datatest-stable::example test_artifact_utf8::c.skip.txt",
9+
"datatest-stable::example test_artifact_utf8::b.txt",
10+
];
11+
412
#[test]
513
fn run_example() {
6-
let output = std::process::Command::new("cargo")
14+
let output = std::process::Command::new(cargo_bin())
715
.args(["nextest", "run", "--test=example", "--color=never"])
816
.env("__DATATEST_FULL_SCAN_FORBIDDEN", "1")
917
.output()
10-
.expect("Failed to run `cargo nextest`");
18+
.expect("`cargo nextest` was successful");
1119

1220
// It's a pain to make assertions on byte slices (even a subslice check isn't easy)
1321
// and it's also difficult to print nice error messages. So we just assume all
@@ -16,27 +24,86 @@ fn run_example() {
1624

1725
assert!(
1826
output.status.success(),
19-
"Command failed (exit status: {}, stderr: {stderr})",
27+
"nextest exited with 0 (exit status: {}, stderr: {stderr})",
2028
output.status
2129
);
2230

23-
let lines: &[&str] = &[
31+
for line in EXPECTED_LINES
32+
.iter()
33+
.copied()
34+
.chain(std::iter::once("5 tests run: 5 passed, 0 skipped"))
35+
{
36+
assert!(
37+
stderr.contains(line),
38+
"Expected to find substring\n {line}\nin stderr\n {stderr}",
39+
);
40+
}
41+
}
42+
43+
#[cfg(unix)]
44+
mod unix {
45+
use super::*;
46+
use camino_tempfile::Utf8TempDir;
47+
48+
static EXPECTED_UNIX_LINES: &[&str] = &[
2449
"datatest-stable::example test_artifact::::colon::dir/::.txt",
2550
"datatest-stable::example test_artifact::::colon::dir/a.txt",
26-
"datatest-stable::example test_artifact::a.txt",
2751
"datatest-stable::example test_artifact_utf8::::colon::dir/::.txt",
28-
"datatest-stable::example test_artifact::b.txt",
2952
"datatest-stable::example test_artifact_utf8::::colon::dir/a.txt",
30-
"datatest-stable::example test_artifact_utf8::a.txt",
31-
"datatest-stable::example test_artifact_utf8::c.skip.txt",
32-
"datatest-stable::example test_artifact_utf8::b.txt",
33-
"9 tests run: 9 passed, 0 skipped",
3453
];
3554

36-
for line in lines {
55+
#[test]
56+
fn run_example_with_colons() {
57+
let temp_dir = Utf8TempDir::with_prefix("datatest-stable").expect("created temp dir");
58+
std::fs::create_dir_all(temp_dir.path().join("tests")).expect("created dir");
59+
let dest = temp_dir.path().join("tests/files");
60+
61+
// Make a copy of tests/files inside the temp dir.
62+
fs_extra::dir::copy(
63+
"tests/files",
64+
temp_dir.path().join("tests"),
65+
&fs_extra::dir::CopyOptions::new(),
66+
)
67+
.expect("copied files");
68+
69+
// Add some files with colons in their names. (They can't be checked into the repo because
70+
// it needs to be cloned on Windows.)
71+
let colon_dir = dest.join("::colon::dir");
72+
std::fs::create_dir_all(&colon_dir).expect("created dir with colons");
73+
std::fs::write(colon_dir.join("::.txt"), b"floop").expect("wrote file with colons");
74+
std::fs::write(colon_dir.join("a.txt"), b"flarp").expect("wrote file with colons");
75+
76+
// Now run the tests.
77+
let output = std::process::Command::new(cargo_bin())
78+
.args(["nextest", "run", "--test=example", "--color=never"])
79+
.env("__DATATEST_FULL_SCAN_FORBIDDEN", "1")
80+
.env("__DATATEST_CWD", temp_dir.path())
81+
.output()
82+
.expect("`cargo nextest` was successful");
83+
84+
let stderr =
85+
std::str::from_utf8(&output.stderr).expect("cargo nextest stderr should be utf-8");
86+
3787
assert!(
38-
stderr.contains(line),
39-
"Expected to find substring\n {line}\nin stderr\n {stderr}",
88+
output.status.success(),
89+
"nextest exited with 0 (exit status: {}, stderr: {stderr})",
90+
output.status
4091
);
92+
93+
for line in EXPECTED_LINES
94+
.iter()
95+
.chain(EXPECTED_UNIX_LINES.iter())
96+
.copied()
97+
.chain(std::iter::once("9 tests run: 9 passed, 0 skipped"))
98+
{
99+
assert!(
100+
stderr.contains(line),
101+
"Expected to find substring\n {line}\nin stderr\n {stderr}",
102+
);
103+
}
41104
}
42105
}
106+
107+
fn cargo_bin() -> String {
108+
std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string())
109+
}

0 commit comments

Comments
 (0)