Skip to content

Commit

Permalink
(GH-125) Honor inline puppet lint directives
Browse files Browse the repository at this point in the history
Previously inline linting directives were not being honored by the document
validator.  This was found due to not calling the load_data method prior to
running the lint process.  This commit calls the load_data method prior to
linting and then adds a check to ignore any linting errors that were ignored by
puppet-lint.  This commit also adds tests for this scenario.
  • Loading branch information
glennsarti committed Sep 15, 2017
1 parent 86415d6 commit d5c6a8e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/puppet-languageserver/document_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,26 @@ def self.validate(content, workspace, _max_problems = 100)

# Find module root and attempt to build PuppetLint options
module_root = find_module_root_from_path(workspace)
linter_options = nil
if module_root.nil?
PuppetLint::OptParser.build
linter_options = PuppetLint::OptParser.build
else
Dir.chdir(module_root.to_s) { PuppetLint::OptParser.build }
Dir.chdir(module_root.to_s) { linter_options = PuppetLint::OptParser.build }
end
linter_options.parse!([])

begin
linter = PuppetLint::Checks.new
linter.load_data(nil, content)

problems = linter.run(nil, content)
unless problems.nil?
problems.each do |problem|
# Syntax errors are better handled by the puppet parser, not puppet lint
next if problem[:kind] == :error && problem[:check] == :syntax

# Ignore linting errors what were ignored by puppet-lint
next if problem[:kind] == :ignored

severity = case problem[:kind]
when :error
LanguageServer::DIAGNOSTICSEVERITY_ERROR
Expand Down
52 changes: 52 additions & 0 deletions spec/integration/puppet-languageserver/document_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,57 @@
end
end

describe "Given a complete manifest with a single linting error" do
let(:manifest) { "
user { 'Bob':
ensure => 'present',
comment => '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890',
}"
}

it "should return an array with one entry" do
expect(subject.validate(manifest, nil).count).to eq(1)
end

it "should return an entry with linting error information" do
lint_error = subject.validate(manifest, nil)[0]

expect(lint_error['source']).to eq('Puppet')
expect(lint_error['message']).to match('140')
expect(lint_error['range']).to_not be_nil
expect(lint_error['code']).to_not be_nil
expect(lint_error['severity']).to_not be_nil
end

context "but disabled" do
context "on a single line" do
let(:manifest) { "
user { 'Bob':
ensure => 'present',
comment => '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890' # lint:ignore:140chars
}"
}

it "should return an empty array" do
expect(subject.validate(manifest, nil)).to eq([])
end
end

context "in a linting block" do
let(:manifest) { "
user { 'Bob':
ensure => 'present',
# lint:ignore:140chars
comment => '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890',
# lint:endignore
}"
}

it "should return an empty array" do
expect(subject.validate(manifest, nil)).to eq([])
end
end
end
end
end
end

0 comments on commit d5c6a8e

Please sign in to comment.