Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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