Skip to content

Commit

Permalink
Add --verbose-version (-V) flag
Browse files Browse the repository at this point in the history
This addresses the second feature mentioned in standardrb#452. For context,
when debugging a RuboCop issue a user is asked to supply the
output of `rubocop -V`, and this streamlines this process for
standard users.
  • Loading branch information
patrickbrown-dev committed Sep 28, 2022
1 parent 830205c commit 635c384
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 18 deletions.
16 changes: 13 additions & 3 deletions lib/standard/loads_runner.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
require_relative "runners/rubocop"
require_relative "runners/version"
require_relative "runners/verbose_version"
require_relative "runners/help"

module Standard
class LoadsRunner
# Warning: clever metaprogramming. 99% of the time this is Runners::Rubocop
RUNNERS = {
rubocop: ::Standard::Runners::Rubocop,
version: ::Standard::Runners::Version,
verbose_version: ::Standard::Runners::VerboseVersion,
help: ::Standard::Runners::Help
}.freeze

def call(command)
require_relative "runners/#{command}"
::Standard::Runners.const_get(command.to_s.capitalize).new
RUNNERS[command].new
end
end
end
4 changes: 3 additions & 1 deletion lib/standard/merges_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def call(argv, standard_yaml)

def separate_argv(argv)
argv.partition do |flag|
["--generate-todo", "--fix", "--no-fix", "--version", "-v", "--help", "-h"].include?(flag)
["--generate-todo", "--fix", "--no-fix", "--version", "-v", "--verbose-version", "-V", "--help", "-h"].include?(flag)
end
end

Expand All @@ -39,6 +39,8 @@ def determine_command(argv)
:help
elsif (argv & ["--version", "-v"]).any?
:version
elsif (argv & ["--verbose-version", "-V"]).any?
:verbose_version
elsif (argv & ["--generate-todo"]).any?
:genignore
else
Expand Down
15 changes: 8 additions & 7 deletions lib/standard/runners/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ def call(config)
Options:
--fix Automatically fix failures where possible
--no-fix Do not automatically fix failures
--format <name> Format output with any RuboCop formatter (e.g. "json")
--generate-todo Create a .standard_todo.yml that lists all the files that contain errors
-v, --version Print the version of Standard
-h, --help Print this message
FILE Files to lint [default: ./]
--fix Automatically fix failures where possible
--no-fix Do not automatically fix failures
--format <name> Format output with any RuboCop formatter (e.g. "json")
--generate-todo Create a .standard_todo.yml that lists all the files that contain errors
-v, --version Print the version of Standard
-V, --verbose-version Print the version of Standard and its dependencies.
-h, --help Print this message
FILE Files to lint [default: ./]
Standard also forwards most CLI arguments to RuboCop. To see them, run:
Expand Down
14 changes: 14 additions & 0 deletions lib/standard/runners/verbose_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require_relative "rubocop"

module Standard
module Runners
class VerboseVersion
def call(config)
puts <<-MSG.gsub(/^ {10}/, "")
Standard version: #{Standard::VERSION}
RuboCop version: #{RuboCop::Version.version(debug: true)}
MSG
end
end
end
end
6 changes: 6 additions & 0 deletions test/standard/loads_runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def test_version
assert_instance_of ::Standard::Runners::Version, result
end

def test_verbose_version
result = @subject.call(:verbose_version)

assert_instance_of ::Standard::Runners::VerboseVersion, result
end

def test_help
result = @subject.call(:help)

Expand Down
15 changes: 8 additions & 7 deletions test/standard/runners/help_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ def test_prints_help
Options:
--fix Automatically fix failures where possible
--no-fix Do not automatically fix failures
--format <name> Format output with any RuboCop formatter (e.g. "json")
--generate-todo Create a .standard_todo.yml that lists all the files that contain errors
-v, --version Print the version of Standard
-h, --help Print this message
FILE Files to lint [default: ./]
--fix Automatically fix failures where possible
--no-fix Do not automatically fix failures
--format <name> Format output with any RuboCop formatter (e.g. "json")
--generate-todo Create a .standard_todo.yml that lists all the files that contain errors
-v, --version Print the version of Standard
-V, --verbose-version Print the version of Standard and its dependencies.
-h, --help Print this message
FILE Files to lint [default: ./]
Standard also forwards most CLI arguments to RuboCop. To see them, run:
Expand Down
31 changes: 31 additions & 0 deletions test/standard/runners/verbose_version_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require_relative "../../test_helper"

require "standard/runners/verbose_version"

class Standard::Runners::VerboseVersionTest < UnitTest
def setup
@subject = Standard::Runners::VerboseVersion.new
end

def test_verbose_version
fake_out, _ = do_with_fake_io {
@subject.call(nil)
}

expect = <<-EXPECT.gsub(/^ {6}/, "")
Standard version: #{Standard::VERSION}
RuboCop version: #{RuboCop::Version.version(debug: true)}
EXPECT

assert_equal expect, fake_out.string
end

def test_includes_rubocop_sub_dependencies
fake_out, _ = do_with_fake_io {
@subject.call(nil)
}

assert_match(/Parser/, fake_out.string)
assert_match(/rubocop-ast/, fake_out.string)
end
end

0 comments on commit 635c384

Please sign in to comment.