Implement `yaml_write' action
This commit is contained in:
parent
9780cdf220
commit
0ba12bfb90
14
features/action_yaml_write.feature
Normal file
14
features/action_yaml_write.feature
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
@sshd
|
||||||
|
Feature: `yaml_write' task action
|
||||||
|
|
||||||
|
Background:
|
||||||
|
Given a recipe with:
|
||||||
|
"""
|
||||||
|
task :yaml_write_action do
|
||||||
|
yaml_write 'some_file', data: { foo: 'bar' }
|
||||||
|
end
|
||||||
|
"""
|
||||||
|
|
||||||
|
Scenario: writes given data as YAML
|
||||||
|
When I successfully execute the recipe on remote target
|
||||||
|
Then the remote file "some_file" must match /^:foo: bar$/
|
@ -22,6 +22,10 @@ Then /^the remote file "([^"]+)" must contain exactly "([^"]+)"$/ do |path, cont
|
|||||||
check_exact_file_content path, content
|
check_exact_file_content path, content
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Then /^the remote file "([^"]+)" must match \/([^\/]+)\/$/ do |path, pattern|
|
||||||
|
check_file_content path, /#{pattern}/, true
|
||||||
|
end
|
||||||
|
|
||||||
def stat_mode(path)
|
def stat_mode(path)
|
||||||
in_current_dir do
|
in_current_dir do
|
||||||
('%o' % [File::Stat.new(path).mode])[-4, 4]
|
('%o' % [File::Stat.new(path).mode])[-4, 4]
|
||||||
|
@ -17,6 +17,7 @@ require 'producer/core/actions/mkdir'
|
|||||||
require 'producer/core/actions/file_append'
|
require 'producer/core/actions/file_append'
|
||||||
require 'producer/core/actions/file_replace_content'
|
require 'producer/core/actions/file_replace_content'
|
||||||
require 'producer/core/actions/file_writer'
|
require 'producer/core/actions/file_writer'
|
||||||
|
require 'producer/core/actions/yaml_writer'
|
||||||
|
|
||||||
# condition tests
|
# condition tests
|
||||||
require 'producer/core/test'
|
require 'producer/core/test'
|
||||||
|
@ -3,7 +3,7 @@ module Producer
|
|||||||
module Actions
|
module Actions
|
||||||
class FileWriter < Action
|
class FileWriter < Action
|
||||||
def setup
|
def setup
|
||||||
check_arguments_size! 2
|
check_arguments_size! arguments_size
|
||||||
@path, @content = arguments
|
@path, @content = arguments
|
||||||
@options[:permissions] = @options.delete :mode if options.key? :mode
|
@options[:permissions] = @options.delete :mode if options.key? :mode
|
||||||
@options[:owner] = @options.delete :user if options.key? :user
|
@options[:owner] = @options.delete :user if options.key? :user
|
||||||
@ -17,6 +17,13 @@ module Producer
|
|||||||
fs.file_write @path, @content
|
fs.file_write @path, @content
|
||||||
fs.setstat @path, @options unless @options.empty?
|
fs.setstat @path, @options unless @options.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def arguments_size
|
||||||
|
2
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
23
lib/producer/core/actions/yaml_writer.rb
Normal file
23
lib/producer/core/actions/yaml_writer.rb
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
module Producer
|
||||||
|
module Core
|
||||||
|
module Actions
|
||||||
|
class YAMLWriter < FileWriter
|
||||||
|
def setup
|
||||||
|
super
|
||||||
|
@content = options.delete(:data).to_yaml
|
||||||
|
end
|
||||||
|
|
||||||
|
def name
|
||||||
|
'yaml_write'
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def arguments_size
|
||||||
|
1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -23,6 +23,7 @@ module Producer
|
|||||||
define_action :file_append, Actions::FileAppend
|
define_action :file_append, Actions::FileAppend
|
||||||
define_action :file_replace_content, Actions::FileReplaceContent
|
define_action :file_replace_content, Actions::FileReplaceContent
|
||||||
define_action :file_write, Actions::FileWriter
|
define_action :file_write, Actions::FileWriter
|
||||||
|
define_action :yaml_write, Actions::YAMLWriter
|
||||||
|
|
||||||
attr_reader :name, :actions, :condition
|
attr_reader :name, :actions, :condition
|
||||||
|
|
||||||
|
25
spec/producer/core/actions/yaml_writer_spec.rb
Normal file
25
spec/producer/core/actions/yaml_writer_spec.rb
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
module Producer::Core
|
||||||
|
module Actions
|
||||||
|
describe YAMLWriter, :env do
|
||||||
|
let(:path) { 'some_path' }
|
||||||
|
let(:data) { { foo: 'bar' } }
|
||||||
|
let(:arguments) { [path] }
|
||||||
|
let(:options) { { data: data } }
|
||||||
|
subject(:writer) { described_class.new(env, *arguments, options) }
|
||||||
|
|
||||||
|
it_behaves_like 'action'
|
||||||
|
|
||||||
|
it { is_expected.to be_a FileWriter}
|
||||||
|
|
||||||
|
describe '#apply' do
|
||||||
|
it 'writes data as YAML to file on remote filesystem' do
|
||||||
|
expect(remote_fs)
|
||||||
|
.to receive(:file_write).with(path, data.to_yaml)
|
||||||
|
writer.apply
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user