From a6653a622d6effc12fe79ea8fb24af7cc25f1dc1 Mon Sep 17 00:00:00 2001 From: Glenn Sarti Date: Thu, 16 May 2019 16:31:07 +0800 Subject: [PATCH] (GH-121) Add tests for Puppet API 3 and 4 loading This commit adds test fixtures for Puppet 4 API style functions and modifies the integration tests to expect these new fixtures. --- .../puppet/functions/default_pup4_function.rb | 8 +++ .../environment/default_env_pup4_function.rb | 8 +++ .../modname/default_mod_pup4_function.rb | 8 +++ .../puppet/functions/badname/pup4_function.rb | 8 +++ .../profile/pup4_envprofile_function.rb | 8 +++ .../lib/puppet/functions/pup4_env_badfile.rb | 10 ++++ .../puppet/functions/pup4_env_badfunction.rb | 9 +++ .../lib/puppet/functions/pup4_env_function.rb | 8 +++ .../badname/fixture_pup4_env_function.rb | 8 +++ .../puppet/functions/fixture_pup4_badfile.rb | 10 ++++ .../functions/fixture_pup4_badfunction.rb | 9 +++ .../puppet/functions/fixture_pup4_function.rb | 8 +++ .../valid/fixture_pup4_mod_function.rb | 8 +++ .../featureflag_puppetstrings_spec.rb | 56 ++++++++++++++++++- 14 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 spec/languageserver-sidecar/fixtures/real_agent/cache/lib/puppet/functions/default_pup4_function.rb create mode 100644 spec/languageserver-sidecar/fixtures/real_agent/cache/lib/puppet/functions/environment/default_env_pup4_function.rb create mode 100644 spec/languageserver-sidecar/fixtures/real_agent/cache/lib/puppet/functions/modname/default_mod_pup4_function.rb create mode 100644 spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/badname/pup4_function.rb create mode 100644 spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/profile/pup4_envprofile_function.rb create mode 100644 spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/pup4_env_badfile.rb create mode 100644 spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/pup4_env_badfunction.rb create mode 100644 spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/pup4_env_function.rb create mode 100644 spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/badname/fixture_pup4_env_function.rb create mode 100644 spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/fixture_pup4_badfile.rb create mode 100644 spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/fixture_pup4_badfunction.rb create mode 100644 spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/fixture_pup4_function.rb create mode 100644 spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/valid/fixture_pup4_mod_function.rb diff --git a/spec/languageserver-sidecar/fixtures/real_agent/cache/lib/puppet/functions/default_pup4_function.rb b/spec/languageserver-sidecar/fixtures/real_agent/cache/lib/puppet/functions/default_pup4_function.rb new file mode 100644 index 00000000..cd958a9d --- /dev/null +++ b/spec/languageserver-sidecar/fixtures/real_agent/cache/lib/puppet/functions/default_pup4_function.rb @@ -0,0 +1,8 @@ +# Example function using the Puppet 4 API +# This should be loaded +Puppet::Functions.create_function(:default_pup4_function) do + # @return [Array] + def default_pup4_function + 'default_pup4_function result' + end +end diff --git a/spec/languageserver-sidecar/fixtures/real_agent/cache/lib/puppet/functions/environment/default_env_pup4_function.rb b/spec/languageserver-sidecar/fixtures/real_agent/cache/lib/puppet/functions/environment/default_env_pup4_function.rb new file mode 100644 index 00000000..32accc47 --- /dev/null +++ b/spec/languageserver-sidecar/fixtures/real_agent/cache/lib/puppet/functions/environment/default_env_pup4_function.rb @@ -0,0 +1,8 @@ +# Example function using the Puppet 4 API +# This should be loaded in the environment namespace +Puppet::Functions.create_function(:'environment::default_env_pup4_function') do + # @return [Array] + def default_env_pup4_function + 'default_env_pup4_function result' + end +end diff --git a/spec/languageserver-sidecar/fixtures/real_agent/cache/lib/puppet/functions/modname/default_mod_pup4_function.rb b/spec/languageserver-sidecar/fixtures/real_agent/cache/lib/puppet/functions/modname/default_mod_pup4_function.rb new file mode 100644 index 00000000..422ea373 --- /dev/null +++ b/spec/languageserver-sidecar/fixtures/real_agent/cache/lib/puppet/functions/modname/default_mod_pup4_function.rb @@ -0,0 +1,8 @@ +# Example function using the Puppet 4 API +# This should be loaded in the module called 'modname' namespace +Puppet::Functions.create_function(:'modname::default_mod_pup4_function') do + # @return [Array] + def default_mod_pup4_function + 'default_env_pup4_function result' + end +end diff --git a/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/badname/pup4_function.rb b/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/badname/pup4_function.rb new file mode 100644 index 00000000..453e94ec --- /dev/null +++ b/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/badname/pup4_function.rb @@ -0,0 +1,8 @@ +# Example function using the Puppet 4 API in a module +# This should not be loaded in the environment namespace +Puppet::Functions.create_function(:'badname::pup4_function') do + # @return [Array] + def pup4_function + 'pup4_function result' + end +end diff --git a/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/profile/pup4_envprofile_function.rb b/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/profile/pup4_envprofile_function.rb new file mode 100644 index 00000000..250d4fb1 --- /dev/null +++ b/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/profile/pup4_envprofile_function.rb @@ -0,0 +1,8 @@ +# Example function using the Puppet 4 API in a module +# This should be loaded in the module namespace +Puppet::Functions.create_function(:'profile::pup4_envprofile_function') do + # @return [Array] + def pup4_envprofile_function + 'pup4_envprofile_function result' + end +end diff --git a/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/pup4_env_badfile.rb b/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/pup4_env_badfile.rb new file mode 100644 index 00000000..3fcc02a3 --- /dev/null +++ b/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/pup4_env_badfile.rb @@ -0,0 +1,10 @@ +require 'a_bad_gem_that_does_not_exist' + +# Example function using the Puppet 4 API in a module +# This should not be loaded +Puppet::Functions.create_function(:pup4_env_badfile) do + # @return [Array] + def pup4_env_badfile + 'pup4_env_badfile result' + end +end diff --git a/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/pup4_env_badfunction.rb b/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/pup4_env_badfunction.rb new file mode 100644 index 00000000..ad31fdec --- /dev/null +++ b/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/pup4_env_badfunction.rb @@ -0,0 +1,9 @@ +# Example function using the Puppet 4 API in a module +# This should be loaded but never actually successfully invoke +Puppet::Functions.create_function(:pup4_env_badfunction) do + # @return [Array] + def pup4_env_badfunction + require 'a_bad_gem_that_does_not_exist' + 'pup4_env_badfunction result' + end +end diff --git a/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/pup4_env_function.rb b/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/pup4_env_function.rb new file mode 100644 index 00000000..88435708 --- /dev/null +++ b/spec/languageserver-sidecar/fixtures/valid_environment_workspace/site/profile/lib/puppet/functions/pup4_env_function.rb @@ -0,0 +1,8 @@ +# Example function using the Puppet 4 API in a module +# ??? This should be loaded as global namespace function +Puppet::Functions.create_function(:pup4_env_function) do + # @return [Array] + def pup4_env_function + 'pup4_env_function result' + end +end diff --git a/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/badname/fixture_pup4_env_function.rb b/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/badname/fixture_pup4_env_function.rb new file mode 100644 index 00000000..af2e8753 --- /dev/null +++ b/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/badname/fixture_pup4_env_function.rb @@ -0,0 +1,8 @@ +# Example function using the Puppet 4 API in a module +# This should not be loaded in the module namespace +Puppet::Functions.create_function(:'badname::fixture_pup4_badname_function') do + # @return [Array] + def fixture_pup4_badname_function + 'fixture_pup4_badname_function result' + end +end diff --git a/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/fixture_pup4_badfile.rb b/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/fixture_pup4_badfile.rb new file mode 100644 index 00000000..1f429c7f --- /dev/null +++ b/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/fixture_pup4_badfile.rb @@ -0,0 +1,10 @@ +require 'a_bad_gem_that_does_not_exist' + +# Example function using the Puppet 4 API in a module +# This should not be loaded +Puppet::Functions.create_function(:fixture_pup4_badfile) do + # @return [Array] + def fixture_pup4_badfile + 'fixture_pup4_badfile result' + end +end diff --git a/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/fixture_pup4_badfunction.rb b/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/fixture_pup4_badfunction.rb new file mode 100644 index 00000000..14f04645 --- /dev/null +++ b/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/fixture_pup4_badfunction.rb @@ -0,0 +1,9 @@ +# Example function using the Puppet 4 API in a module +# This should be loaded but never actually successfully invoke +Puppet::Functions.create_function(:fixture_pup4_badfunction) do + # @return [Array] + def fixture_pup4_badfunction + require 'a_bad_gem_that_does_not_exist' + 'fixture_pup4_badfunction result' + end +end diff --git a/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/fixture_pup4_function.rb b/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/fixture_pup4_function.rb new file mode 100644 index 00000000..9999aafe --- /dev/null +++ b/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/fixture_pup4_function.rb @@ -0,0 +1,8 @@ +# Example function using the Puppet 4 API in a module +# This should be loaded as global namespace function +Puppet::Functions.create_function(:fixture_pup4_function) do + # @return [Array] + def fixture_pup4_function + 'fixture_pup4_function result' + end +end diff --git a/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/valid/fixture_pup4_mod_function.rb b/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/valid/fixture_pup4_mod_function.rb new file mode 100644 index 00000000..8f60b77d --- /dev/null +++ b/spec/languageserver-sidecar/fixtures/valid_module_workspace/lib/puppet/functions/valid/fixture_pup4_mod_function.rb @@ -0,0 +1,8 @@ +# Example function using the Puppet 4 API in a module +# This should be loaded in the module namespace +Puppet::Functions.create_function(:'valid::fixture_pup4_mod_function') do + # @return [Array] + def fixture_pup4_mod_function + 'fixture_pup4_mod_function result' + end +end diff --git a/spec/languageserver-sidecar/integration/puppet-languageserver-sidecar/featureflag_puppetstrings/featureflag_puppetstrings_spec.rb b/spec/languageserver-sidecar/integration/puppet-languageserver-sidecar/featureflag_puppetstrings/featureflag_puppetstrings_spec.rb index 54407b9f..c0f3a311 100644 --- a/spec/languageserver-sidecar/integration/puppet-languageserver-sidecar/featureflag_puppetstrings/featureflag_puppetstrings_spec.rb +++ b/spec/languageserver-sidecar/integration/puppet-languageserver-sidecar/featureflag_puppetstrings/featureflag_puppetstrings_spec.rb @@ -2,7 +2,7 @@ require 'open3' require 'tempfile' -describe 'PuppetLanguageServerSidecar with Feature Flag puppetstrings' do +describe 'PuppetLanguageServerSidecar with Feature Flag puppetstrings', :if => Gem::Version.new(Puppet.version) >= Gem::Version.new('6.0.0') do def run_sidecar(cmd_options) cmd_options << '--no-cache' @@ -48,6 +48,17 @@ def with_temporary_file(content) end end + def should_not_contain_default_functions(deserial) + # These functions should not appear in the deserialised list as they're part + # of the default function set, not the workspace. + # + # They are defined in the fixtures/real_agent/cache/lib/... + expect(deserial).to_not contain_child_with_key(:default_cache_function) + expect(deserial).to_not contain_child_with_key(:default_pup4_function) + expect(deserial).to_not contain_child_with_key(:'environment::default_env_pup4_function') + expect(deserial).to_not contain_child_with_key(:'modname::default_mod_pup4_function') + end + describe 'when running default_classes action' do let (:cmd_options) { ['--action', 'default_classes'] } @@ -81,6 +92,12 @@ def with_temporary_file(content) # These are defined in the fixtures/real_agent/cache/lib/puppet/parser/functions expect(deserial).to contain_child_with_key(:default_cache_function) + # These are defined in the fixtures/real_agent/cache/lib/puppet/functions + expect(deserial).to contain_child_with_key(:default_pup4_function) + # These are defined in the fixtures/real_agent/cache/lib/puppet/functions/environment (Special environent namespace) + expect(deserial).to contain_child_with_key(:'environment::default_env_pup4_function') + # These are defined in the fixtures/real_agent/cache/lib/puppet/functions/modname (module namespaced function) + expect(deserial).to contain_child_with_key(:'modname::default_mod_pup4_function') end end @@ -153,14 +170,35 @@ def with_temporary_file(content) deserial = PuppetLanguageServer::Sidecar::Protocol::PuppetFunctionList.new() expect { deserial.from_json!(result) }.to_not raise_error + # TODO Turn this in a shared example thingy or at least a function + # These are defined in the fixtures/real_agent/cache/lib/... + should_not_contain_default_functions(deserial) + + # Puppet 3 API Functions expect(deserial).to_not contain_child_with_key(:badfile) expect(deserial).to contain_child_with_key(:bad_function) expect(deserial).to contain_child_with_key(:fixture_function) + # Puppet 4 API Functions + # The strings based parser will still see 'fixture_pup4_badfile' because it's never _actually_ loaded + # in Puppet therefore it will never error + expect(deserial).to contain_child_with_key(:fixture_pup4_badfile) + # The strings based parser will still see 'badname::fixture_pup4_badname_function' because it's never _actually_ loaded + # in Puppet therefore it will never error + expect(deserial).to contain_child_with_key(:'badname::fixture_pup4_badname_function') + expect(deserial).to contain_child_with_key(:fixture_pup4_function) + expect(deserial).to contain_child_with_key(:'valid::fixture_pup4_mod_function') + expect(deserial).to contain_child_with_key(:fixture_pup4_badfunction) + # Make sure the function has the right properties func = child_with_key(deserial, :fixture_function) expect(func.doc).to eq('doc_fixture_function') expect(func.source).to match(/valid_module_workspace/) + + # Make sure the function has the right properties + func = child_with_key(deserial, :fixture_pup4_function) + expect(func.doc).to match(/Example function using the Puppet 4 API in a module/) + expect(func.source).to match(/valid_module_workspace/) end end @@ -246,12 +284,28 @@ def with_temporary_file(content) deserial = PuppetLanguageServer::Sidecar::Protocol::PuppetFunctionList.new() expect { deserial.from_json!(result) }.to_not raise_error + should_not_contain_default_functions(deserial) + + # The strings based parser will still see 'pup4_env_badfile' because it's never _actually_ loaded + # in Puppet therefore it will never error + expect(deserial).to contain_child_with_key(:pup4_env_badfile) + # The strings based parser will still see 'badname::pup4_function' because it's never _actually_ loaded + # in Puppet therefore it will never error + expect(deserial).to contain_child_with_key(:'badname::pup4_function') expect(deserial).to contain_child_with_key(:env_function) + expect(deserial).to contain_child_with_key(:pup4_env_function) + expect(deserial).to contain_child_with_key(:pup4_env_badfunction) + expect(deserial).to contain_child_with_key(:'profile::pup4_envprofile_function') # Make sure the function has the right properties func = child_with_key(deserial, :env_function) expect(func.doc).to eq('doc_env_function') expect(func.source).to match(/valid_environment_workspace/) + + # Make sure the function has the right properties + func = child_with_key(deserial, :pup4_env_function) + expect(func.doc).to match(/Example function using the Puppet 4 API in a module/) + expect(func.source).to match(/valid_environment_workspace/) end end