Skip to content

Commit

Permalink
Rescue StandardError when processing a file
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Evanczuk committed Apr 18, 2023
1 parent 4d28712 commit 9eda05a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/packwerk/file_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def call(relative_file)
ProcessedFile.new(unresolved_references: unresolved_references)
rescue Parsers::ParseError => e
ProcessedFile.new(offenses: [e.result])
rescue StandardError => e
offense = Parsers::ParseResult.new(file: relative_file, message: e.message)
ProcessedFile.new(offenses: [offense])
end

private
Expand Down
27 changes: 27 additions & 0 deletions test/integration/packwerk/custom_executable_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,33 @@ class CustomExecutableIntegrationTest < Minitest::Test
end
end

test "'packwerk check' does not blow up when parsing files with syntax issues from a false positive association" do
open_app_file(TIMELINE_PATH.join("app", "models", "bad_file.rb")) do |file|
# This is an example of a file that references `belongs_to`, but as an ActiveAdmin API
# rather than as a Rails Association concept
content = <<~CONTENT
require 'rails_helper'
RSpec.describe ActiveAdmin::Resource::BelongsTo do
it "should have an owner" do
expect(belongs_to.owner).to eq post_config
end
end
CONTENT

file.write(content)
file.flush

refute_successful_run("check")

assert_match(/Packwerk is inspecting 13 files/, captured_output)
assert_match(%r{components/timeline/app/models/bad_file.rb}, captured_output)
assert_match(/Passed `nil` into T.must/, captured_output)
assert_match(/1 offense detected/, captured_output)
assert_match(/No stale violations detected/, captured_output)
end
end

private

def assert_successful_run(command)
Expand Down
7 changes: 6 additions & 1 deletion test/unit/packwerk/file_processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,14 @@ class FileProcessorTest < Minitest::Test
)
)

tempfile(name: "foo", content: "def error") do |file_path|
processed_file = tempfile(name: "foo", content: "def error") do |file_path|
file_processor.call(file_path)
end

assert_equal 0, processed_file.unresolved_references.count
offenses = processed_file.offenses
assert_equal 1, offenses.length
assert_equal "Syntax error: unexpected token $end", offenses.first.message
end

test "#call with a path that can't be parsed outputs error message" do
Expand Down

0 comments on commit 9eda05a

Please sign in to comment.