From a0482d175c1c6da24e2f16e296dd83d8b919ed13 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Thu, 23 Jan 2014 16:37:19 +0000 Subject: [PATCH] Implement `file_contains' condition keyword --- features/steps/remote_steps.rb | 4 ++ features/tests/file_contains.feature | 28 ++++++++++++ lib/producer/core.rb | 1 + lib/producer/core/condition/dsl.rb | 7 +-- lib/producer/core/tests/file_contains.rb | 18 ++++++++ spec/producer/core/condition/dsl_spec.rb | 2 +- .../producer/core/tests/file_contains_spec.rb | 44 +++++++++++++++++++ 7 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 features/tests/file_contains.feature create mode 100644 lib/producer/core/tests/file_contains.rb create mode 100644 spec/producer/core/tests/file_contains_spec.rb diff --git a/features/steps/remote_steps.rb b/features/steps/remote_steps.rb index ab9f4b1..4bbfd5f 100644 --- a/features/steps/remote_steps.rb +++ b/features/steps/remote_steps.rb @@ -6,6 +6,10 @@ Given /^a remote file named "([^"]+)"$/ do |file_name| write_file file_name, '' end +Given /^a remote file named "([^"]+)" with "([^"]+)"$/ do |file_name, content| + write_file file_name, content +end + Then /^the remote directory "([^"]+)" should exists$/ do |path| check_directory_presence([path], true) end diff --git a/features/tests/file_contains.feature b/features/tests/file_contains.feature new file mode 100644 index 0000000..e322953 --- /dev/null +++ b/features/tests/file_contains.feature @@ -0,0 +1,28 @@ +@sshd +Feature: `file_contains' condition keyword + + Background: + Given a recipe with: + """ + target 'some_host.test' + + task :testing_content_in_file_presense do + condition { file_contains 'some_file', 'some_content' } + + echo 'evaluated' + end + """ + + Scenario: succeeds when file contains expected content + Given a remote file named "some_file" with "some_content" + When I successfully execute the recipe + Then the output must contain "evaluated" + + Scenario: fails when file does not contain expected content + Given a remote file named "some_file" with "some_other_content" + When I successfully execute the recipe + Then the output must not contain "evaluated" + + Scenario: fails when file does not exist + When I successfully execute the recipe + Then the output must not contain "evaluated" diff --git a/lib/producer/core.rb b/lib/producer/core.rb index 2b56ccc..2bf9ef2 100644 --- a/lib/producer/core.rb +++ b/lib/producer/core.rb @@ -9,6 +9,7 @@ require 'producer/core/actions/file_writer' # condition tests (need to be defined before the condition DSL) require 'producer/core/test' +require 'producer/core/tests/file_contains' require 'producer/core/tests/has_dir' require 'producer/core/tests/has_env' require 'producer/core/tests/has_file' diff --git a/lib/producer/core/condition/dsl.rb b/lib/producer/core/condition/dsl.rb index 3587053..c573243 100644 --- a/lib/producer/core/condition/dsl.rb +++ b/lib/producer/core/condition/dsl.rb @@ -13,9 +13,10 @@ module Producer end end - define_test :has_env, Tests::HasEnv - define_test :has_dir, Tests::HasDir - define_test :has_file, Tests::HasFile + define_test :file_contains, Tests::FileContains + define_test :has_env, Tests::HasEnv + define_test :has_dir, Tests::HasDir + define_test :has_file, Tests::HasFile attr_reader :block, :env, :tests diff --git a/lib/producer/core/tests/file_contains.rb b/lib/producer/core/tests/file_contains.rb new file mode 100644 index 0000000..e616209 --- /dev/null +++ b/lib/producer/core/tests/file_contains.rb @@ -0,0 +1,18 @@ +module Producer + module Core + module Tests + class FileContains < Test + def verify + content = file_content + content ? content.include?(arguments[1]) : false + end + + private + + def file_content + fs.file_read(arguments[0]) + end + end + end + end +end diff --git a/spec/producer/core/condition/dsl_spec.rb b/spec/producer/core/condition/dsl_spec.rb index 0cea2e7..1ae2e0f 100644 --- a/spec/producer/core/condition/dsl_spec.rb +++ b/spec/producer/core/condition/dsl_spec.rb @@ -6,7 +6,7 @@ module Producer::Core let(:env) { double 'env' } subject(:dsl) { Condition::DSL.new(env, &block) } - %w[has_dir has_env has_file].each do |test| + %w[file_contains has_dir has_env has_file].each do |test| it "has `#{test}' test defined" do expect(dsl).to respond_to test.to_sym end diff --git a/spec/producer/core/tests/file_contains_spec.rb b/spec/producer/core/tests/file_contains_spec.rb new file mode 100644 index 0000000..45f055f --- /dev/null +++ b/spec/producer/core/tests/file_contains_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +module Producer::Core + describe Tests::FileContains do + let(:env) { Env.new } + let(:filepath) { 'some_file' } + let(:content) { 'some_content' } + subject(:test) { Tests::FileContains.new(env, filepath, content) } + + it 'is a kind of test' do + expect(test).to be_a Test + end + + describe '#verify' do + let(:fs) { double 'fs' } + + before { allow(test).to receive(:fs) { fs } } + + context 'when file contains the content' do + before { allow(fs).to receive(:file_read).with(filepath) { "foo#{content}bar" } } + + it 'returns true' do + expect(test.verify).to be true + end + end + + context 'when file does not contain the content' do + before { allow(fs).to receive(:file_read).with(filepath) { 'foo bar' } } + + it 'returns false' do + expect(test.verify).to be false + end + end + + context 'when file does not exist' do + before { allow(fs).to receive(:file_read).with(filepath) { nil } } + + it 'returns false' do + expect(test.verify).to be false + end + end + end + end +end