From 2ff593a6778e57aa3c9646c96ea8259f84526832 Mon Sep 17 00:00:00 2001 From: Nishiki Liu Date: Fri, 12 Nov 2021 19:50:21 -0800 Subject: [PATCH] Fix no "nothing to annotate" bug (#29) * Write tests to guard against bug * Get tests green --- lib/chusaku.rb | 6 ++++-- test/chusaku_test.rb | 16 +++++++++++----- test/mock/rails.rb | 18 ++++++++++++++---- test/test_helper.rb | 13 +++++++++++++ 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/lib/chusaku.rb b/lib/chusaku.rb index 43d1c6e..1271b29 100644 --- a/lib/chusaku.rb +++ b/lib/chusaku.rb @@ -19,6 +19,7 @@ def call(flags = {}) @flags = flags @routes = Chusaku::Routes.call @changes = [] + @changed_files = [] controllers_pattern = "app/controllers/**/*_controller.rb" Dir.glob(Rails.root.join(controllers_pattern)).each do |path| @@ -141,6 +142,7 @@ def write_to_file(path:, parsed_file:) return if parsed_file[:content] == new_content !@flags.include?(:dry) && perform_write(path: path, content: new_content) + @changed_files.push(path) end # Extracts the new file content for the given parsed file. @@ -181,7 +183,7 @@ def file_mode def output_results puts(output_copy) exit_code = 0 - exit_code = 1 if @changes.any? && @flags.include?(:error_on_annotation) + exit_code = 1 if @changed_files.any? && @flags.include?(:error_on_annotation) exit_code end @@ -189,7 +191,7 @@ def output_results # # @return [String] Copy to be outputted to user def output_copy - return "Nothing to annotate." if @changes.empty? + return "Nothing to annotate." if @changed_files.empty? copy = changes_copy copy += "\nChusaku has finished running." diff --git a/test/chusaku_test.rb b/test/chusaku_test.rb index d0189cf..24a076f 100644 --- a/test/chusaku_test.rb +++ b/test/chusaku_test.rb @@ -2,7 +2,6 @@ class ChusakuTest < Minitest::Test def test_dry_run_flag - File.reset_mock exit_code = 0 out, _err = capture_io { exit_code = Chusaku.call(dry: true) } @@ -13,7 +12,6 @@ def test_dry_run_flag end def test_exit_with_error_on_annotation_flag - File.reset_mock exit_code = 0 out, _err = capture_io { exit_code = Chusaku.call(error_on_annotation: true) } @@ -24,7 +22,6 @@ def test_exit_with_error_on_annotation_flag end def test_dry_run_and_exit_with_error_flag - File.reset_mock exit_code = 0 out, _err = capture_io { exit_code = Chusaku.call(dry: true, error_on_annotation: true) } @@ -36,7 +33,6 @@ def test_dry_run_and_exit_with_error_flag end def test_verbose_flag - File.reset_mock exit_code = 0 out, _err = capture_io { exit_code = Chusaku.call(verbose: true) } @@ -62,7 +58,6 @@ def show end def test_mock_app - File.reset_mock exit_code = 0 capture_io { exit_code = Chusaku.call } @@ -125,4 +120,15 @@ def one_off HEREDOC assert_equal(expected, files["#{base_path}/waterlilies_controller.rb"]) end + + def test_mock_app_with_no_pending_annotations + Rails.set_route_allowlist(["api/burritos#create"]) + exit_code = 0 + + out, _err = capture_io { exit_code = Chusaku.call } + + assert_equal(0, exit_code) + assert_empty(File.written_files) + assert_equal("Nothing to annotate.\n", out) + end end diff --git a/test/mock/rails.rb b/test/mock/rails.rb index e1c9cab..4c461a3 100644 --- a/test/mock/rails.rb +++ b/test/mock/rails.rb @@ -87,12 +87,20 @@ def application app = Minitest::Mock.new app_routes = Minitest::Mock.new - app_routes.expect(:routes, routes) + app_routes.expect(:routes, routes.compact) app.expect(:routes, app_routes) app end - # Lets us call `Rails.root` without an skeleton Rails app. + # Define an allowlist of controller/actions that will be mocked. + # + # @param route_allowlist [Array] In format "controller#action" + # @return [void] + def set_route_allowlist(route_allowlist) + @@route_allowlist = route_allowlist + end + + # Lets us call `Rails.root` without a skeleton Rails app. # # @return [Minitest::Mock] Mocked `Rails.root` def root @@ -114,9 +122,11 @@ def root # @param path [String] Mocked Rails path # @param name [String] Mocked route name # @param defaults [Hash] Mocked default params - # @return [Minitest::Mock] Mocked route - # + # @return [Minitest::Mock, nil] Mocked route def mock_route(controller:, action:, verb:, path:, name:, defaults: {}) + @@route_allowlist ||= [] + return if @@route_allowlist.any? && @@route_allowlist.index("#{controller}##{action}").nil? + route = Minitest::Mock.new route.expect(:defaults, controller: controller, action: action, **defaults) route.expect(:verb, verb) diff --git a/test/test_helper.rb b/test/test_helper.rb index c97b8b2..05cebc1 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -4,3 +4,16 @@ require_relative "mock/file" require "chusaku/cli" require "minitest/autorun" + +module ChusakuTestPlugin + def before_setup + super + + File.reset_mock + Rails.set_route_allowlist([]) + end +end + +class Minitest::Test + include ChusakuTestPlugin +end