From eeab386216dfae7d3c1004c384e59a1cc6a92550 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Wed, 5 Mar 2014 07:19:15 +0000 Subject: [PATCH] Implement `has_executable' condition keyword --- features/tests/has_executable.feature | 30 +++++++++++++++++++ lib/producer/core.rb | 1 + lib/producer/core/condition/dsl.rb | 9 +++--- lib/producer/core/tests/has_executable.rb | 14 +++++++++ spec/producer/core/condition/dsl_spec.rb | 8 ++++- .../core/tests/has_executable_spec.rb | 29 ++++++++++++++++++ 6 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 features/tests/has_executable.feature create mode 100644 lib/producer/core/tests/has_executable.rb create mode 100644 spec/producer/core/tests/has_executable_spec.rb diff --git a/features/tests/has_executable.feature b/features/tests/has_executable.feature new file mode 100644 index 0000000..0ff2052 --- /dev/null +++ b/features/tests/has_executable.feature @@ -0,0 +1,30 @@ +@sshd +Feature: `has_executable' condition keyword + + Scenario: succeeds when remote executable is available + Given a recipe with: + """ + target 'some_host.test' + + task :testing_executable_availability do + condition { has_executable 'true' } + + echo 'evaluated' + end + """ + When I successfully execute the recipe + Then the output must contain "evaluated" + + Scenario: succeeds when remote executable is available + Given a recipe with: + """ + target 'some_host.test' + + task :testing_executable_availability do + condition { has_executable 'some_non_existent_executable' } + + echo 'evaluated' + end + """ + 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 ecd5da2..91ef733 100644 --- a/lib/producer/core.rb +++ b/lib/producer/core.rb @@ -19,6 +19,7 @@ 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_executable' require 'producer/core/tests/has_file' require 'producer/core/cli' diff --git a/lib/producer/core/condition/dsl.rb b/lib/producer/core/condition/dsl.rb index c573243..36155b4 100644 --- a/lib/producer/core/condition/dsl.rb +++ b/lib/producer/core/condition/dsl.rb @@ -13,10 +13,11 @@ module Producer end end - define_test :file_contains, Tests::FileContains - 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_executable, Tests::HasExecutable + 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_executable.rb b/lib/producer/core/tests/has_executable.rb new file mode 100644 index 0000000..9c0f032 --- /dev/null +++ b/lib/producer/core/tests/has_executable.rb @@ -0,0 +1,14 @@ +module Producer + module Core + module Tests + class HasExecutable < Test + def verify + remote.execute("type #{arguments.first}") + true + rescue RemoteCommandExecutionError + false + end + end + end + end +end diff --git a/spec/producer/core/condition/dsl_spec.rb b/spec/producer/core/condition/dsl_spec.rb index 22c7433..2765077 100644 --- a/spec/producer/core/condition/dsl_spec.rb +++ b/spec/producer/core/condition/dsl_spec.rb @@ -7,7 +7,13 @@ module Producer::Core let(:env) { double 'env' } subject(:dsl) { DSL.new(env, &block) } - %w[file_contains has_dir has_env has_file].each do |test| + %w[ + file_contains + has_dir + has_env + has_executable + 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_executable_spec.rb b/spec/producer/core/tests/has_executable_spec.rb new file mode 100644 index 0000000..4ea161f --- /dev/null +++ b/spec/producer/core/tests/has_executable_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +module Producer::Core + module Tests + describe HasExecutable, :env do + subject(:has_env) { HasExecutable.new(env, executable) } + + it_behaves_like 'test' + + describe '#verify' do + context 'executable exists' do + let(:executable) { 'true' } + + it 'returns true' do + expect(has_env.verify).to be true + end + end + + context 'executable does not exist' do + let(:executable) { 'some_non_existent_executable' } + + it 'returns false' do + expect(has_env.verify).to be false + end + end + end + end + end +end