Skip to content

Commit

Permalink
Implement configurable formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Evanczuk committed Nov 17, 2022
1 parent 55e5b4f commit b656c9b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/packwerk/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def initialize(
@configuration = T.let(configuration || Configuration.from_path, Configuration)
@progress_formatter = T.let(Formatters::ProgressFormatter.new(@out, style: style), Formatters::ProgressFormatter)
@offenses_formatter = T.let(
offenses_formatter || Formatters::OffensesFormatter.new(style: @style),
offenses_formatter || Configuration.offenses_formatter || Formatters::OffensesFormatter.new(style: @style),
OffensesFormatter
)
end
Expand Down
5 changes: 5 additions & 0 deletions lib/packwerk/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
module Packwerk
class Configuration
class << self
extend T::Sig

def from_path(path = Dir.pwd)
raise ArgumentError, "#{File.expand_path(path)} does not exist" unless File.exist?(path)

Expand All @@ -19,6 +21,9 @@ def from_path(path = Dir.pwd)
end
end

sig { returns(T.nilable(OffensesFormatter)) }
attr_accessor :offenses_formatter

private

def from_packwerk_config(path)
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/extended/config/my_local_extension.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
module MyLocalExtension
end

class MyOffensesFormatter
include Packwerk::OffensesFormatter

def show_offenses(offenses)
["hi i am a custom offense formatter", *offenses].join("\n")
end

def show_stale_violations(_offense_collection, _fileset)
"stale violations report"
end
end

Packwerk::Configuration.offenses_formatter = MyOffensesFormatter.new
5 changes: 5 additions & 0 deletions test/support/application_fixture_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def setup_application_fixture
@old_working_dir = Dir.pwd
end

def remove_extensions
Object.send(:remove_const, :MyLocalExtension) if defined?(MyLocalExtension)
Packwerk::Configuration.offenses_formatter = nil
end

def teardown_application_fixture
Dir.chdir(@old_working_dir)
FileUtils.remove_entry(@app_dir, true) if using_template?
Expand Down
33 changes: 33 additions & 0 deletions test/unit/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class CliTest < Minitest::Test

teardown do
teardown_application_fixture
remove_extensions
end

test "#execute_command with the subcommand check starts processing files" do
Expand Down Expand Up @@ -168,5 +169,37 @@ def show_stale_violations(_offense_collection, _fileset)

refute success
end

test "#execute_command using a custom offenses class loaded in via packwerk.yml" do
use_template(:extended)

file_path = "path/of/exile.rb"
violation_message = "This is a violation of code health."
offense = Offense.new(file: file_path, message: violation_message)

RunContext.any_instance.stubs(:process_file)
.returns([offense])

cli = nil
string_io = StringIO.new
mock_require_method = ->(required_thing) do
next unless required_thing.include?("my_local_extension")

require required_thing
end
ExtensionLoader.stub(:require, mock_require_method) do
cli = ::Packwerk::Cli.new(out: string_io)
end

::Packwerk::FilesForProcessing.stubs(fetch: Set.new([file_path]))

success = cli.execute_command(["check", file_path])

assert_includes string_io.string, "hi i am a custom offense formatter"
assert_includes string_io.string, "stale violations report"
assert_includes string_io.string, violation_message

refute success
end
end
end
2 changes: 1 addition & 1 deletion test/unit/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ConfigurationTest < Minitest::Test

teardown do
teardown_application_fixture
Object.send(:remove_const, :MyLocalExtension) if defined?(MyLocalExtension)
remove_extensions
end

test ".from_path raises ArgumentError if path does not exist" do
Expand Down

0 comments on commit b656c9b

Please sign in to comment.