From 12c93e88fe0e16cc5ac4bded392c5361ff9e0d76 Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Tue, 18 Apr 2023 12:00:23 -0400 Subject: [PATCH] Rescue StandardError when processing a file --- lib/packwerk/file_processor.rb | 3 +++ .../custom_executable_integration_test.rb | 27 +++++++++++++++++++ test/unit/packwerk/file_processor_test.rb | 7 ++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/packwerk/file_processor.rb b/lib/packwerk/file_processor.rb index 0fa28ffe8..e21d122ac 100644 --- a/lib/packwerk/file_processor.rb +++ b/lib/packwerk/file_processor.rb @@ -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 diff --git a/test/integration/packwerk/custom_executable_integration_test.rb b/test/integration/packwerk/custom_executable_integration_test.rb index a87500beb..e7636c3b9 100644 --- a/test/integration/packwerk/custom_executable_integration_test.rb +++ b/test/integration/packwerk/custom_executable_integration_test.rb @@ -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) diff --git a/test/unit/packwerk/file_processor_test.rb b/test/unit/packwerk/file_processor_test.rb index 931a0bb7d..4b3b49821 100644 --- a/test/unit/packwerk/file_processor_test.rb +++ b/test/unit/packwerk/file_processor_test.rb @@ -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