Accept mode as argument in `file_write' action

This commit is contained in:
Thibault Jouan 2014-04-24 23:46:43 +00:00
parent 47412d8bce
commit 5cb6296057
6 changed files with 64 additions and 5 deletions

View File

@ -11,4 +11,18 @@ Feature: `file_write' task action
end
"""
When I successfully execute the recipe
And the remote file "some_file" must contain "some_content"
Then the remote file "some_file" must contain "some_content"
Scenario: creates file with given permissions
Given a recipe with:
"""
target 'some_host.test'
task :write_some_data do
file_write 'some_file_0600', 'some_content', 0600
file_write 'some_file_0700', 'some_content', 0700
end
"""
When I successfully execute the recipe
Then the remote file "some_file_0600" must have 0600 mode
And the remote file "some_file_0700" must have 0700 mode

View File

@ -21,3 +21,10 @@ end
Then /^the remote file "([^"]+)" must contain exactly "([^"]+)"$/ do |path, content|
check_exact_file_content path, content
end
Then /^the remote file "([^"]+)" must have (\d+) mode$/ do |path, mode|
in_current_dir do
puts path, ('%o' % [File::Stat.new(path).mode])[-4, 4], mode
expect(('%o' % [File::Stat.new(path).mode])[-4, 4]).to match mode
end
end

View File

@ -3,7 +3,12 @@ module Producer
module Actions
class FileWriter < Action
def apply
case arguments.size
when 2
fs.file_write path, content
when 3
fs.file_write path, content, mode
end
end
def path
@ -13,6 +18,10 @@ module Producer
def content
arguments[1]
end
def mode
arguments[2]
end
end
end
end

View File

@ -30,8 +30,8 @@ module Producer
nil
end
def file_write(path, content)
sftp.file.open path, 'w' do |f|
def file_write(path, content, mode = nil)
sftp.file.open path, 'w', mode do |f|
f.write content
end
end

View File

@ -14,6 +14,16 @@ module Producer::Core
expect(remote_fs).to receive(:file_write).with(path, content)
writer.apply
end
context 'when a mode was given' do
subject(:writer) { FileWriter.new(env, path, content, 0600) }
it 'specifies the given mode' do
expect(remote_fs)
.to receive(:file_write).with(anything, anything, 0600)
writer.apply
end
end
end
describe '#path' do
@ -27,6 +37,20 @@ module Producer::Core
expect(writer.content).to eq content
end
end
describe '#mode' do
it 'returns nil' do
expect(writer.mode).to be nil
end
context 'when a mode was given' do
subject(:writer) { FileWriter.new(env, path, content, 0600) }
it 'returns the mode' do
expect(writer.mode).to eq 0600
end
end
end
end
end
end

View File

@ -126,7 +126,7 @@ module Producer::Core
let(:content) { 'some_content' }
it 'opens the file' do
expect(sftp_file).to receive(:open).with(path, 'w')
expect(sftp_file).to receive(:open).with(path, 'w', anything)
fs.file_write path, content
end
@ -137,6 +137,11 @@ module Producer::Core
end
fs.file_write path, content
end
it 'accepts an optional mode argument' do
expect(sftp_file).to receive(:open).with(anything, anything, 0600)
fs.file_write path, content, 0600
end
end
end
end