Skip to content

Commit

Permalink
fix: unnecessary verbose output (#45)
Browse files Browse the repository at this point in the history
# Issue

Closes #43.

# Overview

This PR changes/fixes the behavior of `--verbose` such that instead of
printing out all changes on the given run, it will list out all changed
files.
  • Loading branch information
nshki authored Jan 6, 2024
1 parent 82d9e22 commit bef33cd
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 71 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0.0
ruby-version: 3.3
bundler-cache: true
- name: Run Standard
run: bundle exec standardrb --fail-level A
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ["2.7", "3.0", "3.1", "3.2", "3.3"]
ruby-version: ["3.1", "3.2", "3.3"]
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ Usage: chusaku [options]
--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
--verbose Print all annotated files
-v, --version Show Chusaku version number and quit
-h, --help Show this help message and quit
```

### Rake usage
Expand Down
53 changes: 5 additions & 48 deletions lib/chusaku.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class << self
def call(flags = {})
@flags = flags
@routes = Chusaku::Routes.call
@changes = []
@changed_files = []
controllers_pattern = @flags[:controllers_pattern] || "app/controllers/**/*_controller.rb"

Expand Down Expand Up @@ -53,46 +52,18 @@ def load_tasks
def annotate_file(path:, controller:, actions:)
parsed_file = Chusaku::Parser.call(path: path, actions: actions)
parsed_file[:groups].each_cons(2) do |prev, curr|
record_change(group: prev, type: :clean, path: path)
clean_group(prev)
next unless curr[:type] == :action

route_data = @routes[controller][curr[:action]]
next unless route_data.any?

record_change(group: curr, type: :annotate, route_data: route_data, path: path)
annotate_group(group: curr, route_data: route_data)
end

write_to_file(path: path, parsed_file: parsed_file)
end

# Clean or annotate a group and track the group as changed if applicable.
#
# @param group [Hash] { type => Symbol, body => String }
# @param type [Symbol] [:clean, :annotate]
# @param path [String] File path
# @param route_data [Array<Hash>] [{
# verb: String,
# path: String,
# name: String }]
# @return [void]
def record_change(group:, type:, path:, route_data: [])
old_body = group[:body]

case type
when :clean
clean_group(group)
when :annotate
annotate_group(group: group, route_data: route_data)
end
return if old_body == group[:body]

@changes.push \
old_body: old_body,
new_body: group[:body],
path: path,
line_number: group[:line_number]
end

# Given a parsed group, clean out its contents.
#
# @param group [Hash] { type => Symbol, body => String }
Expand Down Expand Up @@ -209,27 +180,13 @@ def output_copy
copy
end

# Returns the copy for recorded changes if `--verbose` flag is passed.
# Returns the copy for changed files if `--verbose` flag is passed.
#
# @return [String] Copy of recorded changes
# @return [String] Copy for changed files
def changes_copy
return "" unless @flags.include?(:verbose)

@changes.map do |change|
<<~CHANGE_OUTPUT
[#{change[:path]}:#{change[:line_number]}]
Before:
```ruby
#{change[:old_body].chomp}
```
After:
```ruby
#{change[:new_body].chomp}
```
CHANGE_OUTPUT
end.join("\n") + "\n"
@changed_files.map { |file| "Annotated #{file}" }.join("\n") + "\n"
end
end
end
2 changes: 1 addition & 1 deletion lib/chusaku/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def add_controllers_pattern_flag(opts)
# @param opts [OptionParser] OptionParser instance
# @return [void]
def add_verbose_flag(opts)
opts.on("--verbose", "Print all annotations") do
opts.on("--verbose", "Print all annotated files") do
@options[:verbose] = true
end
end
Expand Down
21 changes: 4 additions & 17 deletions test/chusaku_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,10 @@ def test_verbose_flag
out, _err = capture_io { exit_code = Chusaku.call(verbose: true) }

assert_equal(0, exit_code)
assert_includes \
out,
<<~CHANGES_COPY
[test/mock/app/controllers/api/tacos_controller.rb:2]
Before:
```ruby
def show
```
After:
```ruby
# @route GET / (root)
# @route GET /api/tacos/:id (taco)
def show
```
CHANGES_COPY
refute_includes(out, "Annotated test/mock/app/controllers/api/burritos_controller.rb")
assert_includes(out, "Annotated test/mock/app/controllers/api/cakes_controller.rb")
assert_includes(out, "Annotated test/mock/app/controllers/api/tacos_controller.rb")
assert_includes(out, "Annotated test/mock/app/controllers/waterlilies_controller.rb")
end

def test_mock_app
Expand Down

0 comments on commit bef33cd

Please sign in to comment.