Skip to content

Commit

Permalink
allow checkouts on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshowers committed Apr 25, 2024
1 parent 7feed70 commit 56c334a
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 15 deletions.
77 changes: 77 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ fancy-regex = "0.13.0"
libtest-mimic = "0.7.2"
walkdir = "2.5.0"

[target.'cfg(unix)'.dev-dependencies]
camino-tempfile = "1.1.1"
fs_extra = "1.3.0"

[[test]]
name = "example"
harness = false
Expand Down
10 changes: 10 additions & 0 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use std::{path::Path, process::ExitCode};

#[doc(hidden)]
pub fn runner(requirements: &[Requirements]) -> ExitCode {
if let Some(cwd) = custom_cwd() {
std::env::set_current_dir(&cwd).expect("set custom working directory");
}

let args = Arguments::from_args();

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

/// One of our tests requires that a custom working directory be set. This function is used to do
/// that.
fn custom_cwd() -> Option<Utf8PathBuf> {
std::env::var("__DATATEST_CWD").ok().map(Utf8PathBuf::from)
}

fn find_tests(args: &Arguments, requirements: &[Requirements]) -> Vec<Trial> {
let tests: Vec<_> = if let Some(exact_filter) = exact_filter(args) {
let exact_tests: Vec<_> = requirements
Expand Down
1 change: 0 additions & 1 deletion tests/files/::colon::dir/::.txt

This file was deleted.

1 change: 0 additions & 1 deletion tests/files/::colon::dir/a.txt

This file was deleted.

93 changes: 80 additions & 13 deletions tests/run_example.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Copyright (c) The datatest-stable Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

static EXPECTED_LINES: &[&str] = &[
"datatest-stable::example test_artifact::a.txt",
"datatest-stable::example test_artifact::b.txt",
"datatest-stable::example test_artifact_utf8::a.txt",
"datatest-stable::example test_artifact_utf8::c.skip.txt",
"datatest-stable::example test_artifact_utf8::b.txt",
];

#[test]
fn run_example() {
let output = std::process::Command::new("cargo")
let output = std::process::Command::new(cargo_bin())
.args(["nextest", "run", "--test=example", "--color=never"])
.env("__DATATEST_FULL_SCAN_FORBIDDEN", "1")
.output()
.expect("Failed to run `cargo nextest`");
.expect("`cargo nextest` was successful");

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

assert!(
output.status.success(),
"Command failed (exit status: {}, stderr: {stderr})",
"nextest exited with 0 (exit status: {}, stderr: {stderr})",
output.status
);

let lines: &[&str] = &[
for line in EXPECTED_LINES
.iter()
.copied()
.chain(std::iter::once("5 tests run: 5 passed, 0 skipped"))
{
assert!(
stderr.contains(line),
"Expected to find substring\n {line}\nin stderr\n {stderr}",
);
}
}

#[cfg(unix)]
mod unix {
use super::*;
use camino_tempfile::Utf8TempDir;

static EXPECTED_UNIX_LINES: &[&str] = &[
"datatest-stable::example test_artifact::::colon::dir/::.txt",
"datatest-stable::example test_artifact::::colon::dir/a.txt",
"datatest-stable::example test_artifact::a.txt",
"datatest-stable::example test_artifact_utf8::::colon::dir/::.txt",
"datatest-stable::example test_artifact::b.txt",
"datatest-stable::example test_artifact_utf8::::colon::dir/a.txt",
"datatest-stable::example test_artifact_utf8::a.txt",
"datatest-stable::example test_artifact_utf8::c.skip.txt",
"datatest-stable::example test_artifact_utf8::b.txt",
"9 tests run: 9 passed, 0 skipped",
];

for line in lines {
#[test]
fn run_example_with_colons() {
let temp_dir = Utf8TempDir::with_prefix("datatest-stable").expect("created temp dir");
std::fs::create_dir_all(temp_dir.path().join("tests")).expect("created dir");
let dest = temp_dir.path().join("tests/files");

// Make a copy of tests/files inside the temp dir.
fs_extra::dir::copy(
"tests/files",
temp_dir.path().join("tests"),
&fs_extra::dir::CopyOptions::new(),
)
.expect("copied files");

// Add some files with colons in their names. (They can't be checked into the repo because
// it needs to be cloned on Windows.)
let colon_dir = dest.join("::colon::dir");
std::fs::create_dir_all(&colon_dir).expect("created dir with colons");
std::fs::write(colon_dir.join("::.txt"), b"floop").expect("wrote file with colons");
std::fs::write(colon_dir.join("a.txt"), b"flarp").expect("wrote file with colons");

// Now run the tests.
let output = std::process::Command::new(cargo_bin())
.args(["nextest", "run", "--test=example", "--color=never"])
.env("__DATATEST_FULL_SCAN_FORBIDDEN", "1")
.env("__DATATEST_CWD", temp_dir.path())
.output()
.expect("`cargo nextest` was successful");

let stderr =
std::str::from_utf8(&output.stderr).expect("cargo nextest stderr should be utf-8");

assert!(
stderr.contains(line),
"Expected to find substring\n {line}\nin stderr\n {stderr}",
output.status.success(),
"nextest exited with 0 (exit status: {}, stderr: {stderr})",
output.status
);

for line in EXPECTED_LINES
.iter()
.chain(EXPECTED_UNIX_LINES.iter())
.copied()
.chain(std::iter::once("9 tests run: 9 passed, 0 skipped"))
{
assert!(
stderr.contains(line),
"Expected to find substring\n {line}\nin stderr\n {stderr}",
);
}
}
}

fn cargo_bin() -> String {
std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string())
}

0 comments on commit 56c334a

Please sign in to comment.