rubocop / guard-rubocop

Guard plugin for RuboCop
MIT License
262 stars 55 forks source link

Always outputs to STDOUT even if --out specified #14

Closed aeberlin closed 9 years ago

aeberlin commented 9 years ago

Hi,

I recently discovered a bug in the runner which results in output being piped to $stdout even if the --out option is specified in the cli. Here are the steps to replicate:

RUBOCOP_OPTS = {
  all_on_start: false,
  cli: '--format RuboCop::Formatter::ClangStyleFormatter --out tmp/rubocop_results.log',
  notification: false
}

Notice how output for the default formatter is redirected to $stdout in addition to the results for ProgressFormatter being written to tmp/progress_results.log.

For comparison, run a few of the statements below from bash and review the results.

rubocop --format RuboCop::Formatter::ProgressFormatter --out tmp/progress_results.log lib/guard_example/foo.rb
rubocop --out tmp/default_results.log --format RuboCop::Formatter::ClangStyleFormatter --out tmp/clang_results.log lib/guard_example/foo.rb

As you can see, the default formatter is properly omitted in the first example and then properly retained in the second example, with the specified formatter results always written to the designated path.

If you try to forcibly redirect output for the default formatter to another file, using the configuration below, you can also see that the results are still piped to $stdout.

RUBOCOP_OPTS = {
  all_on_start: false,
  cli: '--out tmp/default_results.log --format RuboCop::Formatter::ClangStyleFormatter --out tmp/progress_results.log',
  notification: true
}

Any thoughts?

Thanks, cheers!

aeberlin commented 9 years ago

Also, here are the versions I'm using. I will test later to see if this occurs with previous versions:

bundler (1.7.0)
cucumber (1.3.17)
guard (2.6.1)
guard-cucumber (1.4.1)
guard-rspec (4.3.1)
guard-rubocop (1.1.0)
guard-shell (0.6.1)
guard-shotgun (0.4.0)
rake (10.1.0)
rspec (3.1.0, 3.0.0)
rspec-core (3.1.3, 3.1.2, 3.0.4)
rspec-expectations (3.1.1, 3.1.0, 3.0.4)
rspec-mocks (3.1.0, 3.0.4)
rspec-support (3.1.0, 3.0.4)
rubocop (0.26.1, 0.26.0, 0.25.0)
terminal-notifier-guard (1.5.3)
aeberlin commented 9 years ago

Looking through the source, I believe the problem lies here.

# lib/guard/rubocop/runner.rb

def include_formatter_for_console?(cli_args)
  index = -1
  formatter_args = cli_args.group_by do |arg|
    index += 1 if arg == '--format' || arg.start_with?('-f')
    index
  end
  formatter_args.delete(-1)

  formatter_args.each_value.any? do |args|
    args.none? { |a| a == '--out' || a.start_with?('-o') }
  end
end

The way the code is written, it expects that you will always want console output if an additional formatter and output source is specified, but that's not always the case. Please see the pull request in Issue #15.

dolfolife commented 9 years ago

:+1:

ashtonthomas commented 9 years ago

:+1:

aeberlin commented 9 years ago

Are you active?

yujinakayama commented 9 years ago

Closing as #15 has been merged.