diff --git a/CHANGELOG.md b/CHANGELOG.md index f405bcb..19afe31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/gradual/cli.rb b/lib/rubocop/gradual/cli.rb index 8545ddc..88baf76 100644 --- a/lib/rubocop/gradual/cli.rb +++ b/lib/rubocop/gradual/cli.rb @@ -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 diff --git a/lib/rubocop/gradual/commands/autocorrect.rb b/lib/rubocop/gradual/commands/autocorrect.rb index b444c43..ce6cea3 100644 --- a/lib/rubocop/gradual/commands/autocorrect.rb +++ b/lib/rubocop/gradual/commands/autocorrect.rb @@ -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 || [] diff --git a/lib/rubocop/gradual/commands/base.rb b/lib/rubocop/gradual/commands/base.rb index 5be75bd..75826e2 100644 --- a/lib/rubocop/gradual/commands/base.rb +++ b/lib/rubocop/gradual/commands/base.rb @@ -23,6 +23,10 @@ def call 1 end + def lint_paths + Configuration.target_file_paths + end + private def run_rubocop @@ -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) diff --git a/lib/rubocop/gradual/patch.rb b/lib/rubocop/gradual/patch.rb index 78c867a..e1219e0 100644 --- a/lib/rubocop/gradual/patch.rb +++ b/lib/rubocop/gradual/patch.rb @@ -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 diff --git a/spec/rubocop/gradual_spec.rb b/spec/rubocop/gradual_spec.rb index 802ea15..468fe6b 100644 --- a/spec/rubocop/gradual_spec.rb +++ b/spec/rubocop/gradual_spec.rb @@ -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