Skip to content

Commit

Permalink
Add fix
Browse files Browse the repository at this point in the history
  • Loading branch information
raphink committed Feb 10, 2015
1 parent 98dcd0f commit 3edf8d3
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 7 deletions.
24 changes: 19 additions & 5 deletions lib/puppet-lint/plugins/check_file_ensure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@
def check
resource_indexes.each do |resource|
if resource[:type].value == 'file'
attr = resource[:tokens].select { |t| t.type == :NAME && t.value == 'ensure' && t.next_code_token.type == :FARROW }
attr = resource[:tokens].select { |t| t.type == :NAME && \
t.value == 'ensure' && \
t.next_code_token.type == :FARROW }
unless attr.empty?
val_token = attr[0].next_code_token.next_code_token
if val_token.value == 'present'
notify :warning, {
:message => 'ensure set to present on file resource',
:line => val_token.line,
:column => val_token.column,
:token => val_token,
:message => 'ensure set to present on file resource',
:line => val_token.line,
:column => val_token.column,
:token => val_token,
:resource => resource,
}
end
end
end
end
end

def fix(problem)
target_attr = problem[:resource][:tokens].select { |t| t.type == :NAME && \
t.value == 'target' && \
t.next_code_token.type == :FARROW }
if target_attr.empty?
problem[:token].value = 'file'
else
problem[:token].value = 'link'
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
}
file { '/etc/fstab':
ensure => 'file',
ensure => 'target',
target => '/etc/mtab',
}
EOS
}
Expand All @@ -31,11 +32,12 @@
file { '/etc/fstab':
ensure => 'present',
target => '/etc/mtab',
}
EOS
}

it 'should detect a single problem' do
it 'should detect problems' do
expect(problems).to have(2).problems
end

Expand All @@ -45,4 +47,76 @@
end
end
end

context 'with fix enabled' do
before do
PuppetLint.configuration.fix = true
end

after do
PuppetLint.configuration.fix = false
end

context 'correct file resource declarations' do
let (:code) {
<<-EOS
file { '/etc/sudoers':
ensure => file,
}
file { '/etc/fstab':
ensure => 'target',
target => '/etc/mtab',
}
EOS
}

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

it 'should not modify the manifest' do
expect(manifest).to eq(code)
end
end

context 'wrong file resource declarations' do
let (:code) {
<<-EOS
file { '/etc/sudoers':
ensure => present,
}
file { '/etc/fstab':
ensure => 'present',
target => '/etc/mtab',
}
EOS
}

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

it 'should create a warning' do
expect(problems).to contain_fixed(msg).on_line(2).in_column(21)
expect(problems).to contain_fixed(msg).on_line(6).in_column(21)
end

it 'should fix the ensure parameter' do
expect(manifest).to eq(
<<-EOS
file { '/etc/sudoers':
ensure => file,
}
file { '/etc/fstab':
ensure => 'link',
target => '/etc/mtab',
}
EOS
)
end
end
end
end

0 comments on commit 3edf8d3

Please sign in to comment.