Skip to content

Commit

Permalink
Merge pull request #52 from kochalex/check-mode
Browse files Browse the repository at this point in the history
Add a --fail-level check CLI option
  • Loading branch information
elia authored Jun 24, 2024
2 parents 175fd6b + 06ee648 commit cbeab31
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/erb/formatter/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def initialize(argv, stdin: $stdin)
$DEBUG = value
end

parser.on("--fail-level LEVEL", "'check' exits(1) on any formatting changes)") do |value|
@fail_level = value
end

parser.on("-h", "--help", "Prints this help") do
puts parser
exit
Expand Down Expand Up @@ -94,6 +98,8 @@ def run
css_class_sorter = self.class.tailwindcss_class_sorter(@tailwind_output_path)
end

files_changed = false

files.each do |(filename, code)|
if ignore_list.should_ignore_file? filename
print code unless write
Expand All @@ -106,12 +112,15 @@ def run
css_class_sorter: css_class_sorter
)

files_changed = true if html.to_s != code

if write
File.write(filename, html)
else
puts html
end
end
end
exit(1) if files_changed && @fail_level == "check"
end
end
21 changes: 21 additions & 0 deletions test/erb/test_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ def test_fixtures
end
end

def test_error_on_unformattable_file
cli = ERB::Formatter::CommandLine.new(["test/fixtures/unmatched_error.erb"])
error = assert_raises RuntimeError do |error|
cli.run
end
assert_match(/Unmatched close tag/, error.message)
end

def test_fail_level_flag_check_with_changes
cli = ERB::Formatter::CommandLine.new(["--fail-level", "check", "test/fixtures/attributes.html.erb"])
error = assert_raises SystemExit do
cli.run
end
assert_equal(1, error.status)
end

def test_fail_level_flag_check_without_changes
cli = ERB::Formatter::CommandLine.new(["--fail-level", "check", "test/fixtures/attributes.html.expected.erb"])
cli.run
end

def test_format_text_with_extra_long_text
text = <<~HTML * 30
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/unmatched_error.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<% if true %>
<h1>
<%= link_to 'New Order', new_order_path, class: "btn btn-success" %>
<% end %>
</h1>

1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "erb/formatter"
require "erb/formatter/command_line"

require "minitest/autorun"

0 comments on commit cbeab31

Please sign in to comment.