Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rescue StandardError when processing a file #2

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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