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

View File

@ -2,8 +2,7 @@ require 'spec_helper'
module Producer::Core module Producer::Core
module Actions module Actions
describe FileWriter do describe FileWriter, :env do
let(:env) { Env.new }
let(:path) { 'some_path' } let(:path) { 'some_path' }
let(:content) { 'some_content' } let(:content) { 'some_content' }
subject(:writer) { FileWriter.new(env, path, content) } subject(:writer) { FileWriter.new(env, path, content) }
@ -12,7 +11,7 @@ module Producer::Core
describe '#apply' do describe '#apply' do
it 'writes content to file on remote filesystem' 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 writer.apply
end end
end end

View File

@ -2,8 +2,7 @@ require 'spec_helper'
module Producer::Core module Producer::Core
module Actions module Actions
describe Mkdir do describe Mkdir, :env do
let(:env) { Env.new }
let(:path) { 'some_path' } let(:path) { 'some_path' }
subject(:mkdir) { Mkdir.new(env, path) } subject(:mkdir) { Mkdir.new(env, path) }
@ -11,7 +10,7 @@ module Producer::Core
describe '#apply' do describe '#apply' do
it 'creates directory on remote filesystem' 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 mkdir.apply
end end
end end

View File

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

View File

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

View File

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

View File

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

View File

@ -6,6 +6,8 @@ Dir['spec/support/**/*.rb'].map { |e| require e.gsub 'spec/', '' }
RSpec.configure do |c| RSpec.configure do |c|
c.treat_symbols_as_metadata_keys_with_true_values = true c.treat_symbols_as_metadata_keys_with_true_values = true
c.include TestEnvHelpers, :env
c.include NetSSHStoryHelpers, :ssh c.include NetSSHStoryHelpers, :ssh
c.before(:each, :ssh) do c.before(:each, :ssh) do
allow(Net::SSH).to receive(:start) { connection } allow(Net::SSH).to receive(:start) { connection }

View File

@ -1,8 +1,7 @@
module Producer::Core module Producer::Core
shared_examples 'action' do shared_examples 'action' do
let(:input) { StringIO.new } include TestEnvHelpers
let(:output) { StringIO.new }
let(:env) { Env.new(input: input, output: output) }
let(:arguments) { [:some, :arguments] } let(:arguments) { [:some, :arguments] }
subject(:action) { described_class.new(env, *arguments) } subject(:action) { described_class.new(env, *arguments) }
@ -20,25 +19,25 @@ module Producer::Core
describe '#input' do describe '#input' do
it 'returns env input' do it 'returns env input' do
expect(action.input).to be input expect(action.input).to be env.input
end end
end end
describe '#output' do describe '#output' do
it 'returns env output' do it 'returns env output' do
expect(action.output).to be output expect(action.output).to be env.output
end end
end end
describe '#remote' do describe '#remote' do
it 'returns env remote' do it 'returns env remote' do
expect(action.remote).to be action.env.remote expect(action.remote).to be env.remote
end end
end end
describe '#fs' do describe '#fs' do
it 'returns env remote fs' do it 'returns env remote fs' do
expect(action.fs).to be action.env.remote.fs expect(action.fs).to be env.remote.fs
end end
end end
end end

View File

@ -1,6 +1,7 @@
module Producer::Core module Producer::Core
shared_examples 'test' do shared_examples 'test' do
let(:env) { Env.new } include TestEnvHelpers
let(:arguments) { [:some, :arguments] } let(:arguments) { [:some, :arguments] }
subject(:test) { described_class.new(env, *arguments) } subject(:test) { described_class.new(env, *arguments) }

View File

@ -0,0 +1,31 @@
module TestEnvHelpers
require 'producer/core/testing'
def env
@_env ||= build_env
end
def output
env.output.string
end
def remote_fs
env.remote.fs
end
def expect_execution(command)
opts = { expected_from: caller.first }
RSpec::Mocks.expect_message(env.remote, :execute, opts).with(command)
end
private
def build_env
Producer::Core::Env.new(output: StringIO.new, remote: build_remote)
end
def build_remote
Producer::Core::Testing::MockRemote.new('some_host.test')
end
end