From 5cb6296057320d29b8629ffbc8f6faeb916bb524 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Thu, 24 Apr 2014 23:46:43 +0000 Subject: [PATCH] Accept mode as argument in `file_write' action --- features/actions/file_write.feature | 16 ++++++++++++- features/steps/remote_steps.rb | 7 ++++++ lib/producer/core/actions/file_writer.rb | 11 ++++++++- lib/producer/core/remote/fs.rb | 4 ++-- .../producer/core/actions/file_writer_spec.rb | 24 +++++++++++++++++++ spec/producer/core/remote/fs_spec.rb | 7 +++++- 6 files changed, 64 insertions(+), 5 deletions(-) diff --git a/features/actions/file_write.feature b/features/actions/file_write.feature index 9fa3674..42b575c 100644 --- a/features/actions/file_write.feature +++ b/features/actions/file_write.feature @@ -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 diff --git a/features/steps/remote_steps.rb b/features/steps/remote_steps.rb index 2193335..5855d21 100644 --- a/features/steps/remote_steps.rb +++ b/features/steps/remote_steps.rb @@ -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 diff --git a/lib/producer/core/actions/file_writer.rb b/lib/producer/core/actions/file_writer.rb index 1bac1bf..2fea259 100644 --- a/lib/producer/core/actions/file_writer.rb +++ b/lib/producer/core/actions/file_writer.rb @@ -3,7 +3,12 @@ module Producer module Actions class FileWriter < Action def apply - fs.file_write path, content + 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 diff --git a/lib/producer/core/remote/fs.rb b/lib/producer/core/remote/fs.rb index 6760ba5..69bc580 100644 --- a/lib/producer/core/remote/fs.rb +++ b/lib/producer/core/remote/fs.rb @@ -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 diff --git a/spec/producer/core/actions/file_writer_spec.rb b/spec/producer/core/actions/file_writer_spec.rb index d785d38..0614c42 100644 --- a/spec/producer/core/actions/file_writer_spec.rb +++ b/spec/producer/core/actions/file_writer_spec.rb @@ -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 diff --git a/spec/producer/core/remote/fs_spec.rb b/spec/producer/core/remote/fs_spec.rb index 39278d3..ffa720b 100644 --- a/spec/producer/core/remote/fs_spec.rb +++ b/spec/producer/core/remote/fs_spec.rb @@ -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