Skip to content

Commit d747924

Browse files
committed
Pass TestConfig to build_command
1 parent fb89be9 commit d747924

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

src/lib.rs

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,13 @@ pub fn test_command(mut config: Config, path: &Path) -> Result<Command> {
158158

159159
let comments = Comments::parse_file(config.comment_defaults.clone(), path)?
160160
.map_err(|errors| color_eyre::eyre::eyre!("{errors:#?}"))?;
161-
let mut result = build_command(path, &config, "", &comments).unwrap();
161+
let config = TestConfig {
162+
config,
163+
revision: "",
164+
comments: &comments,
165+
path,
166+
};
167+
let mut result = build_command(&config).unwrap();
162168
result.args(extra_args);
163169

164170
Ok(result)
@@ -425,12 +431,13 @@ fn parse_and_test_file(
425431
.collect())
426432
}
427433

428-
fn build_command(
429-
path: &Path,
430-
config: &Config,
431-
revision: &str,
432-
comments: &Comments,
433-
) -> Result<Command, Errored> {
434+
fn build_command(config: &TestConfig) -> Result<Command, Errored> {
435+
let TestConfig {
436+
config,
437+
revision,
438+
comments,
439+
path,
440+
} = config;
434441
let mut cmd = config.program.build(&config.out_dir);
435442
cmd.arg(path);
436443
if !revision.is_empty() {
@@ -518,19 +525,22 @@ fn build_aux(
518525
CrateType::Test | CrateType::Bin | CrateType::Lib => {}
519526
}
520527

521-
// Put aux builds into a separate directory per path so that multiple aux files
522-
// from different directories (but with the same file name) don't collide.
523-
let relative = strip_path_prefix(aux_file.parent().unwrap(), &config.out_dir);
528+
let mut config = TestConfig {
529+
config,
530+
revision: "",
531+
comments: &comments,
532+
path: aux_file,
533+
};
524534

525-
config.out_dir.extend(relative);
535+
config.patch_out_dir();
526536

527-
let mut aux_cmd = build_command(aux_file, &config, "", &comments)?;
537+
let mut aux_cmd = build_command(&config)?;
528538

529539
let mut extra_args = build_aux_files(
530540
aux_file.parent().unwrap(),
531541
&comments,
532542
"",
533-
&config,
543+
&config.config,
534544
build_manager,
535545
)?;
536546
// Make sure we see our dependencies
@@ -560,15 +570,15 @@ fn build_aux(
560570
for file in output.stdout.lines() {
561571
let file = std::str::from_utf8(file).unwrap();
562572
let crate_name = filename.replace('-', "_");
563-
let path = config.out_dir.join(file);
573+
let path = config.config.out_dir.join(file);
564574
extra_args.push("--extern".into());
565575
let mut cname = OsString::from(&crate_name);
566576
cname.push("=");
567577
cname.push(path);
568578
extra_args.push(cname);
569579
// Help cargo find the crates added with `--extern`.
570580
extra_args.push("-L".into());
571-
extra_args.push(config.out_dir.as_os_str().to_os_string());
581+
extra_args.push(config.config.out_dir.as_os_str().to_os_string());
572582
}
573583
Ok(extra_args)
574584
}
@@ -584,12 +594,7 @@ fn run_test(build_manager: &BuildManager<'_>, mut config: TestConfig<'_>) -> Tes
584594

585595
config.patch_out_dir();
586596

587-
let mut cmd = build_command(
588-
config.path,
589-
&config.config,
590-
config.revision,
591-
config.comments,
592-
)?;
597+
let mut cmd = build_command(&config)?;
593598
cmd.args(&extra_args);
594599
let stdin = config.path.with_extension(config.extension("stdin"));
595600
if stdin.exists() {
@@ -838,6 +843,21 @@ fn run_rustfix(
838843
"fixed",
839844
&config,
840845
);
846+
// picking the crate name from the file name is problematic when `.revision_name` is inserted,
847+
// so we compute it here before replacing the path.
848+
let crate_name = config
849+
.path
850+
.file_stem()
851+
.unwrap()
852+
.to_str()
853+
.unwrap()
854+
.replace('-', "_");
855+
let config = TestConfig {
856+
config: config.config,
857+
revision: config.revision,
858+
comments: &rustfix_comments,
859+
path: &rustfix_path,
860+
};
841861
if !errors.is_empty() {
842862
return Err(Errored {
843863
command: Command::new(format!("checking {}", config.path.display())),
@@ -851,23 +871,9 @@ fn run_rustfix(
851871
return Ok(());
852872
}
853873

854-
let mut cmd = build_command(
855-
&rustfix_path,
856-
&config.config,
857-
config.revision,
858-
config.comments,
859-
)?;
874+
let mut cmd = build_command(&config)?;
860875
cmd.args(extra_args);
861-
// picking the crate name from the file name is problematic when `.revision_name` is inserted
862-
cmd.arg("--crate-name").arg(
863-
config
864-
.path
865-
.file_stem()
866-
.unwrap()
867-
.to_str()
868-
.unwrap()
869-
.replace('-', "_"),
870-
);
876+
cmd.arg("--crate-name").arg(crate_name);
871877
let output = cmd.output().unwrap();
872878
if output.status.success() {
873879
Ok(())

0 commit comments

Comments
 (0)