forked from rodjek/puppet-lint
-
Notifications
You must be signed in to change notification settings - Fork 20
(CONT-339) Add top scope facts check #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
lib/puppet-lint/plugins/top_scope_facts/top_scope_facts.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Public: A puppet-lint plugin that will check for the use of top scope facts. | ||
| # For example, the fact `$facts['kernel']` should be used over | ||
| # `$::kernel`. | ||
| # | ||
| # The check only finds facts using the top-scope: ie it will find $::operatingsystem | ||
| # but not $operatingsystem. It also all top scope variables are facts. | ||
| # If you have top scope variables that aren't facts you should configure the | ||
| # linter to ignore them. | ||
| # | ||
| # You can whitelist top scope variables to ignore via the Rake task. | ||
| # You should insert the following line to your Rakefile. | ||
| # `PuppetLint.configuration.top_scope_variables = ['location', 'role']` | ||
| # | ||
| # This plugin was adoped in to puppet-lint from https://github.com/mmckinst/puppet-lint-top_scope_facts-check | ||
| # Thanks to @mmckinst and @seanmil for the original work. | ||
| PuppetLint.new_check(:top_scope_facts) do | ||
| TOP_SCOPE_FACTS_VAR_TYPES = Set[:VARIABLE, :UNENC_VARIABLE] | ||
| def check | ||
| whitelist = ['trusted', 'facts'] + (PuppetLint.configuration.top_scope_variables || []) | ||
| whitelist = whitelist.join('|') | ||
| tokens.select { |x| TOP_SCOPE_FACTS_VAR_TYPES.include?(x.type) }.each do |token| | ||
| next unless %r{^::}.match?(token.value) | ||
| next if %r{^::(#{whitelist})\[?}.match?(token.value) | ||
| next if %r{^::[a-z0-9_][a-zA-Z0-9_]+::}.match?(token.value) | ||
|
|
||
| notify :warning, { | ||
| message: 'top scope fact instead of facts hash', | ||
| line: token.line, | ||
| column: token.column, | ||
| token: token, | ||
| } | ||
| end | ||
| end | ||
|
|
||
| def fix(problem) | ||
| problem[:token].value = "facts['" + problem[:token].value.sub(%r{^::}, '') + "']" | ||
| end | ||
| end | ||
194 changes: 194 additions & 0 deletions
194
spec/unit/puppet-lint/plugins/top_scope_facts/top_scope_facts_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe 'top_scope_facts' do | ||
| let(:msg) { 'top scope fact instead of facts hash' } | ||
|
|
||
| context 'with fix disabled' do | ||
| context 'fact variable using $facts hash' do | ||
| let(:code) { "$facts['operatingsystem']" } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
| end | ||
| context 'non-fact variable with two colons' do | ||
| let(:code) { '$foo::bar' } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
| end | ||
|
|
||
| context 'top scope $::facts hash' do | ||
| let(:code) { "$::facts['os']['family']" } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
| end | ||
|
|
||
| context 'top scope $::trusted hash' do | ||
| let(:code) { "$::trusted['certname']" } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
| end | ||
|
|
||
| context 'fact variable using top scope' do | ||
| let(:code) { '$::operatingsystem' } | ||
|
|
||
| it 'onlies detect a single problem' do | ||
| expect(problems).to have(1).problem | ||
| end | ||
|
|
||
| it 'creates a warning' do | ||
| expect(problems).to contain_warning(msg).on_line(1).in_column(1) | ||
| end | ||
| end | ||
|
|
||
| context 'fact variable using top scope with curly braces in double quote' do | ||
| let(:code) { '"${::operatingsystem}"' } | ||
|
|
||
| it 'onlies detect a single problem' do | ||
| expect(problems).to have(1).problem | ||
| end | ||
|
|
||
| it 'creates a warning' do | ||
| expect(problems).to contain_warning(msg).on_line(1).in_column(4) | ||
| end | ||
| end | ||
|
|
||
| context 'out of scope namespaced variable with leading ::' do | ||
| let(:code) { '$::profile::foo::bar' } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
|
|
||
| context 'inside double quotes' do | ||
| let(:code) { '"$::profile::foo::bar"' } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
| end | ||
|
|
||
| context 'with curly braces in double quote' do | ||
| let(:code) { '"${::profile::foo::bar}"' } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context 'with fix enabled' do | ||
| before(:each) do | ||
| PuppetLint.configuration.fix = true | ||
| end | ||
|
|
||
| after(:each) do | ||
| PuppetLint.configuration.fix = false | ||
| end | ||
|
|
||
| context 'fact variable using $facts hash' do | ||
| let(:code) { "$facts['operatingsystem']" } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
| end | ||
|
|
||
| context 'non-fact variable with two colons' do | ||
| let(:code) { '$foo::bar' } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
| end | ||
|
|
||
| context 'top scope $::facts hash' do | ||
| let(:code) { "$::facts['os']['family']" } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
| end | ||
|
|
||
| context 'top scope $::trusted hash' do | ||
| let(:code) { "$::trusted['certname']" } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
| end | ||
|
|
||
| context 'fact variable using top scope' do | ||
| let(:code) { '$::operatingsystem' } | ||
|
|
||
| it 'onlies detect a single problem' do | ||
| expect(problems).to have(1).problem | ||
| end | ||
|
|
||
| it 'fixes the problem' do | ||
| expect(problems).to contain_fixed(msg).on_line(1).in_column(1) | ||
| end | ||
|
|
||
| it 'shoulds use the facts hash' do | ||
| expect(manifest).to eq("$facts['operatingsystem']") | ||
| end | ||
| end | ||
|
|
||
| context 'fact variable using top scope with curly braces in double quote' do | ||
| let(:code) { '"${::operatingsystem}"' } | ||
|
|
||
| it 'fixes the problem' do | ||
| expect(problems).to contain_fixed(msg).on_line(1).in_column(4) | ||
| end | ||
|
|
||
| it 'shoulds use the facts hash' do | ||
| expect(manifest).to eq('"${facts[\'operatingsystem\']}"') | ||
| end | ||
| end | ||
|
|
||
| context 'with custom top scope fact variables' do | ||
| before(:each) do | ||
| PuppetLint.configuration.top_scope_variables = ['location', 'role'] | ||
| end | ||
|
|
||
| context 'fact variable using $facts hash' do | ||
| let(:code) { "$facts['operatingsystem']" } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
| end | ||
|
|
||
| context 'fact variable using $trusted hash' do | ||
| let(:code) { "$trusted['certname']" } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
| end | ||
|
|
||
| context 'whitelisted top scope variable $::location' do | ||
| let(:code) { '$::location' } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(0).problem | ||
| end | ||
| end | ||
|
|
||
| context 'non-whitelisted top scope variable $::application' do | ||
| let(:code) { '$::application' } | ||
|
|
||
| it 'does not detect any problems' do | ||
| expect(problems).to have(1).problem | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.