Skip to content

Commit

Permalink
Check that selectors aren't used in resource declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
rodjek committed Aug 21, 2011
1 parent 3f51da6 commit a27948e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ At the moment, the following tests have been implemented:
* File modes should be represented as a 4 digit string enclosed in single
quotes.

## Conditionals

* You should not intermingle conditionals inside resource declarations (i.e.
selectors inside resources).

## Reporting bugs or incorrect results

If you find a bug in puppet-lint or its results, please create an issue in the
Expand Down
10 changes: 10 additions & 0 deletions lib/puppet-lint/plugins/check_resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ def test(data)
end
end

resource_tokens.each_index do |resource_token_idx|
if resource_tokens[resource_token_idx].first == :FARROW
if resource_tokens[resource_token_idx + 1].first == :VARIABLE
if resource_tokens[resource_token_idx + 2].first == :QMARK
warn "selector inside resource block on line #{resource_tokens[resource_token_idx].last[:line]}"
end
end
end
end

resource_type_token = tokens[tokens[0..resource[:start]].rindex { |r| r.first == :LBRACE } - 1]
if resource_type_token.last[:value] == "file"
resource_tokens.each_index do |resource_token_idx|
Expand Down
25 changes: 25 additions & 0 deletions spec/puppet-lint/check_resources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,29 @@
its(:warnings) { should include "symlink target specified in ensure attr on line 3" }
its(:errors) { should be_empty }
end

describe 'resource with a selector' do
let(:code) { "
file { 'foo':
ensure => $bar ? {
true => present,
default => absent,
},
}"
}

its(:warnings) { should include "selector inside resource block on line 3" }
its(:errors) { should be_empty }
end

describe 'resource with a variable as a attr value' do
let(:code) { "
file { 'foo',
ensure => $bar,
}"
}

its(:warnings) { should be_empty }
its(:errors) { should be_empty }
end
end

0 comments on commit a27948e

Please sign in to comment.