Skip to content

Commit

Permalink
(puppetlabsGH-189) Reset PuppetLint configuration for each call
Browse files Browse the repository at this point in the history
Previously after a call to fix_validation_errors, many validation errors came
in as hints not errors.  This was due to PuppetLint not resetting the
`PuppetLint.configuration.fix` setting between calls.  This commit updates the
validation provider to reset this setting each call.  This commit also adds
tests for this scenario.
  • Loading branch information
glennsarti committed Jan 6, 2021
1 parent fa980bd commit c70511b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
41 changes: 20 additions & 21 deletions lib/puppet-languageserver/manifest/validation_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ module ValidationProvider
# <String> New Content
# ]
def self.fix_validate_errors(session_state, content)
module_root = session_state.documents.store_root_path
linter_options = nil
if module_root.nil?
linter_options = PuppetLint::OptParser.build
else
Dir.chdir(module_root.to_s) { linter_options = PuppetLint::OptParser.build }
end
linter_options.parse!(['--fix'])
init_puppet_lint(session_state.documents.store_root_path, ['--fix'])

linter = PuppetLint::Checks.new
linter.load_data(nil, content)
Expand All @@ -40,19 +33,7 @@ def self.validate(session_state, content, options = {})
# TODO: Need to implement max_problems
problems = 0

module_root = session_state.documents.store_root_path
linter_options = nil
if module_root.nil?
linter_options = PuppetLint::OptParser.build
else
begin
Dir.chdir(module_root.to_s) { linter_options = PuppetLint::OptParser.build }
rescue OptionParser::InvalidOption => e
PuppetLanguageServer.log_message(:error, "(#{name}) Error reading Puppet Lint configuration. Using default: #{e}")
linter_options = PuppetLint::OptParser.build
end
end
linter_options.parse!([])
init_puppet_lint(session_state.documents.store_root_path, [])

begin
linter = PuppetLint::Checks.new
Expand Down Expand Up @@ -128,6 +109,24 @@ def self.validate(session_state, content, options = {})

result
end

def self.init_puppet_lint(root_dir, lint_options = [])
linter_options = nil
if root_dir.nil?
linter_options = PuppetLint::OptParser.build
else
begin
Dir.chdir(module_root.to_s) { linter_options = PuppetLint::OptParser.build }
rescue OptionParser::InvalidOption => e
PuppetLanguageServer.log_message(:error, "(#{name}) Error reading Puppet Lint configuration. Using default: #{e}")
linter_options = PuppetLint::OptParser.build
end
end
# Reset the fix flag
PuppetLint.configuration.fix = false
linter_options.parse!(lint_options)
end
private_class_method :init_puppet_lint
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,20 @@
end
end
end

describe "Given a complete manifest with validation errors" do
let(:manifest) { "class bad_formatting {\n user { 'username':\n ensure => absent,\n auth_membership => 'false',\n }\n} " }

it "should return errors and warnings even after fix_validate_errors" do
fixes = subject.fix_validate_errors(session_state, manifest)
validation = subject.validate(session_state, manifest)

expect(validation.count).to_not be_zero

validation.each do |problem|
expect([LSP::DiagnosticSeverity::ERROR, LSP::DiagnosticSeverity::WARNING]).to include(problem.severity)
end
end
end
end
end

0 comments on commit c70511b

Please sign in to comment.