Skip to content

Error on self_tests when there are lines that exceed max width #2681

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

Merged
merged 3 commits into from
May 6, 2018
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
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error_on_line_overflow = true
error_on_unformatted = true
5 changes: 3 additions & 2 deletions src/config/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,9 @@ macro_rules! create_config {
///
/// Returns the `Config` to use, and the path of the project file if there was
/// one.
pub(super) fn from_resolved_toml_path(dir: &Path) -> Result<(Config, Option<PathBuf>), Error> {

pub(super) fn from_resolved_toml_path(
dir: &Path,
) -> Result<(Config, Option<PathBuf>), Error> {
/// Try to find a project file in the given directory and its parents.
/// Returns the path of a the nearest project file if one exists,
/// or `None` if no project file was found.
Expand Down
20 changes: 13 additions & 7 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ create_config! {
comment_width: usize, 80, false,
"Maximum length of comments. No effect unless wrap_comments = true";
normalize_comments: bool, false, true, "Convert /* */ comments to // comments where possible";
license_template_path: String, String::default(), false, "Beginning of file must match license template";
license_template_path: String, String::default(), false,
"Beginning of file must match license template";
format_strings: bool, false, false, "Format string literals where necessary";

// Single line expressions and items
Expand Down Expand Up @@ -239,16 +240,21 @@ mod test {
create_config! {
// Options that are used by the generated functions
max_width: usize, 100, true, "Maximum width of each line";
use_small_heuristics: bool, true, false, "Whether to use different formatting for items and \
expressions if they satisfy a heuristic notion of 'small'.";
license_template_path: String, String::default(), false, "Beginning of file must match license template";
required_version: String, env!("CARGO_PKG_VERSION").to_owned(), false, "Require a specific version of rustfmt.";
ignore: IgnoreList, IgnoreList::default(), false, "Skip formatting the specified files and directories.";
use_small_heuristics: bool, true, false,
"Whether to use different formatting for items and \
expressions if they satisfy a heuristic notion of 'small'.";
license_template_path: String, String::default(), false,
"Beginning of file must match license template";
required_version: String, env!("CARGO_PKG_VERSION").to_owned(), false,
"Require a specific version of rustfmt.";
ignore: IgnoreList, IgnoreList::default(), false,
"Skip formatting the specified files and directories.";
verbose: bool, false, false, "Use verbose output";
file_lines: FileLines, FileLines::all(), false,
"Lines to format; this is not supported in rustfmt.toml, and can only be specified \
via the --file-lines option";
width_heuristics: WidthHeuristics, WidthHeuristics::scaled(100), false, "'small' heuristic values";
width_heuristics: WidthHeuristics, WidthHeuristics::scaled(100), false,
"'small' heuristic values";

// Options that are used by the tests
stable_option: bool, false, true, "A stable option";
Expand Down
3 changes: 2 additions & 1 deletion src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ configuration_option_enum! { WriteMode:
// Output the changed lines (for internal value only)
Modified,
// Checks if a diff can be generated. If so, rustfmt outputs a diff and quits with exit code 1.
// This option is designed to be run in CI where a non-zero exit signifies non-standard code formatting.
// This option is designed to be run in CI where a non-zero exit signifies non-standard code
// formatting.
Check,
// Rustfmt shouldn't output anything formatting-like (e.g., emit a help message).
None,
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ pub(crate) type FileRecord = (FileName, String);
pub enum ErrorKind {
// Line has exceeded character limit (found, maximum)
#[fail(
display = "line formatted, but exceeded maximum width (maximum: {} (see `max_width` option), found: {})",
display = "line formatted, but exceeded maximum width \
(maximum: {} (see `max_width` option), found: {})",
_0,
_1
)]
Expand Down
4 changes: 3 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,9 @@ impl MacroBranch {
fn format_lazy_static(context: &RewriteContext, shape: Shape, ts: &TokenStream) -> Option<String> {
let mut result = String::with_capacity(1024);
let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
let nested_shape = shape.block_indent(context.config.tab_spaces());
let nested_shape = shape
.block_indent(context.config.tab_spaces())
.with_max_width(context.config);

result.push_str("lazy_static! {");
result.push_str(&nested_shape.indent.to_string_with_newline(context.config));
Expand Down
28 changes: 18 additions & 10 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn write_message(msg: &str) {
fn system_tests() {
// Get all files in the tests/source directory.
let files = get_test_files(Path::new("tests/source"), true);
let (_reports, count, fails) = check_files(files);
let (_reports, count, fails) = check_files(files, None);

// Display results.
println!("Ran {} system tests.", count);
Expand All @@ -123,7 +123,7 @@ fn system_tests() {
#[test]
fn coverage_tests() {
let files = get_test_files(Path::new("tests/coverage/source"), true);
let (_reports, count, fails) = check_files(files);
let (_reports, count, fails) = check_files(files, None);

println!("Ran {} tests in coverage mode.", count);
assert_eq!(fails, 0, "{} tests failed", fails);
Expand Down Expand Up @@ -192,7 +192,7 @@ fn assert_output(source: &Path, expected_filename: &Path) {
fn idempotence_tests() {
// Get all files in the tests/target directory.
let files = get_test_files(Path::new("tests/target"), true);
let (_reports, count, fails) = check_files(files);
let (_reports, count, fails) = check_files(files, None);

// Display results.
println!("Ran {} idempotent tests.", count);
Expand All @@ -213,7 +213,7 @@ fn self_tests() {
}
files.push(PathBuf::from("src/lib.rs"));

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

// Display results.
Expand Down Expand Up @@ -298,15 +298,15 @@ fn format_lines_errors_are_reported_with_tabs() {

// For each file, run rustfmt and collect the output.
// Returns the number of files checked and the number of failures.
fn check_files(files: Vec<PathBuf>) -> (Vec<FormatReport>, u32, u32) {
fn check_files(files: Vec<PathBuf>, opt_config: Option<PathBuf>) -> (Vec<FormatReport>, u32, u32) {
let mut count = 0;
let mut fails = 0;
let mut reports = vec![];

for file_name in files {
debug!("Testing '{}'...", file_name.display());

match idempotent_check(&file_name) {
match idempotent_check(&file_name, &opt_config) {
Ok(ref report) if report.has_warnings() => {
print!("{}", report);
fails += 1;
Expand Down Expand Up @@ -385,9 +385,16 @@ pub enum IdempotentCheckError {
Parse,
}

pub fn idempotent_check(filename: &PathBuf) -> Result<FormatReport, IdempotentCheckError> {
pub fn idempotent_check(
filename: &PathBuf,
opt_config: &Option<PathBuf>,
) -> Result<FormatReport, IdempotentCheckError> {
let sig_comments = read_significant_comments(filename);
let config = read_config(filename);
let config = if let Some(ref config_file_path) = opt_config {
Config::from_toml_path(config_file_path).expect("rustfmt.toml not found")
} else {
read_config(filename)
};
let (error_summary, file_map, format_report) = format_file(filename, &config);
if error_summary.has_parsing_errors() {
return Err(IdempotentCheckError::Parse);
Expand Down Expand Up @@ -611,8 +618,9 @@ impl ConfigurationSection {
lazy_static! {
static ref CONFIG_NAME_REGEX: regex::Regex =
regex::Regex::new(r"^## `([^`]+)`").expect("Failed creating configuration pattern");
static ref CONFIG_VALUE_REGEX: regex::Regex = regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#)
.expect("Failed creating configuration value pattern");
static ref CONFIG_VALUE_REGEX: regex::Regex =
regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#)
.expect("Failed creating configuration value pattern");
}

loop {
Expand Down