From 2cbe1726f7f1d88da103bfd2c5b3fb0f49b6229f Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Sat, 17 Aug 2013 17:10:48 +0000 Subject: [PATCH] Implement `has_file' condition keyword --- features/steps/remote_steps.rb | 3 +++ features/tests/has_file.feature | 23 +++++++++++++++++++ lib/producer/core.rb | 1 + lib/producer/core/condition/dsl.rb | 3 ++- lib/producer/core/tests/has_file.rb | 11 +++++++++ spec/producer/core/condition/dsl_spec.rb | 2 +- spec/producer/core/tests/has_file_spec.rb | 28 +++++++++++++++++++++++ 7 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 features/steps/remote_steps.rb create mode 100644 features/tests/has_file.feature create mode 100644 lib/producer/core/tests/has_file.rb create mode 100644 spec/producer/core/tests/has_file_spec.rb diff --git a/features/steps/remote_steps.rb b/features/steps/remote_steps.rb new file mode 100644 index 0000000..644baf4 --- /dev/null +++ b/features/steps/remote_steps.rb @@ -0,0 +1,3 @@ +Given(/^a remote file named "(.*?)"$/) do |file_name| + write_file file_name, '' +end diff --git a/features/tests/has_file.feature b/features/tests/has_file.feature new file mode 100644 index 0000000..a3a60fb --- /dev/null +++ b/features/tests/has_file.feature @@ -0,0 +1,23 @@ +@sshd +Feature: `has_file' condition keyword + + Background: + Given a recipe with: + """ + target 'some_host.test' + + task :testing_file_existence do + condition { has_file 'some_file' } + + echo 'evaluated' + end + """ + + Scenario: succeeds when file exists + Given a remote file named "some_file" + When I successfully execute the recipe + Then the output must 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 9af9571..6735974 100644 --- a/lib/producer/core.rb +++ b/lib/producer/core.rb @@ -6,6 +6,7 @@ require 'producer/core/actions/shell_command' # condition tests (need to be defined before the condition DSL) require 'producer/core/test' require 'producer/core/tests/has_env' +require 'producer/core/tests/has_file' require 'producer/core/cli' require 'producer/core/condition' diff --git a/lib/producer/core/condition/dsl.rb b/lib/producer/core/condition/dsl.rb index bfdcab3..46da5fe 100644 --- a/lib/producer/core/condition/dsl.rb +++ b/lib/producer/core/condition/dsl.rb @@ -16,7 +16,8 @@ module Producer end end - define_test :has_env, Tests::HasEnv + define_test :has_env, Tests::HasEnv + define_test :has_file, Tests::HasFile attr_accessor :tests diff --git a/lib/producer/core/tests/has_file.rb b/lib/producer/core/tests/has_file.rb new file mode 100644 index 0000000..fcec3d6 --- /dev/null +++ b/lib/producer/core/tests/has_file.rb @@ -0,0 +1,11 @@ +module Producer + module Core + module Tests + class HasFile < Test + def success? + env.remote.fs.has_file? arguments.first + end + end + end + end +end diff --git a/spec/producer/core/condition/dsl_spec.rb b/spec/producer/core/condition/dsl_spec.rb index 271bbe6..5ab38b9 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_env].each do |test| + %w[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/has_file_spec.rb b/spec/producer/core/tests/has_file_spec.rb new file mode 100644 index 0000000..4c2e73e --- /dev/null +++ b/spec/producer/core/tests/has_file_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +module Producer::Core + describe Tests::HasFile do + let(:env) { Env.new } + let(:filepath) { 'some_file' } + subject(:has_file) { Tests::HasFile.new(env, filepath) } + + it 'is a kind of test' do + expect(has_file).to be_a Test + end + + describe '#success?', :ssh do + before { sftp_story } + + it 'delegates the call on remote FS' do + expect(env.remote.fs).to receive(:has_file?).with(filepath) + has_file.success? + end + + it 'returns the file existence' do + existence = double('existence') + allow(env.remote.fs).to receive(:has_file?) { existence } + expect(has_file.success?).to be existence + end + end + end +end