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
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Top-level application and command routing.
use super::{
EarlyArgs,
common::CommonOpts,
core::{
App, ArchiveApp, ArchiveOpts, BaseApp, BenchOpts, ListOpts, ReplayOpts, RunOpts,
exec_replay,
},
utility::{DebugCommand, SelfCommand, ShowConfigCommand, StoreCommand},
};
use crate::{ExpectedError, Result, output::OutputContext, reuse_build::ReuseBuildOpts};
use clap::{Args, Subcommand};
use guppy::platform::Platform;
use nextest_runner::user_config::UserConfig;
/// A next-generation test runner for Rust.
///
/// This binary should typically be invoked as `cargo nextest` (in which case
/// this message will not be seen), not `cargo-nextest`.
#[derive(Debug, clap::Parser)]
#[command(
version = crate::version::short(),
long_version = crate::version::long(),
bin_name = "cargo",
styles = crate::output::clap_styles::style(),
max_term_width = 100,
)]
pub struct CargoNextestApp {
/// Early args (color, no_pager) flattened at root for early extraction.
#[clap(flatten)]
early_args: EarlyArgs,
#[clap(subcommand)]
subcommand: NextestSubcommand,
}
impl CargoNextestApp {
/// Initializes the output context.
pub fn init_output(&self) -> OutputContext {
match &self.subcommand {
NextestSubcommand::Nextest(args) => args.common.output.init(&self.early_args),
NextestSubcommand::Ntr(args) => args.common.output.init(&self.early_args),
#[cfg(unix)]
// Double-spawned processes should never use coloring.
NextestSubcommand::DoubleSpawn(_) => OutputContext::color_never_init(),
}
}
/// Executes the app.
pub fn exec(
self,
cli_args: Vec<String>,
output: OutputContext,
output_writer: &mut crate::output::OutputWriter,
) -> Result<i32> {
if let Err(err) = nextest_runner::usdt::register_probes() {
tracing::warn!("failed to register USDT probes: {}", err);
}
match self.subcommand {
NextestSubcommand::Nextest(app) => {
app.exec(self.early_args, cli_args, output, output_writer)
}
NextestSubcommand::Ntr(opts) => {
opts.exec(self.early_args, cli_args, output, output_writer)
}
#[cfg(unix)]
NextestSubcommand::DoubleSpawn(opts) => opts.exec(output),
}
}
}
#[derive(Debug, Subcommand)]
enum NextestSubcommand {
/// A next-generation test runner for Rust. <https://nexte.st>.
Nextest(Box<AppOpts>),
/// Build and run tests: a shortcut for `cargo nextest run`.
Ntr(Box<NtrOpts>),
/// Private command, used to double-spawn test processes.
#[cfg(unix)]
#[command(name = nextest_runner::double_spawn::DoubleSpawnInfo::SUBCOMMAND_NAME, hide = true)]
DoubleSpawn(crate::double_spawn::DoubleSpawnOpts),
}
/// Main app options (under `cargo nextest`).
#[derive(Debug, Args)]
#[clap(
version = crate::version::short(),
long_version = crate::version::long(),
display_name = "cargo-nextest",
)]
pub(crate) struct AppOpts {
#[clap(flatten)]
common: CommonOpts,
#[clap(subcommand)]
command: Command,
}
impl AppOpts {
/// Execute the command.
///
/// Returns the exit code.
fn exec(
self,
early_args: EarlyArgs,
cli_args: Vec<String>,
output: OutputContext,
output_writer: &mut crate::output::OutputWriter,
) -> Result<i32> {
match self.command {
Command::List(list_opts) => {
let base = BaseApp::new(
output,
early_args,
list_opts.reuse_build,
list_opts.cargo_options,
self.common.config_opts,
self.common.manifest_path,
output_writer,
)?;
let app = App::new(base, list_opts.build_filter)?;
app.exec_list(list_opts.message_format, list_opts.list_type)?;
Ok(0)
}
Command::Run(run_opts) => {
let base = BaseApp::new(
output,
early_args,
run_opts.reuse_build,
run_opts.cargo_options,
self.common.config_opts,
self.common.manifest_path,
output_writer,
)?;
let app = App::new(base, run_opts.build_filter)?;
app.exec_run(
run_opts.no_capture,
run_opts.rerun.as_ref(),
&run_opts.runner_opts,
&run_opts.reporter_opts,
cli_args,
output_writer,
)?;
Ok(0)
}
Command::Bench(bench_opts) => {
let base = BaseApp::new(
output,
early_args,
ReuseBuildOpts::default(),
bench_opts.cargo_options,
self.common.config_opts,
self.common.manifest_path,
output_writer,
)?;
let app = App::new(base, bench_opts.build_filter)?;
app.exec_bench(
&bench_opts.runner_opts,
&bench_opts.reporter_opts,
cli_args,
output_writer,
)?;
Ok(0)
}
Command::Archive(archive_opts) => {
let app = BaseApp::new(
output,
early_args,
ReuseBuildOpts::default(),
archive_opts.cargo_options,
self.common.config_opts,
self.common.manifest_path,
output_writer,
)?;
let app = ArchiveApp::new(app, archive_opts.archive_build_filter)?;
app.exec_archive(
&archive_opts.archive_file,
archive_opts.archive_format,
archive_opts.zstd_level,
output_writer,
)?;
Ok(0)
}
Command::ShowConfig { command } => command.exec(
early_args,
self.common.manifest_path,
self.common.config_opts,
output,
output_writer,
),
Command::Self_ { command } => command.exec(output),
Command::Debug { command } => command.exec(output),
Command::Replay(replay_opts) => {
exec_replay(&early_args, *replay_opts, self.common.manifest_path, output)
}
Command::Store { command } => {
let host_platform =
Platform::build_target().expect("nextest is built for a supported platform");
let user_config = UserConfig::for_host_platform(
&host_platform,
early_args.user_config_location(),
)
.map_err(|e| ExpectedError::UserConfigError { err: Box::new(e) })?;
command.exec(&early_args, self.common.manifest_path, &user_config, output)
}
}
}
}
/// All commands supported by nextest.
#[derive(Debug, Subcommand)]
pub(crate) enum Command {
/// List tests in workspace.
///
/// This command builds test binaries and queries them for the tests they contain.
///
/// Use --verbose to get more information about tests, including test binary paths and skipped
/// tests.
///
/// Use --message-format json to get machine-readable output.
///
/// For more information, see <https://nexte.st/docs/listing>.
List(Box<ListOpts>),
/// Build and run tests.
///
/// This command builds test binaries and queries them for the tests they contain,
/// then runs each test in parallel.
///
/// For more information, see <https://nexte.st/docs/running>.
#[command(visible_alias = "r")]
Run(Box<RunOpts>),
/// Build and run benchmarks (experimental).
///
/// This command builds benchmark binaries and queries them for the benchmarks they contain,
/// then runs each benchmark **serially**.
///
/// This is an experimental feature. To enable it, set the environment variable
/// `NEXTEST_EXPERIMENTAL_BENCHMARKS=1`.
#[command(visible_alias = "b")]
Bench(Box<BenchOpts>),
/// Build and archive tests.
///
/// This command builds test binaries and archives them to a file. The archive can then be
/// transferred to another machine, and tests within it can be run with `cargo nextest run
/// --archive-file`.
///
/// The archive is a tarball compressed with Zstandard (.tar.zst).
Archive(Box<ArchiveOpts>),
/// Show information about nextest's configuration in this workspace.
///
/// This command shows configuration information about nextest, including overrides applied to
/// individual tests.
///
/// In the future, this will show more information about configurations and overrides.
ShowConfig {
#[clap(subcommand)]
command: ShowConfigCommand,
},
/// Manage the nextest installation.
#[clap(name = "self")]
Self_ {
#[clap(subcommand)]
command: SelfCommand,
},
/// Debug commands.
///
/// The commands in this section are for nextest's own developers and those integrating with it
/// to debug issues. They are not part of the public API and may change at any time.
#[clap(hide = true)]
Debug {
#[clap(subcommand)]
command: DebugCommand,
},
/// Replay a recorded test run (experimental).
///
/// This command replays a recorded test run, displaying events as if the run were happening
/// live.
///
/// This is an experimental feature. To enable it, set the environment variable
/// `NEXTEST_EXPERIMENTAL_RECORD=1`.
#[clap(hide = true)]
Replay(Box<ReplayOpts>),
/// Manage the record store (experimental).
///
/// This command provides operations for managing the record store, such as pruning old runs
/// and showing storage information.
///
/// This is an experimental feature. To enable it, set the environment variable
/// `NEXTEST_EXPERIMENTAL_RECORD=1`.
#[clap(hide = true)]
Store {
#[clap(subcommand)]
command: StoreCommand,
},
}
/// Options for `cargo ntr` (shortcut for `cargo nextest run`).
#[derive(Debug, Args)]
struct NtrOpts {
#[clap(flatten)]
common: CommonOpts,
#[clap(flatten)]
run_opts: RunOpts,
}
impl NtrOpts {
fn exec(
self,
early_args: EarlyArgs,
cli_args: Vec<String>,
output: OutputContext,
output_writer: &mut crate::output::OutputWriter,
) -> Result<i32> {
let base = BaseApp::new(
output,
early_args,
self.run_opts.reuse_build,
self.run_opts.cargo_options,
self.common.config_opts,
self.common.manifest_path,
output_writer,
)?;
let app = App::new(base, self.run_opts.build_filter)?;
app.exec_run(
self.run_opts.no_capture,
self.run_opts.rerun.as_ref(),
&self.run_opts.runner_opts,
&self.run_opts.reporter_opts,
cli_args,
output_writer,
)?;
Ok(0)
}
}