From 387f37f3f54f41656683c89640f7f9c1e6405df3 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Wed, 5 Mar 2014 02:38:00 +0000 Subject: [PATCH] Implement `file_append' task action --- features/actions/file_append.feature | 17 ++++++++ lib/producer/core.rb | 1 + lib/producer/core/actions/file_append.rb | 23 ++++++++++ lib/producer/core/task/dsl.rb | 6 ++- .../producer/core/actions/file_append_spec.rb | 42 +++++++++++++++++++ spec/producer/core/task/dsl_spec.rb | 9 +++- 6 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 features/actions/file_append.feature create mode 100644 lib/producer/core/actions/file_append.rb create mode 100644 spec/producer/core/actions/file_append_spec.rb diff --git a/features/actions/file_append.feature b/features/actions/file_append.feature new file mode 100644 index 0000000..bda1912 --- /dev/null +++ b/features/actions/file_append.feature @@ -0,0 +1,17 @@ +@sshd +Feature: `file_append' task action + + Background: + Given a remote file named "some_file" with "some content" + + Scenario: appends given content to requested file + Given a recipe with: + """ + target 'some_host.test' + + task :append_content_to_file do + file_append 'some_file', ' added' + end + """ + When I successfully execute the recipe + Then the remote file "some_file" must contain exactly "some content added" diff --git a/lib/producer/core.rb b/lib/producer/core.rb index 122eaf8..ecd5da2 100644 --- a/lib/producer/core.rb +++ b/lib/producer/core.rb @@ -10,6 +10,7 @@ require 'producer/core/action' require 'producer/core/actions/echo' require 'producer/core/actions/shell_command' require 'producer/core/actions/mkdir' +require 'producer/core/actions/file_append' require 'producer/core/actions/file_replace_content' require 'producer/core/actions/file_writer' diff --git a/lib/producer/core/actions/file_append.rb b/lib/producer/core/actions/file_append.rb new file mode 100644 index 0000000..ecdece1 --- /dev/null +++ b/lib/producer/core/actions/file_append.rb @@ -0,0 +1,23 @@ +module Producer + module Core + module Actions + class FileAppend < Action + def apply + fs.file_write path, combined_content + end + + def path + arguments[0] + end + + def content + arguments[1] + end + + def combined_content + fs.file_read(path) + content + end + end + end + end +end diff --git a/lib/producer/core/task/dsl.rb b/lib/producer/core/task/dsl.rb index 73830ea..17839af 100644 --- a/lib/producer/core/task/dsl.rb +++ b/lib/producer/core/task/dsl.rb @@ -13,9 +13,11 @@ module Producer define_action :echo, Actions::Echo define_action :sh, Actions::ShellCommand - define_action :mkdir, Actions::Mkdir - define_action :file_write, Actions::FileWriter + define_action :mkdir, Actions::Mkdir + + define_action :file_append, Actions::FileAppend define_action :file_replace_content, Actions::FileReplaceContent + define_action :file_write, Actions::FileWriter attr_reader :env, :block, :actions diff --git a/spec/producer/core/actions/file_append_spec.rb b/spec/producer/core/actions/file_append_spec.rb new file mode 100644 index 0000000..8986ab5 --- /dev/null +++ b/spec/producer/core/actions/file_append_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +module Producer::Core + module Actions + describe FileAppend, :env do + let(:path) { 'some_path' } + let(:content) { 'some content' } + let(:added_content) { ' added' } + subject(:action) { FileAppend.new(env, path, added_content) } + + it_behaves_like 'action' + + before { allow(remote_fs).to receive(:file_read).with(path) { content } } + + describe '#apply' do + it 'appends given content to requested file on remote filesystem' do + expect(remote_fs) + .to receive(:file_write).with(path, action.combined_content) + action.apply + end + end + + describe '#path' do + it 'returns the file path' do + expect(action.path).to eq path + end + end + + describe '#content' do + it 'returns the content to append' do + expect(action.content).to eq added_content + end + end + + describe '#combined_content' do + it 'returns original content and added content combined' do + expect(action.combined_content).to eq 'some content added' + end + end + end + end +end diff --git a/spec/producer/core/task/dsl_spec.rb b/spec/producer/core/task/dsl_spec.rb index f46d139..fec6ba8 100644 --- a/spec/producer/core/task/dsl_spec.rb +++ b/spec/producer/core/task/dsl_spec.rb @@ -7,7 +7,14 @@ module Producer::Core let(:env) { Env.new } subject(:dsl) { DSL.new(env, &block) } - %w[echo sh mkdir file_write file_replace_content].each do |action| + %w[ + echo + sh + mkdir + file_append + file_replace_content + file_write + ].each do |action| it "has `#{action}' action defined" do expect(dsl).to respond_to action.to_sym end