Skip to content

Commit

Permalink
feat: split checks
Browse files Browse the repository at this point in the history
  • Loading branch information
thde committed Sep 27, 2023
1 parent 74e121a commit ccb20da
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 94 deletions.
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
# 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

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
Expand All @@ -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
```


Original file line number Diff line number Diff line change
@@ -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|
Expand All @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down
59 changes: 59 additions & 0 deletions spec/puppet-lint/plugins/check_global_function_spec.rb
Original file line number Diff line number Diff line change
@@ -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
58 changes: 1 addition & 57 deletions spec/puppet-lint/plugins/check_global_resource_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit ccb20da

Please sign in to comment.