Skip to content

Commit

Permalink
Allow #[ignore] tests to run in rustfmt's test suite
Browse files Browse the repository at this point in the history
There are some tests in the rustfmt test suite that are ignored by
default. I believe these tests are ignored because they have caused
issues with the the `rust-lang/rust` test suite.

However, we recently experienced an issue (5395) that would have been
avoided had these tests been running.

With the introduction of the new `#[rustfmt_only_ci_test]` attribute
macro we can run these tests when the `RUSTFMT_CI` environment variable
is set, which will presumably only be set during rustfmts CI runs.
When the environment variable is not set the `#[rustfmt_only_ci_test]`
will be replaced with an `#[ignore]`.
  • Loading branch information
ytmimi authored and calebcartwright committed Jun 29, 2022
1 parent c4416f2 commit b3d4fb4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions ci/build_and_test.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set "RUSTFLAGS=-D warnings"
set "RUSTFMT_CI=1"

:: Print version information
rustc -Vv || exit /b 1
Expand Down
1 change: 1 addition & 0 deletions ci/build_and_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
set -euo pipefail

export RUSTFLAGS="-D warnings"
export RUSTFMT_CI=1

# Print version information
rustc -Vv
Expand Down
13 changes: 13 additions & 0 deletions config_proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,16 @@ pub fn stable_only_test(_args: TokenStream, input: TokenStream) -> TokenStream {
TokenStream::from_str("").unwrap()
}
}

/// Used to conditionally output the TokenStream for tests that should be run as part of rustfmts
/// test suite, but should be ignored when running in the rust-lang/rust test suite.
#[proc_macro_attribute]
pub fn rustfmt_only_ci_test(_args: TokenStream, input: TokenStream) -> TokenStream {
if option_env!("RUSTFMT_CI").is_some() {
input
} else {
let mut token_stream = TokenStream::from_str("#[ignore]").unwrap();
token_stream.extend(input);
token_stream
}
}
10 changes: 6 additions & 4 deletions tests/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::env;
use std::path::Path;
use std::process::Command;

use rustfmt_config_proc_macro::rustfmt_only_ci_test;

/// Run the cargo-fmt executable and return its output.
fn cargo_fmt(args: &[&str]) -> (String, String) {
let mut bin_dir = env::current_exe().unwrap();
Expand Down Expand Up @@ -47,7 +49,7 @@ macro_rules! assert_that {
};
}

#[ignore]
#[rustfmt_only_ci_test]
#[test]
fn version() {
assert_that!(&["--version"], starts_with("rustfmt "));
Expand All @@ -56,7 +58,7 @@ fn version() {
assert_that!(&["--", "--version"], starts_with("rustfmt "));
}

#[ignore]
#[rustfmt_only_ci_test]
#[test]
fn print_config() {
assert_that!(
Expand All @@ -65,15 +67,15 @@ fn print_config() {
);
}

#[ignore]
#[rustfmt_only_ci_test]
#[test]
fn rustfmt_help() {
assert_that!(&["--", "--help"], contains("Format Rust code"));
assert_that!(&["--", "-h"], contains("Format Rust code"));
assert_that!(&["--", "--help=config"], contains("Configuration Options:"));
}

#[ignore]
#[rustfmt_only_ci_test]
#[test]
fn cargo_fmt_out_of_line_test_modules() {
// See also https://github.com/rust-lang/rustfmt/issues/5119
Expand Down
6 changes: 4 additions & 2 deletions tests/rustfmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::fs::remove_file;
use std::path::Path;
use std::process::Command;

use rustfmt_config_proc_macro::rustfmt_only_ci_test;

/// Run the rustfmt executable and return its output.
fn rustfmt(args: &[&str]) -> (String, String) {
let mut bin_dir = env::current_exe().unwrap();
Expand Down Expand Up @@ -47,7 +49,7 @@ macro_rules! assert_that {
};
}

#[ignore]
#[rustfmt_only_ci_test]
#[test]
fn print_config() {
assert_that!(
Expand Down Expand Up @@ -76,7 +78,7 @@ fn print_config() {
remove_file("minimal-config").unwrap();
}

#[ignore]
#[rustfmt_only_ci_test]
#[test]
fn inline_config() {
// single invocation
Expand Down

0 comments on commit b3d4fb4

Please sign in to comment.