diff --git a/features/tests/has_env.feature b/features/tests/has_env.feature new file mode 100644 index 0000000..817ef72 --- /dev/null +++ b/features/tests/has_env.feature @@ -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" diff --git a/lib/producer/core.rb b/lib/producer/core.rb index 267d797..44810d5 100644 --- a/lib/producer/core.rb +++ b/lib/producer/core.rb @@ -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' diff --git a/lib/producer/core/condition/dsl.rb b/lib/producer/core/condition/dsl.rb index 0b1aa2b..bfdcab3 100644 --- a/lib/producer/core/condition/dsl.rb +++ b/lib/producer/core/condition/dsl.rb @@ -16,6 +16,8 @@ module Producer end end + define_test :has_env, Tests::HasEnv + attr_accessor :tests def initialize(env, &block) diff --git a/lib/producer/core/tests/has_env.rb b/lib/producer/core/tests/has_env.rb new file mode 100644 index 0000000..e0a9c31 --- /dev/null +++ b/lib/producer/core/tests/has_env.rb @@ -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 diff --git a/spec/producer/core/condition/dsl_spec.rb b/spec/producer/core/condition/dsl_spec.rb index dc80a02..271bbe6 100644 --- a/spec/producer/core/condition/dsl_spec.rb +++ b/spec/producer/core/condition/dsl_spec.rb @@ -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) diff --git a/spec/producer/core/tests/has_env_spec.rb b/spec/producer/core/tests/has_env_spec.rb new file mode 100644 index 0000000..66b98c7 --- /dev/null +++ b/spec/producer/core/tests/has_env_spec.rb @@ -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