From 0b4df20f55f965fb517959e26e787d55e19d9ad9 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Tue, 21 Jan 2014 15:02:07 +0000 Subject: [PATCH] Implement `has_dir' condition keyword --- features/steps/remote_steps.rb | 4 ++++ features/tests/has_dir.feature | 23 +++++++++++++++++++ lib/producer/core.rb | 1 + lib/producer/core/condition/dsl.rb | 1 + lib/producer/core/tests/has_dir.rb | 11 ++++++++++ spec/producer/core/condition/dsl_spec.rb | 2 +- spec/producer/core/tests/has_dir_spec.rb | 28 ++++++++++++++++++++++++ 7 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 features/tests/has_dir.feature create mode 100644 lib/producer/core/tests/has_dir.rb create mode 100644 spec/producer/core/tests/has_dir_spec.rb diff --git a/features/steps/remote_steps.rb b/features/steps/remote_steps.rb index 427a5a9..0a43e39 100644 --- a/features/steps/remote_steps.rb +++ b/features/steps/remote_steps.rb @@ -1,3 +1,7 @@ +Given /^a remote directory named "([^"]+)"$/ do |path| + create_dir path +end + Given /^a remote file named "([^"]+)"$/ do |file_name| write_file file_name, '' end diff --git a/features/tests/has_dir.feature b/features/tests/has_dir.feature new file mode 100644 index 0000000..ec1b3bf --- /dev/null +++ b/features/tests/has_dir.feature @@ -0,0 +1,23 @@ +@sshd +Feature: `has_dir' condition keyword + + Background: + Given a recipe with: + """ + target 'some_host.test' + + task :testing_directory_existence do + condition { has_dir 'some_directory' } + + echo 'evaluated' + end + """ + + Scenario: succeeds when directory exists + Given a remote directory named "some_directory" + When I successfully execute the recipe + Then the output must contain "evaluated" + + Scenario: fails when directory 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 99ea0fb..b2f9a2d 100644 --- a/lib/producer/core.rb +++ b/lib/producer/core.rb @@ -6,6 +6,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/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 75b8b71..3587053 100644 --- a/lib/producer/core/condition/dsl.rb +++ b/lib/producer/core/condition/dsl.rb @@ -14,6 +14,7 @@ module Producer end 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/has_dir.rb b/lib/producer/core/tests/has_dir.rb new file mode 100644 index 0000000..ff6deaf --- /dev/null +++ b/lib/producer/core/tests/has_dir.rb @@ -0,0 +1,11 @@ +module Producer + module Core + module Tests + class HasDir < Test + def verify + fs.dir? 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 9024d16..0cea2e7 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 has_file].each do |test| + %w[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/has_dir_spec.rb b/spec/producer/core/tests/has_dir_spec.rb new file mode 100644 index 0000000..4f5e5ee --- /dev/null +++ b/spec/producer/core/tests/has_dir_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +module Producer::Core + describe Tests::HasDir do + let(:env) { Env.new } + let(:path) { 'some_directory' } + subject(:has_dir) { Tests::HasDir.new(env, path) } + + it 'is a kind of test' do + expect(has_dir).to be_a Test + end + + describe '#verify', :ssh do + before { sftp_story } + + it 'delegates the call on remote FS' do + expect(env.remote.fs).to receive(:dir?).with(path) + has_dir.verify + end + + it 'returns the dir existence' do + existence = double 'existence' + allow(env.remote.fs).to receive(:dir?) { existence } + expect(has_dir.verify).to be existence + end + end + end +end