Skip to content

Commit db0dac5

Browse files
committed
Auto merge of #7827 - Kinrany:7656-format-placeholder-code-when-generating-a-crate, r=ehuss
Format placeholder code when generating a crate When generating source files in `cargo new` and `cargo init`, try to run `rustfmt` CLI on them. If it fails, log the error and proceed. Tests: * Works for `cargo init --lib` * No changes in behavior if `rustfmt` is missing Closes #7656
2 parents 19d38e5 + bc4c65c commit db0dac5

File tree

7 files changed

+74
-12
lines changed

7 files changed

+74
-12
lines changed

ci/azure-test-all.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ steps:
1616
- bash: rustup component add clippy || echo "clippy not available"
1717
displayName: "Install clippy (maybe)"
1818

19+
# Some tests also rely on rustfmt
20+
- bash: rustup component add rustfmt || echo "rustfmt not available"
21+
displayName: "Install rustfmt (maybe)"
22+
1923
# Deny warnings on CI to avoid warnings getting into the codebase, and note the
2024
# `force-system-lib-on-osx` which is intended to fix compile issues on OSX where
2125
# compiling curl from source on OSX yields linker errors on Azure.

crates/cargo-test-support/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,9 +1801,9 @@ pub fn slow_cpu_multiplier(main: u64) -> Duration {
18011801
Duration::from_secs(*SLOW_CPU_MULTIPLIER * main)
18021802
}
18031803

1804-
pub fn clippy_is_available() -> bool {
1805-
if let Err(e) = process("clippy-driver").arg("-V").exec_with_output() {
1806-
eprintln!("clippy-driver not available, skipping clippy test");
1804+
pub fn command_is_available(cmd: &str) -> bool {
1805+
if let Err(e) = process(cmd).arg("-V").exec_with_output() {
1806+
eprintln!("{} not available, skipping tests", cmd);
18071807
eprintln!("{:?}", e);
18081808
false
18091809
} else {

src/cargo/ops/cargo_new.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use std::fmt;
1212
use std::fs;
1313
use std::io::{BufRead, BufReader, ErrorKind};
1414
use std::path::{Path, PathBuf};
15-
use std::str::FromStr;
15+
use std::process::Command;
16+
use std::str::{from_utf8, FromStr};
1617

1718
use toml;
1819

@@ -707,6 +708,16 @@ mod tests {
707708
.unwrap_or(false)
708709
{
709710
paths::write(&path_of_source_file, default_file_content)?;
711+
712+
// Format the newly created source file
713+
match Command::new("rustfmt").arg(&path_of_source_file).output() {
714+
Err(e) => log::warn!("failed to call rustfmt: {}", e),
715+
Ok(output) => {
716+
if !output.status.success() {
717+
log::warn!("rustfmt failed: {:?}", from_utf8(&output.stdout));
718+
}
719+
}
720+
};
710721
}
711722
}
712723

tests/testsuite/cache_messages.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Tests for caching compiler diagnostics.
22
33
use cargo_test_support::{
4-
basic_manifest, clippy_is_available, is_coarse_mtime, process, project, registry::Package,
4+
basic_manifest, command_is_available, is_coarse_mtime, process, project, registry::Package,
55
sleep_ms,
66
};
77
use std::path::Path;
@@ -276,7 +276,7 @@ fn fix() {
276276

277277
#[cargo_test]
278278
fn clippy() {
279-
if !clippy_is_available() {
279+
if !command_is_available("clippy-driver") {
280280
return;
281281
}
282282

tests/testsuite/clippy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Tests for the `cargo clippy` command.
22
3-
use cargo_test_support::{clippy_is_available, project, registry::Package};
3+
use cargo_test_support::{command_is_available, project, registry::Package};
44

55
#[cargo_test]
66
// Clippy should never be considered fresh.
77
fn clippy_force_rebuild() {
8-
if !clippy_is_available() {
8+
if !command_is_available("clippy-driver") {
99
return;
1010
}
1111

@@ -43,7 +43,7 @@ fn clippy_force_rebuild() {
4343

4444
#[cargo_test]
4545
fn clippy_passes_args() {
46-
if !clippy_is_available() {
46+
if !command_is_available("clippy-driver") {
4747
return;
4848
}
4949

tests/testsuite/fix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::fs::File;
44

55
use cargo_test_support::git;
6-
use cargo_test_support::{basic_manifest, clippy_is_available, project};
6+
use cargo_test_support::{basic_manifest, command_is_available, project};
77

88
use std::io::Write;
99

@@ -1253,7 +1253,7 @@ fn fix_in_existing_repo_weird_ignore() {
12531253

12541254
#[cargo_test]
12551255
fn fix_with_clippy() {
1256-
if !clippy_is_available() {
1256+
if !command_is_available("clippy-driver") {
12571257
return;
12581258
}
12591259

tests/testsuite/init.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::fs::{self, File};
66
use std::io::prelude::*;
77
use std::process::Command;
88

9-
use cargo_test_support::{paths, Execs};
9+
use cargo_test_support::{command_is_available, paths, Execs};
1010

1111
fn cargo_process(s: &str) -> Execs {
1212
let mut execs = cargo_test_support::cargo_process(s);
@@ -657,3 +657,50 @@ fn no_filename() {
657657
)
658658
.run();
659659
}
660+
661+
#[cargo_test]
662+
fn formats_source() {
663+
if !command_is_available("rustfmt") {
664+
return;
665+
}
666+
667+
fs::write(&paths::root().join("rustfmt.toml"), "tab_spaces = 2").unwrap();
668+
669+
cargo_process("init --lib")
670+
.env("USER", "foo")
671+
.with_stderr("[CREATED] library package")
672+
.run();
673+
674+
assert_eq!(
675+
fs::read_to_string(paths::root().join("src/lib.rs")).unwrap(),
676+
r#"#[cfg(test)]
677+
mod tests {
678+
#[test]
679+
fn it_works() {
680+
assert_eq!(2 + 2, 4);
681+
}
682+
}
683+
"#
684+
);
685+
}
686+
687+
#[cargo_test]
688+
fn ignores_failure_to_format_source() {
689+
cargo_process("init --lib")
690+
.env("USER", "foo")
691+
.env("PATH", "") // pretend that `rustfmt` is missing
692+
.with_stderr("[CREATED] library package")
693+
.run();
694+
695+
assert_eq!(
696+
fs::read_to_string(paths::root().join("src/lib.rs")).unwrap(),
697+
r#"#[cfg(test)]
698+
mod tests {
699+
#[test]
700+
fn it_works() {
701+
assert_eq!(2 + 2, 4);
702+
}
703+
}
704+
"#
705+
);
706+
}

0 commit comments

Comments
 (0)