Implement `file_match' condition keyword
This commit is contained in:
parent
1f84b484bd
commit
2543fdeb00
26
features/test_file_match.feature
Normal file
26
features/test_file_match.feature
Normal 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"
|
@ -21,6 +21,7 @@ require 'producer/core/test'
|
|||||||
require 'producer/core/tests/condition_test'
|
require 'producer/core/tests/condition_test'
|
||||||
require 'producer/core/tests/file_contains'
|
require 'producer/core/tests/file_contains'
|
||||||
require 'producer/core/tests/file_eq'
|
require 'producer/core/tests/file_eq'
|
||||||
|
require 'producer/core/tests/file_match'
|
||||||
require 'producer/core/tests/has_dir'
|
require 'producer/core/tests/has_dir'
|
||||||
require 'producer/core/tests/has_env'
|
require 'producer/core/tests/has_env'
|
||||||
require 'producer/core/tests/has_executable'
|
require 'producer/core/tests/has_executable'
|
||||||
|
@ -36,6 +36,7 @@ module Producer
|
|||||||
define_test :sh, Tests::ShellCommandStatus
|
define_test :sh, Tests::ShellCommandStatus
|
||||||
define_test :file_contains, Tests::FileContains
|
define_test :file_contains, Tests::FileContains
|
||||||
define_test :file_eq, Tests::FileEq
|
define_test :file_eq, Tests::FileEq
|
||||||
|
define_test :file_match, Tests::FileMatch
|
||||||
define_test :env?, Tests::HasEnv
|
define_test :env?, Tests::HasEnv
|
||||||
define_test :executable?, Tests::HasExecutable
|
define_test :executable?, Tests::HasExecutable
|
||||||
define_test :dir?, Tests::HasDir
|
define_test :dir?, Tests::HasDir
|
||||||
|
18
lib/producer/core/tests/file_match.rb
Normal file
18
lib/producer/core/tests/file_match.rb
Normal 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
|
@ -9,6 +9,7 @@ module Producer::Core
|
|||||||
sh
|
sh
|
||||||
file_contains
|
file_contains
|
||||||
file_eq
|
file_eq
|
||||||
|
file_match
|
||||||
dir?
|
dir?
|
||||||
env?
|
env?
|
||||||
executable?
|
executable?
|
||||||
|
46
spec/producer/core/tests/file_match_spec.rb
Normal file
46
spec/producer/core/tests/file_match_spec.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user