Skip to content

Commit

Permalink
Add code
Browse files Browse the repository at this point in the history
  • Loading branch information
raphink committed May 13, 2015
1 parent 0e78c48 commit 273df28
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/puppet-lint/plugins/check_file_source_rights.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
PuppetLint.new_check(:source_without_rights) do
def token_attr(resource, name)
resource[:tokens].select do |t|
t.type == :NAME && t.value == name && \
t.next_code_token && t.next_code_token.type == :FARROW
end
end

def check_attr(resource, name, source_t)
if token_attr(resource, name).empty?
notify :warning, {
:message => "file with source missing #{name}",
:line => source_t.line,
:column => source_t.column,
:token => source_t,
}
end
end

def check
resource_indexes.each do |r|
next unless r[:type].value == 'file'
source_t = token_attr(r, 'source')
next if source_t.empty?

check_attr(r, 'owner', source_t[0])
check_attr(r, 'group', source_t[0])
check_attr(r, 'mode', source_t[0])
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'spec_helper'

describe 'source_without_rights' do
context 'when rights are passed' do
let(:code) do
<<-EOS
file { '/tmp/foo':
ensure => file,
owner => 'root',
group => '0',
mode => '0644',
source => 'puppet:///modules/bar/foo',
}
file { '/tmp/bar':
ensure => file,
content => 'qux',
}
EOS
end

it 'should not detect any problems' do
expect(problems).to have(0).problems
end
end

context 'when rights are not passed' do
let(:code) do
<<-EOS
file { '/tmp/foo':
ensure => file,
source => 'puppet:///modules/bar/foo',
}
EOS
end

it 'should detect 3 problems' do
expect(problems).to have(3).problems
end

it 'should create warnings' do
expect(problems).to contain_warning('file with source missing owner').on_line(3).in_column(11)
expect(problems).to contain_warning('file with source missing group').on_line(3).in_column(11)
expect(problems).to contain_warning('file with source missing mode').on_line(3).in_column(11)
end
end
end

0 comments on commit 273df28

Please sign in to comment.