Skip to content

Commit

Permalink
Fix no "nothing to annotate" bug (#29)
Browse files Browse the repository at this point in the history
* Write tests to guard against bug

* Get tests green
  • Loading branch information
nshki authored Nov 13, 2021
1 parent f456f91 commit 2ff593a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
6 changes: 4 additions & 2 deletions lib/chusaku.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -181,15 +183,15 @@ 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

# Determines the copy to be used in the program output.
#
# @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."
Expand Down
16 changes: 11 additions & 5 deletions test/chusaku_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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) }
Expand All @@ -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) }
Expand All @@ -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) }
Expand All @@ -62,7 +58,6 @@ def show
end

def test_mock_app
File.reset_mock
exit_code = 0

capture_io { exit_code = Chusaku.call }
Expand Down Expand Up @@ -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
18 changes: 14 additions & 4 deletions test/mock/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>] 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
Expand All @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2ff593a

Please sign in to comment.