Skip to content

Commit

Permalink
(GH-163) Allow the aggregate to be used in all cases
Browse files Browse the repository at this point in the history
Previously the metadata aggregate action could only be used with the
puppetstrings feature flag.  However it is also very useful for normal operation.
This commit changes the action to be valid, and adds tests for this scenario.
  • Loading branch information
glennsarti committed Aug 19, 2019
1 parent a202b31 commit 34c01bf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/puppet-languageserver/sidecar_protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ def types
@aggregate[:types]
end

def concat!(array)
return if array.nil? || array.empty?
array.each { |item| append!(item) }
end

def append!(obj)
list_for_object_class(obj.class) << obj
end
Expand Down
16 changes: 12 additions & 4 deletions lib/puppet_languageserver_sidecar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ def self.inject_workspace_as_environment
def self.execute(options)
use_puppet_strings = featureflag?('puppetstrings')

log_message(:debug, "Executing #{options[:action]} action")
case options[:action].downcase
when 'noop'
[]
Expand All @@ -265,8 +266,7 @@ def self.execute(options)
if use_puppet_strings
PuppetLanguageServerSidecar::PuppetHelper.retrieve_via_puppet_strings(cache, :object_types => PuppetLanguageServerSidecar::PuppetHelper.available_documentation_types)
else
log_message(:warn, 'The default_aggregate action is only supported with the puppetstrings feature flag')
{}
create_aggregate(cache)
end

when 'default_classes'
Expand Down Expand Up @@ -326,8 +326,7 @@ def self.execute(options)
:object_types => PuppetLanguageServerSidecar::PuppetHelper.available_documentation_types,
:root_path => PuppetLanguageServerSidecar::Workspace.root_path)
else
log_message(:warn, 'The workspace_aggregate action is only supported with the puppetstrings feature flag')
{}
create_aggregate(PuppetLanguageServerSidecar::Cache::Null.new, PuppetLanguageServerSidecar::Workspace.root_path)
end

when 'workspace_classes'
Expand Down Expand Up @@ -374,6 +373,15 @@ def self.execute(options)
end
end

def self.create_aggregate(cache, root_path = nil)
result = PuppetLanguageServer::Sidecar::Protocol::AggregateMetadata.new
result.concat!(PuppetLanguageServerSidecar::PuppetHelper.retrieve_types(cache, :root_path => root_path))
result.concat!(PuppetLanguageServerSidecar::PuppetHelper.retrieve_functions(cache, :root_path => root_path))
result.concat!(PuppetLanguageServerSidecar::PuppetHelper.retrieve_classes(cache, :root_path => root_path))
result
end
private_class_method :create_aggregate

def self.output(result, options)
if options[:output].nil? || options[:output].empty?
STDOUT.binmode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ def with_temporary_file(content)
describe 'when running default_aggregate action' do
let (:cmd_options) { ['--action', 'default_aggregate'] }

it 'should return an empty hash' do
it 'should return a deserializable aggregate object with all default metadata' do
result = run_sidecar(cmd_options)
deserial = PuppetLanguageServer::Sidecar::Protocol::AggregateMetadata.new
expect { deserial.from_json!(result) }.to_not raise_error

# The contents of the result are tested later

expect(result).to eq('{}')
# There should be at least one item per list in the aggregate
deserial.each_list do |_, v|
expect(v.count).to be > 0
end
end
end

Expand Down Expand Up @@ -139,10 +146,17 @@ def with_temporary_file(content)
describe 'when running workspace_aggregate action' do
let (:cmd_options) { ['--action', 'workspace_aggregate', '--local-workspace', workspace] }

it 'should return an empty hash' do
it 'should return a deserializable aggregate object with all workspace metadata' do
result = run_sidecar(cmd_options)
deserial = PuppetLanguageServer::Sidecar::Protocol::AggregateMetadata.new
expect { deserial.from_json!(result) }.to_not raise_error

expect(result).to eq('{}')
# The contents of the result are tested later

# There should be at least one item per list in the aggregate
deserial.each_list do |_, v|
expect(v.count).to be > 0
end
end
end

Expand Down

0 comments on commit 34c01bf

Please sign in to comment.