Implement `file_replace_content' task action
This commit is contained in:
parent
a7f6d7034b
commit
d22b023ff4
31
features/actions/file_replace_content.feature
Normal file
31
features/actions/file_replace_content.feature
Normal file
@ -0,0 +1,31 @@
|
||||
@sshd
|
||||
Feature: `file_replace_content' task action
|
||||
|
||||
Background:
|
||||
Given a remote file named "some_file" with "some content"
|
||||
|
||||
Scenario: replace a string by another in the requested file
|
||||
Given a recipe with:
|
||||
"""
|
||||
target 'some_host.test'
|
||||
|
||||
task :replace_string_in_file do
|
||||
file_replace_content 'some_file', 'content', 'other content'
|
||||
end
|
||||
"""
|
||||
When I execute the recipe
|
||||
Then the exit status must be 0
|
||||
And the remote file "some_file" must contain exactly "some other content"
|
||||
|
||||
Scenario: replace a regular expression by a string in the requested file
|
||||
Given a recipe with:
|
||||
"""
|
||||
target 'some_host.test'
|
||||
|
||||
task :replace_string_in_file do
|
||||
file_replace_content 'some_file', /\w+\z/, 'other content'
|
||||
end
|
||||
"""
|
||||
When I execute the recipe
|
||||
Then the exit status must be 0
|
||||
And the remote file "some_file" must contain exactly "some other content"
|
@ -17,3 +17,7 @@ end
|
||||
Then /^the remote file "([^"]+)" should contain "([^"]+)"/ do |path, content|
|
||||
check_file_content path, content, true
|
||||
end
|
||||
|
||||
Then /^the remote file "([^"]+)" should contain exactly "([^"]+)"/ do |path, content|
|
||||
check_exact_file_content path, content
|
||||
end
|
||||
|
@ -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_replace_content'
|
||||
require 'producer/core/actions/file_writer'
|
||||
|
||||
# condition tests (need to be defined before the condition DSL)
|
||||
|
27
lib/producer/core/actions/file_replace_content.rb
Normal file
27
lib/producer/core/actions/file_replace_content.rb
Normal file
@ -0,0 +1,27 @@
|
||||
module Producer
|
||||
module Core
|
||||
module Actions
|
||||
class FileReplaceContent < Action
|
||||
def apply
|
||||
fs.file_write path, replaced_content
|
||||
end
|
||||
|
||||
def path
|
||||
arguments[0]
|
||||
end
|
||||
|
||||
def pattern
|
||||
arguments[1]
|
||||
end
|
||||
|
||||
def replacement
|
||||
arguments[2]
|
||||
end
|
||||
|
||||
def replaced_content
|
||||
fs.file_read(path).gsub pattern, replacement
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -13,8 +13,9 @@ 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_write, Actions::FileWriter
|
||||
define_action :file_replace_content, Actions::FileReplaceContent
|
||||
|
||||
attr_reader :env, :block, :actions
|
||||
|
||||
|
49
spec/producer/core/actions/file_replace_content_spec.rb
Normal file
49
spec/producer/core/actions/file_replace_content_spec.rb
Normal file
@ -0,0 +1,49 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module Producer::Core
|
||||
module Actions
|
||||
describe FileReplaceContent, :env do
|
||||
let(:path) { 'some_path' }
|
||||
let(:pattern) { 'content' }
|
||||
let(:replacement) { 'other content' }
|
||||
let(:content) { 'some content' }
|
||||
subject(:action) { FileReplaceContent.new(env, path, pattern, replacement) }
|
||||
|
||||
it_behaves_like 'action'
|
||||
|
||||
before { allow(remote_fs).to receive(:file_read).with(path) { content } }
|
||||
|
||||
describe '#apply' do
|
||||
it 'writes replaced content to file on remote filesystem' do
|
||||
expect(remote_fs)
|
||||
.to receive(:file_write).with(path, action.replaced_content)
|
||||
action.apply
|
||||
end
|
||||
end
|
||||
|
||||
describe '#path' do
|
||||
it 'returns the file path' do
|
||||
expect(action.path).to eq path
|
||||
end
|
||||
end
|
||||
|
||||
describe '#pattern' do
|
||||
it 'returns the pattern' do
|
||||
expect(action.pattern).to eq pattern
|
||||
end
|
||||
end
|
||||
|
||||
describe '#replacement' do
|
||||
it 'returns the replacement' do
|
||||
expect(action.replacement).to eq replacement
|
||||
end
|
||||
end
|
||||
|
||||
describe '#replaced_content' do
|
||||
it 'returns content with pattern occurrences pattern replaced' do
|
||||
expect(action.replaced_content).to eq 'some other content'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -7,7 +7,7 @@ module Producer::Core
|
||||
let(:env) { Env.new }
|
||||
subject(:dsl) { DSL.new(env, &block) }
|
||||
|
||||
%w[echo sh mkdir file_write].each do |action|
|
||||
%w[echo sh mkdir file_write file_replace_content].each do |action|
|
||||
it "has `#{action}' action defined" do
|
||||
expect(dsl).to respond_to action.to_sym
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user