Skip to content

Commit

Permalink
Don't call backup unless the FileType supports it
Browse files Browse the repository at this point in the history
Some FileTypes, such as those that deal with crontabs, don't implement the
`backup` method.
  • Loading branch information
Sharpie committed Mar 5, 2014
1 parent 602b58d commit 1e5b8f2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/puppetx/filemapper.rb
Expand Up @@ -296,7 +296,7 @@ def perform_write(filename, contents)
@mapped_files[filename][:filetype] ||= Puppet::Util::FileType.filetype(self.filetype).new(filename)
filetype = @mapped_files[filename][:filetype]

filetype.backup
filetype.backup if filetype.respond_to? :backup
filetype.write(contents)
end

Expand All @@ -308,7 +308,7 @@ def remove_empty_file(filename)
@mapped_files[filename][:filetype] ||= Puppet::Util::FileType.filetype(self.filetype).new(filename)
filetype = @mapped_files[filename][:filetype]

filetype.backup
filetype.backup if filetype.respond_to? :backup

File.unlink(filename)
end
Expand Down
27 changes: 27 additions & 0 deletions spec/unit/puppetx/filemapper_spec.rb
Expand Up @@ -505,6 +505,33 @@ def select_file; '/single/file/provider'; end
subject.load_all_providers_from_disk
end

describe 'that does not implement backup' do
let(:resource) { resource = dummytype.new(params_yay) }
let(:stub_filetype) { stub() }

before :each do
subject.mapped_files['/multiple/file/provider-flush'][:filetype] = stub_filetype
subject.dirty_file!('/multiple/file/provider-flush')

stub_filetype.expects(:respond_to?).with(:backup).returns(false)
stub_filetype.expects(:backup).never
end

it 'should not call backup when writing files' do
stub_filetype.stubs(:write)

resource.flush
end

it 'should not call backup when unlinking files' do
subject.unlink_empty_files = true
subject.stubs(:format_file).returns ''
File.stubs(:exist?).with('/multiple/file/provider-flush').returns true
File.stubs(:unlink)

resource.flush
end
end
end

describe 'flush hooks' do
Expand Down

0 comments on commit 1e5b8f2

Please sign in to comment.