Implement condition DSL negated test prefix (no_*)
* Allow no_* to be used for every tests in condition DSL: condition { no_has_env :shell, '/bin/sh' }; * Modify Test constructor to accept negated: named argument, implement #negated? and #pass?; * Rename #success? to #verify in all test classes.
This commit is contained in:
parent
cfe9a009ae
commit
35621e1f5f
44
features/tests/negated_test.feature
Normal file
44
features/tests/negated_test.feature
Normal file
@ -0,0 +1,44 @@
|
||||
@sshd
|
||||
Feature: negated test prefix (no_)
|
||||
|
||||
Scenario: prefixed test fails when non-prefixed test is successful
|
||||
Given a recipe with:
|
||||
"""
|
||||
target 'some_host.test'
|
||||
|
||||
task :successful_test do
|
||||
condition { has_env :shell }
|
||||
|
||||
echo 'successful_test'
|
||||
end
|
||||
|
||||
task :negated_test do
|
||||
condition { no_has_env :shell }
|
||||
|
||||
echo 'negated_test'
|
||||
end
|
||||
"""
|
||||
When I successfully execute the recipe
|
||||
Then the output must contain "successful_test"
|
||||
And the output must not contain "negated_test"
|
||||
|
||||
Scenario: prefixed test fails when non-prefixed test is failing
|
||||
Given a recipe with:
|
||||
"""
|
||||
target 'some_host.test'
|
||||
|
||||
task :failing_test do
|
||||
condition { has_env :inexistent_var }
|
||||
|
||||
echo 'failing_test'
|
||||
end
|
||||
|
||||
task :negated_test do
|
||||
condition { no_has_env :inexistent_var }
|
||||
|
||||
echo 'negated_test'
|
||||
end
|
||||
"""
|
||||
When I successfully execute the recipe
|
||||
Then the output must not contain "failing_test"
|
||||
And the output must contain "negated_test"
|
@ -14,7 +14,7 @@ module Producer
|
||||
|
||||
def met?
|
||||
return !!@return_value if @tests.empty?
|
||||
@tests.each { |t| return false unless t.success? }
|
||||
@tests.each { |t| return false unless t.pass? }
|
||||
true
|
||||
end
|
||||
|
||||
|
@ -13,6 +13,9 @@ module Producer
|
||||
define_method(keyword) do |*args|
|
||||
@tests << klass.new(@env, *args)
|
||||
end
|
||||
define_method("no_#{keyword}") do |*args|
|
||||
@tests << klass.new(@env, *args, negated: true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -3,9 +3,18 @@ module Producer
|
||||
class Test
|
||||
attr_reader :env, :arguments
|
||||
|
||||
def initialize(env, *arguments)
|
||||
def initialize(env, *arguments, negated: false)
|
||||
@env = env
|
||||
@arguments = arguments
|
||||
@negated = negated
|
||||
end
|
||||
|
||||
def negated?
|
||||
@negated
|
||||
end
|
||||
|
||||
def pass?
|
||||
verify ^ negated?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -2,7 +2,7 @@ module Producer
|
||||
module Core
|
||||
module Tests
|
||||
class HasEnv < Test
|
||||
def success?
|
||||
def verify
|
||||
env.remote.environment.has_key? arguments.first.to_s.upcase
|
||||
end
|
||||
end
|
||||
|
@ -2,7 +2,7 @@ module Producer
|
||||
module Core
|
||||
module Tests
|
||||
class HasFile < Test
|
||||
def success?
|
||||
def verify
|
||||
env.remote.fs.has_file? arguments.first
|
||||
end
|
||||
end
|
||||
|
@ -49,6 +49,10 @@ module Producer::Core
|
||||
it 'defines a new test keyword' do
|
||||
expect(dsl).to respond_to :some_test
|
||||
end
|
||||
|
||||
it 'defines the negated test' do
|
||||
expect(dsl).to respond_to :no_some_test
|
||||
end
|
||||
end
|
||||
|
||||
describe '#initialize' do
|
||||
@ -96,6 +100,16 @@ module Producer::Core
|
||||
dsl.evaluate
|
||||
expect(dsl.tests).to include(some_test)
|
||||
end
|
||||
|
||||
context 'when keyword is prefixed with "no_"' do
|
||||
let(:block) { proc { no_some_test :some, :args } }
|
||||
|
||||
it 'builds a negated test' do
|
||||
expect(some_test_class)
|
||||
.to receive(:new).with(env, :some, :args, negated: true)
|
||||
dsl.evaluate
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -14,6 +14,18 @@ module Producer::Core
|
||||
it 'assigns the arguments' do
|
||||
expect(test.instance_eval { @arguments }).to eq arguments
|
||||
end
|
||||
|
||||
it 'assigns negated as false by default' do
|
||||
expect(test.instance_eval { @negated }).to be false
|
||||
end
|
||||
|
||||
context 'when negated option is true' do
|
||||
subject(:test) { Test.new(env, *arguments, negated: true) }
|
||||
|
||||
it 'assigns negated as true' do
|
||||
expect(test.instance_eval { @negated }).to be true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#env' do
|
||||
@ -27,5 +39,45 @@ module Producer::Core
|
||||
expect(test.arguments).to eq arguments
|
||||
end
|
||||
end
|
||||
|
||||
describe '#negated?' do
|
||||
it 'returns false' do
|
||||
expect(test.negated?).to be false
|
||||
end
|
||||
|
||||
context 'when test is negated' do
|
||||
subject(:test) { Test.new(env, *arguments, negated: true) }
|
||||
|
||||
it 'returns true' do
|
||||
expect(test.negated?).to be true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#pass?' do
|
||||
it 'returns true when #verify is true' do
|
||||
allow(test).to receive(:verify) { true }
|
||||
expect(test.pass?).to be true
|
||||
end
|
||||
|
||||
it 'returns false when #verify is false' do
|
||||
allow(test).to receive(:verify) { false }
|
||||
expect(test.pass?).to be false
|
||||
end
|
||||
|
||||
context 'when test is negated' do
|
||||
subject(:test) { Test.new(env, *arguments, negated: true) }
|
||||
|
||||
it 'returns false when #verify is true' do
|
||||
allow(test).to receive(:verify) { true }
|
||||
expect(test.pass?).to be false
|
||||
end
|
||||
|
||||
it 'returns true when #verify is false' do
|
||||
allow(test).to receive(:verify) { false }
|
||||
expect(test.pass?).to be true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -10,7 +10,7 @@ module Producer::Core
|
||||
expect(has_env).to be_a Test
|
||||
end
|
||||
|
||||
describe '#success?' do
|
||||
describe '#verify' do
|
||||
let(:environment) { double('environment') }
|
||||
|
||||
before do
|
||||
@ -19,22 +19,22 @@ module Producer::Core
|
||||
|
||||
it 'stringifies the queried variable name' do
|
||||
expect(environment).to receive(:has_key?).with(kind_of(String))
|
||||
has_env.success?
|
||||
has_env.verify
|
||||
end
|
||||
|
||||
it 'upcases the queried variable name' do
|
||||
expect(environment).to receive(:has_key?).with('SOME_VARIABLE_NAME')
|
||||
has_env.success?
|
||||
has_env.verify
|
||||
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
|
||||
expect(has_env.verify).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
|
||||
expect(has_env.verify).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -10,18 +10,18 @@ module Producer::Core
|
||||
expect(has_file).to be_a Test
|
||||
end
|
||||
|
||||
describe '#success?', :ssh do
|
||||
describe '#verify', :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?
|
||||
has_file.verify
|
||||
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
|
||||
expect(has_file.verify).to be existence
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,9 +1,9 @@
|
||||
module TestsHelpers
|
||||
def test_ok
|
||||
double('test', success?: true)
|
||||
double('test', pass?: true)
|
||||
end
|
||||
|
||||
def test_ko
|
||||
double('test', success?: false)
|
||||
double('test', pass?: false)
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user