Improve small details in specs:

* Fix coding standards;
* Simplify some expectations (eq instead of be matcher);
* Expect identity on block instead of calling;
* Change some before call as oneliners;
* Avoid shadowing variable names;
* Improve wording where needed.
This commit is contained in:
Thibault Jouan 2013-12-19 21:17:03 +00:00
parent f5224c7569
commit ef0307fbb5
15 changed files with 68 additions and 78 deletions

View File

@ -5,7 +5,7 @@ module Producer::Core
include ExitHelpers include ExitHelpers
include FixturesHelpers include FixturesHelpers
let(:recipe_file) { fixture_path_for('recipes/empty.rb') } let(:recipe_file) { fixture_path_for 'recipes/empty.rb' }
let(:arguments) { [recipe_file] } let(:arguments) { [recipe_file] }
subject(:cli) { CLI.new(arguments) } subject(:cli) { CLI.new(arguments) }
@ -23,13 +23,13 @@ module Producer::Core
describe '#run!' do describe '#run!' do
it 'checks the arguments' do it 'checks the arguments' do
expect(cli).to receive(:check_arguments!) expect(cli).to receive :check_arguments!
cli.run! cli.run!
end end
it 'processes the tasks with the interpreter' do it 'processes the tasks with the interpreter' do
allow(cli.recipe).to receive(:tasks) { [:some_task] } allow(cli.recipe).to receive(:tasks) { [:some_task] }
expect(cli.interpreter).to receive(:process).with([:some_task]) expect(cli.interpreter).to receive(:process).with [:some_task]
cli.run! cli.run!
end end
end end
@ -61,12 +61,12 @@ module Producer::Core
describe '#env' do describe '#env' do
it 'builds an environment with the current recipe' do it 'builds an environment with the current recipe' do
expect(Env).to receive(:new) expect(Env).to receive :new
cli.env cli.env
end end
it 'returns the env' do it 'returns the env' do
env = double('env') env = double 'env'
allow(Env).to receive(:new) { env } allow(Env).to receive(:new) { env }
expect(cli.env).to be env expect(cli.env).to be env
end end
@ -88,12 +88,12 @@ module Producer::Core
describe '#interpreter' do describe '#interpreter' do
it 'builds a interpreter' do it 'builds a interpreter' do
expect(Interpreter).to receive(:new) expect(Interpreter).to receive :new
cli.interpreter cli.interpreter
end end
it 'returns the interpreter' do it 'returns the interpreter' do
interpreter = double('interpreter') interpreter = double 'interpreter'
allow(Interpreter).to receive(:new) { interpreter } allow(Interpreter).to receive(:new) { interpreter }
expect(cli.interpreter).to be interpreter expect(cli.interpreter).to be interpreter
end end

View File

@ -3,7 +3,7 @@ require 'spec_helper'
module Producer::Core module Producer::Core
describe Condition::DSL do describe Condition::DSL do
let(:block) { proc { :some_condition_code } } let(:block) { proc { :some_condition_code } }
let(:env) { double('env') } let(:env) { double 'env' }
subject(:dsl) { Condition::DSL.new(env, &block) } subject(:dsl) { Condition::DSL.new(env, &block) }
%w[has_env has_file].each do |test| %w[has_env has_file].each do |test|
@ -22,7 +22,7 @@ module Producer::Core
it 'evaluates the DSL sandbox code' do it 'evaluates the DSL sandbox code' do
dsl = double('dsl').as_null_object dsl = double('dsl').as_null_object
allow(Condition::DSL).to receive(:new) { dsl } allow(Condition::DSL).to receive(:new) { dsl }
expect(dsl).to receive(:evaluate) expect(dsl).to receive :evaluate
Condition::DSL.evaluate(env, &block) Condition::DSL.evaluate(env, &block)
end end
@ -33,18 +33,16 @@ module Producer::Core
end end
it 'returns the condition' do it 'returns the condition' do
condition = double('task') condition = double 'task'
allow(Condition).to receive(:new) { condition } allow(Condition).to receive(:new) { condition }
expect(Condition::DSL.evaluate(env, &block)).to be condition expect(Condition::DSL.evaluate(env, &block)).to be condition
end end
end end
describe '.define_test' do describe '.define_test' do
let(:some_test_class) { double('SomeTest class') } let(:some_test_class) { double 'SomeTest class' }
before do before { Condition::DSL.define_test(:some_test, some_test_class) }
Condition::DSL.define_test(:some_test, some_test_class)
end
it 'defines a new test keyword' do it 'defines a new test keyword' do
expect(dsl).to respond_to :some_test expect(dsl).to respond_to :some_test
@ -82,12 +80,10 @@ module Producer::Core
end end
context 'when a defined test keyword is called' do context 'when a defined test keyword is called' do
let(:some_test_class) { double('SomeTest class') } let(:some_test_class) { double 'SomeTest class' }
let(:block) { proc { some_test :some, :args } } let(:block) { proc { some_test :some, :args } }
before do before { Condition::DSL.define_test(:some_test, some_test_class) }
Condition::DSL.define_test(:some_test, some_test_class)
end
it 'builds a new test with the env and given arguments' do it 'builds a new test with the env and given arguments' do
expect(some_test_class).to receive(:new).with(env, :some, :args) expect(some_test_class).to receive(:new).with(env, :some, :args)
@ -95,7 +91,7 @@ module Producer::Core
end end
it 'registers the new test' do it 'registers the new test' do
some_test = double('SomeTest instance') some_test = double 'SomeTest instance'
allow(some_test_class).to receive(:new) { some_test } allow(some_test_class).to receive(:new) { some_test }
dsl.evaluate dsl.evaluate
expect(dsl.tests).to include(some_test) expect(dsl.tests).to include(some_test)

View File

@ -8,7 +8,7 @@ module Producer::Core
subject(:condition) { Condition.new(tests) } subject(:condition) { Condition.new(tests) }
describe '.evaluate' do describe '.evaluate' do
let(:env) { double('env') } let(:env) { double 'env' }
let(:block) { proc { :some_condition_code } } let(:block) { proc { :some_condition_code } }
it 'delegates to DSL.evaluate' do it 'delegates to DSL.evaluate' do
@ -20,7 +20,7 @@ module Producer::Core
end end
it 'returns the evaluated condition' do it 'returns the evaluated condition' do
condition = double('condition') condition = double 'condition'
allow(Condition::DSL).to receive(:evaluate) { condition } allow(Condition::DSL).to receive(:evaluate) { condition }
expect(Condition.evaluate(env, &block)).to be condition expect(Condition.evaluate(env, &block)).to be condition
end end

View File

@ -41,7 +41,7 @@ module Producer::Core
end end
it 'returns the remote' do it 'returns the remote' do
remote = double('remote') remote = double 'remote'
allow(Remote).to receive(:new) { remote } allow(Remote).to receive(:new) { remote }
expect(env.remote).to eq remote expect(env.remote).to eq remote
end end

View File

@ -12,24 +12,22 @@ module Producer::Core
end end
describe '#process_task' do describe '#process_task' do
let(:action) { double('action') } let(:action) { double 'action' }
let(:task) { double('task', actions: [action]).as_null_object } let(:task) { double('task', actions: [action]).as_null_object }
context 'when task condition is met' do context 'when task condition is met' do
it 'applies the actions' do it 'applies the actions' do
expect(action).to receive(:apply) expect(action).to receive :apply
interpreter.process_task(task) interpreter.process_task task
end end
end end
context 'when task condition is not met' do context 'when task condition is not met' do
before do before { allow(task).to receive(:condition_met?) { false } }
allow(task).to receive(:condition_met?) { false }
end
it 'does not apply the actions' do it 'does not apply the actions' do
expect(action).not_to receive(:apply) expect(action).not_to receive :apply
interpreter.process_task(task) interpreter.process_task task
end end
end end
end end

View File

@ -4,14 +4,14 @@ module Producer::Core
describe Recipe::DSL do describe Recipe::DSL do
include FixturesHelpers include FixturesHelpers
let(:code) { proc { } } let(:code) { proc { :some_recipe_code } }
let(:env) { double('env').as_null_object } let(:env) { double('env').as_null_object }
subject(:dsl) { Recipe::DSL.new(&code) } subject(:dsl) { Recipe::DSL.new(&code) }
describe '.evaluate' do describe '.evaluate' do
let(:code) { 'nil' } let(:code) { 'nil' }
it 'builds a new DSL sandbox with given code' do it 'builds a new DSL sandbox with given code as string' do
expect(Recipe::DSL).to receive(:new).with(code).and_call_original expect(Recipe::DSL).to receive(:new).with(code).and_call_original
Recipe::DSL.evaluate(code, env) Recipe::DSL.evaluate(code, env)
end end
@ -24,7 +24,7 @@ module Producer::Core
end end
it 'builds a recipe with evaluated tasks' do it 'builds a recipe with evaluated tasks' do
dsl = Recipe::DSL.new('task(:some_task) { }') dsl = Recipe::DSL.new { task(:some_task) { } }
allow(Recipe::DSL).to receive(:new) { dsl } allow(Recipe::DSL).to receive(:new) { dsl }
expect(Recipe).to receive(:new).with(dsl.tasks) expect(Recipe).to receive(:new).with(dsl.tasks)
Recipe::DSL.evaluate(code, env) Recipe::DSL.evaluate(code, env)
@ -62,7 +62,7 @@ module Producer::Core
let(:code) { proc { task(:some_task) { } } } let(:code) { proc { task(:some_task) { } } }
it 'returns registered tasks' do it 'returns registered tasks' do
dsl.evaluate(env) dsl.evaluate env
expect(dsl.tasks[0].name).to eq :some_task expect(dsl.tasks[0].name).to eq :some_task
end end
end end
@ -70,11 +70,11 @@ module Producer::Core
describe '#evaluate' do describe '#evaluate' do
it 'evaluates its code' do it 'evaluates its code' do
dsl = Recipe::DSL.new { throw :recipe_code } dsl = Recipe::DSL.new { throw :recipe_code }
expect { dsl.evaluate(env) }.to throw_symbol :recipe_code expect { dsl.evaluate env }.to throw_symbol :recipe_code
end end
it 'returns itself' do it 'returns itself' do
expect(dsl.evaluate(env)).to eq dsl expect(dsl.evaluate env).to eq dsl
end end
end end
@ -85,8 +85,8 @@ module Producer::Core
let(:code) { proc { env.some_message } } let(:code) { proc { env.some_message } }
it 'returns the current environment' do it 'returns the current environment' do
expect(env).to receive(:some_message) expect(env).to receive :some_message
dsl.evaluate(env) dsl.evaluate env
end end
end end
@ -96,7 +96,7 @@ module Producer::Core
subject(:dsl) { Recipe::DSL.new(code) } subject(:dsl) { Recipe::DSL.new(code) }
it 'sources the recipe given as argument' do it 'sources the recipe given as argument' do
expect { dsl.evaluate(env) }.to throw_symbol :recipe_code expect { dsl.evaluate env }.to throw_symbol :recipe_code
end end
end end
@ -114,8 +114,8 @@ module Producer::Core
it 'builds a new evaluated task' do it 'builds a new evaluated task' do
expect(Task) expect(Task)
.to receive(:evaluate).with(:some_task, env) do |&block| .to receive(:evaluate).with(:some_task, env) do |&b|
expect(block.call).to eq :some_value expect(b.call).to eq :some_value
end end
dsl dsl
end end

View File

@ -7,7 +7,7 @@ module Producer::Core
subject(:recipe) { Recipe.new } subject(:recipe) { Recipe.new }
describe '.evaluate_from_file' do describe '.evaluate_from_file' do
let(:env) { double('env') } let(:env) { double 'env' }
let(:filepath) { fixture_path_for 'recipes/empty.rb' } let(:filepath) { fixture_path_for 'recipes/empty.rb' }
it 'delegates to DSL.evaluate with the recipe file content' do it 'delegates to DSL.evaluate with the recipe file content' do
@ -17,7 +17,7 @@ module Producer::Core
end end
it 'returns the evaluated recipe' do it 'returns the evaluated recipe' do
recipe = double('recipe') recipe = double 'recipe'
allow(Recipe::DSL).to receive(:evaluate) { recipe } allow(Recipe::DSL).to receive(:evaluate) { recipe }
expect(Recipe.evaluate_from_file(filepath, env)).to be recipe expect(Recipe.evaluate_from_file(filepath, env)).to be recipe
end end

View File

@ -15,12 +15,12 @@ module Producer::Core
before { sftp_story } before { sftp_story }
it 'builds a new SFTP session' do it 'builds a new SFTP session' do
expect(remote.session.sftp).to receive(:connect) expect(remote.session.sftp).to receive :connect
fs.sftp fs.sftp
end end
it 'returns the new SFTP session' do it 'returns the new SFTP session' do
session = double('session') session = double 'session'
allow(remote.session.sftp).to receive(:connect) { session } allow(remote.session.sftp).to receive(:connect) { session }
expect(fs.sftp).to be session expect(fs.sftp).to be session
end end
@ -35,7 +35,7 @@ module Producer::Core
# part of net-ssh story helpers, which are more close to integration tests. # part of net-ssh story helpers, which are more close to integration tests.
describe '#has_file?', :ssh do describe '#has_file?', :ssh do
let(:file_path) { "some_file_path" } let(:file_path) { "some_file_path" }
let(:stat) { double('stat') } let(:stat) { double 'stat' }
before do before do
sftp_story sftp_story
@ -44,35 +44,31 @@ module Producer::Core
context 'when path given as argument exists' do context 'when path given as argument exists' do
context 'when path is a file' do context 'when path is a file' do
before do before { allow(stat).to receive(:file?) { true } }
allow(stat).to receive(:file?) { true }
end
it 'returns true' do it 'returns true' do
expect(fs.has_file?(file_path)).to be true expect(fs.has_file? file_path).to be true
end end
end end
context 'when path is not a file' do context 'when path is not a file' do
before do before { allow(stat).to receive(:file?) { false } }
allow(stat).to receive(:file?) { false }
end
it 'returns false' do it 'returns false' do
expect(fs.has_file?(file_path)).to be false expect(fs.has_file? file_path).to be false
end end
end end
end end
context 'when querying the path raises a Net::SFTP::StatusException' do context 'when querying the path raises a Net::SFTP::StatusException' do
before do before do
response = double('response', code: '42', message: 'some message') response = double 'response', code: '42', message: 'some message'
ex = Net::SFTP::StatusException.new(response) ex = Net::SFTP::StatusException.new(response)
allow(stat).to receive(:file?).and_raise(ex) allow(stat).to receive(:file?).and_raise(ex)
end end
it 'returns false' do it 'returns false' do
expect(fs.has_file?(file_path)).to be false expect(fs.has_file? file_path).to be false
end end
end end
end end

View File

@ -7,7 +7,7 @@ module Producer::Core
describe '#hostname' do describe '#hostname' do
it 'returns the assignated hostname' do it 'returns the assignated hostname' do
expect(remote.hostname).to be hostname expect(remote.hostname).to eq hostname
end end
end end
@ -69,12 +69,12 @@ module Producer::Core
describe '#fs' do describe '#fs' do
it 'builds a new FS' do it 'builds a new FS' do
expect(Remote::FS).to receive(:new) expect(Remote::FS).to receive :new
remote.fs remote.fs
end end
it 'returns the new FS instance' do it 'returns the new FS instance' do
fs = double('fs') fs = double 'fs'
allow(Remote::FS).to receive(:new) { fs } allow(Remote::FS).to receive(:new) { fs }
expect(remote.fs).to be fs expect(remote.fs).to be fs
end end
@ -103,7 +103,7 @@ module Producer::Core
ch.sends_exec command ch.sends_exec command
ch.gets_data arguments ch.gets_data arguments
end end
expect(remote.execute(command)).to eq arguments expect(remote.execute command).to eq arguments
end end
it 'raises an exception when the exit status code is not 0' do it 'raises an exception when the exit status code is not 0' do
@ -112,7 +112,7 @@ module Producer::Core
ch.gets_data arguments ch.gets_data arguments
ch.gets_exit_status 1 ch.gets_exit_status 1
end end
expect { remote.execute(command) } expect { remote.execute command }
.to raise_error(RemoteCommandExecutionError) .to raise_error(RemoteCommandExecutionError)
end end
end end
@ -134,7 +134,7 @@ module Producer::Core
end end
it 'returns the environment' do it 'returns the environment' do
environment = double('environment') environment = double 'environment'
allow(Remote::Environment).to receive(:new) { environment } allow(Remote::Environment).to receive(:new) { environment }
expect(remote.environment).to be environment expect(remote.environment).to be environment
end end

View File

@ -3,7 +3,7 @@ require 'spec_helper'
module Producer::Core module Producer::Core
describe Task::DSL do describe Task::DSL do
let(:block) { proc { } } let(:block) { proc { } }
let(:env) { double('env') } let(:env) { double 'env' }
subject(:dsl) { Task::DSL.new(&block) } subject(:dsl) { Task::DSL.new(&block) }
%w[echo sh].each do |action| %w[echo sh].each do |action|
@ -42,7 +42,7 @@ module Producer::Core
end end
it 'returns the task' do it 'returns the task' do
task = double('task') task = double 'task'
allow(Task).to receive(:new) { task } allow(Task).to receive(:new) { task }
expect(Task::DSL.evaluate(name, env, &block)).to be task expect(Task::DSL.evaluate(name, env, &block)).to be task
end end
@ -76,7 +76,7 @@ module Producer::Core
let(:block) { proc { throw :task_code } } let(:block) { proc { throw :task_code } }
it 'evaluates its code' do it 'evaluates its code' do
expect { dsl.evaluate(env) } expect { dsl.evaluate env }
.to throw_symbol :task_code .to throw_symbol :task_code
end end
@ -86,7 +86,7 @@ module Producer::Core
before do before do
Task::DSL.define_action(:some_action, some_action_class) Task::DSL.define_action(:some_action, some_action_class)
dsl.evaluate(env) dsl.evaluate env
end end
it 'registers the action' do it 'registers the action' do

View File

@ -3,24 +3,24 @@ require 'spec_helper'
module Producer::Core module Producer::Core
describe Task do describe Task do
let(:name) { :some_task } let(:name) { :some_task }
let(:action) { double('action') } let(:action) { double 'action' }
let(:condition) { double('condition') } let(:condition) { double 'condition' }
subject(:task) { Task.new(name, [action], condition) } subject(:task) { Task.new(name, [action], condition) }
describe '.evaluate' do describe '.evaluate' do
let(:env) { double('env') } let(:env) { double 'env' }
let(:block) { proc { :some_value } } let(:block) { proc { :some_task_code } }
it 'delegates to DSL.evaluate' do it 'delegates to DSL.evaluate' do
expect(Task::DSL) expect(Task::DSL)
.to receive(:evaluate).with(name, env) do |&b| .to receive(:evaluate).with(name, env) do |&b|
expect(b.call).to eq :some_value expect(b).to be block
end end
Task.evaluate(name, env, &block) Task.evaluate(name, env, &block)
end end
it 'returns the evaluated task' do it 'returns the evaluated task' do
task = double('task') task = double 'task'
allow(Task::DSL).to receive(:evaluate) { task } allow(Task::DSL).to receive(:evaluate) { task }
expect(Task.evaluate(name, env, &block)).to be task expect(Task.evaluate(name, env, &block)).to be task
end end
@ -40,7 +40,7 @@ module Producer::Core
end end
context 'when only the name is given as argument' do context 'when only the name is given as argument' do
subject(:task) { Task.new(name) } subject(:task) { Task.new(name) }
it 'assigns no action' do it 'assigns no action' do
expect(task.actions).to be_empty expect(task.actions).to be_empty

View File

@ -11,7 +11,7 @@ module Producer::Core
end end
describe '#verify' do describe '#verify' do
let(:environment) { double('environment') } let(:environment) { double 'environment' }
before do before do
allow(env.remote).to receive(:environment) { environment } allow(env.remote).to receive(:environment) { environment }

View File

@ -19,7 +19,7 @@ module Producer::Core
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(:has_file?) { existence } allow(env.remote.fs).to receive(:has_file?) { existence }
expect(has_file.verify).to be existence expect(has_file.verify).to be existence
end end

View File

@ -36,7 +36,7 @@ module NetSSHStoryHelpers
def sftp_story def sftp_story
story do |session| story do |session|
ch = session.opens_channel ch = session.opens_channel
ch.sends_subsystem('sftp') ch.sends_subsystem 'sftp'
ch.sends_packet( ch.sends_packet(
Net::SFTP::Constants::PacketTypes::FXP_INIT, :long, Net::SFTP::Constants::PacketTypes::FXP_INIT, :long,
Net::SFTP::Session::HIGHEST_PROTOCOL_VERSION_SUPPORTED Net::SFTP::Session::HIGHEST_PROTOCOL_VERSION_SUPPORTED

View File

@ -1,9 +1,9 @@
module TestsHelpers module TestsHelpers
def test_ok def test_ok
double('test', pass?: true) double 'test', pass?: true
end end
def test_ko def test_ko
double('test', pass?: false) double 'test', pass?: false
end end
end end