Skip to content

Commit

Permalink
Support stylelint 16+, which emits messages on stderr (#848)
Browse files Browse the repository at this point in the history
Before Stylelint 16, Stylelint emitted its messages on stdout. Hence
Overcommit's Stylelint hook was built to read output from stdout.

Starting with Stylelint 16, Stylelint now emits its messages to stderr,
which means the existing Overcommit implementation no longer works.

This PR updates Overcommit's Stylelint plugin to check for messages on
_both_ stdout and stderr. That way <16 and >=16 versions of Stylelint
are supported.

Fixes #823
  • Loading branch information
mattbrictson authored May 12, 2024
1 parent 841954c commit 0e717f9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/overcommit/hook/pre_commit/stylelint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Stylelint < Base

def run
result = execute(command, args: applicable_files)
output = result.stdout.chomp
output = result.stdout + result.stderr.chomp
return :pass if result.success? && output.empty?

extract_messages(
Expand Down
29 changes: 28 additions & 1 deletion spec/overcommit/hook/pre_commit/stylelint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
before do
result = double('result')
result.stub(:success?).and_return(true)
result.stub(:stderr).and_return('')
result.stub(:stdout).and_return('')
subject.stub(:execute).and_return(result)
end

it { should pass }
end

context 'when stylelint exits unsucessfully' do
context 'when stylelint exits unsucessfully with messages on stdout (stylelint < 16)' do
let(:result) { double('result') }

before do
Expand All @@ -32,6 +33,7 @@
context 'and it reports an error' do
before do
result.stub(:success?).and_return(false)
result.stub(:stderr).and_return('')
result.stub(:stdout).and_return([
'index.css: line 4, col 4, error - Expected indentation of 2 spaces (indentation)',
'form.css: line 10, col 6, error - Expected indentation of 4 spaces (indentation)',
Expand All @@ -45,4 +47,29 @@
end
end
end

context 'when stylelint exits unsucessfully with messages on stderr (stylelint >= 16)' do
let(:result) { double('result') }

before do
subject.stub(:execute).and_return(result)
end

context 'and it reports an error' do
before do
result.stub(:success?).and_return(false)
result.stub(:stdout).and_return('')
result.stub(:stderr).and_return([
'index.css: line 4, col 4, error - Expected indentation of 2 spaces (indentation)',
'form.css: line 10, col 6, error - Expected indentation of 4 spaces (indentation)',
].join("\n"))
end

it { should fail_hook }

it 'extracts lines numbers correctly from output' do
expect(subject.run.map(&:line)).to eq([4, 10])
end
end
end
end

0 comments on commit 0e717f9

Please sign in to comment.