Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format placeholder code when generating a crate #7827

4 changes: 4 additions & 0 deletions ci/azure-test-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ steps:
- bash: rustup component add clippy || echo "clippy not available"
displayName: "Install clippy (maybe)"

# Some tests also rely on rustfmt
- bash: rustup component add rustfmt || echo "rustfmt not available"
displayName: "Install rustfmt (maybe)"

# Deny warnings on CI to avoid warnings getting into the codebase, and note the
# `force-system-lib-on-osx` which is intended to fix compile issues on OSX where
# compiling curl from source on OSX yields linker errors on Azure.
Expand Down
6 changes: 3 additions & 3 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,9 +1801,9 @@ pub fn slow_cpu_multiplier(main: u64) -> Duration {
Duration::from_secs(*SLOW_CPU_MULTIPLIER * main)
}

pub fn clippy_is_available() -> bool {
if let Err(e) = process("clippy-driver").arg("-V").exec_with_output() {
eprintln!("clippy-driver not available, skipping clippy test");
pub fn command_is_available(cmd: &str) -> bool {
if let Err(e) = process(cmd).arg("-V").exec_with_output() {
eprintln!("{} not available, skipping tests", cmd);
eprintln!("{:?}", e);
false
} else {
Expand Down
13 changes: 12 additions & 1 deletion src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use std::fmt;
use std::fs;
use std::io::{BufRead, BufReader, ErrorKind};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::process::Command;
use std::str::{from_utf8, FromStr};

use toml;

Expand Down Expand Up @@ -707,6 +708,16 @@ mod tests {
.unwrap_or(false)
{
paths::write(&path_of_source_file, default_file_content)?;

// Format the newly created source file
match Command::new("rustfmt").arg(&path_of_source_file).output() {
Err(e) => log::warn!("failed to call rustfmt: {}", e),
Ok(output) => {
if !output.status.success() {
log::warn!("rustfmt failed: {:?}", from_utf8(&output.stdout));
}
}
};
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/cache_messages.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Tests for caching compiler diagnostics.

use cargo_test_support::{
basic_manifest, clippy_is_available, is_coarse_mtime, process, project, registry::Package,
basic_manifest, command_is_available, is_coarse_mtime, process, project, registry::Package,
sleep_ms,
};
use std::path::Path;
Expand Down Expand Up @@ -276,7 +276,7 @@ fn fix() {

#[cargo_test]
fn clippy() {
if !clippy_is_available() {
if !command_is_available("clippy-driver") {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/testsuite/clippy.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Tests for the `cargo clippy` command.

use cargo_test_support::{clippy_is_available, project, registry::Package};
use cargo_test_support::{command_is_available, project, registry::Package};

#[cargo_test]
// Clippy should never be considered fresh.
fn clippy_force_rebuild() {
if !clippy_is_available() {
if !command_is_available("clippy-driver") {
return;
}

Expand Down Expand Up @@ -43,7 +43,7 @@ fn clippy_force_rebuild() {

#[cargo_test]
fn clippy_passes_args() {
if !clippy_is_available() {
if !command_is_available("clippy-driver") {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::fs::File;

use cargo_test_support::git;
use cargo_test_support::{basic_manifest, clippy_is_available, project};
use cargo_test_support::{basic_manifest, command_is_available, project};

use std::io::Write;

Expand Down Expand Up @@ -1253,7 +1253,7 @@ fn fix_in_existing_repo_weird_ignore() {

#[cargo_test]
fn fix_with_clippy() {
if !clippy_is_available() {
if !command_is_available("clippy-driver") {
return;
}

Expand Down
49 changes: 48 additions & 1 deletion tests/testsuite/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fs::{self, File};
use std::io::prelude::*;
use std::process::Command;

use cargo_test_support::{paths, Execs};
use cargo_test_support::{command_is_available, paths, Execs};

fn cargo_process(s: &str) -> Execs {
let mut execs = cargo_test_support::cargo_process(s);
Expand Down Expand Up @@ -657,3 +657,50 @@ fn no_filename() {
)
.run();
}

#[cargo_test]
fn formats_source() {
if !command_is_available("rustfmt") {
return;
}

fs::write(&paths::root().join("rustfmt.toml"), "tab_spaces = 2").unwrap();
Kinrany marked this conversation as resolved.
Show resolved Hide resolved

cargo_process("init --lib")
.env("USER", "foo")
.with_stderr("[CREATED] library package")
.run();

assert_eq!(
fs::read_to_string(paths::root().join("src/lib.rs")).unwrap(),
r#"#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
"#
);
}

#[cargo_test]
fn ignores_failure_to_format_source() {
cargo_process("init --lib")
.env("USER", "foo")
.env("PATH", "") // pretend that `rustfmt` is missing
.with_stderr("[CREATED] library package")
.run();

assert_eq!(
fs::read_to_string(paths::root().join("src/lib.rs")).unwrap(),
r#"#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
"#
);
}