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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use crate::{
cargo_cli::{CargoCli, CargoOptions},
output::{OutputContext, OutputOpts},
ExpectedError,
};
use camino::{Utf8Path, Utf8PathBuf};
use clap::{Args, Parser, Subcommand};
use color_eyre::eyre::{Report, Result, WrapErr};
use guppy::graph::PackageGraph;
use nextest_runner::{
config::NextestConfig,
partition::PartitionerBuilder,
reporter::{StatusLevel, TestOutputDisplay, TestReporterBuilder},
runner::TestRunnerBuilder,
signal::SignalHandler,
test_filter::{RunIgnored, TestFilterBuilder},
test_list::{OutputFormat, RustTestArtifact, TestList},
};
use std::io::Cursor;
use supports_color::Stream;
#[derive(Debug, Parser)]
#[clap(author, version, bin_name = "cargo")]
pub struct CargoNextestApp {
#[clap(subcommand)]
subcommand: NextestSubcommand,
}
impl CargoNextestApp {
pub fn exec(self) -> Result<()> {
let NextestSubcommand::Nextest(app) = self.subcommand;
app.exec()
}
}
#[derive(Debug, Subcommand)]
enum NextestSubcommand {
Nextest(AppImpl),
}
#[derive(Debug, Args)]
struct AppImpl {
#[clap(long, global = true, value_name = "PATH")]
manifest_path: Option<Utf8PathBuf>,
#[clap(flatten)]
output: OutputOpts,
#[clap(flatten)]
config_opts: ConfigOpts,
#[clap(subcommand)]
command: Command,
}
#[derive(Debug, Args)]
struct ConfigOpts {
#[clap(long, global = true, value_name = "PATH")]
pub config_file: Option<Utf8PathBuf>,
}
impl ConfigOpts {
pub fn make_config(&self, workspace_root: &Utf8Path) -> Result<NextestConfig, ExpectedError> {
NextestConfig::from_sources(workspace_root, self.config_file.as_deref())
.map_err(ExpectedError::config_parse_error)
}
}
#[derive(Debug, Subcommand)]
enum Command {
List {
#[clap(flatten)]
build_filter: TestBuildFilter,
#[clap(short = 'T', long, default_value_t, possible_values = OutputFormat::variants(), help_heading = "OUTPUT OPTIONS")]
format: OutputFormat,
},
Run {
#[clap(long, short = 'P')]
profile: Option<String>,
#[clap(
long,
alias = "nocapture",
help_heading = "RUNNER OPTIONS",
display_order = 100
)]
no_capture: bool,
#[clap(flatten)]
build_filter: TestBuildFilter,
#[clap(flatten)]
runner_opts: TestRunnerOpts,
#[clap(flatten)]
reporter_opts: TestReporterOpts,
},
}
#[derive(Debug, Args)]
#[clap(help_heading = "FILTER OPTIONS")]
struct TestBuildFilter {
#[clap(flatten)]
cargo_options: CargoOptions,
#[clap(long, possible_values = RunIgnored::variants(), default_value_t, value_name = "WHICH")]
run_ignored: RunIgnored,
#[clap(long)]
partition: Option<PartitionerBuilder>,
#[clap(name = "FILTERS", help_heading = None)]
filter: Vec<String>,
}
impl TestBuildFilter {
fn compute<'g>(&self, graph: &'g PackageGraph, output: OutputContext) -> Result<TestList<'g>> {
let manifest_path = graph.workspace().root().join("Cargo.toml");
let mut cargo_cli = CargoCli::new("test", Some(&manifest_path), output);
cargo_cli.add_args(["--no-run", "--message-format", "json-render-diagnostics"]);
cargo_cli.add_options(&self.cargo_options);
let expression = cargo_cli.to_expression();
let output = expression
.stdout_capture()
.unchecked()
.run()
.wrap_err("failed to build tests")?;
if !output.status.success() {
return Err(Report::new(ExpectedError::build_failed(
cargo_cli.all_args(),
output.status.code(),
)));
}
let test_artifacts = RustTestArtifact::from_messages(graph, Cursor::new(output.stdout))?;
let test_filter =
TestFilterBuilder::new(self.run_ignored, self.partition.clone(), &self.filter);
TestList::new(test_artifacts, &test_filter).wrap_err("error building test list")
}
}
#[derive(Debug, Default, Args)]
#[clap(help_heading = "RUNNER OPTIONS")]
pub struct TestRunnerOpts {
#[clap(
long,
short = 'j',
visible_alias = "jobs",
value_name = "THREADS",
conflicts_with = "no-capture"
)]
test_threads: Option<usize>,
#[clap(long)]
retries: Option<usize>,
#[clap(long)]
fail_fast: bool,
#[clap(long, overrides_with = "fail-fast")]
no_fail_fast: bool,
}
impl TestRunnerOpts {
fn to_builder(&self, no_capture: bool) -> TestRunnerBuilder {
let mut builder = TestRunnerBuilder::default();
builder.set_no_capture(no_capture);
if let Some(retries) = self.retries {
builder.set_retries(retries);
}
if self.no_fail_fast {
builder.set_fail_fast(false);
} else if self.fail_fast {
builder.set_fail_fast(true);
}
if let Some(test_threads) = self.test_threads {
builder.set_test_threads(test_threads);
}
builder
}
}
#[derive(Debug, Default, Args)]
#[clap(help_heading = "REPORTER OPTIONS")]
struct TestReporterOpts {
#[clap(
long,
possible_values = TestOutputDisplay::variants(),
conflicts_with = "no-capture",
value_name = "WHEN"
)]
failure_output: Option<TestOutputDisplay>,
#[clap(
long,
possible_values = TestOutputDisplay::variants(),
conflicts_with = "no-capture",
value_name = "WHEN"
)]
success_output: Option<TestOutputDisplay>,
#[clap(long, possible_values = StatusLevel::variants(), value_name = "LEVEL")]
status_level: Option<StatusLevel>,
}
impl TestReporterOpts {
fn to_builder(&self, no_capture: bool) -> TestReporterBuilder {
let mut builder = TestReporterBuilder::default();
builder.set_no_capture(no_capture);
if let Some(failure_output) = self.failure_output {
builder.set_failure_output(failure_output);
}
if let Some(success_output) = self.success_output {
builder.set_success_output(success_output);
}
if let Some(status_level) = self.status_level {
builder.set_status_level(status_level);
}
builder
}
}
impl AppImpl {
fn exec(self) -> Result<()> {
let output = self.output.init();
let graph = build_graph(self.manifest_path.as_deref(), output)?;
match self.command {
Command::List {
build_filter,
format,
} => {
let mut test_list = build_filter.compute(&graph, output)?;
if output.color.should_colorize(Stream::Stdout) {
test_list.colorize();
}
let stdout = std::io::stdout();
let lock = stdout.lock();
test_list.write(format, lock)?;
}
Command::Run {
ref profile,
no_capture,
ref build_filter,
ref runner_opts,
ref reporter_opts,
} => {
let config = self.config_opts.make_config(graph.workspace().root())?;
let profile = config
.profile(profile.as_deref().unwrap_or(NextestConfig::DEFAULT_PROFILE))
.map_err(ExpectedError::profile_not_found)?;
let store_dir = profile.store_dir();
std::fs::create_dir_all(&store_dir)
.wrap_err_with(|| format!("failed to create store dir '{}'", store_dir))?;
let test_list = build_filter.compute(&graph, output)?;
let mut reporter = reporter_opts
.to_builder(no_capture)
.build(&test_list, &profile);
if output.color.should_colorize(Stream::Stderr) {
reporter.colorize();
}
let handler = SignalHandler::new().wrap_err("failed to set up Ctrl-C handler")?;
let runner = runner_opts
.to_builder(no_capture)
.build(&test_list, &profile, handler);
let stderr = std::io::stderr();
let run_stats = runner.try_execute(|event| {
let lock = stderr.lock();
reporter.report_event(event, lock)
})?;
if !run_stats.is_success() {
return Err(Report::new(ExpectedError::test_run_failed()));
}
}
}
Ok(())
}
}
fn build_graph(manifest_path: Option<&Utf8Path>, output: OutputContext) -> Result<PackageGraph> {
let mut cargo_cli = CargoCli::new("metadata", manifest_path, output);
cargo_cli.add_args(["--format-version=1", "--all-features", "--no-deps"]);
let output = cargo_cli
.to_expression()
.stdout_capture()
.unchecked()
.run()
.wrap_err("cargo metadata execution failed")?;
if !output.status.success() {
return Err(ExpectedError::cargo_metadata_failed().into());
}
let json =
String::from_utf8(output.stdout).wrap_err("cargo metadata output is invalid UTF-8")?;
Ok(guppy::CargoMetadata::parse_json(&json)?.build_graph()?)
}