diff --git a/features/action_yaml_write.feature b/features/action_yaml_write.feature new file mode 100644 index 0000000..b20371e --- /dev/null +++ b/features/action_yaml_write.feature @@ -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$/ diff --git a/features/steps/remote_steps.rb b/features/steps/remote_steps.rb index 84011ef..0db6927 100644 --- a/features/steps/remote_steps.rb +++ b/features/steps/remote_steps.rb @@ -22,6 +22,10 @@ Then /^the remote file "([^"]+)" must contain exactly "([^"]+)"$/ do |path, cont check_exact_file_content path, content end +Then /^the remote file "([^"]+)" must match \/([^\/]+)\/$/ do |path, pattern| + check_file_content path, /#{pattern}/, true +end + def stat_mode(path) in_current_dir do ('%o' % [File::Stat.new(path).mode])[-4, 4] diff --git a/lib/producer/core.rb b/lib/producer/core.rb index 99d1530..aab0839 100644 --- a/lib/producer/core.rb +++ b/lib/producer/core.rb @@ -17,6 +17,7 @@ require 'producer/core/actions/mkdir' require 'producer/core/actions/file_append' require 'producer/core/actions/file_replace_content' require 'producer/core/actions/file_writer' +require 'producer/core/actions/yaml_writer' # condition tests require 'producer/core/test' diff --git a/lib/producer/core/actions/file_writer.rb b/lib/producer/core/actions/file_writer.rb index e432380..bd127a1 100644 --- a/lib/producer/core/actions/file_writer.rb +++ b/lib/producer/core/actions/file_writer.rb @@ -3,7 +3,7 @@ module Producer module Actions class FileWriter < Action def setup - check_arguments_size! 2 + check_arguments_size! arguments_size @path, @content = arguments @options[:permissions] = @options.delete :mode if options.key? :mode @options[:owner] = @options.delete :user if options.key? :user @@ -17,6 +17,13 @@ module Producer fs.file_write @path, @content fs.setstat @path, @options unless @options.empty? end + + + private + + def arguments_size + 2 + end end end end diff --git a/lib/producer/core/actions/yaml_writer.rb b/lib/producer/core/actions/yaml_writer.rb new file mode 100644 index 0000000..58ed8f0 --- /dev/null +++ b/lib/producer/core/actions/yaml_writer.rb @@ -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 diff --git a/lib/producer/core/task.rb b/lib/producer/core/task.rb index df31246..7ea6de9 100644 --- a/lib/producer/core/task.rb +++ b/lib/producer/core/task.rb @@ -23,6 +23,7 @@ module Producer define_action :file_append, Actions::FileAppend define_action :file_replace_content, Actions::FileReplaceContent define_action :file_write, Actions::FileWriter + define_action :yaml_write, Actions::YAMLWriter attr_reader :name, :actions, :condition diff --git a/spec/producer/core/actions/yaml_writer_spec.rb b/spec/producer/core/actions/yaml_writer_spec.rb new file mode 100644 index 0000000..b5c0c8b --- /dev/null +++ b/spec/producer/core/actions/yaml_writer_spec.rb @@ -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