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

Use cargo-fmt in self_tests #5212

Merged
merged 2 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config_proc_macro/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub fn is_unit(v: &syn::Variant) -> bool {
#[cfg(feature = "debug-with-rustfmt")]
/// Pretty-print the output of proc macro using rustfmt.
pub fn debug_with_rustfmt(input: &TokenStream) {
use std::io::Write;
use std::process::{Command, Stdio};
use std::env;
use std::ffi::OsStr;
use std::io::Write;
use std::process::{Command, Stdio};

let rustfmt_var = env::var_os("RUSTFMT");
let rustfmt = match &rustfmt_var {
Expand Down
46 changes: 12 additions & 34 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,43 +375,21 @@ fn idempotence_tests() {
});
}

// Run rustfmt on itself. This operation must be idempotent. We also check that
// no warnings are emitted.
// Issue-3443: these tests require nightly
#[nightly_only_test]
#[test]
fn self_tests() {
init_log();
let mut files = get_test_files(Path::new("tests"), false);
let bin_directories = vec!["cargo-fmt", "git-rustfmt", "bin", "format-diff"];
for dir in bin_directories {
let mut path = PathBuf::from("src");
path.push(dir);
path.push("main.rs");
files.push(path);
}
files.push(PathBuf::from("src/lib.rs"));

let (reports, count, fails) = check_files(files, &Some(PathBuf::from("rustfmt.toml")));
let mut warnings = 0;

// Display results.
println!("Ran {} self tests.", count);
assert_eq!(fails, 0, "{} self tests failed", fails);

for format_report in reports {
println!(
"{}",
FormatReportFormatterBuilder::new(&format_report).build()
);
warnings += format_report.warning_count();
}

assert_eq!(
warnings, 0,
"Rustfmt's code generated {} warnings",
warnings
);
let get_exe_path = |name| {
let mut path = env::current_exe().unwrap();
path.pop();
path.set_file_name(format!("{name}{}", env::consts::EXE_SUFFIX));
path
};
let status = Command::new(get_exe_path("cargo-fmt"))
.args(["--check", "--all"])
.env("RUSTFMT", get_exe_path("rustfmt"))
.status()
.unwrap();
assert!(status.success());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion tests/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Integration tests for cargo-fmt.

use std::env;
use std::process::Command;
use std::path::Path;
use std::process::Command;

/// Run the cargo-fmt executable and return its output.
fn cargo_fmt(args: &[&str]) -> (String, String) {
Expand Down