Skip to content

Commit

Permalink
Write everything except autocorrected file content to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebarrie committed Jan 10, 2025
1 parent 57208a9 commit 33a7869
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 47 deletions.
6 changes: 3 additions & 3 deletions lib/erb_lint/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(config, cache_dir = nil)
@cache_dir = cache_dir || CACHE_DIRECTORY
@hits = []
@new_results = []
puts "Cache mode is on"
$stderr.puts "Cache mode is on"
end

def get(filename, file_content)
Expand Down Expand Up @@ -44,7 +44,7 @@ def close

def prune_cache
if hits.empty?
puts "Cache being created for the first time, skipping prune"
$stderr.puts "Cache being created for the first time, skipping prune"
return
end

Expand All @@ -63,7 +63,7 @@ def cache_dir_exists?
def clear
return unless cache_dir_exists?

puts "Clearing cache by deleting cache directory"
$stderr.puts "Clearing cache by deleting cache directory"
FileUtils.rm_r(@cache_dir)
end

Expand Down
14 changes: 7 additions & 7 deletions lib/erb_lint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ def run(args = ARGV)
file_content = run_on_file(runner, filename)
rescue => e
@stats.exceptions += 1
puts "Exception occurred when processing: #{relative_filename(filename)}"
puts "If this file cannot be processed by erb_lint, " \
$stderr.puts "Exception occurred when processing: #{relative_filename(filename)}"
$stderr.puts "If this file cannot be processed by erb_lint, " \
"you can exclude it in your configuration file."
puts e.message
puts Rainbow(e.backtrace.join("\n")).red
puts
$stderr.puts e.message
$stderr.puts Rainbow(e.backtrace.join("\n")).red
$stderr.puts
end
end

Expand All @@ -102,7 +102,7 @@ def run(args = ARGV)

if stdin? && autocorrect?
# When running from stdin, we only lint a single file
puts "================ #{lint_files.first} ==================\n"
$stderr.puts "================ #{lint_files.first} =================="
puts file_content
end

Expand All @@ -111,7 +111,7 @@ def run(args = ARGV)
$stderr.puts(Rainbow(e.message).red)
false
rescue ExitWithSuccess => e
puts e.message
$stderr.puts e.message
true
rescue => e
$stderr.puts(Rainbow("#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}").red)
Expand Down
8 changes: 4 additions & 4 deletions lib/erb_lint/reporters/compact_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module ERBLint
module Reporters
class CompactReporter < Reporter
def preview
puts "#{linting} #{stats.files} files with #{linters}..."
$stderr.puts "#{linting} #{stats.files} files with #{linters}..."
end

def show
processed_files.each do |filename, offenses|
offenses.each do |offense|
puts format_offense(filename, offense)
$stderr.puts format_offense(filename, offense)
end
end

Expand Down Expand Up @@ -52,7 +52,7 @@ def summary
$stderr.puts(Rainbow("#{stats.found} error(s) were found in ERB files").red)
end
else
puts Rainbow("No errors were found in ERB files").green
$stderr.puts Rainbow("No errors were found in ERB files").green
end
end

Expand All @@ -66,7 +66,7 @@ def report_corrected_offenses

$stderr.puts(message)
else
puts Rainbow("#{stats.corrected} error(s) corrected in ERB files").green
$stderr.puts Rainbow("#{stats.corrected} error(s) corrected in ERB files").green
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/erb_lint/reporters/multiline_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def format_offense(filename, offense)
end

def footer
puts
$stderr.puts
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/erb_lint/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
it "skips prune if no cache hits" do
allow(cache).to(receive(:hits).and_return([]))

expect { cache.prune_cache }.to(output(/Cache being created for the first time, skipping prune/).to_stdout)
expect { cache.prune_cache }.to(output(/Cache being created for the first time, skipping prune/).to_stderr)
end

it "does not prune actual cache hits" do
Expand Down
51 changes: 26 additions & 25 deletions spec/erb_lint/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,21 @@ def run(processed_source)

context "with --version" do
let(:args) { ["--version"] }
it { expect { subject }.to(output("#{ERBLint::VERSION}\n").to_stdout) }
it { expect { subject }.to(output("#{ERBLint::VERSION}\n").to_stderr) }
end

context "with --help" do
let(:args) { ["--help"] }

it "shows usage" do
expect { subject }.to(output(/erblint \[options\] \[file1, file2, ...\]/).to_stdout)
expect { subject }.to(output(/erblint \[options\] \[file1, file2, ...\]/).to_stderr)
end

it "shows format instructions" do
expect { subject }.to(
output(Regexp.new("Report offenses in the given format: " \
"\\(compact, gitlab, json, junit, multiline\\) " \
"\\(default: multiline\\)")).to_stdout,
"\\(default: multiline\\)")).to_stderr,
)
end

Expand Down Expand Up @@ -143,7 +143,7 @@ def run(processed_source)
end

it "shows all errors regardless of inline disables " do
expect { subject }.to(output(/ERBLint::Linters::FakeLinter error/).to_stdout)
expect { subject }.to(output(/ERBLint::Linters::FakeLinter error/).to_stderr)
end
end

Expand All @@ -163,7 +163,8 @@ def run(processed_source)
it {
expect do
subject
end.to(output(<<~EOF).to_stdout)
end.to(output(<<~EOF).to_stderr)
#{Rainbow(".erb_lint.yml not found: using default config").yellow}
Cache mode is on
Clearing cache by deleting cache directory
cache directory cleared
Expand Down Expand Up @@ -217,7 +218,7 @@ def run(processed_source)
end

it "uses the specified directory" do
expect { subject }.to(output(/cache directory cleared/).to_stdout)
expect { subject }.to(output(/cache directory cleared/).to_stderr)
end
end

Expand Down Expand Up @@ -247,12 +248,12 @@ def run(processed_source)
end

it "shows how many files and linters are used" do
expect { subject }.to(output(/Linting 1 files with 2 linters/).to_stdout)
expect { subject }.to(output(/Linting 1 files with 2 linters/).to_stderr)
end

context "when errors are found" do
it "shows all error messages and line numbers" do
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stdout)
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stderr)
fake message from a fake linter
In file: /app/views/template.html.erb:1
Expand All @@ -276,7 +277,7 @@ def run(processed_source)
let(:args) { ["--enable-linter", "linter_with_info_errors", linted_file] }

it "shows all error messages and line numbers" do
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stdout)
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stderr)
fake info message from a fake linter
In file: /app/views/template.html.erb:1
Expand All @@ -297,7 +298,7 @@ def run(processed_source)
let(:args) { ["--enable-linter", "linter_without_errors", linted_file] }

it "shows that no errors were found to stdout" do
expect { subject }.to(output(/No errors were found in ERB files/).to_stdout)
expect { subject }.to(output(/No errors were found in ERB files/).to_stderr)
end

it "is successful" do
Expand Down Expand Up @@ -327,7 +328,7 @@ def run(processed_source)
it "lints the file and adds it to the cache" do
expect(Dir[ERBLint::Cache::CACHE_DIRECTORY].length).to(be(0))

expect { subject }.to(output(/Cache mode is on/).to_stdout)
expect { subject }.to(output(/Cache mode is on/).to_stderr)
expect(subject).to(be(true))

expect(Dir[ERBLint::Cache::CACHE_DIRECTORY].length).to(be(1))
Expand All @@ -350,7 +351,7 @@ def run(processed_source)
context "with the default glob" do
it "shows how many files and linters are used" do
allow(cli).to(receive(:glob).and_return(cli.class::DEFAULT_LINT_ALL_GLOB))
expect { subject }.to(output(/Linting 1 files with 2 linters/).to_stdout)
expect { subject }.to(output(/Linting 1 files with 2 linters/).to_stderr)
end
end

Expand All @@ -377,7 +378,7 @@ def run(processed_source)

context "with the default glob" do
it "shows all error messages and line numbers" do
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stdout)
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stderr)
fake info message from a fake linter
In file: /app/views/template.html.erb:1
Expand All @@ -400,7 +401,7 @@ def run(processed_source)

context "with the default glob" do
it "shows all error messages and line numbers" do
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stdout)
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stderr)
fake info message from a fake linter
In file: /app/views/template.html.erb:1
Expand Down Expand Up @@ -464,12 +465,12 @@ def run(processed_source)
end

it "shows how many files and linters are used" do
expect { subject }.to(output(/Linting 1 files with 2 linters/).to_stdout)
expect { subject }.to(output(/Linting 1 files with 2 linters/).to_stderr)
end

context "when errors are found" do
it "shows all error messages and line numbers" do
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stdout)
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stderr)
fake message from a fake linter
In file: /app/views/template.html.erb:1
Expand Down Expand Up @@ -501,7 +502,7 @@ def run(processed_source)
end

it "shows all error messages and line numbers" do
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stdout)
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stderr)
/app/views/template.html.erb:1:1: fake message from a fake linter
/app/views/template.html.erb:1:19: Missing a trailing newline at the end of the file.
EOF
Expand Down Expand Up @@ -543,7 +544,7 @@ def run(processed_source)
let(:args) { ["--enable-linter", "linter_without_errors", linted_dir] }

it "shows that no errors were found to stdout" do
expect { subject }.to(output(/No errors were found in ERB files/).to_stdout)
expect { subject }.to(output(/No errors were found in ERB files/).to_stderr)
end

it "is successful" do
Expand All @@ -556,7 +557,7 @@ def run(processed_source)
it "lints the file and adds it to the cache" do
expect(Dir[ERBLint::Cache::CACHE_DIRECTORY].length).to(be(0))

expect { subject }.to(output(/Cache mode is on/).to_stdout)
expect { subject }.to(output(/Cache mode is on/).to_stderr)
expect(subject).to(be(true))

expect(Dir[ERBLint::Cache::CACHE_DIRECTORY].length).to(be(1))
Expand Down Expand Up @@ -623,12 +624,12 @@ def run(processed_source)
end

it "shows how many files and linters are used" do
expect { subject }.to(output(/Linting 1 files with 2 linters/).to_stdout)
expect { subject }.to(output(/Linting 1 files with 2 linters/).to_stderr)
end

context "when errors are found" do
it "shows all error messages and line numbers" do
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stdout)
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF))).to_stderr)
fake message from a fake linter
In file: /app/views/template.html.erb:1
Expand All @@ -654,11 +655,11 @@ def run(processed_source)
end

it "tells the user it is autocorrecting" do
expect { subject }.to(output(/Linting and autocorrecting/).to_stdout)
expect { subject }.to(output(/Linting and autocorrecting/).to_stderr)
end

it "shows how many total and autocorrectable linters are used" do
expect { subject }.to(output(/2 linters \(1 autocorrectable\)/).to_stdout)
expect { subject }.to(output(/2 linters \(1 autocorrectable\)/).to_stderr)
end

it "outputs the corrected ERB" do
Expand All @@ -682,7 +683,7 @@ def run(processed_source)
let(:args) { ["--enable-linter", "linter_without_errors", "--stdin", linted_file] }

it "shows that no errors were found to stdout" do
expect { subject }.to(output(/No errors were found in ERB files/).to_stdout)
expect { subject }.to(output(/No errors were found in ERB files/).to_stderr)
end

it "is successful" do
Expand Down Expand Up @@ -721,7 +722,7 @@ def run(processed_source)
end

it "exits with success status" do
expect { subject }.to(output(/no files found\.\.\./).to_stdout)
expect { subject }.to(output(/no files found\.\.\./).to_stderr)
expect(subject).to(be(true))
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/erb_lint/reporters/compact_reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
end

it "displays formatted offenses output" do
expect { subject }.to(output(<<~MESSAGE).to_stdout)
expect { subject }.to(output(a_string_starting_with(<<~MESSAGE)).to_stderr)
app/views/users/show.html.erb:61:10: Extra space detected where there should be no space.
app/views/users/show.html.erb:125:1: Remove multiple trailing newline at the end of the file.
app/views/users/show.html.erb:145:4: Trailing comma expected.
Expand All @@ -88,7 +88,7 @@
let(:show_linter_names) { true }

it "displays formatted offenses output with linter names" do
expect { subject }.to(output(<<~MESSAGE).to_stdout)
expect { subject }.to(output(a_string_starting_with(<<~MESSAGE)).to_stderr)
app/views/users/show.html.erb:61:10: [SpaceInHtmlTag] Extra space detected where there should be no space.
app/views/users/show.html.erb:125:1: [FinalNewline] Remove multiple trailing newline at the end of the file.
app/views/users/show.html.erb:145:4: [TrailingComma] Trailing comma expected.
Expand All @@ -100,7 +100,7 @@
end

it "outputs offenses summary to stderr" do
expect { subject }.to(output(<<~MESSAGE).to_stderr)
expect { subject }.to(output(a_string_ending_with(<<~MESSAGE)).to_stderr)
#{Rainbow("2 error(s) were ignored in ERB files").yellow}
#{Rainbow("4 error(s) were found in ERB files").red}
MESSAGE
Expand Down
6 changes: 3 additions & 3 deletions spec/erb_lint/reporters/multiline_reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
let(:show_linter_names) { false }

it "displays formatted offenses output" do
expect { subject }.to(output(<<~MESSAGE).to_stdout)
expect { subject }.to(output(a_string_starting_with(<<~MESSAGE)).to_stderr)
Extra space detected where there should be no space.
In file: app/views/subscriptions/_loader.html.erb:1
Expand All @@ -56,7 +56,7 @@
let(:show_linter_names) { true }

it "displays formatted offenses output" do
expect { subject }.to(output(<<~MESSAGE).to_stdout)
expect { subject }.to(output(a_string_starting_with(<<~MESSAGE)).to_stderr)
[SpaceInHtmlTag] Extra space detected where there should be no space.
In file: app/views/subscriptions/_loader.html.erb:1
Expand All @@ -73,7 +73,7 @@
let(:show_linter_names) { false }

it "displays not autocorrected warning" do
expect { subject }.to(output(/(not autocorrected)/).to_stdout)
expect { subject }.to(output(/(not autocorrected)/).to_stderr)
end
end
end
Expand Down

0 comments on commit 33a7869

Please sign in to comment.