Skip to content

Commit b1dd772

Browse files
committed
[nextest-runner] combine CargoConfigsConstructError and CargoConfigSearchError
We're going to need this once we start reading CLI arguments as files in an upcoming commit.
1 parent 6ca1aa7 commit b1dd772

File tree

4 files changed

+60
-72
lines changed

4 files changed

+60
-72
lines changed

cargo-nextest/src/dispatch.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,7 @@ impl App {
924924
fn new(base: BaseApp, build_filter: TestBuildFilter) -> Result<Self> {
925925
check_experimental_filtering(base.output);
926926

927-
let cargo_configs = CargoConfigs::new(&base.cargo_opts.config)
928-
.map_err(|err| ExpectedError::CargoConfigsConstructError { err })?;
927+
let cargo_configs = CargoConfigs::new(&base.cargo_opts.config)?;
929928

930929
Ok(Self {
931930
base,

cargo-nextest/src/errors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ pub enum ExpectedError {
4949
#[source]
5050
err: std::io::Error,
5151
},
52-
#[error("cargo configs construction error")]
53-
CargoConfigsConstructError {
52+
#[error("cargo config error")]
53+
CargoConfigError {
5454
#[from]
55-
err: CargoConfigsConstructError,
55+
err: CargoConfigError,
5656
},
5757
#[error("config parse error")]
5858
ConfigParseError {
@@ -292,7 +292,7 @@ impl ExpectedError {
292292
Self::ProfileNotFound { .. }
293293
| Self::StoreDirCreateError { .. }
294294
| Self::RootManifestNotFound { .. }
295-
| Self::CargoConfigsConstructError { .. }
295+
| Self::CargoConfigError { .. }
296296
| Self::ConfigParseError { .. }
297297
| Self::ArgumentFileReadError { .. }
298298
| Self::UnknownArchiveFormat { .. }
@@ -376,7 +376,7 @@ impl ExpectedError {
376376
);
377377
Some(err as &dyn Error)
378378
}
379-
Self::CargoConfigsConstructError { err } => {
379+
Self::CargoConfigError { err } => {
380380
log::error!("{}", err);
381381
err.source()
382382
}

nextest-runner/src/cargo_config.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
//! Since `cargo config get` is not stable as of Rust 1.61, nextest must do its own config file
77
//! search.
88
9-
use crate::errors::{
10-
CargoConfigSearchError, CargoConfigsConstructError, InvalidCargoCliConfigReason,
11-
TargetTripleError,
12-
};
9+
use crate::errors::{CargoConfigError, InvalidCargoCliConfigReason, TargetTripleError};
1310
use camino::{Utf8Path, Utf8PathBuf};
1411
use once_cell::sync::OnceCell;
1512
use serde::Deserialize;
@@ -162,13 +159,12 @@ impl CargoConfigs {
162159
/// Discover Cargo config files using the same algorithm that Cargo uses.
163160
pub fn new(
164161
cli_configs: impl IntoIterator<Item = impl AsRef<str>>,
165-
) -> Result<Self, CargoConfigsConstructError> {
162+
) -> Result<Self, CargoConfigError> {
166163
let cli_configs = parse_cli_configs(cli_configs.into_iter())?;
167164
let cwd = std::env::current_dir()
168-
.map_err(CargoConfigsConstructError::GetCurrentDir)
165+
.map_err(CargoConfigError::GetCurrentDir)
169166
.and_then(|cwd| {
170-
Utf8PathBuf::try_from(cwd)
171-
.map_err(CargoConfigsConstructError::CurrentDirInvalidUtf8)
167+
Utf8PathBuf::try_from(cwd).map_err(CargoConfigError::CurrentDirInvalidUtf8)
172168
})?;
173169

174170
Ok(Self {
@@ -187,7 +183,7 @@ impl CargoConfigs {
187183
cli_configs: impl IntoIterator<Item = impl AsRef<str>>,
188184
cwd: &Utf8Path,
189185
terminate_search_at: &Utf8Path,
190-
) -> Result<Self, CargoConfigsConstructError> {
186+
) -> Result<Self, CargoConfigError> {
191187
let cli_configs = parse_cli_configs(cli_configs.into_iter())?;
192188

193189
Ok(Self {
@@ -206,7 +202,7 @@ impl CargoConfigs {
206202
&self,
207203
) -> Result<
208204
impl Iterator<Item = &(CargoConfigSource, CargoConfig)> + DoubleEndedIterator + '_,
209-
CargoConfigSearchError,
205+
CargoConfigError,
210206
> {
211207
let cli_iter = self.cli_configs.iter();
212208
let file_iter = self
@@ -219,7 +215,7 @@ impl CargoConfigs {
219215

220216
fn parse_cli_configs(
221217
cli_configs: impl Iterator<Item = impl AsRef<str>>,
222-
) -> Result<Vec<(CargoConfigSource, CargoConfig)>, CargoConfigsConstructError> {
218+
) -> Result<Vec<(CargoConfigSource, CargoConfig)>, CargoConfigError> {
223219
cli_configs
224220
.into_iter()
225221
.map(|config_str| {
@@ -231,7 +227,7 @@ fn parse_cli_configs(
231227
.collect()
232228
}
233229

234-
fn parse_cli_config(config_str: &str) -> Result<CargoConfig, CargoConfigsConstructError> {
230+
fn parse_cli_config(config_str: &str) -> Result<CargoConfig, CargoConfigError> {
235231
// This implementation is copied over from https://github.com/rust-lang/cargo/pull/10176.
236232

237233
// We only want to allow "dotted key" (see https://toml.io/en/v1.0.0#keys)
@@ -242,7 +238,7 @@ fn parse_cli_config(config_str: &str) -> Result<CargoConfig, CargoConfigsConstru
242238
let doc: toml_edit::Document =
243239
config_str
244240
.parse()
245-
.map_err(|error| CargoConfigsConstructError::CliConfigParseError {
241+
.map_err(|error| CargoConfigError::CliConfigParseError {
246242
config_str: config_str.to_owned(),
247243
error,
248244
})?;
@@ -267,22 +263,22 @@ fn parse_cli_config(config_str: &str) -> Result<CargoConfig, CargoConfigsConstru
267263
if table.key_decor(k).map_or(false, non_empty_decor)
268264
|| non_empty_decor(nt.decor())
269265
{
270-
return Err(CargoConfigsConstructError::InvalidCliConfig {
266+
return Err(CargoConfigError::InvalidCliConfig {
271267
config_str: config_str.to_owned(),
272268
reason: InvalidCargoCliConfigReason::IncludesNonWhitespaceDecoration,
273269
})?;
274270
}
275271
table = nt;
276272
}
277273
Item::Value(v) if v.is_inline_table() => {
278-
return Err(CargoConfigsConstructError::InvalidCliConfig {
274+
return Err(CargoConfigError::InvalidCliConfig {
279275
config_str: config_str.to_owned(),
280276
reason: InvalidCargoCliConfigReason::SetsValueToInlineTable,
281277
})?;
282278
}
283279
Item::Value(v) => {
284280
if non_empty_decor(v.decor()) {
285-
return Err(CargoConfigsConstructError::InvalidCliConfig {
281+
return Err(CargoConfigError::InvalidCliConfig {
286282
config_str: config_str.to_owned(),
287283
reason: InvalidCargoCliConfigReason::IncludesNonWhitespaceDecoration,
288284
})?;
@@ -291,13 +287,13 @@ fn parse_cli_config(config_str: &str) -> Result<CargoConfig, CargoConfigsConstru
291287
break;
292288
}
293289
Item::ArrayOfTables(_) => {
294-
return Err(CargoConfigsConstructError::InvalidCliConfig {
290+
return Err(CargoConfigError::InvalidCliConfig {
295291
config_str: config_str.to_owned(),
296292
reason: InvalidCargoCliConfigReason::SetsValueToArrayOfTables,
297293
})?;
298294
}
299295
Item::None => {
300-
return Err(CargoConfigsConstructError::InvalidCliConfig {
296+
return Err(CargoConfigError::InvalidCliConfig {
301297
config_str: config_str.to_owned(),
302298
reason: InvalidCargoCliConfigReason::DoesntProvideValue,
303299
})?;
@@ -307,14 +303,14 @@ fn parse_cli_config(config_str: &str) -> Result<CargoConfig, CargoConfigsConstru
307303
got_to_value
308304
};
309305
if !ok {
310-
return Err(CargoConfigsConstructError::InvalidCliConfig {
306+
return Err(CargoConfigError::InvalidCliConfig {
311307
config_str: config_str.to_owned(),
312308
reason: InvalidCargoCliConfigReason::NotDottedKv,
313309
})?;
314310
}
315311

316312
let cargo_config: CargoConfig = toml_edit::easy::from_document(doc).map_err(|error| {
317-
CargoConfigsConstructError::CliConfigDeError {
313+
CargoConfigError::CliConfigDeError {
318314
config_str: config_str.to_owned(),
319315
error,
320316
}
@@ -325,7 +321,7 @@ fn parse_cli_config(config_str: &str) -> Result<CargoConfig, CargoConfigsConstru
325321
fn discover_impl(
326322
start_search_at: &Utf8Path,
327323
terminate_search_at: Option<&Utf8Path>,
328-
) -> Result<Vec<(CargoConfigSource, CargoConfig)>, CargoConfigSearchError> {
324+
) -> Result<Vec<(CargoConfigSource, CargoConfig)>, CargoConfigError> {
329325
fn read_config_dir(dir: &mut Utf8PathBuf) -> Option<Utf8PathBuf> {
330326
// Check for config before config.toml, same as cargo does
331327
dir.push("config");
@@ -345,7 +341,7 @@ fn discover_impl(
345341
}
346342

347343
let mut dir = start_search_at.canonicalize_utf8().map_err(|error| {
348-
CargoConfigSearchError::FailedPathCanonicalization {
344+
CargoConfigError::FailedPathCanonicalization {
349345
path: start_search_at.to_owned(),
350346
error,
351347
}
@@ -377,10 +373,8 @@ fn discover_impl(
377373
// Attempt lookup the $CARGO_HOME directory from the cwd, as that can
378374
// contain a default config.toml
379375
let mut cargo_home_path = home::cargo_home_with_cwd(start_search_at.as_std_path())
380-
.map_err(CargoConfigSearchError::GetCargoHome)
381-
.and_then(|home| {
382-
Utf8PathBuf::try_from(home).map_err(CargoConfigSearchError::NonUtf8Path)
383-
})?;
376+
.map_err(CargoConfigError::GetCargoHome)
377+
.and_then(|home| Utf8PathBuf::try_from(home).map_err(CargoConfigError::NonUtf8Path))?;
384378

385379
if let Some(home_config) = read_config_dir(&mut cargo_home_path) {
386380
// Ensure we don't add a duplicate if the current directory is underneath
@@ -395,21 +389,21 @@ fn discover_impl(
395389
.into_iter()
396390
.map(|path| {
397391
let config_contents = std::fs::read_to_string(&path).map_err(|error| {
398-
CargoConfigSearchError::ConfigReadError {
392+
CargoConfigError::ConfigReadError {
399393
path: path.clone(),
400394
error,
401395
}
402396
})?;
403397
let config: CargoConfig =
404398
toml_edit::easy::from_str(&config_contents).map_err(|error| {
405-
CargoConfigSearchError::ConfigParseError {
399+
CargoConfigError::ConfigParseError {
406400
path: path.clone(),
407401
error,
408402
}
409403
})?;
410404
Ok((CargoConfigSource::File(path), config))
411405
})
412-
.collect::<Result<Vec<_>, CargoConfigSearchError>>()?;
406+
.collect::<Result<Vec<_>, CargoConfigError>>()?;
413407

414408
Ok(configs)
415409
}
@@ -502,7 +496,7 @@ mod tests {
502496
// Disallow inline tables
503497
let err = parse_cli_config(arg).unwrap_err();
504498
let actual_reason = match err {
505-
CargoConfigsConstructError::InvalidCliConfig { reason, .. } => reason,
499+
CargoConfigError::InvalidCliConfig { reason, .. } => reason,
506500
other => panic!(
507501
"expected input {arg} to fail with InvalidCliConfig, actual failure: {other}"
508502
),

nextest-runner/src/errors.rs

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ pub enum WriteEventError {
787787
/// instance.
788788
#[derive(Debug, Error)]
789789
#[non_exhaustive]
790-
pub enum CargoConfigsConstructError {
790+
pub enum CargoConfigError {
791791
/// Failed to retrieve the current directory.
792792
#[error("failed to retrieve current directory")]
793793
GetCurrentDir(#[source] std::io::Error),
@@ -830,39 +830,7 @@ pub enum CargoConfigsConstructError {
830830
#[source]
831831
reason: InvalidCargoCliConfigReason,
832832
},
833-
}
834-
835-
/// The reason an invalid CLI config failed.
836-
///
837-
/// Part of [`CargoConfigsConstructError::InvalidCliConfig`].
838-
#[derive(Copy, Clone, Debug, Error, Eq, PartialEq)]
839-
#[non_exhaustive]
840-
pub enum InvalidCargoCliConfigReason {
841-
/// The argument is not a TOML dotted key expression.
842-
#[error("was not a TOML dotted key expression (such as `build.jobs = 2`)")]
843-
NotDottedKv,
844-
845-
/// The argument includes non-whitespace decoration.
846-
#[error("includes non-whitespace decoration")]
847-
IncludesNonWhitespaceDecoration,
848833

849-
/// The argument sets a value to an inline table.
850-
#[error("sets a value to an inline table, which is not accepted")]
851-
SetsValueToInlineTable,
852-
853-
/// The argument sets a value to an array of tables.
854-
#[error("sets a value to an array of tables, which is not accepted")]
855-
SetsValueToArrayOfTables,
856-
857-
/// The argument doesn't provide a value.
858-
#[error("doesn't provide a value")]
859-
DoesntProvideValue,
860-
}
861-
862-
/// An error occurred while looking for Cargo configuration files.
863-
#[derive(Debug, Error)]
864-
#[non_exhaustive]
865-
pub enum CargoConfigSearchError {
866834
/// A non-UTF-8 path was encountered.
867835
#[error("non-UTF-8 path encountered")]
868836
NonUtf8Path(#[source] FromPathBufError),
@@ -905,6 +873,33 @@ pub enum CargoConfigSearchError {
905873
},
906874
}
907875

876+
/// The reason an invalid CLI config failed.
877+
///
878+
/// Part of [`CargoConfigError::InvalidCliConfig`].
879+
#[derive(Copy, Clone, Debug, Error, Eq, PartialEq)]
880+
#[non_exhaustive]
881+
pub enum InvalidCargoCliConfigReason {
882+
/// The argument is not a TOML dotted key expression.
883+
#[error("was not a TOML dotted key expression (such as `build.jobs = 2`)")]
884+
NotDottedKv,
885+
886+
/// The argument includes non-whitespace decoration.
887+
#[error("includes non-whitespace decoration")]
888+
IncludesNonWhitespaceDecoration,
889+
890+
/// The argument sets a value to an inline table.
891+
#[error("sets a value to an inline table, which is not accepted")]
892+
SetsValueToInlineTable,
893+
894+
/// The argument sets a value to an array of tables.
895+
#[error("sets a value to an array of tables, which is not accepted")]
896+
SetsValueToArrayOfTables,
897+
898+
/// The argument doesn't provide a value.
899+
#[error("doesn't provide a value")]
900+
DoesntProvideValue,
901+
}
902+
908903
/// An error occurred while determining the cross-compiling target triple.
909904
#[derive(Debug, Error)]
910905
pub enum TargetTripleError {
@@ -920,7 +915,7 @@ pub enum TargetTripleError {
920915
CargoConfigSearchError(
921916
#[from]
922917
#[source]
923-
CargoConfigSearchError,
918+
CargoConfigError,
924919
),
925920
}
926921

@@ -963,7 +958,7 @@ pub enum TargetRunnerError {
963958
CargoConfigSearchError(
964959
#[from]
965960
#[source]
966-
CargoConfigSearchError,
961+
CargoConfigError,
967962
),
968963
}
969964

0 commit comments

Comments
 (0)