Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a --fail-level check CLI option #52

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading