Skip to content

Commit 3350bb6

Browse files
Alexendoooli-obk
authored andcommitted
Move the bless command to a field in Config
1 parent 963dfb2 commit 3350bb6

File tree

15 files changed

+54
-56
lines changed

15 files changed

+54
-56
lines changed

src/config.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub struct Config {
3131
pub cfgs: CommandBuilder,
3232
/// What to do in case the stdout/stderr output differs from the expected one.
3333
pub output_conflict_handling: OutputConflictHandling,
34+
/// The recommended command to bless failing tests.
35+
pub bless_command: Option<String>,
3436
/// Path to a `Cargo.toml` that describes which dependencies the tests can access.
3537
pub dependencies_crate_manifest_path: Option<PathBuf>,
3638
/// The command to run can be changed from `cargo` to any custom command to build the
@@ -85,6 +87,7 @@ impl Config {
8587
program: CommandBuilder::rustc(),
8688
cfgs: CommandBuilder::cfgs(),
8789
output_conflict_handling: OutputConflictHandling::Bless,
90+
bless_command: None,
8891
dependencies_crate_manifest_path: None,
8992
dependency_builder: CommandBuilder::cargo(),
9093
out_dir: std::env::var_os("CARGO_TARGET_DIR")
@@ -118,11 +121,7 @@ impl Config {
118121
}
119122

120123
/// Populate the config with the values from parsed command line arguments.
121-
/// If neither `--bless` or `--check` are provided `default_bless` is used.
122-
///
123-
/// The default output conflict handling command suggests adding `--bless`
124-
/// to the end of the current command.
125-
pub fn with_args(&mut self, args: &Args, default_bless: bool) {
124+
pub fn with_args(&mut self, args: &Args) {
126125
let Args {
127126
ref filters,
128127
check,
@@ -144,22 +143,11 @@ impl Config {
144143

145144
self.list = list;
146145

147-
let bless = match (bless, check) {
148-
(_, true) => false,
149-
(true, _) => true,
150-
_ => default_bless,
151-
};
152-
self.output_conflict_handling = if bless {
153-
OutputConflictHandling::Bless
154-
} else {
155-
OutputConflictHandling::Error(format!(
156-
"{} --bless",
157-
std::env::args()
158-
.map(|s| format!("{s:?}"))
159-
.collect::<Vec<_>>()
160-
.join(" ")
161-
))
162-
};
146+
if check {
147+
self.output_conflict_handling = OutputConflictHandling::Error;
148+
} else if bless {
149+
self.output_conflict_handling = OutputConflictHandling::Bless;
150+
}
163151
}
164152

165153
/// Replace all occurrences of a path in stderr/stdout with a byte string.
@@ -289,8 +277,10 @@ impl Config {
289277
#[derive(Debug, Clone)]
290278
/// The different options for what to do when stdout/stderr files differ from the actual output.
291279
pub enum OutputConflictHandling {
292-
/// The string should be a command that can be executed to bless all tests.
293-
Error(String),
280+
/// Fail the test when mismatches are found, if provided the command string
281+
/// in [`Config::bless_command`] will be suggested as a way to bless the
282+
/// test.
283+
Error,
294284
/// Ignore mismatches in the stderr/stdout files.
295285
Ignore,
296286
/// Instead of erroring if the stderr/stdout differs from the expected

src/dependencies.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(crate) fn build_dependencies(config: &Config) -> Result<Dependencies> {
6969
.unwrap(),
7070
) {
7171
(_, Mode::Yolo { .. }) => {}
72-
(OutputConflictHandling::Error(_), _) => {
72+
(OutputConflictHandling::Error, _) => {
7373
cmd.arg("--locked");
7474
}
7575
_ => {}

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub enum Error {
4343
/// The contents of the file.
4444
expected: Vec<u8>,
4545
/// A command, that when run, causes the output to get blessed instead of erroring.
46-
bless_command: String,
46+
bless_command: Option<String>,
4747
},
4848
/// There were errors that don't have a pattern.
4949
ErrorsWithoutPattern {

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn run_tests(mut config: Config) -> Result<()> {
127127
Format::Terse => status_emitter::Text::quiet(),
128128
Format::Pretty => status_emitter::Text::verbose(),
129129
};
130-
config.with_args(&args, true);
130+
config.with_args(&args);
131131

132132
run_tests_generic(
133133
vec![config],
@@ -1183,14 +1183,14 @@ fn check_output(
11831183
let output = normalize(output, comments, revision, kind);
11841184
let path = output_path(path, comments, revised(revision, kind), target, revision);
11851185
match &config.output_conflict_handling {
1186-
OutputConflictHandling::Error(bless_command) => {
1186+
OutputConflictHandling::Error => {
11871187
let expected_output = std::fs::read(&path).unwrap_or_default();
11881188
if output != expected_output {
11891189
errors.push(Error::OutputDiffers {
11901190
path: path.clone(),
11911191
actual: output.clone(),
11921192
expected: expected_output,
1193-
bless_command: bless_command.clone(),
1193+
bless_command: config.bless_command.clone(),
11941194
});
11951195
}
11961196
}

src/status_emitter.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,13 @@ fn print_error(error: &Error, path: &Path) {
466466
bless_command,
467467
} => {
468468
print_error_header("actual output differed from expected");
469-
println!(
470-
"Execute `{}` to update `{}` to the actual output",
471-
bless_command,
472-
output_path.display()
473-
);
469+
if let Some(bless_command) = bless_command {
470+
println!(
471+
"Execute `{}` to update `{}` to the actual output",
472+
bless_command,
473+
output_path.display()
474+
);
475+
}
474476
println!("{}", format!("--- {}", output_path.display()).red());
475477
println!(
476478
"{}",
@@ -670,11 +672,13 @@ fn gha_error(error: &Error, test_path: &str, revision: &str) {
670672
test_path,
671673
"test generated output, but there was no output file",
672674
);
673-
writeln!(
674-
err,
675-
"you likely need to bless the tests with `{bless_command}`"
676-
)
677-
.unwrap();
675+
if let Some(bless_command) = bless_command {
676+
writeln!(
677+
err,
678+
"you likely need to bless the tests with `{bless_command}`"
679+
)
680+
.unwrap();
681+
}
678682
return;
679683
}
680684

tests/integration.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ use ui_test::{spanned::Spanned, *};
66
fn main() -> Result<()> {
77
let path = Path::new(file!()).parent().unwrap();
88
let root_dir = path.join("integrations");
9-
let mut config = Config::cargo(root_dir.clone());
9+
let mut config = Config {
10+
bless_command: Some("cargo test".to_string()),
11+
..Config::cargo(root_dir.clone())
12+
};
1013
let args = Args::test()?;
11-
config.with_args(&args, true);
14+
config.with_args(&args);
1215

1316
config.program.args = vec![
1417
"test".into(),

tests/integrations/basic-bin/tests/ui_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ fn main() -> ui_test::color_eyre::Result<()> {
77
output_conflict_handling: if std::env::var_os("BLESS").is_some() {
88
OutputConflictHandling::Bless
99
} else {
10-
OutputConflictHandling::Error("cargo test".to_string())
10+
OutputConflictHandling::Error
1111
},
12+
bless_command: Some("cargo test".to_string()),
1213
..Config::rustc("tests/actual_tests")
1314
};
1415
config.stderr_filter("in ([0-9]m )?[0-9\\.]+s", "");

tests/integrations/basic-fail-mode/tests/ui_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ fn main() -> ui_test::color_eyre::Result<()> {
88
output_conflict_handling: if std::env::var_os("BLESS").is_some() {
99
OutputConflictHandling::Bless
1010
} else {
11-
OutputConflictHandling::Error("cargo test".to_string())
11+
OutputConflictHandling::Error
1212
},
13+
bless_command: Some("cargo test".to_string()),
1314
..Config::rustc("tests/actual_tests")
1415
};
1516
config.comment_defaults.base().mode = Spanned::dummy(Mode::Fail {

tests/integrations/basic-fail/Cargo.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ error: test failed, to rerun pass `--test ui_tests`
66

77
Caused by:
88
process didn't exit successfully: `$DIR/target/ui/tests/integrations/basic-fail/debug/deps/ui_tests-HASH` (exit status: 1)
9-
thread 'main' panicked at tests/ui_tests_bless.rs:52:18:
9+
thread 'main' panicked at tests/ui_tests_bless.rs:53:18:
1010
invalid mode/result combo: yolo: Err(tests failed
1111

1212
Location:

tests/integrations/basic-fail/tests/ui_tests.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ fn main() -> ui_test::color_eyre::Result<()> {
55
let mut config = Config {
66
dependencies_crate_manifest_path: Some("Cargo.toml".into()),
77
// Never bless integrations-fail tests, we want to see stderr mismatches
8-
output_conflict_handling: OutputConflictHandling::Error(
9-
"DO NOT BLESS. These are meant to fail".into(),
10-
),
11-
8+
output_conflict_handling: OutputConflictHandling::Error,
9+
bless_command: Some("DO NOT BLESS. These are meant to fail".to_string()),
1210
..Config::rustc("tests/actual_tests")
1311
};
1412

0 commit comments

Comments
 (0)