Merge pull request #1122 from studoot/add-test-for-1111

Add test case for issue #1111
This commit is contained in:
Nick Cameron 2016-08-10 18:12:16 +12:00 committed by GitHub
commit 9ed9618f1c
5 changed files with 31 additions and 7 deletions

View File

@ -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!

1
tests/config/issue-1111.toml Executable file
View File

@ -0,0 +1 @@
reorder_imports = true

View File

@ -0,0 +1 @@
use bar;

View File

@ -205,7 +205,17 @@ fn print_mismatches(result: HashMap<String, Vec<Mismatch>>) {
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)[..]));
// 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 {
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" {
@ -246,13 +256,18 @@ pub fn idempotent_check(filename: String) -> Result<FormatReport, HashMap<String
handle_result(write_result, target).map(|_| format_report)
}
// Reads test config file from comments and reads its contents.
// Reads test config file using the supplied (optional) file name. If there's no file name or the
// file doesn't exist, just return the default config. Otherwise, the file must be read
// successfully.
fn get_config(config_file: Option<&str>) -> Config {
let config_file_name = match config_file {
None => return Default::default(),
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
}
};

View File

@ -0,0 +1 @@
use bar;