diff --git a/lib/erb/formatter/command_line.rb b/lib/erb/formatter/command_line.rb index 356b708..92cfa51 100644 --- a/lib/erb/formatter/command_line.rb +++ b/lib/erb/formatter/command_line.rb @@ -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 @@ -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 @@ -106,6 +112,8 @@ def run css_class_sorter: css_class_sorter ) + files_changed = true if html.to_s != code + if write File.write(filename, html) else @@ -113,5 +121,6 @@ def run end end end + exit(1) if files_changed && @fail_level == "check" end end diff --git a/test/erb/test_formatter.rb b/test/erb/test_formatter.rb index d99e6b1..895d3f0 100644 --- a/test/erb/test_formatter.rb +++ b/test/erb/test_formatter.rb @@ -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 diff --git a/test/fixtures/unmatched_error.erb b/test/fixtures/unmatched_error.erb new file mode 100644 index 0000000..b74f981 --- /dev/null +++ b/test/fixtures/unmatched_error.erb @@ -0,0 +1,6 @@ +<% if true %> +