Skip to content

Commit

Permalink
Refactor ParseRun#find_offenses
Browse files Browse the repository at this point in the history
- Return an `Array` of `Offense`s instead of an `OffenseCollection`
- Take in a proc instead of `show_errors` boolean to make behaviour configurable
  • Loading branch information
davidstosik committed Mar 30, 2023
1 parent b1f2519 commit d8ca547
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
5 changes: 5 additions & 0 deletions lib/packwerk/offense_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def listed?(offense)
package_todo_for(reference.package).listed?(reference, violation_type: offense.violation_type)
end

sig { params(offenses: T::Array[Offense]).void }
def add_offenses(offenses)
offenses.each { |offense| add_offense(offense) }
end

sig do
params(offense: Packwerk::Offense).void
end
Expand Down
46 changes: 31 additions & 15 deletions lib/packwerk/parse_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ def update_todo
end

run_context = RunContext.from_configuration(@configuration)
offense_collection = find_offenses(run_context)
offenses = find_offenses(run_context) { update_progress }

offense_collection = OffenseCollection.new(@configuration.root_path)
offense_collection.add_offenses(offenses)
offense_collection.persist_package_todo_files(run_context.package_set)

message = <<~EOS
Expand All @@ -60,7 +63,12 @@ def update_todo
sig { returns(Cli::Result) }
def check
run_context = RunContext.from_configuration(@configuration)
offense_collection = find_offenses(run_context, show_errors: true)
offense_collection = OffenseCollection.new(@configuration.root_path)
offenses = find_offenses(run_context) do |offenses|
failed = offenses.any? { |offense| !offense_collection.listed?(offense) }
update_progress(failed: failed)
end
offense_collection.add_offenses(offenses)

messages = [
@offenses_formatter.show_offenses(offense_collection.outstanding_offenses),
Expand All @@ -76,27 +84,35 @@ def check

private

sig { params(run_context: RunContext, show_errors: T::Boolean).returns(OffenseCollection) }
def find_offenses(run_context, show_errors: false)
offense_collection = OffenseCollection.new(@configuration.root_path)
all_offenses = T.let([], T::Array[Offense])
process_file = T.let(->(relative_file) do
run_context.process_file(relative_file: relative_file).tap do |offenses|
failed = show_errors && offenses.any? { |offense| !offense_collection.listed?(offense) }
update_progress(failed: failed)
end
end, ProcessFileProc)
sig do
params(
run_context: RunContext,
block: T.nilable(T.proc.params(
offenses: T::Array[Packwerk::Offense],
).void)
).returns(T::Array[Offense])
end
def find_offenses(run_context, &block)
offenses = T.let([], T::Array[Offense])
process_file = if block_given?
T.let(proc do |relative_file|
run_context.process_file(relative_file: relative_file).tap(&block)
end, ProcessFileProc)
else
T.let(proc do |relative_file|
run_context.process_file(relative_file: relative_file)
end, ProcessFileProc)
end

@progress_formatter.started_inspection(@relative_file_set) do
all_offenses = if @configuration.parallel?
offenses = if @configuration.parallel?
Parallel.flat_map(@relative_file_set, &process_file)
else
serial_find_offenses(&process_file)
end
end

all_offenses.each { |offense| offense_collection.add_offense(offense) }
offense_collection
offenses
end

sig { params(block: ProcessFileProc).returns(T::Array[Offense]) }
Expand Down

0 comments on commit d8ca547

Please sign in to comment.