Skip to content

Commit d259969

Browse files
committed
(GH-26) Use document store root path during manifest validation
Now that the workspace root detection has been moved from the manifest validator to the Document Store, the manifest validator can use the new methods. This commit; * Updates the validator to use the new Document Store root path detection * Removes redundant method from the validator * Removes the workspace argument as it's no longer required * Updates all call-sites and tests with the removal of the workspace argument
1 parent 4728afe commit d259969

File tree

7 files changed

+52
-82
lines changed

7 files changed

+52
-82
lines changed

lib/puppet-languageserver/epp/validation_provider.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module PuppetLanguageServer
22
module Epp
33
module ValidationProvider
4-
def self.validate(content, _workspace, _max_problems = 100)
4+
def self.validate(content, _max_problems = 100)
55
result = []
66
# TODO: Need to implement max_problems
77
_problems = 0

lib/puppet-languageserver/manifest/validation_provider.rb

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,15 @@
22
module PuppetLanguageServer
33
module Manifest
44
module ValidationProvider
5-
def self.find_module_root_from_path(path)
6-
return nil if path.nil?
7-
8-
filepath = Pathname.new(path).expand_path
9-
return nil unless filepath.exist?
10-
11-
if filepath.directory?
12-
directory = filepath
13-
else
14-
directory = filepath.dirname
15-
end
16-
17-
module_root = nil
18-
directory.ascend do |p|
19-
if p.join('metadata.json').exist?
20-
module_root = p
21-
break
22-
end
23-
end
24-
25-
module_root
26-
end
27-
285
# Similar to 'validate' this will run puppet-lint and returns
296
# the manifest with any fixes applied
307
#
318
# Returns:
329
# [ <Int> Number of problems fixed,
3310
# <String> New Content
3411
# ]
35-
def self.fix_validate_errors(content, workspace)
36-
# Find module root and attempt to build PuppetLint options
37-
module_root = find_module_root_from_path(workspace)
12+
def self.fix_validate_errors(content)
13+
module_root = PuppetLanguageServer::DocumentStore.store_root_path
3814
linter_options = nil
3915
if module_root.nil?
4016
linter_options = PuppetLint::OptParser.build
@@ -52,13 +28,12 @@ def self.fix_validate_errors(content, workspace)
5228
[problems_fixed, linter.manifest]
5329
end
5430

55-
def self.validate(content, workspace, _max_problems = 100)
31+
def self.validate(content, _max_problems = 100)
5632
result = []
5733
# TODO: Need to implement max_problems
5834
problems = 0
5935

60-
# Find module root and attempt to build PuppetLint options
61-
module_root = find_module_root_from_path(workspace)
36+
module_root = PuppetLanguageServer::DocumentStore.store_root_path
6237
linter_options = nil
6338
if module_root.nil?
6439
linter_options = PuppetLint::OptParser.build

lib/puppet-languageserver/message_router.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ module PuppetLanguageServer
22
class MessageRouter
33
attr_accessor :json_rpc_handler
44

5-
def initialize(options = {})
6-
options = {} if options.nil?
7-
@workspace = options[:workspace]
5+
def initialize(_options = {})
86
end
97

108
def documents
@@ -90,7 +88,7 @@ def receive_request(request)
9088

9189
case documents.document_type(file_uri)
9290
when :manifest
93-
changes, new_content = PuppetLanguageServer::Manifest::ValidationProvider.fix_validate_errors(content, @workspace)
91+
changes, new_content = PuppetLanguageServer::Manifest::ValidationProvider.fix_validate_errors(content)
9492
else
9593
raise "Unable to fixDiagnosticErrors on #{file_uri}"
9694
end
@@ -194,7 +192,7 @@ def receive_notification(method, params)
194192
content = params['textDocument']['text']
195193
doc_version = params['textDocument']['version']
196194
documents.set_document(file_uri, content, doc_version)
197-
PuppetLanguageServer::ValidationQueue.enqueue(file_uri, doc_version, @workspace, @json_rpc_handler)
195+
PuppetLanguageServer::ValidationQueue.enqueue(file_uri, doc_version, @json_rpc_handler)
198196

199197
when 'textDocument/didClose'
200198
PuppetLanguageServer.log_message(:info, 'Received textDocument/didClose notification.')
@@ -207,7 +205,7 @@ def receive_notification(method, params)
207205
content = params['contentChanges'][0]['text'] # TODO: Bad hardcoding zero
208206
doc_version = params['textDocument']['version']
209207
documents.set_document(file_uri, content, doc_version)
210-
PuppetLanguageServer::ValidationQueue.enqueue(file_uri, doc_version, @workspace, @json_rpc_handler)
208+
PuppetLanguageServer::ValidationQueue.enqueue(file_uri, doc_version, @json_rpc_handler)
211209

212210
when 'textDocument/didSave'
213211
PuppetLanguageServer.log_message(:info, 'Received textDocument/didSave notification.')

lib/puppet-languageserver/validation_queue.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module ValidationQueue
1111
@queue_thread = nil
1212

1313
# Enqueue a file to be validated
14-
def self.enqueue(file_uri, doc_version, workspace, connection_object)
14+
def self.enqueue(file_uri, doc_version, connection_object)
1515
document_type = PuppetLanguageServer::DocumentStore.document_type(file_uri)
1616

1717
unless %i[manifest epp puppetfile].include?(document_type)
@@ -27,7 +27,6 @@ def self.enqueue(file_uri, doc_version, workspace, connection_object)
2727
'file_uri' => file_uri,
2828
'doc_version' => doc_version,
2929
'document_type' => document_type,
30-
'workspace' => workspace,
3130
'connection_object' => connection_object
3231
}
3332
end
@@ -47,11 +46,11 @@ def self.enqueue(file_uri, doc_version, workspace, connection_object)
4746
end
4847

4948
# Synchronously validate a file
50-
def self.validate_sync(file_uri, doc_version, workspace, connection_object)
49+
def self.validate_sync(file_uri, doc_version, connection_object)
5150
document_type = PuppetLanguageServer::DocumentStore.document_type(file_uri)
5251
content = documents.document(file_uri, doc_version)
5352
return nil if content.nil?
54-
result = validate(document_type, content, workspace)
53+
result = validate(document_type, content)
5554

5655
# Send the response
5756
connection_object.reply_diagnostics(file_uri, result)
@@ -79,15 +78,15 @@ def self.reset_queue(initial_state = [])
7978
end
8079

8180
# Validate a document
82-
def self.validate(document_type, content, workspace)
81+
def self.validate(document_type, content)
8382
# Perform validation
8483
case document_type
8584
when :manifest
86-
PuppetLanguageServer::Manifest::ValidationProvider.validate(content, workspace)
85+
PuppetLanguageServer::Manifest::ValidationProvider.validate(content)
8786
when :epp
88-
PuppetLanguageServer::Epp::ValidationProvider.validate(content, workspace)
87+
PuppetLanguageServer::Epp::ValidationProvider.validate(content)
8988
when :puppetfile
90-
PuppetLanguageServer::Puppetfile::ValidationProvider.validate(content, workspace)
89+
PuppetLanguageServer::Puppetfile::ValidationProvider.validate(content)
9190
else
9291
[]
9392
end
@@ -109,7 +108,6 @@ def self.worker
109108
doc_version = work_item['doc_version']
110109
connection_object = work_item['connection_object']
111110
document_type = work_item['document_type']
112-
workspace = work_item['workspace']
113111

114112
# Check if the document is the latest version
115113
content = documents.document(file_uri, doc_version)
@@ -119,7 +117,7 @@ def self.worker
119117
end
120118

121119
# Perform validation
122-
result = validate(document_type, content, workspace)
120+
result = validate(document_type, content)
123121

124122
# Check if the document is still latest version
125123
current_version = documents.document_version(file_uri)

spec/languageserver/integration/puppet-languageserver/manifest/validation_provider_spec.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
let(:manifest) { 'user { \'Bob\'' }
99

1010
it "should return no changes" do
11-
problems_fixed, new_content = subject.fix_validate_errors(manifest, nil)
11+
problems_fixed, new_content = subject.fix_validate_errors(manifest)
1212
expect(problems_fixed).to eq(0)
1313
expect(new_content).to eq(manifest)
1414
end
@@ -27,7 +27,7 @@
2727
}
2828

2929
it "should return changes" do
30-
problems_fixed, new_content = subject.fix_validate_errors(manifest, nil)
30+
problems_fixed, new_content = subject.fix_validate_errors(manifest)
3131
expect(problems_fixed).to eq(1)
3232
expect(new_content).to eq(new_manifest)
3333
end
@@ -50,7 +50,7 @@
5050
}
5151

5252
it "should return changes" do
53-
problems_fixed, new_content = subject.fix_validate_errors(manifest, nil)
53+
problems_fixed, new_content = subject.fix_validate_errors(manifest)
5454
expect(problems_fixed).to eq(3)
5555
expect(new_content).to eq(new_manifest)
5656
end
@@ -66,7 +66,7 @@
6666
}
6767

6868
it "should return no changes" do
69-
problems_fixed, new_content = subject.fix_validate_errors(manifest, nil)
69+
problems_fixed, new_content = subject.fix_validate_errors(manifest)
7070
expect(problems_fixed).to eq(0)
7171
expect(new_content).to eq(manifest)
7272
end
@@ -78,7 +78,7 @@
7878

7979
it "should preserve CRLF" do
8080
pending('Release of https://github.com/rodjek/puppet-lint/commit/2a850ab3fd3694a4dd0c4d2f22a1e60b9ca0a495')
81-
problems_fixed, new_content = subject.fix_validate_errors(manifest, nil)
81+
problems_fixed, new_content = subject.fix_validate_errors(manifest)
8282
expect(problems_fixed).to eq(1)
8383
expect(new_content).to eq(new_manifest)
8484
end
@@ -92,7 +92,7 @@
9292
}
9393

9494
it "should return no changes" do
95-
problems_fixed, new_content = subject.fix_validate_errors(manifest, nil)
95+
problems_fixed, new_content = subject.fix_validate_errors(manifest)
9696
expect(problems_fixed).to eq(0)
9797
expect(new_content).to eq(manifest)
9898
end
@@ -104,7 +104,7 @@
104104
let(:manifest) { 'user { "Bob"' }
105105

106106
it "should return at least one error" do
107-
result = subject.validate(manifest, nil)
107+
result = subject.validate(manifest)
108108
expect(result.length).to be > 0
109109
end
110110
end
@@ -113,7 +113,7 @@
113113
let(:manifest) { "user { 'Bob': ensure => 'present' }" }
114114

115115
it "should return an empty array" do
116-
expect(subject.validate(manifest, nil)).to eq([])
116+
expect(subject.validate(manifest)).to eq([])
117117
end
118118
end
119119

@@ -123,8 +123,8 @@
123123
let(:manifest_crlf) { File.open(manifest_fixture, 'r') { |file| file.read }.gsub("\n","\r\n") }
124124

125125
it "should return same errors for both LF and CRLF line endings" do
126-
lint_error_lf = subject.validate(manifest_lf, nil)
127-
lint_error_crlf = subject.validate(manifest_crlf, nil)
126+
lint_error_lf = subject.validate(manifest_lf)
127+
lint_error_crlf = subject.validate(manifest_crlf)
128128
expect(lint_error_crlf).to eq(lint_error_lf)
129129
end
130130
end
@@ -138,11 +138,11 @@
138138
}
139139

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

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

147147
expect(lint_error['source']).to eq('Puppet')
148148
expect(lint_error['message']).to match('140')
@@ -161,7 +161,7 @@
161161
}
162162

163163
it "should return an empty array" do
164-
expect(subject.validate(manifest, nil)).to eq([])
164+
expect(subject.validate(manifest)).to eq([])
165165
end
166166
end
167167

@@ -176,7 +176,7 @@
176176
}
177177

178178
it "should return an empty array" do
179-
expect(subject.validate(manifest, nil)).to eq([])
179+
expect(subject.validate(manifest)).to eq([])
180180
end
181181
end
182182
end

spec/languageserver/unit/puppet-languageserver/message_router_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@
538538
end
539539

540540
it 'should enqueue the file for validation' do
541-
expect(PuppetLanguageServer::ValidationQueue).to receive(:enqueue).with(file_uri, 1, Object, Object)
541+
expect(PuppetLanguageServer::ValidationQueue).to receive(:enqueue).with(file_uri, 1, Object)
542542
subject.receive_notification(notification_method, notification_params)
543543
end
544544
end
@@ -609,7 +609,7 @@
609609
end
610610

611611
it 'should enqueue the file for validation' do
612-
expect(PuppetLanguageServer::ValidationQueue).to receive(:enqueue).with(file_uri, 2, Object, Object)
612+
expect(PuppetLanguageServer::ValidationQueue).to receive(:enqueue).with(file_uri, 2, Object)
613613
subject.receive_notification(notification_method, notification_params)
614614
end
615615
end

0 commit comments

Comments
 (0)