forked from rust-lang/rustfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add integration test to check deprecated configuration warnings
I went through the Configurations.md file and created a test case for all the deprecated options.
- Loading branch information
Showing
8 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
use super::rustfmt; | ||
use rustfmt_nightly::test_helpers::{get_test_files, read_significant_comments}; | ||
use std::path::{Path, PathBuf}; | ||
|
||
/// Transforms the `// rustfmt-{config}: {value}` comments in each test file into a String | ||
/// that can be passed to rustfmt on the command line like | ||
/// `--config={config1}={value1},{config2}={value2} | ||
fn command_line_args_from_significant_comments(path: &Path) -> String { | ||
let config_comments = read_significant_comments(path); | ||
|
||
if config_comments.is_empty() { | ||
return String::new(); | ||
} | ||
|
||
let mut config = String::with_capacity(256); | ||
config.push_str("--config="); | ||
|
||
let mut iter = config_comments.into_iter().peekable(); | ||
|
||
while let Some((key, value)) = iter.next() { | ||
let is_last = iter.peek().is_none(); | ||
|
||
config.push_str(&key); | ||
config.push('='); | ||
config.push_str(&value); | ||
|
||
if !is_last { | ||
config.push(',') | ||
} | ||
} | ||
config | ||
} | ||
|
||
enum CheckOutputResult { | ||
/// The output matches what's in the expected file | ||
Okay, | ||
/// The expected file doesn't exist and there was no output generated by rustfmt | ||
NothingToCheck, | ||
/// The output generated by rustfmt doesn't match what's in the expected file | ||
MismatchedContent, | ||
/// rustfmt generated output but the expected output file does not exist | ||
MissingExpectedFile, | ||
} | ||
|
||
/// Compares the input against the output read from a file. | ||
/// returns `true` if the check was successfull, and `false` otherwise. | ||
fn check_output_file(output: &str, expected: &Path) -> CheckOutputResult { | ||
match (expected.exists(), !output.is_empty()) { | ||
(true, _) => { | ||
// The file exists so compare it's content with the output. | ||
let expected_output = | ||
std::fs::read_to_string(expected).expect("to read the file successfully"); | ||
if expected_output.trim() == output.trim() { | ||
CheckOutputResult::Okay | ||
} else { | ||
CheckOutputResult::MismatchedContent | ||
} | ||
} | ||
(false, true) => { | ||
// The file does not exist and the output is not empty. | ||
CheckOutputResult::MissingExpectedFile | ||
} | ||
(false, false) => { | ||
// The file doesn't exist, and the output isn't empty | ||
CheckOutputResult::NothingToCheck | ||
} | ||
} | ||
} | ||
|
||
fn ui_test_inenr(files: Vec<PathBuf>, check_stdout: bool, check_stderr: bool) -> usize { | ||
if !check_stderr && !check_stdout { | ||
panic!("We need to check stdout, stderr, or both"); | ||
} | ||
|
||
let mut errors = 0; | ||
|
||
let has_errors = |rustfmt_output: &str, output_file: &Path| -> bool { | ||
match check_output_file(&rustfmt_output, &output_file) { | ||
CheckOutputResult::Okay | CheckOutputResult::NothingToCheck => false, | ||
CheckOutputResult::MissingExpectedFile => { | ||
eprintln!("Missing {}", output_file.display()); | ||
eprintln!("Expected content: {}", rustfmt_output); | ||
true | ||
} | ||
CheckOutputResult::MismatchedContent => { | ||
eprintln!( | ||
"Expected {} to contain\n{}", | ||
output_file.display(), | ||
rustfmt_output | ||
); | ||
true | ||
} | ||
} | ||
}; | ||
|
||
for file in files { | ||
let config = command_line_args_from_significant_comments(&file); | ||
|
||
// deprecation warnings are only emitted on stderr | ||
let (stdout, stderr) = rustfmt(&[config.as_str()]); | ||
|
||
if check_stdout { | ||
let stdout_file = file.with_extension("stdout"); | ||
errors += has_errors(&stdout, &stdout_file) as usize; | ||
} | ||
|
||
if check_stderr { | ||
let stderr_file = file.with_extension("stderr"); | ||
errors += has_errors(&stderr, &stderr_file) as usize; | ||
} | ||
} | ||
|
||
errors | ||
} | ||
|
||
/// Check the output for deprecated warnings | ||
#[test] | ||
fn deprecated() { | ||
let files = get_test_files(&PathBuf::from("tests/ui/deprecated"), true, &[]); | ||
let errors = ui_test_inenr(files, false, true); | ||
assert!(errors == 0, "All deprecation UI tests should pass") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// rustfmt-fn_args_layout: Tall |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Warning: the `fn_args_layout` option is deprecated. Use `fn_params_layout`. instead |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// rustfmt-hide_parse_errors: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Warning: the `hide_parse_errors` option is deprecated. Use `show_parse_errors` instead |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// rustfmt-merge_imports: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Warning: the `merge_imports` option is deprecated. Use `imports_granularity="Crate"` instead |