Skip to content

Commit ade959f

Browse files
authored
Merge pull request #129 from glennsarti/fix-puppet-lint
(GH-125) Honor inline puppet lint directives
2 parents 9249e22 + a3c4373 commit ade959f

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

server/lib/puppet-languageserver/document_validator.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,26 @@ def self.validate(content, workspace, _max_problems = 100)
3131

3232
# Find module root and attempt to build PuppetLint options
3333
module_root = find_module_root_from_path(workspace)
34+
linter_options = nil
3435
if module_root.nil?
35-
PuppetLint::OptParser.build
36+
linter_options = PuppetLint::OptParser.build
3637
else
37-
Dir.chdir(module_root.to_s) { PuppetLint::OptParser.build }
38+
Dir.chdir(module_root.to_s) { linter_options = PuppetLint::OptParser.build }
3839
end
40+
linter_options.parse!([])
3941

4042
begin
4143
linter = PuppetLint::Checks.new
44+
linter.load_data(nil, content)
45+
4246
problems = linter.run(nil, content)
4347
unless problems.nil?
4448
problems.each do |problem|
4549
# Syntax errors are better handled by the puppet parser, not puppet lint
4650
next if problem[:kind] == :error && problem[:check] == :syntax
47-
51+
# Ignore linting errors what were ignored by puppet-lint
52+
next if problem[:kind] == :ignored
53+
4854
severity = case problem[:kind]
4955
when :error
5056
LanguageServer::DIAGNOSTICSEVERITY_ERROR

server/spec/integration/puppet-languageserver/document_validator_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,57 @@
2121
end
2222
end
2323

24+
describe "Given a complete manifest with a single linting error" do
25+
let(:manifest) { "
26+
user { 'Bob':
27+
ensure => 'present',
28+
comment => '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890',
29+
}"
30+
}
31+
32+
it "should return an array with one entry" do
33+
expect(subject.validate(manifest, nil).count).to eq(1)
34+
end
35+
36+
it "should return an entry with linting error information" do
37+
lint_error = subject.validate(manifest, nil)[0]
38+
39+
expect(lint_error['source']).to eq('Puppet')
40+
expect(lint_error['message']).to match('140')
41+
expect(lint_error['range']).to_not be_nil
42+
expect(lint_error['code']).to_not be_nil
43+
expect(lint_error['severity']).to_not be_nil
44+
end
45+
46+
context "but disabled" do
47+
context "on a single line" do
48+
let(:manifest) { "
49+
user { 'Bob':
50+
ensure => 'present',
51+
comment => '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890' # lint:ignore:140chars
52+
}"
53+
}
54+
55+
it "should return an empty array" do
56+
expect(subject.validate(manifest, nil)).to eq([])
57+
end
58+
end
59+
60+
context "in a linting block" do
61+
let(:manifest) { "
62+
user { 'Bob':
63+
ensure => 'present',
64+
# lint:ignore:140chars
65+
comment => '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890',
66+
# lint:endignore
67+
}"
68+
}
69+
70+
it "should return an empty array" do
71+
expect(subject.validate(manifest, nil)).to eq([])
72+
end
73+
end
74+
end
75+
end
2476
end
2577
end

0 commit comments

Comments
 (0)