Skip to content

Commit

Permalink
Raise helpful error for malformed output
Browse files Browse the repository at this point in the history
I haven't actually observed this happening, but I want this engine to be
more assertive about its expectations.
  • Loading branch information
maxjacobson committed Oct 26, 2016
1 parent 219598d commit 3a95195
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/cc/engine/markdownlint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Engine
class Markdownlint
CONFIG_FILE = "./.mdlrc".freeze
EXTENSIONS = %w[.markdown .md].freeze
UnexpectedOutputFormat = Class.new(StandardError)

def initialize(root, engine_config, io, err_io)
@root = root
Expand Down Expand Up @@ -51,8 +52,10 @@ def mdl_options
options
end

ISSUE_PATTERN = /(?<path>.*):(?<line_number>\d+): (?<code>MD\d+) (?<description>.*)/

def issue(line)
match_data = line.match(/(?<path>.*):(?<line_number>\d+): (?<code>MD\d+) (?<description>.*)/)
match_data = line.match(ISSUE_PATTERN) or raise UnexpectedOutputFormat, line
line_number = match_data[:line_number].to_i
path = match_data[:path]
relative_path = File.absolute_path(path).sub(root + "/", "")
Expand Down
11 changes: 11 additions & 0 deletions spec/cc/engine/markdownlint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ module Engine
expect(issue["check_name"]).to eq("MD001")
end

it "exits cleanly when the underlying tool outputs an unexpected format" do
io = StringIO.new
path = File.expand_path("../../fixtures/default", File.dirname(__FILE__))

child = double(out: "weird output", err: "")
expect(POSIX::Spawn::Child).to receive(:new).and_return(child)
expect {
CC::Engine::Markdownlint.new(path, {}, io, STDERR).run
}.to raise_error(CC::Engine::Markdownlint::UnexpectedOutputFormat)
end

it "exits cleanly with empty include_paths" do
io = StringIO.new
path = File.expand_path("../../fixtures/default", File.dirname(__FILE__))
Expand Down

0 comments on commit 3a95195

Please sign in to comment.