Skip to content

Commit

Permalink
Merge pull request #343 from Shopify/ae-always-spit-out-error-when-pr…
Browse files Browse the repository at this point in the history
…ocessing-files

Rescue StandardError when processing file
  • Loading branch information
Alex Evanczuk authored Apr 20, 2023
2 parents 9967367 + c3bba85 commit 6bcf2ae
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/packwerk/file_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ def call(relative_file)
ProcessedFile.new(unresolved_references: unresolved_references)
rescue Parsers::ParseError => e
ProcessedFile.new(offenses: [e.result])
rescue StandardError => e
message = <<~MSG
Packwerk encountered an internal error.
For now, you can add this file to `packwerk.yml` `exclude` list.
Please file an issue and include this error message and stacktrace:
#{e.message} #{e.backtrace}"
MSG

offense = Parsers::ParseResult.new(file: relative_file, message: message)
ProcessedFile.new(offenses: [offense])
end

private
Expand Down
23 changes: 23 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,29 @@ 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 has an object called `belongs_to` that accepts methods
content = <<~CONTENT
belongs_to.some_method
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(/Packwerk encountered an internal error/, captured_output)
assert_match(/For now, you can add this file to `packwerk.yml` `exclude` list./, captured_output)
assert_match(/Please file an issue and include this error message and stacktrace:/, 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 6bcf2ae

Please sign in to comment.