From 06ee64887cbd3095aef9f92c0fad57428968d477 Mon Sep 17 00:00:00 2001 From: Alex Koch Date: Thu, 30 May 2024 11:38:03 -0500 Subject: [PATCH] flag to exit 1 if any files changed remove write flag from test, be consistent about quotes --- lib/erb/formatter/command_line.rb | 9 +++++++++ test/erb/test_formatter.rb | 21 +++++++++++++++++++++ test/fixtures/unmatched_error.erb | 6 ++++++ test/test_helper.rb | 1 + 4 files changed, 37 insertions(+) create mode 100644 test/fixtures/unmatched_error.erb 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 %> +

+ <%= link_to 'New Order', new_order_path, class: "btn btn-success" %> + <% end %> +

+ diff --git a/test/test_helper.rb b/test/test_helper.rb index 017b9ba..d8f8851 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,5 +2,6 @@ $LOAD_PATH.unshift File.expand_path("../lib", __dir__) require "erb/formatter" +require "erb/formatter/command_line" require "minitest/autorun"