Implement `file_contains' condition keyword

This commit is contained in:
Thibault Jouan 2014-01-23 16:37:19 +00:00
parent 05331d334d
commit a0482d175c
7 changed files with 100 additions and 4 deletions

View File

@ -6,6 +6,10 @@ Given /^a remote file named "([^"]+)"$/ do |file_name|
write_file file_name, ''
end
Given /^a remote file named "([^"]+)" with "([^"]+)"$/ do |file_name, content|
write_file file_name, content
end
Then /^the remote directory "([^"]+)" should exists$/ do |path|
check_directory_presence([path], true)
end

View File

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

View File

@ -9,6 +9,7 @@ require 'producer/core/actions/file_writer'
# condition tests (need to be defined before the condition DSL)
require 'producer/core/test'
require 'producer/core/tests/file_contains'
require 'producer/core/tests/has_dir'
require 'producer/core/tests/has_env'
require 'producer/core/tests/has_file'

View File

@ -13,9 +13,10 @@ module Producer
end
end
define_test :has_env, Tests::HasEnv
define_test :has_dir, Tests::HasDir
define_test :has_file, Tests::HasFile
define_test :file_contains, Tests::FileContains
define_test :has_env, Tests::HasEnv
define_test :has_dir, Tests::HasDir
define_test :has_file, Tests::HasFile
attr_reader :block, :env, :tests

View File

@ -0,0 +1,18 @@
module Producer
module Core
module Tests
class FileContains < Test
def verify
content = file_content
content ? content.include?(arguments[1]) : false
end
private
def file_content
fs.file_read(arguments[0])
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_dir has_env has_file].each do |test|
%w[file_contains has_dir 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,44 @@
require 'spec_helper'
module Producer::Core
describe Tests::FileContains do
let(:env) { Env.new }
let(:filepath) { 'some_file' }
let(:content) { 'some_content' }
subject(:test) { Tests::FileContains.new(env, filepath, content) }
it 'is a kind of test' do
expect(test).to be_a Test
end
describe '#verify' do
let(:fs) { double 'fs' }
before { allow(test).to receive(:fs) { fs } }
context 'when file contains the content' do
before { allow(fs).to receive(:file_read).with(filepath) { "foo#{content}bar" } }
it 'returns true' do
expect(test.verify).to be true
end
end
context 'when file does not contain the content' do
before { allow(fs).to receive(:file_read).with(filepath) { 'foo bar' } }
it 'returns false' do
expect(test.verify).to be false
end
end
context 'when file does not exist' do
before { allow(fs).to receive(:file_read).with(filepath) { nil } }
it 'returns false' do
expect(test.verify).to be false
end
end
end
end
end