diff --git a/README.md b/README.md index 79ca832..785191c 100644 --- a/README.md +++ b/README.md @@ -52,12 +52,13 @@ Chusaku has some flags available for use as well: ``` $ bundle exec chusaku --help Usage: chusaku [options] - --dry-run Run without file modifications - --exit-with-error-on-annotation - Fail if any file was annotated - --verbose Print all annotations - -v, --version Show Chusaku version number and quit - -h, --help Show this help message and quit + --dry-run Run without file modifications + --exit-with-error-on-annotation Fail if any file was annotated + -c, --controllers-pattern=GLOB Specify alternative controller files glob pattern + --verbose Print all annotations + -v, --version Show Chusaku version number and quit + -h, --help Show this help message and quit + ``` diff --git a/lib/chusaku.rb b/lib/chusaku.rb index 1271b29..834c20b 100644 --- a/lib/chusaku.rb +++ b/lib/chusaku.rb @@ -20,7 +20,7 @@ def call(flags = {}) @routes = Chusaku::Routes.call @changes = [] @changed_files = [] - controllers_pattern = "app/controllers/**/*_controller.rb" + controllers_pattern = @flags[:controllers_pattern] || "app/controllers/**/*_controller.rb" Dir.glob(Rails.root.join(controllers_pattern)).each do |path| controller = %r{controllers/(.*)_controller\.rb}.match(path)[1] diff --git a/lib/chusaku/cli.rb b/lib/chusaku/cli.rb index b076d19..b2963ad 100644 --- a/lib/chusaku/cli.rb +++ b/lib/chusaku/cli.rb @@ -52,8 +52,10 @@ def check_for_rails_project def optparser OptionParser.new do |opts| opts.banner = "Usage: chusaku [options]" + opts.set_summary_width(35) add_dry_run_flag(opts) add_error_on_annotation_flag(opts) + add_controllers_pattern_flag(opts) add_verbose_flag(opts) add_version_flag(opts) add_help_flag(opts) @@ -80,6 +82,16 @@ def add_error_on_annotation_flag(opts) end end + # Adds `--controllers-pattern` flag. + # + # @param opts [OptionParser] OptionParser instance + # @return [void] + def add_controllers_pattern_flag(opts) + opts.on("-c", "--controllers-pattern", "=GLOB", "Specify alternative controller files glob pattern") do |value| + @options[:controllers_pattern] = value + end + end + # Adds `--verbose` flag. # # @param opts [OptionParser] OptionParser instance diff --git a/test/chusaku_test.rb b/test/chusaku_test.rb index 065e9bd..f3e0644 100644 --- a/test/chusaku_test.rb +++ b/test/chusaku_test.rb @@ -144,4 +144,15 @@ def test_mock_app_with_no_pending_annotations assert_empty(File.written_files) assert_equal("Nothing to annotate.\n", out) end + + def test_mock_app_with_non_matching_controllers_pattern + exit_code = 0 + + args = {controllers_pattern: "**/nomatch/*_controller.rb"} + out, = capture_io { exit_code = Chusaku.call(args) } + + assert_equal(0, exit_code) + assert_empty(File.written_files) + assert_equal("Nothing to annotate.\n", out) + end end diff --git a/test/cli_test.rb b/test/cli_test.rb index 240fa35..83bd185 100644 --- a/test/cli_test.rb +++ b/test/cli_test.rb @@ -76,4 +76,24 @@ def test_verbose_flag assert_equal({verbose: true}, cli.options) end end + + def test_controllers_pattern_flag + # --controllers-pattern + cli = Chusaku::CLI.new + cli.stub(:check_for_rails_project, nil) do + capture_io do + assert_equal(0, cli.call(["--controllers-pattern=**/controllers/**/*_controller.rb"])) + end + assert_equal({controllers_pattern: "**/controllers/**/*_controller.rb"}, cli.options) + end + + # -c + cli = Chusaku::CLI.new + cli.stub(:check_for_rails_project, nil) do + capture_io do + assert_equal(0, cli.call(["-c", "**/controllers/**/*_controller.rb"])) + end + assert_equal({controllers_pattern: "**/controllers/**/*_controller.rb"}, cli.options) + end + end end diff --git a/test/mock/rails.rb b/test/mock/rails.rb index 05e1b01..3c2771a 100644 --- a/test/mock/rails.rb +++ b/test/mock/rails.rb @@ -4,6 +4,9 @@ # for each version. # # The mocks used should reflect the files located in `test/mock/app/`. + +require "pathname" + module Rails class << self # Lets us call `Rails.application.routes.routes` without a skeleton Rails @@ -116,14 +119,9 @@ def set_route_allowlist(route_allowlist) # Lets us call `Rails.root` without a skeleton Rails app. # - # @return [Minitest::Mock] Mocked `Rails.root` + # @return [Pathname] Pathname object like Rails.root def root - rails_root = Minitest::Mock.new - rails_root.expect \ - :join, - "test/mock/app/controllers/**/*_controller.rb", - [String] - rails_root + Pathname.new("test/mock") end private