Implement `file_match' condition keyword

This commit is contained in:
Thibault Jouan 2014-09-27 12:14:16 +00:00
parent 1f84b484bd
commit 2543fdeb00
6 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,26 @@
@sshd
Feature: `file_match' condition keyword
Background:
Given a recipe with:
"""
task :file_match_test do
condition { file_match 'some_file', /\Asome_content\z/ }
echo 'evaluated'
end
"""
Scenario: succeeds when file match pattern
Given a remote file named "some_file" with "some_content"
When I successfully execute the recipe on remote target
Then the output must contain "evaluated"
Scenario: fails when file does not match pattern
Given a remote file named "some_file" with "some_other_content"
When I successfully execute the recipe on remote target
Then the output must not contain "evaluated"
Scenario: fails when file does not exist
When I successfully execute the recipe on remote target
Then the output must not contain "evaluated"

View File

@ -21,6 +21,7 @@ require 'producer/core/test'
require 'producer/core/tests/condition_test'
require 'producer/core/tests/file_contains'
require 'producer/core/tests/file_eq'
require 'producer/core/tests/file_match'
require 'producer/core/tests/has_dir'
require 'producer/core/tests/has_env'
require 'producer/core/tests/has_executable'

View File

@ -36,6 +36,7 @@ module Producer
define_test :sh, Tests::ShellCommandStatus
define_test :file_contains, Tests::FileContains
define_test :file_eq, Tests::FileEq
define_test :file_match, Tests::FileMatch
define_test :env?, Tests::HasEnv
define_test :executable?, Tests::HasExecutable
define_test :dir?, Tests::HasDir

View File

@ -0,0 +1,18 @@
module Producer
module Core
module Tests
class FileMatch < Test
def verify
!!(file_content =~ arguments[1])
end
private
def file_content
fs.file_read(arguments[0]) or ''
end
end
end
end
end

View File

@ -9,6 +9,7 @@ module Producer::Core
sh
file_contains
file_eq
file_match
dir?
env?
executable?

View File

@ -0,0 +1,46 @@
require 'spec_helper'
module Producer::Core
module Tests
describe FileMatch, :env do
let(:filepath) { 'some_file' }
let(:pattern) { /\Asome_content\z/ }
subject(:test) { described_class.new(env, filepath, pattern) }
it_behaves_like 'test'
describe '#verify' do
context 'when file matches the pattern' do
before do
allow(remote_fs)
.to receive(:file_read).with(filepath) { 'some_content' }
end
it 'returns true' do
expect(test.verify).to be true
end
end
context 'when file does not match the pattern' do
before do
allow(remote_fs).to receive(:file_read).with(filepath) { 'foo bar' }
end
it 'returns false' do
expect(test.verify).to be false
end
end
context 'when file does not exist' do
before do
allow(remote_fs).to receive(:file_read).with(filepath) { nil }
end
it 'returns false' do
expect(test.verify).to be false
end
end
end
end
end
end