Skip to content

Commit 850f67e

Browse files
committed
Use TestConfig more often
1 parent cc503c0 commit 850f67e

File tree

3 files changed

+99
-84
lines changed

3 files changed

+99
-84
lines changed

src/lib.rs

Lines changed: 50 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -573,61 +573,47 @@ fn build_aux(
573573
Ok(extra_args)
574574
}
575575

576-
fn run_test(
577-
build_manager: &BuildManager<'_>,
578-
TestConfig {
579-
mut config,
580-
revision,
581-
comments,
582-
path,
583-
}: TestConfig<'_>,
584-
) -> TestResult {
576+
fn run_test(build_manager: &BuildManager<'_>, mut config: TestConfig<'_>) -> TestResult {
585577
let extra_args = build_aux_files(
586-
&path.parent().unwrap().join("auxiliary"),
587-
comments,
588-
revision,
589-
&config,
578+
&config.path.parent().unwrap().join("auxiliary"),
579+
config.comments,
580+
config.revision,
581+
&config.config,
590582
build_manager,
591583
)?;
592584

593-
// Put aux builds into a separate directory per path so that multiple aux files
594-
// from different directories (but with the same file name) don't collide.
595-
let relative = strip_path_prefix(path.parent().unwrap(), &config.out_dir);
596-
597-
config.out_dir.extend(relative);
585+
config.patch_out_dir();
598586

599-
let mut cmd = build_command(path, &config, revision, comments)?;
587+
let mut cmd = build_command(
588+
config.path,
589+
&config.config,
590+
config.revision,
591+
config.comments,
592+
)?;
600593
cmd.args(&extra_args);
601-
let stdin = path.with_extension(if revision.is_empty() {
602-
"stdin".into()
603-
} else {
604-
format!("{revision}.stdin")
605-
});
594+
let stdin = config.path.with_extension(config.extension("stdin"));
606595
if stdin.exists() {
607596
cmd.stdin(std::fs::File::open(stdin).unwrap());
608597
}
609598

610599
let (cmd, output) = run_command(cmd)?;
611600

612-
let mode = comments.mode(revision)?;
601+
let mode = config.mode()?;
613602
let (cmd, output) = check_test_result(
614603
cmd,
615604
match *mode {
616605
Mode::Run { .. } => Mode::Pass,
617606
_ => *mode,
618607
},
619-
path,
620608
&config,
621-
revision,
622-
comments,
623609
output,
624610
)?;
625611

626612
if let Mode::Run { .. } = *mode {
627-
return run_test_binary(mode, path, revision, comments, cmd, &config);
613+
return run_test_binary(mode, cmd, &config);
628614
}
629615

630-
run_rustfix(output, path, comments, revision, &config, *mode, extra_args)?;
616+
run_rustfix(output, &config, *mode, extra_args)?;
631617
Ok(TestOk::Ok)
632618
}
633619

@@ -703,19 +689,8 @@ fn build_aux_files(
703689
Ok(extra_args)
704690
}
705691

706-
fn run_test_binary(
707-
mode: Spanned<Mode>,
708-
path: &Path,
709-
revision: &str,
710-
comments: &Comments,
711-
mut cmd: Command,
712-
config: &Config,
713-
) -> TestResult {
714-
let revision = if revision.is_empty() {
715-
"run".to_string()
716-
} else {
717-
format!("run.{revision}")
718-
};
692+
fn run_test_binary(mode: Spanned<Mode>, mut cmd: Command, config: &TestConfig) -> TestResult {
693+
let revision = config.extension("run");
719694
cmd.arg("--print").arg("file-names");
720695
let output = cmd.output().unwrap();
721696
assert!(output.status.success());
@@ -724,9 +699,9 @@ fn run_test_binary(
724699
let file = files.next().unwrap();
725700
assert_eq!(files.next(), None);
726701
let file = std::str::from_utf8(file).unwrap();
727-
let exe_file = config.out_dir.join(file);
702+
let exe_file = config.config.out_dir.join(file);
728703
let mut exe = Command::new(&exe_file);
729-
let stdin = path.with_extension(format!("{revision}.stdin"));
704+
let stdin = config.path.with_extension(format!("{revision}.stdin"));
730705
if stdin.exists() {
731706
exe.stdin(std::fs::File::open(stdin).unwrap());
732707
}
@@ -737,11 +712,11 @@ fn run_test_binary(
737712
let mut errors = vec![];
738713

739714
check_test_output(
740-
path,
715+
config.path,
741716
&mut errors,
742717
&revision,
743-
config,
744-
comments,
718+
&config.config,
719+
config.comments,
745720
&output.stdout,
746721
&output.stderr,
747722
);
@@ -761,17 +736,11 @@ fn run_test_binary(
761736

762737
fn run_rustfix(
763738
output: Output,
764-
path: &Path,
765-
comments: &Comments,
766-
revision: &str,
767-
config: &Config,
739+
config: &TestConfig,
768740
mode: Mode,
769741
extra_args: Vec<OsString>,
770742
) -> Result<(), Errored> {
771-
let no_run_rustfix =
772-
comments.find_one_for_revision(revision, "`no-rustfix` annotations", |r| {
773-
r.no_rustfix.clone()
774-
})?;
743+
let no_run_rustfix = config.find_one("`no-rustfix` annotations", |r| r.no_rustfix.clone())?;
775744

776745
let global_rustfix = match mode {
777746
Mode::Pass | Mode::Run { .. } | Mode::Panic => RustfixMode::Disabled,
@@ -805,7 +774,7 @@ fn run_rustfix(
805774
if suggestions.is_empty() {
806775
None
807776
} else {
808-
let path_str = path.display().to_string();
777+
let path_str = config.path.display().to_string();
809778
for sugg in &suggestions {
810779
for snip in &sugg.snippets {
811780
if snip.file_name != path_str {
@@ -814,20 +783,20 @@ fn run_rustfix(
814783
}
815784
}
816785
Some(rustfix::apply_suggestions(
817-
&std::fs::read_to_string(path).unwrap(),
786+
&std::fs::read_to_string(config.path).unwrap(),
818787
&suggestions,
819788
))
820789
}
821790
})
822791
.transpose()
823792
.map_err(|err| Errored {
824-
command: Command::new(format!("rustfix {}", path.display())),
793+
command: Command::new(format!("rustfix {}", config.path.display())),
825794
errors: vec![Error::Rustfix(err)],
826795
stderr: output.stderr,
827796
stdout: output.stdout,
828797
})?;
829798

830-
let edition = comments.edition(revision)?.into();
799+
let edition = config.edition()?.into();
831800
let rustfix_comments = Comments {
832801
revisions: None,
833802
revisioned: std::iter::once((
@@ -837,23 +806,14 @@ fn run_rustfix(
837806
ignore: vec![],
838807
only: vec![],
839808
stderr_per_bitwidth: false,
840-
compile_flags: comments
841-
.for_revision(revision)
842-
.flat_map(|r| r.compile_flags.iter().cloned())
843-
.collect(),
844-
env_vars: comments
845-
.for_revision(revision)
846-
.flat_map(|r| r.env_vars.iter().cloned())
847-
.collect(),
809+
compile_flags: config.collect(|r| r.compile_flags.iter().cloned()),
810+
env_vars: config.collect(|r| r.env_vars.iter().cloned()),
848811
normalize_stderr: vec![],
849812
normalize_stdout: vec![],
850813
error_in_other_files: vec![],
851814
error_matches: vec![],
852815
require_annotations_for_level: Default::default(),
853-
aux_builds: comments
854-
.for_revision(revision)
855-
.flat_map(|r| r.aux_builds.iter().cloned())
856-
.collect(),
816+
aux_builds: config.collect(|r| r.aux_builds.iter().cloned()),
857817
edition,
858818
mode: OptWithLine::new(Mode::Pass, Span::default()),
859819
no_rustfix: OptWithLine::new((), Span::default()),
@@ -870,16 +830,16 @@ fn run_rustfix(
870830
// Always check for `.fixed` files, even if there were reasons not to run rustfix.
871831
// We don't want to leave around stray `.fixed` files
872832
fixed_code.unwrap_or_default().as_bytes(),
873-
path,
833+
config.path,
874834
&mut errors,
875835
"fixed",
876-
config,
836+
&config.config,
877837
&rustfix_comments,
878-
revision,
838+
config.revision,
879839
);
880840
if !errors.is_empty() {
881841
return Err(Errored {
882-
command: Command::new(format!("checking {}", path.display())),
842+
command: Command::new(format!("checking {}", config.path.display())),
883843
errors,
884844
stderr: vec![],
885845
stdout: vec![],
@@ -890,11 +850,18 @@ fn run_rustfix(
890850
return Ok(());
891851
}
892852

893-
let mut cmd = build_command(&rustfix_path, config, revision, &rustfix_comments)?;
853+
let mut cmd = build_command(
854+
&rustfix_path,
855+
&config.config,
856+
config.revision,
857+
&rustfix_comments,
858+
)?;
894859
cmd.args(extra_args);
895860
// picking the crate name from the file name is problematic when `.revision_name` is inserted
896861
cmd.arg("--crate-name").arg(
897-
path.file_stem()
862+
config
863+
.path
864+
.file_stem()
898865
.unwrap()
899866
.to_str()
900867
.unwrap()
@@ -927,21 +894,21 @@ fn revised(revision: &str, extension: &str) -> String {
927894
fn check_test_result(
928895
command: Command,
929896
mode: Mode,
930-
path: &Path,
931-
config: &Config,
932-
revision: &str,
933-
comments: &Comments,
897+
config: &TestConfig,
934898
output: Output,
935899
) -> Result<(Command, Output), Errored> {
936900
let mut errors = vec![];
937901
errors.extend(mode.ok(output.status).err());
902+
let path = config.path;
903+
let comments = config.comments;
904+
let revision = config.revision;
938905
// Always remove annotation comments from stderr.
939906
let diagnostics = rustc_stderr::process(path, &output.stderr);
940907
check_test_output(
941908
path,
942909
&mut errors,
943910
revision,
944-
config,
911+
&config.config,
945912
comments,
946913
&output.stdout,
947914
&diagnostics.rendered,

src/per_test_config.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,61 @@
55
66
use std::path::Path;
77

8+
use spanned::Spanned;
9+
10+
use crate::parser::OptWithLine;
811
pub use crate::parser::{Comments, Condition, Revisioned};
912
pub use crate::rustc_stderr::Level;
10-
use crate::Config;
13+
use crate::test_result::Errored;
14+
use crate::{strip_path_prefix, Config, Mode};
1115

1216
pub(crate) struct TestConfig<'a> {
1317
pub config: Config,
1418
pub revision: &'a str,
1519
pub comments: &'a Comments,
1620
pub path: &'a Path,
1721
}
22+
23+
impl TestConfig<'_> {
24+
pub fn patch_out_dir(&mut self) {
25+
// Put aux builds into a separate directory per path so that multiple aux files
26+
// from different directories (but with the same file name) don't collide.
27+
let relative = strip_path_prefix(self.path.parent().unwrap(), &self.config.out_dir);
28+
29+
self.config.out_dir.extend(relative);
30+
}
31+
32+
pub fn extension(&self, extension: &str) -> String {
33+
if self.revision.is_empty() {
34+
extension.to_string()
35+
} else {
36+
format!("{}.{extension}", self.revision)
37+
}
38+
}
39+
40+
pub fn mode(&self) -> Result<Spanned<Mode>, Errored> {
41+
self.comments.mode(self.revision)
42+
}
43+
44+
pub fn edition(&self) -> Result<Option<Spanned<String>>, Errored> {
45+
self.comments.edition(self.revision)
46+
}
47+
48+
pub fn find_one<'a, T: 'a>(
49+
&'a self,
50+
kind: &str,
51+
f: impl Fn(&'a Revisioned) -> OptWithLine<T>,
52+
) -> Result<OptWithLine<T>, Errored> {
53+
self.comments.find_one_for_revision(self.revision, kind, f)
54+
}
55+
56+
pub fn collect<'a, T, I: Iterator<Item = T>, R: FromIterator<T>>(
57+
&'a self,
58+
f: impl Fn(&'a Revisioned) -> I,
59+
) -> R {
60+
self.comments
61+
.for_revision(self.revision)
62+
.flat_map(f)
63+
.collect()
64+
}
65+
}

0 commit comments

Comments
 (0)