-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
mod.rs
253 lines (230 loc) · 8.42 KB
/
mod.rs
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use std::{
env::{self, consts::EXE_SUFFIX},
ffi::OsStr,
path::{Path, PathBuf},
process::{Command, ExitStatus},
sync::OnceLock,
};
use anyhow::{Context as _, Result};
pub use build_context::TARGET;
use easy_ext::ext;
use fs_err as fs;
use tempfile::TempDir;
use walkdir::WalkDir;
pub fn fixtures_path() -> &'static Path {
Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures"))
}
pub fn cargo_bin_exe() -> Command {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_cargo-hack"));
cmd.env("CARGO_HACK_DENY_WARNINGS", "true");
cmd.env_remove("RUSTFLAGS");
cmd.env_remove("CARGO_TERM_COLOR");
cmd
}
fn test_toolchain() -> String {
if let Some(toolchain) = test_version() {
format!("+1.{toolchain} ")
} else {
String::new()
}
}
fn test_version() -> Option<u32> {
static TEST_VERSION: OnceLock<Option<u32>> = OnceLock::new();
*TEST_VERSION.get_or_init(|| {
let toolchain =
env::var_os("CARGO_HACK_TEST_TOOLCHAIN")?.to_string_lossy().parse().unwrap();
// Install toolchain first to avoid toolchain installation conflicts.
let _ = Command::new("rustup")
.args(["toolchain", "add", &format!("1.{toolchain}"), "--no-self-update"])
.output();
Some(toolchain)
})
}
pub fn has_stable_toolchain() -> bool {
static HAS_STABLE_TOOLCHAIN: OnceLock<Option<bool>> = OnceLock::new();
HAS_STABLE_TOOLCHAIN
.get_or_init(|| {
let output = Command::new("rustup").args(["toolchain", "list"]).output().ok()?;
Some(String::from_utf8(output.stdout).ok()?.contains("stable"))
})
.unwrap_or_default()
}
pub fn cargo_hack<O: AsRef<OsStr>>(args: impl AsRef<[O]>) -> Command {
let args = args.as_ref();
let mut cmd = cargo_bin_exe();
cmd.arg("hack");
if let Some(toolchain) = test_version() {
if !args.iter().any(|a| a.as_ref().to_str().unwrap().starts_with("--version-range")) {
cmd.arg(format!("--version-range=1.{toolchain}..=1.{toolchain}"));
}
}
cmd.args(args);
cmd
}
#[ext(CommandExt)]
impl Command {
#[track_caller]
pub fn assert_output(&mut self, test_model: &str, require: Option<u32>) -> AssertOutput {
match (test_version(), require) {
(Some(toolchain), Some(require)) if require > toolchain => {
return AssertOutput(None);
}
_ => {}
}
let (_test_project, cur_dir) = test_project(test_model).unwrap();
let output =
self.current_dir(cur_dir).output().context("could not execute process").unwrap();
AssertOutput(Some(AssertOutputInner {
stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
status: output.status,
}))
}
#[track_caller]
pub fn assert_success(&mut self, test_model: &str) -> AssertOutput {
self.assert_success2(test_model, None)
}
#[track_caller]
pub fn assert_success2(&mut self, test_model: &str, require: Option<u32>) -> AssertOutput {
let output = self.assert_output(test_model, require);
if let Some(output) = &output.0 {
if !output.status.success() {
panic!(
"assertion failed: `self.status.success()`:\n\nSTDOUT:\n{0}\n{1}\n{0}\n\nSTDERR:\n{0}\n{2}\n{0}\n",
"-".repeat(60),
output.stdout,
output.stderr,
);
}
}
output
}
#[track_caller]
pub fn assert_failure(&mut self, test_model: &str) -> AssertOutput {
self.assert_failure2(test_model, None)
}
#[track_caller]
pub fn assert_failure2(&mut self, test_model: &str, require: Option<u32>) -> AssertOutput {
let output = self.assert_output(test_model, require);
if let Some(output) = &output.0 {
if output.status.success() {
panic!(
"assertion failed: `!self.status.success()`:\n\nSTDOUT:\n{0}\n{1}\n{0}\n\nSTDERR:\n{0}\n{2}\n{0}\n",
"-".repeat(60),
output.stdout,
output.stderr,
);
}
}
output
}
}
pub struct AssertOutput(Option<AssertOutputInner>);
struct AssertOutputInner {
stdout: String,
stderr: String,
status: ExitStatus,
}
fn replace_command(lines: &str) -> String {
if lines.contains("cargo +") || lines.contains(&format!("cargo{EXE_SUFFIX} +")) {
lines.to_owned()
} else {
lines.replace("cargo ", &format!("cargo {}", test_toolchain()))
}
}
fn line_separated(lines: &str) -> impl Iterator<Item = &'_ str> {
lines.split('\n').map(str::trim).filter(|line| !line.is_empty())
}
impl AssertOutput {
/// Receives a line(`\n`)-separated list of patterns and asserts whether stderr contains each pattern.
#[track_caller]
pub fn stderr_contains(&self, pats: impl AsRef<str>) -> &Self {
if let Some(output) = &self.0 {
for pat in line_separated(&replace_command(pats.as_ref())) {
if !output.stderr.contains(pat) {
panic!(
"assertion failed: `self.stderr.contains(..)`:\n\nEXPECTED:\n{0}\n{pat}\n{0}\n\nACTUAL:\n{0}\n{1}\n{0}\n",
"-".repeat(60),
output.stderr
);
}
}
}
self
}
/// Receives a line(`\n`)-separated list of patterns and asserts whether stdout contains each pattern.
#[track_caller]
pub fn stderr_not_contains(&self, pats: impl AsRef<str>) -> &Self {
if let Some(output) = &self.0 {
for pat in line_separated(&replace_command(pats.as_ref())) {
if output.stderr.contains(pat) {
panic!(
"assertion failed: `!self.stderr.contains(..)`:\n\nEXPECTED:\n{0}\n{pat}\n{0}\n\nACTUAL:\n{0}\n{1}\n{0}\n",
"-".repeat(60),
output.stderr
);
}
}
}
self
}
/// Receives a line(`\n`)-separated list of patterns and asserts whether stdout contains each pattern.
#[track_caller]
pub fn stdout_contains(&self, pats: impl AsRef<str>) -> &Self {
if let Some(output) = &self.0 {
for pat in line_separated(&replace_command(pats.as_ref())) {
if !output.stdout.contains(pat) {
panic!(
"assertion failed: `self.stdout.contains(..)`:\n\nEXPECTED:\n{0}\n{pat}\n{0}\n\nACTUAL:\n{0}\n{1}\n{0}\n",
"-".repeat(60),
output.stdout
);
}
}
}
self
}
/// Receives a line(`\n`)-separated list of patterns and asserts whether stdout contains each pattern.
#[track_caller]
pub fn stdout_not_contains(&self, pats: impl AsRef<str>) -> &Self {
if let Some(output) = &self.0 {
for pat in line_separated(&replace_command(pats.as_ref())) {
if output.stdout.contains(pat) {
panic!(
"assertion failed: `!self.stdout.contains(..)`:\n\nEXPECTED:\n{0}\n{pat}\n{0}\n\nACTUAL:\n{0}\n{1}\n{0}\n",
"-".repeat(60),
output.stdout
);
}
}
}
self
}
}
fn test_project(model: &str) -> Result<(TempDir, PathBuf)> {
let tmpdir = tempfile::tempdir()?;
let tmpdir_path = tmpdir.path();
let model_path;
let workspace_root;
if model.contains('/') {
let mut model = model.splitn(2, '/');
model_path = fixtures_path().join(model.next().unwrap());
workspace_root = tmpdir_path.join(model.next().unwrap());
assert!(model.next().is_none());
} else {
model_path = fixtures_path().join(model);
workspace_root = tmpdir_path.to_path_buf();
}
for entry in WalkDir::new(&model_path).into_iter().filter_map(Result::ok) {
let path = entry.path();
let tmp_path = &tmpdir_path.join(path.strip_prefix(&model_path)?);
if !tmp_path.exists() {
if path.is_dir() {
fs::create_dir_all(tmp_path)?;
} else {
fs::copy(path, tmp_path)?;
}
}
}
Ok((tmpdir, workspace_root))
}