From 2543fdeb00f57a7b20713a376442ff456f4c6b8e Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Sat, 27 Sep 2014 12:14:16 +0000 Subject: [PATCH] Implement `file_match' condition keyword --- features/test_file_match.feature | 26 ++++++++++++ lib/producer/core.rb | 1 + lib/producer/core/condition.rb | 1 + lib/producer/core/tests/file_match.rb | 18 ++++++++ spec/producer/core/condition_spec.rb | 1 + spec/producer/core/tests/file_match_spec.rb | 46 +++++++++++++++++++++ 6 files changed, 93 insertions(+) create mode 100644 features/test_file_match.feature create mode 100644 lib/producer/core/tests/file_match.rb create mode 100644 spec/producer/core/tests/file_match_spec.rb diff --git a/features/test_file_match.feature b/features/test_file_match.feature new file mode 100644 index 0000000..5775f35 --- /dev/null +++ b/features/test_file_match.feature @@ -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" diff --git a/lib/producer/core.rb b/lib/producer/core.rb index 4e1c85f..3bf9a8a 100644 --- a/lib/producer/core.rb +++ b/lib/producer/core.rb @@ -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' diff --git a/lib/producer/core/condition.rb b/lib/producer/core/condition.rb index 633e05d..2e8e143 100644 --- a/lib/producer/core/condition.rb +++ b/lib/producer/core/condition.rb @@ -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 diff --git a/lib/producer/core/tests/file_match.rb b/lib/producer/core/tests/file_match.rb new file mode 100644 index 0000000..4cbccb3 --- /dev/null +++ b/lib/producer/core/tests/file_match.rb @@ -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 diff --git a/spec/producer/core/condition_spec.rb b/spec/producer/core/condition_spec.rb index 02dc75d..99fae3b 100644 --- a/spec/producer/core/condition_spec.rb +++ b/spec/producer/core/condition_spec.rb @@ -9,6 +9,7 @@ module Producer::Core sh file_contains file_eq + file_match dir? env? executable? diff --git a/spec/producer/core/tests/file_match_spec.rb b/spec/producer/core/tests/file_match_spec.rb new file mode 100644 index 0000000..fe06770 --- /dev/null +++ b/spec/producer/core/tests/file_match_spec.rb @@ -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