Skip to content

Commit da5f13d

Browse files
committed
(GH-98) Add test for function loading and puppet fixtures
This commit adds tests for loading functions with errors. This commit adds the required file structure and fixture setting for a custom puppet environment. This is needed to isolate the spec test fixtures from a potentially misconfigured local puppet agent environment. Resulting in possible false negative and positive test results.
1 parent c22e64d commit da5f13d

File tree

10 files changed

+120
-3
lines changed

10 files changed

+120
-3
lines changed

server/spec/fixtures/cache/facts.d/.gitkeep

Whitespace-only changes.

server/spec/fixtures/cache/lib/facter/.gitkeep

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'a_bad_gem_that_does_not_exist'
2+
3+
module Puppet::Parser::Functions
4+
newfunction(:bad_file, :type => :rvalue, :doc => <<-EOS
5+
Function that will fail loading
6+
EOS
7+
) do |arguments|
8+
# Do nothing
9+
end
10+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Puppet::Parser::Functions
2+
newfunction(:bad_function, :type => :rvalue, :doc => <<-EOS
3+
Function that will fail loading
4+
EOS
5+
) do |arguments|
6+
7+
require 'a_bad_gem_that_does_not_exist'
8+
end
9+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# concat.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:concat, :type => :rvalue, :doc => <<-EOS
7+
Appends the contents of multiple arrays into array 1.
8+
9+
*Example:*
10+
11+
concat(['1','2','3'],['4','5','6'],['7','8','9'])
12+
13+
Would result in:
14+
15+
['1','2','3','4','5','6','7','8','9']
16+
EOS
17+
) do |arguments|
18+
19+
# Check that more than 2 arguments have been given ...
20+
raise(Puppet::ParseError, "concat(): Wrong number of arguments " +
21+
"given (#{arguments.size} for < 2)") if arguments.size < 2
22+
23+
a = arguments[0]
24+
25+
# Check that the first parameter is an array
26+
unless a.is_a?(Array)
27+
raise(Puppet::ParseError, 'concat(): Requires array to work with')
28+
end
29+
30+
result = a
31+
arguments.shift
32+
33+
arguments.each do |x|
34+
result = result + (x.is_a?(Array) ? x : [x])
35+
end
36+
37+
return result
38+
end
39+
end
40+
41+
# vim: set ts=2 sw=2 et :
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Text fixture puppet.conf which redirects all Puppet configuration into the
2+
# fixtures directory
3+
[main]
4+
environment = test-fixtures
5+
environmentpath = $codedir/environments
6+
codedir = $vardir/..
7+
logdir = $vardir/../logdir

server/spec/fixtures/environments/.gitkeep

Whitespace-only changes.

server/spec/fixtures/logdir/.gitkeep

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'spec_helper'
2+
3+
describe 'puppet_helper' do
4+
let(:subject) { PuppetLanguageServer::PuppetHelper }
5+
6+
# The concat function is loaded from the fixtures/cache/lib/puppet/parser/functions/concat.rb file
7+
# The bad_function function is loaded from the fixtures/cache/lib/puppet/parser/functions/badfunction.rb file
8+
# The bad_file function is loaded from the fixtures/cache/lib/puppet/parser/functions/badfile.rb file
9+
10+
let(:good_function) { 'concat' } # A valid function that works
11+
let(:bad_function) { 'bad_function' } # A function that has an error inside the function code
12+
let(:unloadable_function) { 'bad_file' } # A function that cannot be loaded
13+
14+
describe '#function_names' do
15+
it 'should list functions which do not error' do
16+
expect(subject.function_names).to include(good_function)
17+
end
18+
it 'should list functions which error inside the function code' do
19+
expect(subject.function_names).to include(bad_function)
20+
end
21+
it 'should not list functions which error during loading' do
22+
expect(subject.function_names).to_not include(unloadable_function)
23+
end
24+
end
25+
26+
describe '#function' do
27+
it 'should get details about functions which do not error' do
28+
result = subject.function(good_function)
29+
30+
expect(result).to include(:name)
31+
expect(result).to include(:type)
32+
end
33+
it 'should get details about functions which error inside the function code' do
34+
result = subject.function(bad_function)
35+
36+
expect(result).to include(:name)
37+
expect(result).to include(:type)
38+
end
39+
it 'should not get details about functions which error during loading' do
40+
result = subject.function(unloadable_function)
41+
42+
expect(result).to be_nil
43+
end
44+
end
45+
end

server/spec/spec_helper.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),'..','vendor','puppet-lint','lib'))
77

88
require 'puppet-languageserver'
9-
PuppetLanguageServer::init_puppet(PuppetLanguageServer::CommandLineParser.parse([]))
9+
fixtures_dir = File.join(File.dirname(__FILE__),'fixtures')
1010

11-
# Custom RSpec Matchers
11+
# Currently there is no way to re-initialize the puppet loader so for the moment
12+
# all tests must run off the single puppet config settings instead of per example setting
13+
server_options = PuppetLanguageServer::CommandLineParser.parse([])
14+
server_options[:puppet_settings] = ['--vardir',File.join(fixtures_dir,'cache'),
15+
'--confdir',File.join(fixtures_dir,'confdir')]
16+
PuppetLanguageServer::init_puppet(server_options)
1217

18+
# Custom RSpec Matchers
1319
RSpec::Matchers.define :be_completion_item_with_type do |value|
1420
value = [value] unless value.is_a?(Array)
1521

@@ -19,4 +25,3 @@
1925
"be a Completion Item with a data type in the list of #{value}"
2026
end
2127
end
22-

0 commit comments

Comments
 (0)