diff --git a/features/tests/shell_command_status.feature b/features/tests/shell_command_status.feature new file mode 100644 index 0000000..5c03ab5 --- /dev/null +++ b/features/tests/shell_command_status.feature @@ -0,0 +1,30 @@ +@sshd +Feature: `` condition keyword + + Scenario: succeeds when remote command execution is a success + Given a recipe with: + """ + target 'some_host.test' + + task :testing_remote_command do + condition { `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_remote_command do + condition { `false` } + + 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 91ef733..98fb8da 100644 --- a/lib/producer/core.rb +++ b/lib/producer/core.rb @@ -21,6 +21,7 @@ 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/tests/shell_command_status' 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 36155b4..6d4eaec 100644 --- a/lib/producer/core/condition/dsl.rb +++ b/lib/producer/core/condition/dsl.rb @@ -13,6 +13,7 @@ module Producer end end + define_test :`, Tests::ShellCommandStatus define_test :file_contains, Tests::FileContains define_test :has_env, Tests::HasEnv define_test :has_executable, Tests::HasExecutable diff --git a/lib/producer/core/tests/shell_command_status.rb b/lib/producer/core/tests/shell_command_status.rb new file mode 100644 index 0000000..9bac3c4 --- /dev/null +++ b/lib/producer/core/tests/shell_command_status.rb @@ -0,0 +1,18 @@ +module Producer + module Core + module Tests + class ShellCommandStatus < Test + def verify + remote.execute(command) + true + rescue RemoteCommandExecutionError + false + end + + def command + 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 2765077..065cee3 100644 --- a/spec/producer/core/condition/dsl_spec.rb +++ b/spec/producer/core/condition/dsl_spec.rb @@ -8,6 +8,7 @@ module Producer::Core subject(:dsl) { DSL.new(env, &block) } %w[ + ` file_contains has_dir has_env diff --git a/spec/producer/core/tests/shell_command_status_spec.rb b/spec/producer/core/tests/shell_command_status_spec.rb new file mode 100644 index 0000000..e30da30 --- /dev/null +++ b/spec/producer/core/tests/shell_command_status_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +module Producer::Core + module Tests + describe ShellCommandStatus, :env do + let(:command) { 'true' } + subject(:test) { described_class.new(env, command) } + + it_behaves_like 'test' + + describe '#verify' do + context 'command return status is 0' do + it 'returns true' do + expect(test.verify).to be true + end + end + + context 'command return status is not 0' do + let(:command) { 'false' } + + it 'returns false' do + expect(test.verify).to be false + end + end + end + + describe '#command' do + it 'returns the first argument' do + expect(test.command).to eq command + end + end + end + end +end