Skip to content

Refactor run family of functions #897

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 6 commits into from
Apr 4, 2016
Merged

Conversation

matklad
Copy link
Member

@matklad matklad commented Apr 2, 2016

Hi! This is a bit of refactoring to ease the implementation of #150.

The intent is to reduce the duplication between run, run_from_stdin and, ideally, run_rustfmt from the test crate.

matklad added 2 commits April 2, 2016 19:36
This also unifies `write_all_files` and `write_file` functions
pub fn run(input: Input, config: &Config) {
let (file_map, report) = format_input(input, config);

let ignore_errors = config.write_mode == WriteMode::Plain;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This roughly matches the previous behavior, but it feels ugly.

The behavior was

  • swallow errors when reading from stdin (presumably because the errors and the output are written to stdout)
  • print errors to stdout even with --write-mode plain which kinda contradicts the reasoning behind the previous bullet

Now we swallow errors every time we print to stdout.

Note that both versions have the following bug: with --write-mode checkstyle errors are printed to stdout alongside the checkstyle xml.

Maybe we should simply print errors to stderr? Parsing errors from libsyntax go to the stderr anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we should definitely write errors to stderr instead of stdout

@matklad matklad changed the title Refactor run Refactor run family of functions Apr 2, 2016
@@ -1,2 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<checkstyle version="4.3"><file name="tests/source/fn-single-line.rs"><error line="1" severity="warning" message="Should be `fn foo_expr() { 1 }`" /><error line="1" severity="warning" message="Should be `fn foo_stmt() { foo(); }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_local() { let z = 5; }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_item(x: &amp;mut i32) { x = 3; }`" /><error line="1" severity="warning" message="Should be `fn empty() {}`" /><error line="1" severity="warning" message="Should be `fn foo_return() -&gt; String { &quot;yay&quot; }`" /><error line="1" severity="warning" message="Should be `fn foo_where() -&gt; T`" /><error line="1" severity="warning" message="Should be ` where T: Sync`" /><error line="1" severity="warning" message="Should be `{`" /><error line="50" severity="warning" message="Should be `fn lots_of_space() { 1 }`" /><error line="57" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `trait CoolerTypes {`" /><error line="57" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `fn Foo&lt;T&gt;() where T: Bar {}`" /><error line="57" severity="warning" message="Should be ``" /></file></checkstyle>
<checkstyle version="4.3"><file name="tests/source/fn-single-line.rs"><error line="1" severity="warning" message="Should be `fn foo_expr() { 1 }`" /><error line="1" severity="warning" message="Should be `fn foo_stmt() { foo(); }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_local() { let z = 5; }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_item(x: &amp;mut i32) { x = 3; }`" /><error line="1" severity="warning" message="Should be `fn empty() {}`" /><error line="1" severity="warning" message="Should be `fn foo_return() -&gt; String { &quot;yay&quot; }`" /><error line="1" severity="warning" message="Should be `fn foo_where() -&gt; T`" /><error line="1" severity="warning" message="Should be ` where T: Sync`" /><error line="1" severity="warning" message="Should be `{`" /><error line="50" severity="warning" message="Should be `fn lots_of_space() { 1 }`" /><error line="57" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `trait CoolerTypes {`" /><error line="57" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `fn Foo&lt;T&gt;() where T: Bar {}`" /></file></checkstyle>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<error line="57" severity="warning" message="Should be ``" /> bit at the very end is removed.

I don't know if this introduces or fixes a bug in a test case :)

@matklad
Copy link
Member Author

matklad commented Apr 2, 2016

Is there any reason to special case this write_mode Option which is threaded throughout the test code? Can we use comment syntax for it, as for all other options?

@nrc
Copy link
Member

nrc commented Apr 4, 2016

Is there any reason to special case this write_mode Option which is threaded throughout the test code? Can we use comment syntax for it, as for all other options?

AFAIK, there is no reason to special case and we can probably use the comment syntax.

@@ -403,7 +403,7 @@ pub fn format_string(input: String, config: &Config) -> FileMap {
file_map
}

pub fn format(file: &Path, config: &Config) -> FileMap {
fn format(file: &Path, config: &Config) -> FileMap {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rename format to format_file please? "format" seems too generic for a function which isn't API

@nrc
Copy link
Member

nrc commented Apr 4, 2016

Thanks! r+ with the two renamings.

@matklad
Copy link
Member Author

matklad commented Apr 4, 2016

Renamed! FYI, this was the first automated refactoring performed with the help of IntelliJ Rust (here is the implementation) :)

@nrc
Copy link
Member

nrc commented Apr 4, 2016

Thanks for the changes, and nice tool!

@nrc nrc merged commit 4fdf859 into rust-lang:master Apr 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants