diff --git a/README.md b/README.md index eb553e7..85161a7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# puppet-lint global resource check +# puppet-lint global definition check -[![Build Status](https://travis-ci.org/ninech/puppet-lint-global_resource-check.svg?branch=master)](https://travis-ci.org/ninech/puppet-lint-global_resource-check) -[![Gem Version](https://badge.fury.io/rb/puppet-lint-global_resource-check.svg)](https://badge.fury.io/rb/puppet-lint-global_resource-check) +[![rspec](https://github.com/ninech/puppet-lint-global_definition-check/actions/workflows/rspec.yml/badge.svg)](https://github.com/ninech/puppet-lint-global_definition-check/actions/workflows/rspec.yml) +[![Gem Version](https://badge.fury.io/rb/puppet-lint-global_definition-check.svg)](https://badge.fury.io/rb/puppet-lint-global_definition-check) ## Installation @@ -9,13 +9,7 @@ To use this plugin, add the following like to the Gemfile in your Puppet code base and run `bundle install`. ```ruby -gem 'puppet-lint-global_resource-check', '~> 0.3.0' -``` - -For puppet-lint version 1.1 - -```ruby -gem 'puppet-lint-global_resource-check', '~> 0.2.0' +gem "puppet-lint-global_definition-check" ``` ## Usage @@ -26,9 +20,20 @@ This plugin provides a new check to `puppet-lint`. **--fix support: No** -This check will raise a error for any global defines resource. +This check will raise a error for any global resource. + +``` +ERROR: resource file in global space on line 19 +``` + +### global_function + +**--fix support: No** + +This check will raise a error for any global function. ``` -ERROR: Resource file in global space on line 19 +ERROR: token ensure_resouce in global space on line 19 ``` + diff --git a/lib/puppet-lint/plugins/check_global_resource.rb b/lib/puppet-lint/plugins/check_global_definition.rb similarity index 84% rename from lib/puppet-lint/plugins/check_global_resource.rb rename to lib/puppet-lint/plugins/check_global_definition.rb index bd6530b..934af4b 100644 --- a/lib/puppet-lint/plugins/check_global_resource.rb +++ b/lib/puppet-lint/plugins/check_global_definition.rb @@ -1,20 +1,5 @@ -PuppetLint.new_check(:global_resource) do - def check - check_for_global_resources - check_for_global_token(:NAME, "include") - check_for_global_token(:FUNCTION_NAME) - end - - def check_for_global_resources - resource_indexes.each do |r| - next if secure_ranges.any? { |s| s[0] < r[:start] && s[1] > r[:end] } - - notify :error, - message: "resource #{r[:type].value} in global space", - line: r[:type].line, - column: r[:type].column - end - end +module PuppetLintGlobalDefinionCheck + private def check_for_global_token(type, value = nil) global_tokens.each_with_index do |token, i| @@ -24,14 +9,12 @@ def check_for_global_token(type, value = nil) message = value.nil? ? token.value : "#{token.value} #{token.next_code_token.value}" notify :error, - message: "token #{message} in global space", + message: "definition #{message} in global space", line: token.line, column: token.column end end - private - def global_tokens @global_tokens ||= tokens.reject.with_index { |_, i| secure_ranges.any? { |s| s[0] < i && s[1] > i } } end @@ -48,3 +31,31 @@ def secure_ranges @secure_ranges end end + +PuppetLint.new_check(:global_resource) do + include PuppetLintGlobalDefinionCheck + + def check + check_for_global_resources + check_for_global_token(:NAME, "include") + end + + def check_for_global_resources + resource_indexes.each do |r| + next if secure_ranges.any? { |s| s[0] < r[:start] && s[1] > r[:end] } + + notify :error, + message: "resource #{r[:type].value} in global space", + line: r[:type].line, + column: r[:type].column + end + end +end + +PuppetLint.new_check(:global_function) do + include PuppetLintGlobalDefinionCheck + + def check + check_for_global_token(:FUNCTION_NAME) + end +end diff --git a/puppet-lint-global_resource-check.gemspec b/puppet-lint-global_definition-check.gemspec similarity index 80% rename from puppet-lint-global_resource-check.gemspec rename to puppet-lint-global_definition-check.gemspec index 1ae5536..6d0a020 100644 --- a/puppet-lint-global_resource-check.gemspec +++ b/puppet-lint-global_definition-check.gemspec @@ -1,17 +1,17 @@ Gem::Specification.new do |spec| - spec.name = "puppet-lint-global_resource-check" + spec.name = "puppet-lint-global_definition-check" spec.version = ENV.fetch("CI_COMMIT_TAG", "v0.0.1").delete_prefix("v") - spec.homepage = "https://github.com/ninech/puppet-lint-global_resource-check" + spec.homepage = "https://github.com/ninech/puppet-lint-global_definition-check" spec.license = "MIT" - spec.author = "Marius Rieder" - spec.email = "marius.rieder@nine.ch" + spec.author = "Nine Internet Solutions AG" + spec.email = "support@nine.ch" spec.files = Dir[ "README.md", "LICENSE", "lib/**/*", "spec/**/*", ] - spec.summary = "puppet-lint global_resource check" + spec.summary = "puppet-lint checks for global definions" spec.description = <<-EOF Extends puppet-lint to ensure that your manifests have no global resources. EOF diff --git a/spec/puppet-lint/plugins/check_global_function_spec.rb b/spec/puppet-lint/plugins/check_global_function_spec.rb new file mode 100644 index 0000000..4ca0e5d --- /dev/null +++ b/spec/puppet-lint/plugins/check_global_function_spec.rb @@ -0,0 +1,59 @@ +require "spec_helper" + +describe "global_resource" do + context "just a class" do + let(:code) { "class test { file { 'file': } }" } + + it "should not detect any problems" do + expect(problems).to have(0).problems + end + end + + context "just a define" do + let(:code) { "define test ($param = undef) { file { 'file': } }" } + + it "should not detect any problems" do + expect(problems).to have(0).problems + end + end + + context "allow node resources" do + let(:code) { "node 'test' { file { 'file': } }" } + + it "should not detect any problems" do + expect(problems).to have(0).problems + end + end + + context "global file" do + let(:code) do + "file { 'file': } define test ($param = undef) { file { 'file': } }" + end + + it "should detect a problem" do + expect(problems).to have(1).problems + end + end + + context "global includes" do + let(:code) { "class test { file { 'file': } } \ninclude testclass" } + + it "should detect a problem" do + expect(problems).to have(1).problems + end + end + + context "global defaults" do + let(:code) do + <<-EOS + Exec { + path => '/usr/bin:/usr/sbin/:/bin:/sbin', + } + EOS + end + + it "should not detect any problems" do + expect(problems).to have(0).problems + end + end +end diff --git a/spec/puppet-lint/plugins/check_global_resource_spec.rb b/spec/puppet-lint/plugins/check_global_resource_spec.rb index dfb5f6c..2954c31 100644 --- a/spec/puppet-lint/plugins/check_global_resource_spec.rb +++ b/spec/puppet-lint/plugins/check_global_resource_spec.rb @@ -1,48 +1,6 @@ require "spec_helper" -describe "global_resource" do - context "just a class" do - let(:code) { "class test { file { 'file': } }" } - - it "should not detect any problems" do - expect(problems).to have(0).problems - end - end - - context "just a define" do - let(:code) { "define test ($param = undef) { file { 'file': } }" } - - it "should not detect any problems" do - expect(problems).to have(0).problems - end - end - - context "allow node resources" do - let(:code) { "node 'test' { file { 'file': } }" } - - it "should not detect any problems" do - expect(problems).to have(0).problems - end - end - - context "global file" do - let(:code) do - "file { 'file': } define test ($param = undef) { file { 'file': } }" - end - - it "should detect a problem" do - expect(problems).to have(1).problems - end - end - - context "global includes" do - let(:code) { "class test { file { 'file': } } \ninclude testclass" } - - it "should detect a problem" do - expect(problems).to have(1).problems - end - end - +describe "global_function" do context "just a function" do let(:code) do <<-EOS @@ -72,18 +30,4 @@ class test { expect(problems).to have(1).problems end end - - context "global defaults" do - let(:code) do - <<-EOS - Exec { - path => '/usr/bin:/usr/sbin/:/bin:/sbin', - } - EOS - end - - it "should not detect any problems" do - expect(problems).to have(0).problems - end - end end