From adad1561c1dcada511d22415468a64afeb25e8f0 Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Tue, 28 Feb 2023 13:24:23 +0100 Subject: [PATCH] Add Unit tests for facts hash with topscope --- .../plugins/topscope_variable_spec.rb | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/spec/puppet-lint/plugins/topscope_variable_spec.rb b/spec/puppet-lint/plugins/topscope_variable_spec.rb index 94a59ec..859597a 100644 --- a/spec/puppet-lint/plugins/topscope_variable_spec.rb +++ b/spec/puppet-lint/plugins/topscope_variable_spec.rb @@ -201,6 +201,36 @@ class profile::foo { end end + context 'with incorrect topscope from facts hash' do + let(:code) do + <<~PUP + class profile::foo { + notify { 'foo': + message => $::facts['some_component_module'], + } + } + PUP + end + + it 'should detect one problem' do + expect(problems).to have(1).problem + end + + it 'should fix the problem' do + expect(problems).to contain_fixed('use $some_component_module::bar instead of $::some_component_module::bar').on_line(3).in_column(16) + end + + it 'should remove :: after the $' do + expect(manifest).to eq <<~PUP + class profile::foo { + notify { 'foo': + message => $facts['some_component_module'], + } + } + PUP + end + end + context 'with incorrect topscope in quoted variable' do let(:code) do <<~PUP @@ -230,6 +260,36 @@ class foo::blub { PUP end end + + context 'with incorrect topscope facts hash in quoted variable' do + let(:code) do + <<~PUP + class foo::blub { + notify { 'foo': + message => ">${::facts['bar'}<" + } + } + PUP + end + + it 'should detect one problem' do + expect(problems).to have(1).problem + end + + it 'should fix the problem' do + expect(problems).to contain_fixed(msg).on_line(3).in_column(20) + end + + it 'should remove :: after the $' do + expect(manifest).to eq <<~PUP + class foo::blub { + notify { 'foo': + message => ">${facts['ar']}<" + } + } + PUP + end + end end context 'without a class scope' do