Implement `has_env' condition keyword

This commit is contained in:
Thibault Jouan 2013-08-18 20:06:36 +00:00
parent b0ea7d876e
commit 04afc82a29
6 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,32 @@
@sshd
Feature: `has_env' condition keyword
Scenario: succeeds when remote environment variable is defined
Given a recipe with:
"""
target 'some_host.test'
task :testing_env_var_definition do
condition { has_env :shell }
echo 'evaluated'
end
"""
When I execute the recipe
Then the exit status must be 0
And the output must contain "evaluated"
Scenario: fails when remote environment variable is not defined
Given a recipe with:
"""
target 'some_host.test'
task :testing_env_var_definition do
condition { has_env :inexistent_var }
echo 'evaluated'
end
"""
When I execute the recipe
Then the exit status must be 0
And the output must not contain "evaluated"

View File

@ -5,6 +5,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/cli'
require 'producer/core/condition'

View File

@ -16,6 +16,8 @@ module Producer
end
end
define_test :has_env, Tests::HasEnv
attr_accessor :tests
def initialize(env, &block)

View File

@ -0,0 +1,11 @@
module Producer
module Core
class Tests
class HasEnv < Test
def success?
env.remote.environment.has_key? arguments.first.to_s.upcase
end
end
end
end
end

View File

@ -6,6 +6,12 @@ module Producer::Core
let(:env) { double('env') }
subject(:dsl) { Condition::DSL.new(env, &block) }
%w[has_env].each do |test|
it "has `#{test}' test defined" do
expect(dsl).to respond_to test.to_sym
end
end
describe '.evaluate' do
it 'builds a new DSL sandbox with given env and code' do
expect(Condition::DSL)

View File

@ -0,0 +1,41 @@
require 'spec_helper'
module Producer::Core
describe Tests::HasEnv do
let(:env) { Env.new }
let(:variable_name) { :some_variable_name }
subject(:has_env) { Tests::HasEnv.new(env, variable_name) }
it 'is a kind of test' do
expect(has_env).to be_a Test
end
describe '#success?' do
let(:environment) { double('environment') }
before do
allow(env.remote).to receive(:environment) { environment }
end
it 'stringifies the queried variable name' do
expect(environment).to receive(:has_key?).with(kind_of(String))
has_env.success?
end
it 'upcases the queried variable name' do
expect(environment).to receive(:has_key?).with('SOME_VARIABLE_NAME')
has_env.success?
end
it 'returns true when remote environment var is defined' do
allow(environment).to receive(:has_key?) { true }
expect(has_env.success?).to be true
end
it 'returns false when remote environment var is not defined' do
allow(environment).to receive(:has_key?) { false }
expect(has_env.success?).to be false
end
end
end
end