2015-05-28 12:23:07 -05:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
extern crate rustfmt;
|
|
|
|
extern crate diff;
|
|
|
|
extern crate regex;
|
2015-08-30 13:47:46 -05:00
|
|
|
extern crate term;
|
2015-05-28 12:23:07 -05:00
|
|
|
|
2015-09-10 17:27:22 -05:00
|
|
|
use std::collections::HashMap;
|
2015-05-28 12:23:07 -05:00
|
|
|
use std::fs;
|
|
|
|
use std::io::{self, Read, BufRead, BufReader};
|
2015-10-07 07:10:07 -05:00
|
|
|
use std::path::Path;
|
2015-09-17 13:21:06 -05:00
|
|
|
|
2015-05-28 12:23:07 -05:00
|
|
|
use rustfmt::*;
|
2015-09-17 13:21:06 -05:00
|
|
|
use rustfmt::config::{Config, ReportTactic};
|
2015-09-10 17:27:22 -05:00
|
|
|
use rustfmt::rustfmt_diff::*;
|
2015-05-28 12:23:07 -05:00
|
|
|
|
2015-08-30 13:47:46 -05:00
|
|
|
static DIFF_CONTEXT_SIZE: usize = 3;
|
|
|
|
|
2015-05-28 12:23:07 -05:00
|
|
|
fn get_path_string(dir_entry: io::Result<fs::DirEntry>) -> String {
|
2015-09-10 17:53:21 -05:00
|
|
|
let path = dir_entry.ok().expect("Couldn't get DirEntry.").path();
|
2015-05-28 12:23:07 -05:00
|
|
|
|
2015-09-10 17:53:21 -05:00
|
|
|
path.to_str().expect("Couldn't stringify path.").to_owned()
|
2015-05-28 12:23:07 -05:00
|
|
|
}
|
|
|
|
|
2015-06-23 06:26:04 -05:00
|
|
|
// Integration tests. The files in the tests/source are formatted and compared
|
|
|
|
// to their equivalent in tests/target. The target file and config can be
|
|
|
|
// overriden by annotations in the source file. The input and output must match
|
|
|
|
// exactly.
|
|
|
|
// FIXME(#28) would be good to check for error messages and fail on them, or at
|
|
|
|
// least report.
|
2015-05-28 12:23:07 -05:00
|
|
|
#[test]
|
|
|
|
fn system_tests() {
|
2015-09-17 13:21:06 -05:00
|
|
|
// Get all files in the tests/source directory.
|
2015-09-10 17:53:21 -05:00
|
|
|
let files = fs::read_dir("tests/source").ok().expect("Couldn't read source dir.");
|
2015-09-17 13:21:06 -05:00
|
|
|
// Turn a DirEntry into a String that represents the relative path to the
|
|
|
|
// file.
|
2015-05-28 12:23:07 -05:00
|
|
|
let files = files.map(get_path_string);
|
2015-09-17 13:21:06 -05:00
|
|
|
let (_reports, count, fails) = check_files(files);
|
2015-05-28 12:23:07 -05:00
|
|
|
|
2015-09-17 13:21:06 -05:00
|
|
|
// Display results.
|
2015-06-23 06:26:04 -05:00
|
|
|
println!("Ran {} system tests.", count);
|
|
|
|
assert!(fails == 0, "{} system tests failed", fails);
|
|
|
|
}
|
2015-05-28 12:23:07 -05:00
|
|
|
|
2015-06-23 06:26:04 -05:00
|
|
|
// Idempotence tests. Files in tests/target are checked to be unaltered by
|
|
|
|
// rustfmt.
|
|
|
|
#[test]
|
|
|
|
fn idempotence_tests() {
|
2015-09-17 13:21:06 -05:00
|
|
|
// Get all files in the tests/target directory.
|
|
|
|
let files = fs::read_dir("tests/target")
|
|
|
|
.ok()
|
|
|
|
.expect("Couldn't read target dir.")
|
|
|
|
.map(get_path_string);
|
|
|
|
let (_reports, count, fails) = check_files(files);
|
|
|
|
|
|
|
|
// Display results.
|
|
|
|
println!("Ran {} idempotent tests.", count);
|
|
|
|
assert!(fails == 0, "{} idempotent tests failed", fails);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run rustfmt on itself. This operation must be idempotent. We also check that
|
|
|
|
// no warnings are emitted.
|
|
|
|
#[test]
|
|
|
|
fn self_tests() {
|
|
|
|
let files = fs::read_dir("src/bin")
|
|
|
|
.ok()
|
|
|
|
.expect("Couldn't read src dir.")
|
|
|
|
.chain(fs::read_dir("tests").ok().expect("Couldn't read tests dir."))
|
|
|
|
.map(get_path_string);
|
|
|
|
// Hack because there's no `IntoIterator` impl for `[T; N]`.
|
2015-06-23 06:26:04 -05:00
|
|
|
let files = files.chain(Some("src/lib.rs".to_owned()).into_iter());
|
2015-05-28 12:23:07 -05:00
|
|
|
|
2015-09-17 13:21:06 -05:00
|
|
|
let (reports, count, fails) = check_files(files);
|
|
|
|
let mut warnings = 0;
|
2015-05-28 12:23:07 -05:00
|
|
|
|
2015-09-17 13:21:06 -05:00
|
|
|
// Display results.
|
|
|
|
println!("Ran {} self tests.", count);
|
|
|
|
assert!(fails == 0, "{} self tests failed", fails);
|
|
|
|
|
|
|
|
for format_report in reports {
|
|
|
|
println!("{}", format_report);
|
|
|
|
warnings += format_report.warning_count();
|
|
|
|
}
|
|
|
|
|
2015-09-23 18:15:08 -05:00
|
|
|
assert!(warnings == 0,
|
|
|
|
"Rustfmt's code generated {} warnings",
|
|
|
|
warnings);
|
2015-05-28 12:23:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// For each file, run rustfmt and collect the output.
|
|
|
|
// Returns the number of files checked and the number of failures.
|
2015-09-17 13:21:06 -05:00
|
|
|
fn check_files<I>(files: I) -> (Vec<FormatReport>, u32, u32)
|
2015-05-28 12:23:07 -05:00
|
|
|
where I: Iterator<Item = String>
|
|
|
|
{
|
|
|
|
let mut count = 0;
|
|
|
|
let mut fails = 0;
|
2015-09-17 13:21:06 -05:00
|
|
|
let mut reports = vec![];
|
2015-05-28 12:23:07 -05:00
|
|
|
|
|
|
|
for file_name in files.filter(|f| f.ends_with(".rs")) {
|
|
|
|
println!("Testing '{}'...", file_name);
|
2015-09-17 13:21:06 -05:00
|
|
|
|
|
|
|
match idempotent_check(file_name) {
|
|
|
|
Ok(report) => reports.push(report),
|
|
|
|
Err(msg) => {
|
|
|
|
print_mismatches(msg);
|
|
|
|
fails += 1;
|
|
|
|
}
|
2015-05-28 12:23:07 -05:00
|
|
|
}
|
2015-09-17 13:21:06 -05:00
|
|
|
|
2015-05-28 12:23:07 -05:00
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
2015-09-17 13:21:06 -05:00
|
|
|
(reports, count, fails)
|
2015-05-28 12:23:07 -05:00
|
|
|
}
|
|
|
|
|
2015-08-30 13:47:46 -05:00
|
|
|
fn print_mismatches(result: HashMap<String, Vec<Mismatch>>) {
|
|
|
|
let mut t = term::stdout().unwrap();
|
|
|
|
|
|
|
|
for (file_name, diff) in result {
|
2015-09-23 18:15:08 -05:00
|
|
|
print_diff(diff,
|
|
|
|
|line_num| format!("\nMismatch at {}:{}:", file_name, line_num));
|
2015-05-28 12:23:07 -05:00
|
|
|
}
|
2015-08-30 13:47:46 -05:00
|
|
|
|
|
|
|
assert!(t.reset().unwrap());
|
2015-05-28 12:23:07 -05:00
|
|
|
}
|
|
|
|
|
2015-09-17 13:21:06 -05:00
|
|
|
pub fn idempotent_check(filename: String) -> Result<FormatReport, HashMap<String, Vec<Mismatch>>> {
|
2015-08-19 14:41:19 -05:00
|
|
|
let sig_comments = read_significant_comments(&filename);
|
|
|
|
let mut config = get_config(sig_comments.get("config").map(|x| &(*x)[..]));
|
|
|
|
|
2015-09-17 13:21:06 -05:00
|
|
|
for (key, val) in &sig_comments {
|
2015-08-19 14:41:19 -05:00
|
|
|
if key != "target" && key != "config" {
|
2015-09-17 13:21:06 -05:00
|
|
|
config.override_value(key, val);
|
2015-08-19 14:41:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 13:21:06 -05:00
|
|
|
// Don't generate warnings for to-do items.
|
|
|
|
config.report_todo = ReportTactic::Never;
|
2015-05-28 12:23:07 -05:00
|
|
|
|
2015-10-21 02:26:24 -05:00
|
|
|
let mut file_map = format(Path::new(&filename), &config, WriteMode::Return);
|
2015-09-17 13:21:06 -05:00
|
|
|
let format_report = fmt_lines(&mut file_map, &config);
|
|
|
|
|
|
|
|
// Won't panic, as we're not doing any IO.
|
|
|
|
let write_result = filemap::write_all_files(&file_map, WriteMode::Return, &config).unwrap();
|
|
|
|
let target = sig_comments.get("target").map(|x| &(*x)[..]);
|
|
|
|
|
|
|
|
handle_result(write_result, target).map(|_| format_report)
|
|
|
|
}
|
2015-08-19 14:41:19 -05:00
|
|
|
|
|
|
|
// Reads test config file from comments and reads its contents.
|
2015-09-17 13:21:06 -05:00
|
|
|
fn get_config(config_file: Option<&str>) -> Config {
|
2015-07-31 18:21:44 -05:00
|
|
|
let config_file_name = match config_file {
|
2015-09-17 13:21:06 -05:00
|
|
|
None => return Default::default(),
|
2015-07-31 18:21:44 -05:00
|
|
|
Some(file_name) => {
|
|
|
|
let mut full_path = "tests/config/".to_owned();
|
|
|
|
full_path.push_str(&file_name);
|
|
|
|
full_path
|
|
|
|
}
|
|
|
|
};
|
2015-05-28 12:23:07 -05:00
|
|
|
|
2015-09-01 16:51:57 -05:00
|
|
|
let mut def_config_file = fs::File::open(config_file_name)
|
|
|
|
.ok()
|
2015-09-10 17:53:21 -05:00
|
|
|
.expect("Couldn't open config.");
|
2015-05-28 12:23:07 -05:00
|
|
|
let mut def_config = String::new();
|
2015-09-10 17:53:21 -05:00
|
|
|
def_config_file.read_to_string(&mut def_config).ok().expect("Couldn't read config.");
|
2015-05-28 12:23:07 -05:00
|
|
|
|
2015-09-17 13:21:06 -05:00
|
|
|
Config::from_toml(&def_config)
|
2015-05-28 12:23:07 -05:00
|
|
|
}
|
|
|
|
|
2015-08-19 14:41:19 -05:00
|
|
|
// Reads significant comments of the form: // rustfmt-key: value
|
|
|
|
// into a hash map.
|
|
|
|
fn read_significant_comments(file_name: &str) -> HashMap<String, String> {
|
2015-09-01 16:51:57 -05:00
|
|
|
let file = fs::File::open(file_name)
|
|
|
|
.ok()
|
2015-09-10 17:53:21 -05:00
|
|
|
.expect(&format!("Couldn't read file {}.", file_name));
|
2015-05-28 12:23:07 -05:00
|
|
|
let reader = BufReader::new(file);
|
2015-08-19 14:41:19 -05:00
|
|
|
let pattern = r"^\s*//\s*rustfmt-([^:]+):\s*(\S+)";
|
2015-05-28 12:23:07 -05:00
|
|
|
let regex = regex::Regex::new(&pattern).ok().expect("Failed creating pattern 1.");
|
|
|
|
|
2015-08-19 14:41:19 -05:00
|
|
|
// Matches lines containing significant comments or whitespace.
|
2015-09-10 17:52:57 -05:00
|
|
|
let line_regex = regex::Regex::new(r"(^\s*$)|(^\s*//\s*rustfmt-[^:]+:\s*\S+)")
|
|
|
|
.ok()
|
2015-09-09 16:09:39 -05:00
|
|
|
.expect("Failed creating pattern 2.");
|
2015-05-28 12:23:07 -05:00
|
|
|
|
|
|
|
reader.lines()
|
2015-09-09 16:14:54 -05:00
|
|
|
.map(|line| line.ok().expect("Failed getting line."))
|
|
|
|
.take_while(|line| line_regex.is_match(&line))
|
|
|
|
.filter_map(|line| {
|
2015-09-09 16:17:31 -05:00
|
|
|
regex.captures_iter(&line).next().map(|capture| {
|
2015-09-10 17:53:21 -05:00
|
|
|
(capture.at(1).expect("Couldn't unwrap capture.").to_owned(),
|
|
|
|
capture.at(2).expect("Couldn't unwrap capture.").to_owned())
|
2015-09-09 16:17:31 -05:00
|
|
|
})
|
2015-09-09 16:14:54 -05:00
|
|
|
})
|
|
|
|
.collect()
|
2015-05-28 12:23:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compare output to input.
|
2015-08-19 14:41:19 -05:00
|
|
|
// TODO: needs a better name, more explanation.
|
2015-09-17 13:21:06 -05:00
|
|
|
fn handle_result(result: HashMap<String, String>,
|
|
|
|
target: Option<&str>)
|
|
|
|
-> Result<(), HashMap<String, Vec<Mismatch>>> {
|
2015-05-28 12:23:07 -05:00
|
|
|
let mut failures = HashMap::new();
|
|
|
|
|
|
|
|
for (file_name, fmt_text) in result {
|
2015-08-19 14:41:19 -05:00
|
|
|
// If file is in tests/source, compare to file with same name in tests/target.
|
2015-09-17 13:21:06 -05:00
|
|
|
let target = get_target(&file_name, target);
|
2015-09-10 17:53:21 -05:00
|
|
|
let mut f = fs::File::open(&target).ok().expect("Couldn't open target.");
|
2015-05-28 12:23:07 -05:00
|
|
|
|
|
|
|
let mut text = String::new();
|
|
|
|
f.read_to_string(&mut text).ok().expect("Failed reading target.");
|
2015-09-17 13:21:06 -05:00
|
|
|
|
2015-05-28 12:23:07 -05:00
|
|
|
if fmt_text != text {
|
2015-09-10 17:27:22 -05:00
|
|
|
let diff = make_diff(&text, &fmt_text, DIFF_CONTEXT_SIZE);
|
2015-08-30 13:47:46 -05:00
|
|
|
failures.insert(file_name, diff);
|
2015-05-28 12:23:07 -05:00
|
|
|
}
|
|
|
|
}
|
2015-08-30 13:47:46 -05:00
|
|
|
|
2015-09-17 13:21:06 -05:00
|
|
|
if failures.is_empty() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(failures)
|
2015-05-28 12:23:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map source file paths to their target paths.
|
2015-08-19 14:41:19 -05:00
|
|
|
fn get_target(file_name: &str, target: Option<&str>) -> String {
|
2015-10-07 07:10:07 -05:00
|
|
|
let file_path = Path::new(file_name);
|
|
|
|
let source_path_prefix = Path::new("tests/source/");
|
|
|
|
if file_path.starts_with(source_path_prefix) {
|
|
|
|
let mut components = file_path.components();
|
|
|
|
// Can't skip(2) as the resulting iterator can't as_path()
|
|
|
|
components.next();
|
|
|
|
components.next();
|
|
|
|
|
|
|
|
let new_target = match components.as_path().to_str() {
|
|
|
|
Some(string) => string,
|
|
|
|
None => file_name,
|
|
|
|
};
|
|
|
|
let base = target.unwrap_or(new_target);
|
2015-05-28 12:23:07 -05:00
|
|
|
|
2015-08-19 14:41:19 -05:00
|
|
|
format!("tests/target/{}", base)
|
2015-05-28 12:23:07 -05:00
|
|
|
} else {
|
|
|
|
file_name.to_owned()
|
|
|
|
}
|
|
|
|
}
|