Implement rspec TestEnvHelpers:

Allow use of our test environment, with a mocked remote and convenient
methods to write more concise expectations.

  #env, builds and returns a memoized test environment (with mocked
  remote)

  #output, returns env output as a string

  #remote_fs, returns env remote fs

  #expect_execution, setup a remote command execution expectation
This commit is contained in:
Thibault Jouan
2014-02-14 18:24:42 +00:00
parent 85e3bf2eac
commit fcba1a5c0b
11 changed files with 64 additions and 45 deletions

View File

@@ -2,8 +2,7 @@ require 'spec_helper'
module Producer::Core
module Actions
describe Echo do
let(:env) { Env.new(output: StringIO.new) }
describe Echo, :env do
let(:text) { 'hello' }
subject(:echo) { Echo.new(env, text) }
@@ -12,7 +11,7 @@ module Producer::Core
describe '#apply' do
it 'writes the given string to env output with a record separator' do
echo.apply
expect(env.output.string).to eq "hello\n"
expect(output).to eq "hello\n"
end
end
end

View File

@@ -2,8 +2,7 @@ require 'spec_helper'
module Producer::Core
module Actions
describe FileWriter do
let(:env) { Env.new }
describe FileWriter, :env do
let(:path) { 'some_path' }
let(:content) { 'some_content' }
subject(:writer) { FileWriter.new(env, path, content) }
@@ -12,7 +11,7 @@ module Producer::Core
describe '#apply' do
it 'writes content to file on remote filesystem' do
expect(writer.fs).to receive(:file_write).with(path, content)
expect(remote_fs).to receive(:file_write).with(path, content)
writer.apply
end
end

View File

@@ -2,8 +2,7 @@ require 'spec_helper'
module Producer::Core
module Actions
describe Mkdir do
let(:env) { Env.new }
describe Mkdir, :env do
let(:path) { 'some_path' }
subject(:mkdir) { Mkdir.new(env, path) }
@@ -11,7 +10,7 @@ module Producer::Core
describe '#apply' do
it 'creates directory on remote filesystem' do
expect(mkdir.fs).to receive(:mkdir).with(path)
expect(remote_fs).to receive(:mkdir).with(path)
mkdir.apply
end
end

View File

@@ -2,8 +2,7 @@ require 'spec_helper'
module Producer::Core
module Actions
describe ShellCommand do
let(:env) { Env.new(output: StringIO.new) }
describe ShellCommand, :env do
let(:command_args) { 'hello from remote host' }
let(:command) { "echo #{command_args}" }
subject(:sh) { ShellCommand.new(env, command) }
@@ -12,14 +11,13 @@ module Producer::Core
describe '#apply' do
it 'executes the remote command' do
expect(sh.remote).to receive(:execute).with(command)
expect_execution(command)
sh.apply
end
it 'writes the returned output with a record separator' do
allow(sh.remote).to receive(:execute) { command_args }
sh.apply
expect(sh.output.string).to eq "#{command_args}\n"
expect(output).to eq "#{command_args}\n"
end
end
end

View File

@@ -2,8 +2,7 @@ require 'spec_helper'
module Producer::Core
module Tests
describe FileContains do
let(:env) { Env.new }
describe FileContains, :env do
let(:filepath) { 'some_file' }
let(:content) { 'some_content' }
subject(:test) { FileContains.new(env, filepath, content) }
@@ -11,13 +10,9 @@ module Producer::Core
it_behaves_like 'test'
describe '#verify' do
let(:fs) { double 'fs' }
before { allow(test).to receive(:fs) { fs } }
context 'when file contains the content' do
before do
allow(fs)
allow(remote_fs)
.to receive(:file_read).with(filepath) { "foo#{content}bar" }
end
@@ -28,7 +23,7 @@ module Producer::Core
context 'when file does not contain the content' do
before do
allow(fs).to receive(:file_read).with(filepath) { 'foo bar' }
allow(remote_fs).to receive(:file_read).with(filepath) { 'foo bar' }
end
it 'returns false' do
@@ -37,7 +32,9 @@ module Producer::Core
end
context 'when file does not exist' do
before { allow(fs).to receive(:file_read).with(filepath) { nil } }
before do
allow(remote_fs).to receive(:file_read).with(filepath) { nil }
end
it 'returns false' do
expect(test.verify).to be false

View File

@@ -2,24 +2,21 @@ require 'spec_helper'
module Producer::Core
module Tests
describe HasDir do
let(:env) { Env.new }
describe HasDir, :env do
let(:path) { 'some_directory' }
subject(:has_dir) { HasDir.new(env, path) }
it_behaves_like 'test'
describe '#verify', :ssh do
before { sftp_story }
describe '#verify' do
it 'delegates the call on remote FS' do
expect(env.remote.fs).to receive(:dir?).with(path)
expect(remote_fs).to receive(:dir?).with(path)
has_dir.verify
end
it 'returns the dir existence' do
existence = double 'existence'
allow(env.remote.fs).to receive(:dir?) { existence }
allow(remote_fs).to receive(:dir?) { existence }
expect(has_dir.verify).to be existence
end
end

View File

@@ -2,24 +2,21 @@ require 'spec_helper'
module Producer::Core
module Tests
describe HasFile do
let(:env) { Env.new }
describe HasFile, :env do
let(:filepath) { 'some_file' }
subject(:has_file) { HasFile.new(env, filepath) }
it_behaves_like 'test'
describe '#verify', :ssh do
before { sftp_story }
describe '#verify' do
it 'delegates the call on remote FS' do
expect(env.remote.fs).to receive(:file?).with(filepath)
expect(remote_fs).to receive(:file?).with(filepath)
has_file.verify
end
it 'returns the file existence' do
existence = double 'existence'
allow(env.remote.fs).to receive(:file?) { existence }
allow(remote_fs).to receive(:file?) { existence }
expect(has_file.verify).to be existence
end
end