Skip to content

Commit eb92983

Browse files
authored
Capture stdout from Packwerk CLI (#60)
* Use the default Plain style. * Add a beter explanation of how we call the packwerk cli * Remove unnecessary rescue in PackwerkWrapper This seems to be from a time when the CLI would exit. We don't use the CLI in a way where this can happen anymore.
1 parent 30148a2 commit eb92983

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

Gemfile.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ PATH
2525
packwerk
2626
parse_packwerk
2727
sorbet-runtime
28+
stringio
2829

2930
GEM
3031
remote: https://rubygems.org/

danger-packwerk.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ Gem::Specification.new do |spec|
3535
spec.add_dependency 'packwerk'
3636
spec.add_dependency 'parse_packwerk'
3737
spec.add_dependency 'sorbet-runtime'
38+
spec.add_dependency 'stringio'
3839
end

lib/danger-packwerk/packwerk_wrapper.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# typed: strict
22

3+
require 'stringio'
4+
35
module DangerPackwerk
46
# This class wraps packwerk to give us precisely what we want, which is the `Packwerk::ReferenceOffense` from a set of files.
57
# Note that statically packwerk returns `Packwerk::Offense` from running `bin/packwerk check`. The two types of `Packwerk::Offense` are
@@ -14,24 +16,20 @@ module DangerPackwerk
1416
class PackwerkWrapper
1517
extend T::Sig
1618

19+
# This code is partially copied from exe/packwerk within the packwerk gem. We're imitating the
20+
# cli here but with our own offense formatter to collect the violating data directly.
21+
#
22+
# We capture and ignore the output of the Cli so that we don't leak it to the build system logs.
23+
# When packwerk produces errors it can make the build system look like it's failing when really
24+
# this is expected behavior.
1725
sig { params(files: T::Array[String]).returns(T::Array[Packwerk::ReferenceOffense]) }
1826
def self.get_offenses_for_files(files)
1927
formatter = OffensesAggregatorFormatter.new
20-
# This is mostly copied from exe/packwerk within the packwerk gem, but we use our own formatters
2128
ENV['RAILS_ENV'] = 'test'
22-
style = Packwerk::OutputStyles::Coloured.new
23-
cli = Packwerk::Cli.new(style: style, offenses_formatter: formatter)
29+
cli = Packwerk::Cli.new(offenses_formatter: formatter, out: StringIO.new)
2430
cli.execute_command(['check', *files])
2531
reference_offenses = formatter.aggregated_offenses.compact.select { |offense| offense.is_a?(Packwerk::ReferenceOffense) }
2632
T.cast(reference_offenses, T::Array[Packwerk::ReferenceOffense])
27-
rescue SystemExit => e
28-
# Packwerk should probably exit positively here rather than raising an error -- there should be no
29-
# errors if the user has excluded all files being checked.
30-
if e.message == 'No files found or given. Specify files or check the include and exclude glob in the config file.'
31-
[]
32-
else
33-
raise
34-
end
3533
end
3634

3735
#

0 commit comments

Comments
 (0)