Skip to content

Commit

Permalink
Bug/652 markdown lint hook fails with unexpected output (#657)
Browse files Browse the repository at this point in the history
Markdown lint 0.5.0 added a link to the rules after printing the
results. This caused the Mdl hook to fail as it was not expecting
this additional output.

Using the JSON output prevents the link to the rules from being
displayed and provided a more structured output for parsing.
  • Loading branch information
steinybot authored and sds committed Jul 9, 2019
1 parent ae69744 commit aba1868
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Relax `childprocess` dependency to allow version 1.x
* Add `CodeSpellCheck` pre-commit hook
* Fix deadlock which was more likely to occur when setting `parallelize` on a hook to `false`
* Fix `Mdl` hook to use JSON output and not fail on unexpected output

## 0.48.1

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ PreCommit:
enabled: false
description: 'Analyze markdown files with mdl'
required_executable: 'mdl'
flags: ['--json']
install_command: 'gem install mdl'
include: '**/*.md'

Expand Down
18 changes: 11 additions & 7 deletions lib/overcommit/hook/pre_commit/mdl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ module Overcommit::Hook::PreCommit
#
# @see https://github.com/mivok/markdownlint
class Mdl < Base
MESSAGE_REGEX = /^(?<file>(?:\w:)?[^:]+):(?<line>\d+):\s(?<msg>.+)/

def run
result = execute(command, args: applicable_files)
output = result.stdout.chomp
Expand All @@ -15,11 +13,17 @@ def run
return [:fail, result.stderr] unless result.stderr.empty?

# example message:
# path/to/file.md:1: MD001 Error message
extract_messages(
output.split("\n"),
MESSAGE_REGEX
)
# [{"filename":"file1.md","line":1,"rule":"MD013","aliases":["line-length"],
# "description":"Line length"}]
json_messages = JSON.parse(output)
json_messages.map do |message|
Overcommit::Hook::Message.new(
:error,
message[:filename],
message[:line],
message[:description]
)
end
end
end
end
5 changes: 4 additions & 1 deletion spec/overcommit/hook/pre_commit/mdl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
let(:success) { false }

context 'and it reports an error' do
let(:stdout) { 'file1.md:1: MD013 Line length' }
let(:stdout) do
'[{"filename":"file1.md","line":1,"rule":"MD013","aliases":["line-length"],'\
'"description":"Line length"}]'
end
let(:stderr) { '' }

it { should fail_hook }
Expand Down

0 comments on commit aba1868

Please sign in to comment.