Skip to content

Commit ea76677

Browse files
committed
Merge pull request #908 from matklad/simplify-tests
simplify tests
2 parents 4fdf859 + 07cf62f commit ea76677

File tree

6 files changed

+115
-49
lines changed

6 files changed

+115
-49
lines changed

tests/coverage-source/comments.rs renamed to tests/coverage/source/comments.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-write_mode: coverage
12
/// Here's a doc comment!
23
fn main() {
34
// foo is bar

tests/coverage-target/comments.rs renamed to tests/coverage/target/comments.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
XX XXXXXXXXXXXXXXXXXXX XXXXXXXX
12
/// Here's a doc comment!
23
fn main() {
34
XX XXX XX XXX

tests/system.rs

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::path::{Path, PathBuf};
2020

2121
use rustfmt::*;
2222
use rustfmt::filemap::{write_system_newlines, FileMap};
23-
use rustfmt::config::{Config, ReportTactic, WriteMode};
23+
use rustfmt::config::{Config, ReportTactic};
2424
use rustfmt::rustfmt_diff::*;
2525

2626
const DIFF_CONTEXT_SIZE: usize = 3;
@@ -44,7 +44,7 @@ fn system_tests() {
4444
// Turn a DirEntry into a String that represents the relative path to the
4545
// file.
4646
let files = files.map(get_path_string);
47-
let (_reports, count, fails) = check_files(files, None);
47+
let (_reports, count, fails) = check_files(files);
4848

4949
// Display results.
5050
println!("Ran {} system tests.", count);
@@ -55,26 +55,26 @@ fn system_tests() {
5555
// the only difference is the coverage mode
5656
#[test]
5757
fn coverage_tests() {
58-
let files = fs::read_dir("tests/coverage-source").expect("Couldn't read source dir");
58+
let files = fs::read_dir("tests/coverage/source").expect("Couldn't read source dir");
5959
let files = files.map(get_path_string);
60-
let (_reports, count, fails) = check_files(files, Some(WriteMode::Coverage));
60+
let (_reports, count, fails) = check_files(files);
6161

6262
println!("Ran {} tests in coverage mode.", count);
6363
assert!(fails == 0, "{} tests failed", fails);
6464
}
6565

6666
#[test]
6767
fn checkstyle_test() {
68-
let filename = "tests/source/fn-single-line.rs";
69-
let expected_filename = "tests/writemode/checkstyle.xml";
70-
assert_output(filename, expected_filename, Some(WriteMode::Checkstyle));
68+
let filename = "tests/writemode/source/fn-single-line.rs";
69+
let expected_filename = "tests/writemode/target/checkstyle.xml";
70+
assert_output(filename, expected_filename);
7171
}
7272

7373

7474
// Helper function for comparing the results of rustfmt
7575
// to a known output file generated by one of the write modes.
76-
fn assert_output(source: &str, expected_filename: &str, write_mode: Option<WriteMode>) {
77-
let config = read_config(&source, write_mode);
76+
fn assert_output(source: &str, expected_filename: &str) {
77+
let config = read_config(&source);
7878
let (file_map, _report) = format_file(source, &config);
7979

8080
// Populate output by writing to a vec.
@@ -104,7 +104,7 @@ fn idempotence_tests() {
104104
let files = fs::read_dir("tests/target")
105105
.expect("Couldn't read target dir")
106106
.map(get_path_string);
107-
let (_reports, count, fails) = check_files(files, None);
107+
let (_reports, count, fails) = check_files(files);
108108

109109
// Display results.
110110
println!("Ran {} idempotent tests.", count);
@@ -122,7 +122,7 @@ fn self_tests() {
122122
// Hack because there's no `IntoIterator` impl for `[T; N]`.
123123
let files = files.chain(Some("src/lib.rs".to_owned()).into_iter());
124124

125-
let (reports, count, fails) = check_files(files, None);
125+
let (reports, count, fails) = check_files(files);
126126
let mut warnings = 0;
127127

128128
// Display results.
@@ -141,7 +141,7 @@ fn self_tests() {
141141

142142
// For each file, run rustfmt and collect the output.
143143
// Returns the number of files checked and the number of failures.
144-
fn check_files<I>(files: I, write_mode: Option<WriteMode>) -> (Vec<FormatReport>, u32, u32)
144+
fn check_files<I>(files: I) -> (Vec<FormatReport>, u32, u32)
145145
where I: Iterator<Item = String>
146146
{
147147
let mut count = 0;
@@ -151,7 +151,7 @@ fn check_files<I>(files: I, write_mode: Option<WriteMode>) -> (Vec<FormatReport>
151151
for file_name in files.filter(|f| f.ends_with(".rs")) {
152152
println!("Testing '{}'...", file_name);
153153

154-
match idempotent_check(file_name, write_mode) {
154+
match idempotent_check(file_name) {
155155
Ok(report) => reports.push(report),
156156
Err(msg) => {
157157
print_mismatches(msg);
@@ -176,7 +176,7 @@ fn print_mismatches(result: HashMap<String, Vec<Mismatch>>) {
176176
assert!(t.reset().unwrap());
177177
}
178178

179-
fn read_config(filename: &str, write_mode: Option<WriteMode>) -> Config {
179+
fn read_config(filename: &str) -> Config {
180180
let sig_comments = read_significant_comments(&filename);
181181
let mut config = get_config(sig_comments.get("config").map(|x| &(*x)[..]));
182182

@@ -189,10 +189,6 @@ fn read_config(filename: &str, write_mode: Option<WriteMode>) -> Config {
189189
// Don't generate warnings for to-do items.
190190
config.report_todo = ReportTactic::Never;
191191

192-
if let Some(mode) = write_mode {
193-
config.write_mode = mode
194-
}
195-
196192
config
197193
}
198194

@@ -201,11 +197,9 @@ fn format_file<P: Into<PathBuf>>(filename: P, config: &Config) -> (FileMap, Form
201197
format_input(input, &config)
202198
}
203199

204-
pub fn idempotent_check(filename: String,
205-
write_mode: Option<WriteMode>)
206-
-> Result<FormatReport, HashMap<String, Vec<Mismatch>>> {
200+
pub fn idempotent_check(filename: String) -> Result<FormatReport, HashMap<String, Vec<Mismatch>>> {
207201
let sig_comments = read_significant_comments(&filename);
208-
let config = read_config(&filename, write_mode);
202+
let config = read_config(&filename);
209203
let (file_map, format_report) = format_file(filename, &config);
210204

211205
let mut write_result = HashMap::new();
@@ -220,7 +214,7 @@ pub fn idempotent_check(filename: String,
220214

221215
let target = sig_comments.get("target").map(|x| &(*x)[..]);
222216

223-
handle_result(write_result, target, write_mode).map(|_| format_report)
217+
handle_result(write_result, target).map(|_| format_report)
224218
}
225219

226220
// Reads test config file from comments and reads its contents.
@@ -268,14 +262,13 @@ fn read_significant_comments(file_name: &str) -> HashMap<String, String> {
268262
// Compare output to input.
269263
// TODO: needs a better name, more explanation.
270264
fn handle_result(result: HashMap<String, String>,
271-
target: Option<&str>,
272-
write_mode: Option<WriteMode>)
265+
target: Option<&str>)
273266
-> Result<(), HashMap<String, Vec<Mismatch>>> {
274267
let mut failures = HashMap::new();
275268

276269
for (file_name, fmt_text) in result {
277270
// If file is in tests/source, compare to file with same name in tests/target.
278-
let target = get_target(&file_name, target, write_mode);
271+
let target = get_target(&file_name, target);
279272
let mut f = fs::File::open(&target).expect("Couldn't open target");
280273

281274
let mut text = String::new();
@@ -297,29 +290,20 @@ fn handle_result(result: HashMap<String, String>,
297290
}
298291

299292
// Map source file paths to their target paths.
300-
fn get_target(file_name: &str, target: Option<&str>, write_mode: Option<WriteMode>) -> String {
301-
let file_path = Path::new(file_name);
302-
let (source_path_prefix, target_path_prefix) = match write_mode {
303-
Some(WriteMode::Coverage) => {
304-
(Path::new("tests/coverage-source/"), "tests/coverage-target/")
293+
fn get_target(file_name: &str, target: Option<&str>) -> String {
294+
if file_name.contains("source") {
295+
let target_file_name = file_name.replace("source", "target");
296+
if let Some(replace_name) = target {
297+
Path::new(&target_file_name)
298+
.with_file_name(replace_name)
299+
.into_os_string()
300+
.into_string()
301+
.unwrap()
302+
} else {
303+
target_file_name
305304
}
306-
_ => (Path::new("tests/source/"), "tests/target/"),
307-
};
308-
309-
if file_path.starts_with(source_path_prefix) {
310-
let mut components = file_path.components();
311-
// Can't skip(2) as the resulting iterator can't as_path()
312-
components.next();
313-
components.next();
314-
315-
let new_target = match components.as_path().to_str() {
316-
Some(string) => string,
317-
None => file_name,
318-
};
319-
let base = target.unwrap_or(new_target);
320-
321-
format!("{}{}", target_path_prefix, base)
322305
} else {
306+
// This is either and idempotence check or a self check
323307
file_name.to_owned()
324308
}
325309
}

tests/writemode/checkstyle.xml

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// rustfmt-fn_single_line: true
2+
// rustfmt-write_mode: checkstyle
3+
// Test single-line functions.
4+
5+
fn foo_expr() {
6+
1
7+
}
8+
9+
fn foo_stmt() {
10+
foo();
11+
}
12+
13+
fn foo_decl_local() {
14+
let z = 5;
15+
}
16+
17+
fn foo_decl_item(x: &mut i32) {
18+
x = 3;
19+
}
20+
21+
fn empty() {
22+
23+
}
24+
25+
fn foo_return() -> String {
26+
"yay"
27+
}
28+
29+
fn foo_where() -> T where T: Sync {
30+
let x = 2;
31+
}
32+
33+
fn fooblock() {
34+
{
35+
"inner-block"
36+
}
37+
}
38+
39+
fn fooblock2(x: i32) {
40+
let z = match x {
41+
_ => 2,
42+
};
43+
}
44+
45+
fn comment() {
46+
// this is a test comment
47+
1
48+
}
49+
50+
fn comment2() {
51+
// multi-line comment
52+
let z = 2;
53+
1
54+
}
55+
56+
fn only_comment() {
57+
// Keep this here
58+
}
59+
60+
fn aaaaaaaaaaaaaaaaa_looooooooooooooooooooooong_name() {
61+
let z = "aaaaaaawwwwwwwwwwwwwwwwwwwwwwwwwwww";
62+
}
63+
64+
fn lots_of_space () {
65+
1
66+
}
67+
68+
fn mac() -> Vec<i32> { vec![] }
69+
70+
trait CoolTypes {
71+
fn dummy(&self) {
72+
}
73+
}
74+
75+
trait CoolerTypes { fn dummy(&self) {
76+
}
77+
}
78+
79+
fn Foo<T>() where T: Bar {
80+
}

tests/writemode/target/checkstyle.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<checkstyle version="4.3"><file name="tests/writemode/source/fn-single-line.rs"><error line="2" severity="warning" message="Should be `fn foo_expr() { 1 }`" /><error line="2" severity="warning" message="Should be `fn foo_stmt() { foo(); }`" /><error line="2" severity="warning" message="Should be `fn foo_decl_local() { let z = 5; }`" /><error line="2" severity="warning" message="Should be `fn foo_decl_item(x: &amp;mut i32) { x = 3; }`" /><error line="2" severity="warning" message="Should be `fn empty() {}`" /><error line="2" severity="warning" message="Should be `fn foo_return() -&gt; String { &quot;yay&quot; }`" /><error line="2" severity="warning" message="Should be `fn foo_where() -&gt; T`" /><error line="2" severity="warning" message="Should be ` where T: Sync`" /><error line="2" severity="warning" message="Should be `{`" /><error line="51" severity="warning" message="Should be `fn lots_of_space() { 1 }`" /><error line="58" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="58" severity="warning" message="Should be `trait CoolerTypes {`" /><error line="58" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="58" severity="warning" message="Should be `fn Foo&lt;T&gt;() where T: Bar {}`" /></file></checkstyle>

0 commit comments

Comments
 (0)