Implement `file_append' task action

This commit is contained in:
Thibault Jouan 2014-03-05 02:38:00 +00:00
parent b1c7ff27b4
commit 387f37f3f5
6 changed files with 95 additions and 3 deletions

View File

@ -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"

View File

@ -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'

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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