Implement `file_eq' condition keyword

This commit is contained in:
Thibault Jouan 2014-09-22 08:25:50 +00:00
parent 9e70e9aec4
commit 4a83e8c71a
6 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,26 @@
@sshd
Feature: `file_eq' condition keyword
Background:
Given a recipe with:
"""
task :file_eq_test do
condition { file_eq 'some_file', 'some content' }
echo 'evaluated'
end
"""
Scenario: succeeds when file content is expected content
Given a remote file named "some_file" with "some content"
When I successfully execute the recipe on remote target
Then the output must contain "evaluated"
Scenario: fails when file content is not expected content
Given a remote file named "some_file" with "some content padded"
When I successfully execute the recipe on remote target
Then the output must not contain "evaluated"
Scenario: fails when file does not exist
When I successfully execute the recipe on remote target
Then the output must not contain "evaluated"

View File

@ -18,6 +18,7 @@ require 'producer/core/actions/file_writer'
require 'producer/core/test' require 'producer/core/test'
require 'producer/core/tests/condition_test' require 'producer/core/tests/condition_test'
require 'producer/core/tests/file_contains' require 'producer/core/tests/file_contains'
require 'producer/core/tests/file_eq'
require 'producer/core/tests/has_dir' require 'producer/core/tests/has_dir'
require 'producer/core/tests/has_env' require 'producer/core/tests/has_env'
require 'producer/core/tests/has_executable' require 'producer/core/tests/has_executable'

View File

@ -25,6 +25,7 @@ module Producer
define_test :`, Tests::ShellCommandStatus define_test :`, Tests::ShellCommandStatus
define_test :sh, Tests::ShellCommandStatus define_test :sh, Tests::ShellCommandStatus
define_test :file_contains, Tests::FileContains define_test :file_contains, Tests::FileContains
define_test :file_eq, Tests::FileEq
define_test :env?, Tests::HasEnv define_test :env?, Tests::HasEnv
define_test :executable?, Tests::HasExecutable define_test :executable?, Tests::HasExecutable
define_test :dir?, Tests::HasDir define_test :dir?, Tests::HasDir

View File

@ -0,0 +1,22 @@
module Producer
module Core
module Tests
class FileEq < Test
def verify
file_content ? file_content == expected_content : false
end
private
def file_content
@file_content ||= fs.file_read(arguments[0])
end
def expected_content
arguments[1]
end
end
end
end
end

View File

@ -11,6 +11,7 @@ module Producer::Core
` `
sh sh
file_contains file_contains
file_eq
dir? dir?
env? env?
executable? executable?

View File

@ -0,0 +1,45 @@
require 'spec_helper'
module Producer::Core
module Tests
describe FileEq, :env do
let(:filepath) { 'some_file' }
let(:content) { 'some content' }
subject(:test) { FileEq.new(env, filepath, content) }
it_behaves_like 'test'
describe '#verify' do
context 'when file content matches' do
before do
allow(remote_fs).to receive(:file_read).with(filepath) { content }
end
it 'returns true' do
expect(test.verify).to be true
end
end
context 'when file content does not match' do
before do
allow(remote_fs).to receive(:file_read).with(filepath) { 'foo bar' }
end
it 'returns false' do
expect(test.verify).to be false
end
end
context 'when file does not exist' do
before do
allow(remote_fs).to receive(:file_read).with(filepath) { nil }
end
it 'returns false' do
expect(test.verify).to be false
end
end
end
end
end
end