File: integration.rs

package info (click to toggle)
rust-datatest-stable 0.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 440 kB
  • sloc: awk: 19; sh: 5; makefile: 2
file content (159 lines) | stat: -rw-r--r-- 7,556 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) The datatest-stable Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

static EXPECTED_LINES: &[&str] = &[
    "datatest-stable::example test_artifact_utf8::dir/a.txt",
    "datatest-stable::example test_artifact_utf8::b.txt",
    "datatest-stable::example test_artifact_utf8::c.skip.txt",
    "datatest-stable::example test_artifact_utf8_abs::dir/a.txt",
    "datatest-stable::example test_artifact_utf8_abs::b.txt",
    "datatest-stable::example test_artifact_utf8_abs::c.skip.txt",
    "datatest-stable::example test_artifact::dir/a.txt",
    "datatest-stable::example test_artifact::b.txt",
    "datatest-stable::example with_contents::test_artifact_bytes::dir/a.txt",
    "datatest-stable::example with_contents::test_artifact_bytes::b.txt",
    "datatest-stable::example with_contents::test_artifact_bytes::c.skip.txt",
    "datatest-stable::example with_contents::test_artifact_string::dir/a.txt",
    "datatest-stable::example with_contents::test_artifact_string::b.txt",
    "datatest-stable::example with_contents::test_artifact_string::c.skip.txt",
    "datatest-stable::example with_contents::test_artifact_utf8_bytes::dir/a.txt",
    "datatest-stable::example with_contents::test_artifact_utf8_bytes::b.txt",
    "datatest-stable::example with_contents::test_artifact_utf8_bytes::c.skip.txt",
    "datatest-stable::example with_contents::test_artifact_bytes::other.json",
    "datatest-stable::example with_contents::test_artifact_utf8_string::dir/a.txt",
    "datatest-stable::example with_contents::test_artifact_utf8_string::b.txt",
    "datatest-stable::example with_contents::test_artifact_utf8_string::c.skip.txt",
];

#[test]
#[ignore = "requires cargo-nextest"]
fn run_example() {
    let output = std::process::Command::new(cargo_bin())
        .args(["nextest", "run", "--test=example", "--color=never"])
        .env("__DATATEST_FULL_SCAN_FORBIDDEN", "1")
        .output()
        .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
    // nextest output will be utf8 and convert it.
    let stderr = std::str::from_utf8(&output.stderr).expect("cargo nextest stderr should be utf-8");

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

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

#[test]
fn ui() {
    let t = trybuild::TestCases::new();
    t.compile_fail("tests/compile-fail/*.rs");
}

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

    static EXPECTED_UNIX_LINES: &[&str] = &[
        "datatest-stable::example test_artifact_utf8::::colon::dir/::.txt",
        "datatest-stable::example test_artifact_utf8::::colon::dir/a.txt",
        "datatest-stable::example test_artifact_utf8::dir/a.txt",
        "datatest-stable::example test_artifact_utf8::b.txt",
        "datatest-stable::example test_artifact_utf8::c.skip.txt",
        "datatest-stable::example test_artifact_utf8_abs::dir/a.txt",
        "datatest-stable::example test_artifact_utf8_abs::b.txt",
        "datatest-stable::example test_artifact_utf8_abs::c.skip.txt",
        "datatest-stable::example test_artifact::::colon::dir/::.txt",
        "datatest-stable::example test_artifact::::colon::dir/a.txt",
        "datatest-stable::example test_artifact::dir/a.txt",
        "datatest-stable::example test_artifact::b.txt",
        "datatest-stable::example with_contents::test_artifact_bytes::::colon::dir/::.txt",
        "datatest-stable::example with_contents::test_artifact_bytes::::colon::dir/a.txt",
        "datatest-stable::example with_contents::test_artifact_bytes::dir/a.txt",
        "datatest-stable::example with_contents::test_artifact_bytes::b.txt",
        "datatest-stable::example with_contents::test_artifact_bytes::c.skip.txt",
        "datatest-stable::example with_contents::test_artifact_string::dir/a.txt",
        "datatest-stable::example with_contents::test_artifact_string::b.txt",
        "datatest-stable::example with_contents::test_artifact_string::c.skip.txt",
        "datatest-stable::example with_contents::test_artifact_utf8_bytes::::colon::dir/::.txt",
        "datatest-stable::example with_contents::test_artifact_utf8_bytes::::colon::dir/a.txt",
        "datatest-stable::example with_contents::test_artifact_utf8_bytes::dir/a.txt",
        "datatest-stable::example with_contents::test_artifact_utf8_bytes::b.txt",
        "datatest-stable::example with_contents::test_artifact_utf8_bytes::c.skip.txt",
        "datatest-stable::example with_contents::test_artifact_bytes::other.json",
        "datatest-stable::example with_contents::test_artifact_utf8_string::::colon::dir/::.txt",
        "datatest-stable::example with_contents::test_artifact_utf8_string::::colon::dir/a.txt",
        "datatest-stable::example with_contents::test_artifact_utf8_string::dir/a.txt",
        "datatest-stable::example with_contents::test_artifact_utf8_string::b.txt",
        "datatest-stable::example with_contents::test_artifact_utf8_string::c.skip.txt",
    ];

    #[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!(
            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("31 tests run: 31 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())
}