From cb0b7108ca32caacb4c7d7c8beff24966d2485e1 Mon Sep 17 00:00:00 2001 From: Stuart Dootson Date: Mon, 8 Aug 2016 23:13:45 +0200 Subject: [PATCH 1/3] Add test case for issue #1111, by adding another route by which a test file's config can be located --- tests/config/issue-1111.toml | 1 + tests/source/issue-1111.rs | 1 + tests/system.rs | 9 ++++++++- tests/target/issue-1111.rs | 1 + 4 files changed, 11 insertions(+), 1 deletion(-) create mode 100755 tests/config/issue-1111.toml create mode 100644 tests/source/issue-1111.rs create mode 100644 tests/target/issue-1111.rs diff --git a/tests/config/issue-1111.toml b/tests/config/issue-1111.toml new file mode 100755 index 000000000000..44148a2d3c3e --- /dev/null +++ b/tests/config/issue-1111.toml @@ -0,0 +1 @@ +reorder_imports = true diff --git a/tests/source/issue-1111.rs b/tests/source/issue-1111.rs new file mode 100644 index 000000000000..2e1a89ad78eb --- /dev/null +++ b/tests/source/issue-1111.rs @@ -0,0 +1 @@ +use bar; diff --git a/tests/system.rs b/tests/system.rs index 08ec576f2218..0e24505260e2 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -205,7 +205,11 @@ fn print_mismatches(result: HashMap>) { fn read_config(filename: &str) -> Config { let sig_comments = read_significant_comments(&filename); - let mut config = get_config(sig_comments.get("config").map(|x| &(*x)[..])); + let mut config = if !sig_comments.is_empty() { + get_config(sig_comments.get("config").map(|x| &(*x)[..])) + } else { + get_config(Path::new(filename).with_extension("toml").file_name().and_then(std::ffi::OsStr::to_str)) + }; for (key, val) in &sig_comments { if key != "target" && key != "config" { @@ -253,6 +257,9 @@ fn get_config(config_file: Option<&str>) -> Config { Some(file_name) => { let mut full_path = "tests/config/".to_owned(); full_path.push_str(&file_name); + if !Path::new(&full_path).exists() { + return Default::default(); + }; full_path } }; diff --git a/tests/target/issue-1111.rs b/tests/target/issue-1111.rs new file mode 100644 index 000000000000..2e1a89ad78eb --- /dev/null +++ b/tests/target/issue-1111.rs @@ -0,0 +1 @@ +use bar; From 4055e272da45e382cdf06e37c3bb3a57df4a85f7 Mon Sep 17 00:00:00 2001 From: Stuart Dootson Date: Tue, 9 Aug 2016 22:10:48 +0200 Subject: [PATCH 2/3] Reformat the source to actually pass the tests! --- tests/system.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/system.rs b/tests/system.rs index 0e24505260e2..1e690c647a1d 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -208,8 +208,11 @@ fn read_config(filename: &str) -> Config { let mut config = if !sig_comments.is_empty() { get_config(sig_comments.get("config").map(|x| &(*x)[..])) } else { - get_config(Path::new(filename).with_extension("toml").file_name().and_then(std::ffi::OsStr::to_str)) - }; + get_config(Path::new(filename) + .with_extension("toml") + .file_name() + .and_then(std::ffi::OsStr::to_str)) + }; for (key, val) in &sig_comments { if key != "target" && key != "config" { From 5dda986a2c6bd7e16f4f5a874de7bd7455d06c63 Mon Sep 17 00:00:00 2001 From: Stuart Dootson Date: Tue, 9 Aug 2016 22:11:27 +0200 Subject: [PATCH 3/3] Add commentary --- Contributing.md | 16 +++++++++++----- tests/system.rs | 7 ++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Contributing.md b/Contributing.md index 50b489cc1300..7c66d27da410 100644 --- a/Contributing.md +++ b/Contributing.md @@ -44,17 +44,23 @@ colourised diff will be printed so that the offending line(s) can quickly be identified. Without explicit settings, the tests will be run using rustfmt's default -configuration. It is possible to run a test using non-default settings by -including configuration parameters in comments at the top of the file. For -example: to use 3 spaces per tab, start your test with +configuration. It is possible to run a test using non-default settings in several +ways. Firstly, you can include configuration parameters in comments at the top +of the file. For example: to use 3 spaces per tab, start your test with `// rustfmt-tab_spaces: 3`. Just remember that the comment is part of the input, so include in both the source and target files! It is also possible to explicitly specify the name of the expected output file in the target directory. -Use `// rustfmt-target: filename.rs` for this. Finally, you can use a custom +Use `// rustfmt-target: filename.rs` for this. You can also specify a custom configuration by using the `rustfmt-config` directive. Rustfmt will then use that toml file located in `./tests/config/` for its configuration. Including `// rustfmt-config: small_tabs.toml` will run your test with the configuration -file found at `./tests/config/small_tabs.toml`. +file found at `./tests/config/small_tabs.toml`. The final option is used when the +test source file contains no configuration parameter comments. In this case, the +test harness looks for a configuration file with the same filename as the test +file in the `./tests/config/` directory, so a test source file named `test-indent.rs` +would need a configuration file named `test-indent.toml` in that directory. As an +example, the `issue-1111.rs` test file is configured by the file +`./tests/config/issue-1111.toml`. ## Hack! diff --git a/tests/system.rs b/tests/system.rs index 1e690c647a1d..ce52bf5fd697 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -205,6 +205,9 @@ fn print_mismatches(result: HashMap>) { fn read_config(filename: &str) -> Config { let sig_comments = read_significant_comments(&filename); + // Look for a config file... If there is a 'config' property in the significant comments, use + // that. Otherwise, if there are no significant comments at all, look for a config file with + // the same name as the test file. let mut config = if !sig_comments.is_empty() { get_config(sig_comments.get("config").map(|x| &(*x)[..])) } else { @@ -253,7 +256,9 @@ pub fn idempotent_check(filename: String) -> Result) -> Config { let config_file_name = match config_file { None => return Default::default(),