Skip to content

Commit ca1bc4a

Browse files
authored
Merge pull request #220 from glennsarti/md-lint
(maint) Add markdown lint checking to markdown tests
2 parents 59e9f1b + 3749855 commit ca1bc4a

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ group :test do
2020
gem 'simplecov-console'
2121
gem 'rspec', '~> 3.1'
2222
gem 'json_spec', '~> 1.1', '>= 1.1.5'
23+
gem 'mdl', '~> 0.8.0' if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.4.0')
2324
end
2425

2526
group :acceptance do

spec/spec_helper.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,45 @@
4747
YARD::Registry.clear
4848
end
4949
end
50+
51+
def mdl_available
52+
@mdl_available ||= !Gem::Specification.select { |item| item.name.casecmp('mdl').zero? }.empty?
53+
end
54+
55+
def lint_markdown(content)
56+
return [] unless mdl_available
57+
require 'mdl'
58+
59+
ruleset = MarkdownLint::RuleSet.new
60+
ruleset.load_default
61+
62+
# All rules
63+
style = MarkdownLint::Style.load('all', ruleset.rules)
64+
65+
# Create a document
66+
doc = MarkdownLint::Doc.new(content, false)
67+
68+
# Run the rules
69+
violations = []
70+
ruleset.rules.each do |id, rule|
71+
error_lines = rule.check.call(doc)
72+
next if error_lines.nil? or error_lines.empty?
73+
# record the error
74+
error_lines.each do |line|
75+
line += doc.offset # Correct line numbers for any yaml front matter
76+
violations << "#{filename}:#{line}: #{id} #{rule.description}"
77+
end
78+
end
79+
violations
80+
end
81+
82+
RSpec::Matchers.define :have_no_markdown_lint_errors do
83+
match do |actual|
84+
@violations = lint_markdown(actual)
85+
@violations.empty?
86+
end
87+
88+
failure_message do |actual|
89+
"expected that #{actual.length > 80 ? actual.slice(0,80).inspect + '...' : actual.inspect} would have no markdown lint errors but got #{@violations.join("\n")}"
90+
end
91+
end

spec/unit/puppet-strings/markdown_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,17 @@ def parse_data_type_content
325325
let(:baseline_path) { File.join(File.dirname(__FILE__), "../../fixtures/unit/markdown/#{filename}") }
326326
let(:baseline) { File.read(baseline_path) }
327327

328+
RSpec.shared_examples 'markdown lint checker' do |parameter|
329+
it 'should not generate markdown lint errors from the rendered markdown', if: mdl_available do
330+
pending('Failures are expected')
331+
Tempfile.open('md') do |file|
332+
PuppetStrings::Markdown.render(file.path)
333+
334+
expect(File.read(file.path)).to have_no_markdown_lint_errors
335+
end
336+
end
337+
end
338+
328339
describe 'rendering markdown to a file' do
329340
before(:each) do
330341
parse_shared_content
@@ -339,6 +350,8 @@ def parse_data_type_content
339350
expect(File.read(file.path)).to eq(baseline)
340351
end
341352
end
353+
354+
include_examples 'markdown lint checker'
342355
end
343356

344357
describe 'with Puppet Plans', :if => TEST_PUPPET_PLANS do
@@ -354,6 +367,8 @@ def parse_data_type_content
354367
expect(File.read(file.path)).to eq(baseline)
355368
end
356369
end
370+
371+
include_examples 'markdown lint checker'
357372
end
358373

359374
describe 'with Puppet Data Types', :if => TEST_PUPPET_DATATYPES do
@@ -369,6 +384,8 @@ def parse_data_type_content
369384
expect(File.read(file.path)).to eq(baseline)
370385
end
371386
end
387+
388+
include_examples 'markdown lint checker'
372389
end
373390
end
374391
end

0 commit comments

Comments
 (0)