Support `file_write' status attributes
This commit is contained in:
parent
a84b34b7ca
commit
c4006416e4
@ -6,8 +6,8 @@ Feature: `file_write' task action
|
|||||||
"""
|
"""
|
||||||
task :file_write_action do
|
task :file_write_action do
|
||||||
file_write 'some_file', 'some_content'
|
file_write 'some_file', 'some_content'
|
||||||
file_write 'some_file_0600', 'some_content', 0600
|
file_write 'some_file_0600', 'some_content', mode: 0600
|
||||||
file_write 'some_file_0700', 'some_content', 0700
|
file_write 'some_file_0700', 'some_content', mode: 0700
|
||||||
end
|
end
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -2,29 +2,20 @@ module Producer
|
|||||||
module Core
|
module Core
|
||||||
module Actions
|
module Actions
|
||||||
class FileWriter < Action
|
class FileWriter < Action
|
||||||
|
def initialize(env, *args, **options)
|
||||||
|
super
|
||||||
|
@path, @content = arguments
|
||||||
|
@options[:permissions] = @options.delete :mode if @options.key? :mode
|
||||||
|
@options[:owner] = @options.delete :user if @options.key? :user
|
||||||
|
end
|
||||||
|
|
||||||
def name
|
def name
|
||||||
'file_write'
|
'file_write'
|
||||||
end
|
end
|
||||||
|
|
||||||
def apply
|
def apply
|
||||||
case arguments.size
|
fs.file_write @path, @content
|
||||||
when 2
|
fs.setstat @path, @options unless @options.empty?
|
||||||
fs.file_write path, content
|
|
||||||
when 3
|
|
||||||
fs.file_write path, content, mode
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def path
|
|
||||||
arguments[0]
|
|
||||||
end
|
|
||||||
|
|
||||||
def content
|
|
||||||
arguments[1]
|
|
||||||
end
|
|
||||||
|
|
||||||
def mode
|
|
||||||
arguments[2]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -38,8 +38,8 @@ module Producer
|
|||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def file_write(path, content, mode = nil)
|
def file_write(path, content)
|
||||||
sftp.file.open path, 'w', mode do |f|
|
sftp.file.open path, 'w' do |f|
|
||||||
f.write content
|
f.write content
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -5,52 +5,39 @@ module Producer::Core
|
|||||||
describe FileWriter, :env do
|
describe FileWriter, :env do
|
||||||
let(:path) { 'some_path' }
|
let(:path) { 'some_path' }
|
||||||
let(:content) { 'some_content' }
|
let(:content) { 'some_content' }
|
||||||
subject(:writer) { described_class.new(env, path, content) }
|
let(:options) { { } }
|
||||||
|
subject(:writer) { described_class.new(env, path, content, options) }
|
||||||
|
|
||||||
it_behaves_like 'action'
|
it_behaves_like 'action'
|
||||||
|
|
||||||
|
describe '#initialize' do
|
||||||
|
let(:options) { { mode: 0700, user: 'root' } }
|
||||||
|
|
||||||
|
it 'translates mode option as permissions' do
|
||||||
|
expect(writer.options[:permissions]).to eq 0700
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'translates user option as owner' do
|
||||||
|
expect(writer.options[:owner]).to eq 'root'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#apply' do
|
describe '#apply' do
|
||||||
it 'writes content to file on remote filesystem' do
|
it 'writes content to file on remote filesystem' do
|
||||||
expect(remote_fs).to receive(:file_write).with(path, content)
|
expect(remote_fs)
|
||||||
|
.to receive(:file_write).with(path, content)
|
||||||
writer.apply
|
writer.apply
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when a mode was given' do
|
context 'when status options are given' do
|
||||||
subject(:writer) { described_class.new(env, path, content, 0600) }
|
let(:options) { { group: 'wheel' } }
|
||||||
|
|
||||||
it 'specifies the given mode' do
|
it 'changes the directory status with given options' do
|
||||||
expect(remote_fs)
|
expect(remote_fs).to receive(:setstat).with(path, options)
|
||||||
.to receive(:file_write).with(anything, anything, 0600)
|
|
||||||
writer.apply
|
writer.apply
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#path' do
|
|
||||||
it 'returns the path' do
|
|
||||||
expect(writer.path).to eq path
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#content' do
|
|
||||||
it 'returns the content' do
|
|
||||||
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) { described_class.new(env, path, content, 0600) }
|
|
||||||
|
|
||||||
it 'returns the mode' do
|
|
||||||
expect(writer.mode).to eq 0600
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -127,7 +127,7 @@ module Producer::Core
|
|||||||
let(:content) { 'some_content' }
|
let(:content) { 'some_content' }
|
||||||
|
|
||||||
it 'opens the file' do
|
it 'opens the file' do
|
||||||
expect(sftp_file).to receive(:open).with(path, 'w', anything)
|
expect(sftp_file).to receive(:open).with(path, 'w')
|
||||||
fs.file_write path, content
|
fs.file_write path, content
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -138,11 +138,6 @@ module Producer::Core
|
|||||||
end
|
end
|
||||||
fs.file_write path, content
|
fs.file_write path, content
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user