Implement `has_file' condition keyword

This commit is contained in:
Thibault Jouan 2013-08-17 17:10:48 +00:00
parent 3492382968
commit 2cbe1726f7
7 changed files with 69 additions and 2 deletions

View File

@ -0,0 +1,3 @@
Given(/^a remote file named "(.*?)"$/) do |file_name|
write_file file_name, ''
end

View File

@ -0,0 +1,23 @@
@sshd
Feature: `has_file' condition keyword
Background:
Given a recipe with:
"""
target 'some_host.test'
task :testing_file_existence do
condition { has_file 'some_file' }
echo 'evaluated'
end
"""
Scenario: succeeds when file exists
Given a remote file named "some_file"
When I successfully execute the recipe
Then the output must contain "evaluated"
Scenario: fails when file does not exist
When I successfully execute the recipe
Then the output must not contain "evaluated"

View File

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

View File

@ -16,7 +16,8 @@ module Producer
end
end
define_test :has_env, Tests::HasEnv
define_test :has_env, Tests::HasEnv
define_test :has_file, Tests::HasFile
attr_accessor :tests

View File

@ -0,0 +1,11 @@
module Producer
module Core
module Tests
class HasFile < Test
def success?
env.remote.fs.has_file? arguments.first
end
end
end
end
end

View File

@ -6,7 +6,7 @@ module Producer::Core
let(:env) { double('env') }
subject(:dsl) { Condition::DSL.new(env, &block) }
%w[has_env].each do |test|
%w[has_env has_file].each do |test|
it "has `#{test}' test defined" do
expect(dsl).to respond_to test.to_sym
end

View File

@ -0,0 +1,28 @@
require 'spec_helper'
module Producer::Core
describe Tests::HasFile do
let(:env) { Env.new }
let(:filepath) { 'some_file' }
subject(:has_file) { Tests::HasFile.new(env, filepath) }
it 'is a kind of test' do
expect(has_file).to be_a Test
end
describe '#success?', :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?
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
end
end
end
end