Skip to content

Commit

Permalink
Merge pull request #270 from nicksieger/use-rubocop-execute-runner
Browse files Browse the repository at this point in the history
Use RuboCop::CLI::Command::ExecuteRunner
  • Loading branch information
searls authored Mar 5, 2021
2 parents 6851272 + e872688 commit 341604d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
8 changes: 1 addition & 7 deletions lib/standard/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

module Standard
class Cli
SUCCESS_STATUS_CODE = 0
FAILURE_STATUS_CODE = 1

def initialize(argv)
@argv = argv
@builds_config = BuildsConfig.new
Expand All @@ -14,10 +11,7 @@ def initialize(argv)

def run
config = @builds_config.call(@argv)

success = @loads_runner.call(config.runner).call(config)

success ? SUCCESS_STATUS_CODE : FAILURE_STATUS_CODE
@loads_runner.call(config.runner).call(config)
end
end
end
3 changes: 2 additions & 1 deletion lib/standard/runners/genignore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def call(config)
config.rubocop_options[:formatters] = [["files", temp_file.path]]
config.rubocop_options[:format] = "files"
config.rubocop_options[:out] = temp_file.path
Runners::Rubocop.new.call(config)
exit_code = Runners::Rubocop.new.call(config)

# Read in the files with errors. It will have the absolute paths
# of the files but we only want the relative path.
Expand All @@ -37,6 +37,7 @@ def call(config)
file.puts "# Remove from this list as you refactor files."
file.write(yaml_format_errors.to_yaml)
end
exit_code
ensure
# Clean up temp file.
temp_file.close
Expand Down
35 changes: 7 additions & 28 deletions lib/standard/runners/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,15 @@ module Standard
module Runners
class Rubocop
def call(config)
rubocop_runner = RuboCop::Runner.new(
config.rubocop_options,
config.rubocop_config_store
rubocop_runner = RuboCop::CLI::Command::ExecuteRunner.new(
RuboCop::CLI::Environment.new(
config.rubocop_options,
config.rubocop_config_store,
config.paths
)
)

rubocop_runner.run(config.paths).tap do |success|
print_errors_and_warnings(success, rubocop_runner)
print_corrected_code_if_fixing_stdin(config.rubocop_options)
end
end

private

def print_errors_and_warnings(success, rubocop_runner)
return unless success

(rubocop_runner.warnings + rubocop_runner.errors).each do |message|
warn message
end
end

def print_corrected_code_if_fixing_stdin(rubocop_options)
return unless rubocop_options[:stdin] && rubocop_options[:auto_correct]

if rubocop_options[:stderr]
warn "=" * 20
else
puts "=" * 20
end
print rubocop_options[:stdin]
rubocop_runner.run
end
end
end
Expand Down
33 changes: 32 additions & 1 deletion test/standard/runners/rubocop_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_print_corrected_output_on_stdin
))
}

expected_out = <<-OUT.gsub(/^ {6}/, "")
expected_out = <<~OUT
== test/fixture/runner/agreeable.rb ==
C: 1: 1: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
C: 1: 1: [Corrected] Style/SingleLineMethods: Avoid single-line method definitions.
Expand All @@ -66,6 +66,37 @@ def Foo
assert_equal "", fake_err.string
end

def test_print_corrected_output_on_stdin_with_corrections_on_stderr
fake_out, fake_err = do_with_fake_io {
@subject.call(create_config(
auto_correct: true,
safe_auto_correct: true,
stderr: true,
stdin: "def Foo;'hi'end\n"
))
}

expected_out = <<~OUT
def Foo
'hi'
end
OUT
expected_err = <<~ERR
== test/fixture/runner/agreeable.rb ==
C: 1: 1: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
C: 1: 1: [Corrected] Style/SingleLineMethods: Avoid single-line method definitions.
C: 1: 5: Naming/MethodName: Use snake_case for method names.
C: 1: 8: [Corrected] Layout/SpaceAfterSemicolon: Space missing after semicolon.
C: 1: 8: [Corrected] Style/Semicolon: Do not use semicolons to terminate expressions.
C: 1: 9: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
1 file inspected, 6 offenses detected, 4 offenses corrected, 1 more offense can be corrected with `rubocop -A`
====================
ERR
assert_equal expected_out, fake_out.string
assert_equal expected_err, fake_err.string
end

private

def create_config(options = {}, path = "test/fixture/runner/agreeable.rb")
Expand Down

0 comments on commit 341604d

Please sign in to comment.