diff --git a/features/test_file_eq.feature b/features/test_file_eq.feature new file mode 100644 index 0000000..0f96ca8 --- /dev/null +++ b/features/test_file_eq.feature @@ -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" diff --git a/lib/producer/core.rb b/lib/producer/core.rb index 6e0d665..ca67a75 100644 --- a/lib/producer/core.rb +++ b/lib/producer/core.rb @@ -18,6 +18,7 @@ require 'producer/core/actions/file_writer' require 'producer/core/test' require 'producer/core/tests/condition_test' require 'producer/core/tests/file_contains' +require 'producer/core/tests/file_eq' require 'producer/core/tests/has_dir' require 'producer/core/tests/has_env' require 'producer/core/tests/has_executable' diff --git a/lib/producer/core/condition/dsl.rb b/lib/producer/core/condition/dsl.rb index d6e17ae..19fb07c 100644 --- a/lib/producer/core/condition/dsl.rb +++ b/lib/producer/core/condition/dsl.rb @@ -25,6 +25,7 @@ module Producer define_test :`, Tests::ShellCommandStatus define_test :sh, Tests::ShellCommandStatus define_test :file_contains, Tests::FileContains + define_test :file_eq, Tests::FileEq define_test :env?, Tests::HasEnv define_test :executable?, Tests::HasExecutable define_test :dir?, Tests::HasDir diff --git a/lib/producer/core/tests/file_eq.rb b/lib/producer/core/tests/file_eq.rb new file mode 100644 index 0000000..d317a35 --- /dev/null +++ b/lib/producer/core/tests/file_eq.rb @@ -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 diff --git a/spec/producer/core/condition/dsl_spec.rb b/spec/producer/core/condition/dsl_spec.rb index 5baca4a..1866f13 100644 --- a/spec/producer/core/condition/dsl_spec.rb +++ b/spec/producer/core/condition/dsl_spec.rb @@ -11,6 +11,7 @@ module Producer::Core ` sh file_contains + file_eq dir? env? executable? diff --git a/spec/producer/core/tests/file_eq_spec.rb b/spec/producer/core/tests/file_eq_spec.rb new file mode 100644 index 0000000..f96e428 --- /dev/null +++ b/spec/producer/core/tests/file_eq_spec.rb @@ -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