Skip to content

Commit

Permalink
Add support for the RuboCop --list option
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Mar 13, 2024
1 parent 79372de commit dd23f28
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 22 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ and this project adheres to [Semantic Versioning].

## [Unreleased]

### Added

- Add support for the RuboCop `--list` option. ([@skryukov])

### Fixed

- Respect files passed to rubocop in required mode. ([@skryukov])
- Respect files passed to RuboCop in required mode. ([@skryukov])

## [0.3.4] - 2023-10-26

Expand Down
10 changes: 9 additions & 1 deletion lib/rubocop/gradual/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ class CLI
def run(argv = ARGV)
Configuration.apply(*Options.new.parse(argv))
puts "Gradual mode: #{Configuration.mode}" if Configuration.debug?
load_command(Configuration.command).call.to_i
cmd = load_command(Configuration.command)
return list_target_files(cmd) if Configuration.rubocop_options[:list_target_files]

cmd.call.to_i
end

private

def list_target_files(cmd)
cmd.lint_paths.each { |path| puts PathUtil.relative_path(path) }
1
end

def load_command(command)
require_relative "commands/#{command}"
::RuboCop::Gradual::Commands.const_get(command.to_s.capitalize).new
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/gradual/commands/autocorrect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ def call
Base.new.call
end

private

def lint_paths
return Configuration.target_file_paths if Configuration.lint_paths.any?

changed_or_untracked_files.map(&:path)
end

private

def changed_or_untracked_files
tracked_files = LockFile.new(Configuration.path).read_results&.files || []

Expand Down
10 changes: 4 additions & 6 deletions lib/rubocop/gradual/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def call
1
end

def lint_paths
Configuration.target_file_paths
end

private

def run_rubocop
Expand All @@ -36,12 +40,6 @@ def run_rubocop
rubocop_runner.run
end

def lint_paths
return [] if Configuration.lint_paths.empty?

Configuration.target_file_paths
end

def rubocop_options
Configuration.rubocop_options
.slice(:config, :debug, :display_time)
Expand Down
23 changes: 11 additions & 12 deletions lib/rubocop/gradual/patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ module Patch
def run_command(name)
return super if name != :execute_runner || (ARGV & %w[--stdin -s]).any?

Configuration.apply(*parse_options)
puts "Gradual mode: #{Configuration.mode}" if Configuration.debug?
load_command(Configuration.command).call.to_i
RuboCop::Gradual::CLI.new.run(patched_argv)
end

private

def load_command(command)
require_relative "commands/#{command}"
::RuboCop::Gradual::Commands.const_get(command.to_s.capitalize).new
end
def patched_argv
return ARGV if ARGV[0] != "gradual"

def parse_options
options, *tail_options = Options.new.parse(ARGV)
options[:mode] = :force_update if @env.paths[0..1] == %w[gradual force_update]
options[:mode] = :check if @env.paths[0..1] == %w[gradual check]
tail_options.unshift(options)
case ARGV[1]
when "force_update"
ARGV[2..] + ["--force-update"]
when "check"
ARGV[2..] + ["--check"]
else
raise ArgumentError, "Unknown gradual command #{ARGV[1]}"
end
end
end
end
Expand Down
38 changes: 38 additions & 0 deletions spec/rubocop/gradual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,42 @@
.and include("RuboCop Gradual got 13 issue(s) fixed, 9 left. Keep going!")
end
end

context "with --list option" do
let(:options) { %w[--list --gradual-file] }

it "lists project files" do
expect(gradual_cli).to eq(1)
expect($stdout.string.split("\n")).to match_array(Dir.glob("**/*.rb"))
end

context "with --autocorrect option" do
let(:options) { %w[--list --autocorrect --gradual-file] }

it "lists project files" do
expect(gradual_cli).to eq(1)
expect($stdout.string.split("\n")).to match_array(Dir.glob("**/*.rb"))
end
end

context "with --autocorrect option without changes" do
let(:options) { %w[--list --autocorrect --gradual-file] }
let(:actual_lock_path) { File.expand_path("full.lock") }

it "lists project files" do
expect(gradual_cli).to eq(1)
expect($stdout.string).to eq("")
end
end

context "with --autocorrect option and outdated lock file" do
let(:options) { %w[--list --autocorrect --gradual-file] }
let(:actual_lock_path) { File.expand_path("outdated.lock") }

it "lists project files" do
expect(gradual_cli).to eq(1)
expect($stdout.string).to eq("app/controllers/books_controller.rb\n")
end
end
end
end

0 comments on commit dd23f28

Please sign in to comment.