Fix coding standards (module nesting) in specs

This commit is contained in:
Thibault Jouan 2014-02-08 17:59:33 +00:00
parent 115561ee88
commit 48a9da9365
14 changed files with 590 additions and 559 deletions

View File

@ -1,15 +1,17 @@
require 'spec_helper' require 'spec_helper'
module Producer::Core module Producer::Core
describe Actions::Echo do module Actions
let(:env) { Env.new(output: StringIO.new) } describe Echo do
let(:text) { 'hello' } let(:env) { Env.new(output: StringIO.new) }
subject(:echo) { Actions::Echo.new(env, text) } let(:text) { 'hello' }
subject(:echo) { Echo.new(env, text) }
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(env.output.string).to eq "hello\n"
end
end end
end end
end end

View File

@ -1,28 +1,30 @@
require 'spec_helper' require 'spec_helper'
module Producer::Core module Producer::Core
describe Actions::FileWriter do module Actions
let(:env) { Env.new } describe FileWriter do
let(:path) { 'some_path' } let(:env) { Env.new }
let(:content) { 'some_content' } let(:path) { 'some_path' }
subject(:writer) { Actions::FileWriter.new(env, path, content) } let(:content) { 'some_content' }
subject(:writer) { FileWriter.new(env, path, content) }
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(writer.fs).to receive(:file_write).with(path, content)
writer.apply writer.apply
end
end end
end
describe '#path' do describe '#path' do
it 'returns the path' do it 'returns the path' do
expect(writer.path).to eq path expect(writer.path).to eq path
end
end end
end
describe '#content' do describe '#content' do
it 'returns the content' do it 'returns the content' do
expect(writer.content).to eq content expect(writer.content).to eq content
end
end end
end end
end end

View File

@ -1,21 +1,23 @@
require 'spec_helper' require 'spec_helper'
module Producer::Core module Producer::Core
describe Actions::Mkdir do module Actions
let(:env) { Env.new } describe Mkdir do
let(:path) { 'some_path' } let(:env) { Env.new }
subject(:mkdir) { Actions::Mkdir.new(env, path) } let(:path) { 'some_path' }
subject(:mkdir) { Mkdir.new(env, path) }
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(mkdir.fs).to receive(:mkdir).with(path)
mkdir.apply mkdir.apply
end
end end
end
describe '#path' do describe '#path' do
it 'returns the path' do it 'returns the path' do
expect(mkdir.path).to eq path expect(mkdir.path).to eq path
end
end end
end end
end end

View File

@ -1,22 +1,24 @@
require 'spec_helper' require 'spec_helper'
module Producer::Core module Producer::Core
describe Actions::ShellCommand do module Actions
let(:env) { Env.new(output: StringIO.new) } describe ShellCommand do
let(:command_args) { 'hello from remote host' } let(:env) { Env.new(output: StringIO.new) }
let(:command) { "echo #{command_args}" } let(:command_args) { 'hello from remote host' }
subject(:sh) { Actions::ShellCommand.new(env, command) } let(:command) { "echo #{command_args}" }
subject(:sh) { ShellCommand.new(env, command) }
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(sh.remote).to receive(:execute).with(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 } allow(sh.remote).to receive(:execute) { command_args }
sh.apply sh.apply
expect(sh.output.string).to eq "#{command_args}\n" expect(sh.output.string).to eq "#{command_args}\n"
end
end end
end end
end end

View File

@ -1,76 +1,78 @@
require 'spec_helper' require 'spec_helper'
module Producer::Core module Producer::Core
describe Condition::DSL do class Condition
let(:block) { proc { :some_condition_code } } describe DSL do
let(:env) { double 'env' } let(:block) { proc { :some_condition_code } }
subject(:dsl) { Condition::DSL.new(env, &block) } let(:env) { double 'env' }
subject(:dsl) { DSL.new(env, &block) }
%w[file_contains has_dir has_env has_file].each do |test| %w[file_contains has_dir has_env has_file].each do |test|
it "has `#{test}' test defined" do it "has `#{test}' test defined" do
expect(dsl).to respond_to test.to_sym expect(dsl).to respond_to test.to_sym
end
end
describe '.define_test' do
let(:some_test_class) { Test }
before { Condition::DSL.define_test(:some_test, some_test_class) }
it 'defines a new test keyword' do
expect(dsl).to respond_to :some_test
end
it 'defines the negated test' do
expect(dsl).to respond_to :no_some_test
end
context 'when a test keyword is called' do
it 'registers the test' do
expect { dsl.some_test }.to change { dsl.tests.count }.by 1
end
it 'registers the test with current env' do
dsl.some_test
expect(dsl.tests.first.env).to be env
end
it 'registers the test with given arguments' do
dsl.some_test :some, :args
expect(dsl.tests.first.arguments).to eq [:some, :args]
end end
end end
context 'when a negated test keyword is called' do describe '.define_test' do
it 'registers a negated test' do let(:some_test_class) { Test }
dsl.no_some_test
expect(dsl.tests.first).to be_negated before { described_class.define_test(:some_test, some_test_class) }
it 'defines a new test keyword' do
expect(dsl).to respond_to :some_test
end
it 'defines the negated test' do
expect(dsl).to respond_to :no_some_test
end
context 'when a test keyword is called' do
it 'registers the test' do
expect { dsl.some_test }.to change { dsl.tests.count }.by 1
end
it 'registers the test with current env' do
dsl.some_test
expect(dsl.tests.first.env).to be env
end
it 'registers the test with given arguments' do
dsl.some_test :some, :args
expect(dsl.tests.first.arguments).to eq [:some, :args]
end
end
context 'when a negated test keyword is called' do
it 'registers a negated test' do
dsl.no_some_test
expect(dsl.tests.first).to be_negated
end
end end
end end
end
describe '#initialize' do describe '#initialize' do
it 'assigns the env' do it 'assigns the env' do
expect(dsl.env).to be env expect(dsl.env).to be env
end
it 'assigns the code' do
expect(dsl.block).to be block
end
it 'assigns no test' do
expect(dsl.tests).to be_empty
end
end end
it 'assigns the code' do describe '#evaluate' do
expect(dsl.block).to be block it 'evaluates its code' do
end dsl = described_class.new(env) { throw :condition_code }
expect { dsl.evaluate }.to throw_symbol :condition_code
end
it 'assigns no test' do it 'returns the value returned by the assigned block' do
expect(dsl.tests).to be_empty expect(dsl.evaluate).to eq block.call
end end
end
describe '#evaluate' do
it 'evaluates its code' do
dsl = described_class.new(env) { throw :condition_code }
expect { dsl.evaluate }.to throw_symbol :condition_code
end
it 'returns the value returned by the assigned block' do
expect(dsl.evaluate).to eq block.call
end end
end end
end end

View File

@ -1,117 +1,119 @@
require 'spec_helper' require 'spec_helper'
module Producer::Core module Producer::Core
describe Recipe::DSL do class Recipe
include FixturesHelpers describe DSL do
include FixturesHelpers
let(:code) { proc { :some_recipe_code } } let(:code) { proc { :some_recipe_code } }
let(:env) { Env.new } let(:env) { Env.new }
subject(:dsl) { Recipe::DSL.new(env, &code) } subject(:dsl) { DSL.new(env, &code) }
describe '#initialize' do describe '#initialize' do
it 'assigns the given env' do it 'assigns the given env' do
expect(dsl.env).to be env expect(dsl.env).to be env
end end
it 'assigns no task' do it 'assigns no task' do
expect(dsl.tasks).to be_empty expect(dsl.tasks).to be_empty
end end
context 'when a string of code is given as argument' do context 'when a string of code is given as argument' do
let(:code) { 'some_code' } let(:code) { 'some_code' }
subject(:dsl) { described_class.new(env, code) } subject(:dsl) { described_class.new(env, code) }
it 'assigns the string of code' do it 'assigns the string of code' do
expect(dsl.code).to eq code expect(dsl.code).to eq code
end
end
context 'when a code block is given as argument' do
it 'assigns the code block' do
expect(dsl.block).to be code
end
end end
end end
context 'when a code block is given as argument' do describe '#tasks' do
it 'assigns the code block' do let(:code) { proc { task(:some_task) {} } }
expect(dsl.block).to be code
end
end
end
describe '#tasks' do it 'returns registered tasks' do
let(:code) { proc { task(:some_task) {} } } dsl.evaluate
expect(dsl.tasks[0].name).to eq :some_task
it 'returns registered tasks' do
dsl.evaluate
expect(dsl.tasks[0].name).to eq :some_task
end
end
describe '#evaluate' do
it 'evaluates its code' do
dsl = described_class.new(env) { throw :recipe_code }
expect { dsl.evaluate }.to throw_symbol :recipe_code
end
it 'returns itself' do
expect(dsl.evaluate).to eq dsl
end
end
describe '#source' do
let(:filepath) { fixture_path_for 'recipes/throw' }
it 'sources the recipe given as argument' do
expect { dsl.source filepath }.to throw_symbol :recipe_code
end
end
describe '#target' do
let(:host) { 'some_host.example' }
it 'registers the target host in the env' do
dsl.target host
expect(env.target).to eq host
end
end
describe '#task' do
it 'registers a new evaluated task' do
expect { dsl.task(:some_task) { :some_task_code } }
.to change { dsl.tasks.count }.by 1
end
end
describe '#macro' do
it 'defines the new recipe keyword' do
dsl.macro :hello
expect(dsl).to respond_to(:hello)
end
context 'when a defined macro is called' do
before { dsl.macro(:hello) { :some_macro_code } }
it 'registers the new task' do
expect { dsl.hello }.to change { dsl.tasks.count }.by 1
end end
end end
context 'when a defined macro is called with arguments' do describe '#evaluate' do
before { dsl.macro(:hello) { |a, b| echo a, b } } it 'evaluates its code' do
dsl = described_class.new(env) { throw :recipe_code }
expect { dsl.evaluate }.to throw_symbol :recipe_code
end
it 'evaluates task code with arguments' do it 'returns itself' do
dsl.hello :some, :args expect(dsl.evaluate).to eq dsl
expect(dsl.tasks.first.actions.first.arguments).to eq [:some, :args]
end end
end end
end
describe '#set' do describe '#source' do
it 'registers a key/value pair in env registry' do let(:filepath) { fixture_path_for 'recipes/throw' }
dsl.set :some_key, :some_value
expect(env[:some_key]).to eq :some_value it 'sources the recipe given as argument' do
expect { dsl.source filepath }.to throw_symbol :recipe_code
end
end end
end
describe '#get' do describe '#target' do
it 'fetches a value from the registry at given index' do let(:host) { 'some_host.example' }
dsl.set :some_key, :some_value
expect(dsl.get :some_key).to eq :some_value it 'registers the target host in the env' do
dsl.target host
expect(env.target).to eq host
end
end
describe '#task' do
it 'registers a new evaluated task' do
expect { dsl.task(:some_task) { :some_task_code } }
.to change { dsl.tasks.count }.by 1
end
end
describe '#macro' do
it 'defines the new recipe keyword' do
dsl.macro :hello
expect(dsl).to respond_to(:hello)
end
context 'when a defined macro is called' do
before { dsl.macro(:hello) { :some_macro_code } }
it 'registers the new task' do
expect { dsl.hello }.to change { dsl.tasks.count }.by 1
end
end
context 'when a defined macro is called with arguments' do
before { dsl.macro(:hello) { |a, b| echo a, b } }
it 'evaluates task code with arguments' do
dsl.hello :some, :args
expect(dsl.tasks.first.actions.first.arguments).to eq [:some, :args]
end
end
end
describe '#set' do
it 'registers a key/value pair in env registry' do
dsl.set :some_key, :some_value
expect(env[:some_key]).to eq :some_value
end
end
describe '#get' do
it 'fetches a value from the registry at given index' do
dsl.set :some_key, :some_value
expect(dsl.get :some_key).to eq :some_value
end
end end
end end
end end

View File

@ -1,48 +1,50 @@
require 'spec_helper' require 'spec_helper'
module Producer::Core module Producer::Core
describe Remote::Environment do class Remote
let(:variables) { { 'FOO' => 'bar', 'BAZ' => 'qux' } } describe Environment do
let(:string) { "FOO=bar\nBAZ=qux" } let(:variables) { { 'FOO' => 'bar', 'BAZ' => 'qux' } }
let(:argument) { variables } let(:string) { "FOO=bar\nBAZ=qux" }
subject(:environment) { Remote::Environment.new(argument) } let(:argument) { variables }
subject(:environment) { Environment.new(argument) }
describe '.string_to_hash' do describe '.string_to_hash' do
it 'converts key=value pairs separated by new lines to a hash' do it 'converts key=value pairs separated by new lines to a hash' do
expect(described_class.string_to_hash(string)).to eq variables expect(described_class.string_to_hash(string)).to eq variables
end
end
describe '.new_from_string' do
it 'returns a new instance with converted keys and values' do
environment = described_class.new_from_string string
expect(environment.variables).to eq variables
end
end
describe '#initialize' do
it 'assigns the key/value pairs' do
expect(environment.variables).to eq variables
end
end
describe '#key?' do
context 'when key is defined' do
it 'returns true' do
expect(environment.key? 'FOO').to be true
end end
end end
context 'when key is not defined' do describe '.new_from_string' do
it 'returns false' do it 'returns a new instance with converted keys and values' do
expect(environment.key? 'INEXISTENT_KEY').to be false environment = described_class.new_from_string string
expect(environment.variables).to eq variables
end end
end end
end
describe '#[]' do describe '#initialize' do
it 'returns the value indexed by given key' do it 'assigns the key/value pairs' do
expect(environment['FOO']).to eq 'bar' expect(environment.variables).to eq variables
end
end
describe '#key?' do
context 'when key is defined' do
it 'returns true' do
expect(environment.key? 'FOO').to be true
end
end
context 'when key is not defined' do
it 'returns false' do
expect(environment.key? 'INEXISTENT_KEY').to be false
end
end
end
describe '#[]' do
it 'returns the value indexed by given key' do
expect(environment['FOO']).to eq 'bar'
end
end end
end end
end end

View File

@ -1,97 +1,111 @@
require 'spec_helper' require 'spec_helper'
module Producer::Core module Producer::Core
describe Remote::FS do class Remote
let(:remote) { Remote.new('some_host.example') } describe FS do
subject(:fs) { Remote::FS.new(remote) } let(:remote) { Remote.new('some_host.example') }
subject(:fs) { FS.new(remote) }
describe '#new' do describe '#new' do
it 'assigns the remote given as argument' do it 'assigns the remote given as argument' do
expect(fs.remote).to be remote expect(fs.remote).to be remote
end
end
describe '#sftp', :ssh do
before { sftp_story }
it 'builds a new SFTP session' do
expect(remote.session.sftp).to receive :connect
fs.sftp
end
it 'returns the new SFTP session' do
session = double 'session'
allow(remote.session.sftp).to receive(:connect) { session }
expect(fs.sftp).to be session
end
it 'memoizes the FS' do
allow(remote.session.sftp).to receive(:connect) { Object.new }
expect(fs.sftp).to be fs.sftp
end
end
describe '#dir?' do
let(:sftp) { double('sftp').as_null_object }
let(:path) { 'some_directory_path' }
let(:stat) { double 'stat' }
before do
allow(fs).to receive(:sftp) { sftp }
allow(sftp).to receive(:stat!).with(path) { stat }
end
context 'when path given as argument is a directory' do
before { allow(stat).to receive(:directory?) { true } }
it 'returns true' do
expect(fs.dir? path).to be true
end end
end end
context 'when path given as argument is not a directory' do describe '#sftp', :ssh do
before { allow(stat).to receive(:directory?) { false } } before { sftp_story }
it 'returns false' do it 'builds a new SFTP session' do
expect(fs.dir? path).to be false expect(remote.session.sftp).to receive :connect
fs.sftp
end
it 'returns the new SFTP session' do
session = double 'session'
allow(remote.session.sftp).to receive(:connect) { session }
expect(fs.sftp).to be session
end
it 'memoizes the FS' do
allow(remote.session.sftp).to receive(:connect) { Object.new }
expect(fs.sftp).to be fs.sftp
end end
end end
context 'when querying the path raises a Net::SFTP::StatusException' do describe '#dir?' do
let(:sftp) { double('sftp').as_null_object }
let(:path) { 'some_directory_path' }
let(:stat) { double 'stat' }
before do before do
response = double 'response', code: '42', message: 'some message' allow(fs).to receive(:sftp) { sftp }
ex = Net::SFTP::StatusException.new(response) allow(sftp).to receive(:stat!).with(path) { stat }
allow(sftp).to receive(:stat!).with(path).and_raise(ex)
end end
it 'returns false' do context 'when path given as argument is a directory' do
expect(fs.dir? path).to be false before { allow(stat).to receive(:directory?) { true } }
end
end
end
# FIXME: We rely a lot on mocking net-sftp heavily, while we already use a
# part of net-ssh story helpers, which are more close to integration tests.
describe '#file?', :ssh do
let(:file_path) { 'some_file_path' }
let(:stat) { double 'stat' }
before do
sftp_story
allow(fs.sftp).to receive(:stat!) { stat }
end
context 'when path given as argument exists' do
context 'when path is a file' do
before { allow(stat).to receive(:file?) { true } }
it 'returns true' do it 'returns true' do
expect(fs.file? file_path).to be true expect(fs.dir? path).to be true
end end
end end
context 'when path is not a file' do context 'when path given as argument is not a directory' do
before { allow(stat).to receive(:file?) { false } } before { allow(stat).to receive(:directory?) { false } }
it 'returns false' do
expect(fs.dir? path).to be false
end
end
context 'when querying the path raises a Net::SFTP::StatusException' do
before do
response = double 'response', code: '42', message: 'some message'
ex = Net::SFTP::StatusException.new(response)
allow(sftp).to receive(:stat!).with(path).and_raise(ex)
end
it 'returns false' do
expect(fs.dir? path).to be false
end
end
end
# FIXME: We rely a lot on mocking net-sftp heavily, while we already use a
# part of net-ssh story helpers, which are more close to integration tests.
describe '#file?', :ssh do
let(:file_path) { 'some_file_path' }
let(:stat) { double 'stat' }
before do
sftp_story
allow(fs.sftp).to receive(:stat!) { stat }
end
context 'when path given as argument exists' do
context 'when path is a file' do
before { allow(stat).to receive(:file?) { true } }
it 'returns true' do
expect(fs.file? file_path).to be true
end
end
context 'when path is not a file' do
before { allow(stat).to receive(:file?) { false } }
it 'returns false' do
expect(fs.file? file_path).to be false
end
end
end
context 'when querying the path raises a Net::SFTP::StatusException' do
before do
response = double 'response', code: '42', message: 'some message'
ex = Net::SFTP::StatusException.new(response)
allow(stat).to receive(:file?).and_raise(ex)
end
it 'returns false' do it 'returns false' do
expect(fs.file? file_path).to be false expect(fs.file? file_path).to be false
@ -99,84 +113,72 @@ module Producer::Core
end end
end end
context 'when querying the path raises a Net::SFTP::StatusException' do describe '#mkdir' do
let(:sftp) { double 'sftp' }
let(:path) { 'some_directory_path' }
before { allow(fs).to receive(:sftp) { sftp } }
it 'creates the directory' do
expect(sftp).to receive(:mkdir!).with(path)
fs.mkdir path
end
end
describe '#file_read' do
let(:sftp) { double 'sftp' }
let(:file) { double 'file' }
let(:f) { double 'f' }
let(:path) { 'some_file_path' }
let(:content) { 'some_content' }
before do before do
response = double 'response', code: '42', message: 'some message' allow(fs).to receive(:sftp) { sftp }
ex = Net::SFTP::StatusException.new(response) allow(sftp).to receive(:file) { file }
allow(stat).to receive(:file?).and_raise(ex) allow(file).to receive(:open).and_yield(f)
allow(f).to receive(:read) { content }
end end
it 'returns false' do it 'returns the file content' do
expect(fs.file? file_path).to be false expect(fs.file_read(path)).to eq content
end
context 'when opening the file raises a Net::SFTP::StatusException' do
before do
response = double 'response', code: '42', message: 'some message'
ex = Net::SFTP::StatusException.new(response)
allow(file).to receive(:open).and_raise(ex)
end
it 'returns nil' do
expect(fs.file_read(path)).to be nil
end
end end
end end
end
describe '#mkdir' do describe '#file_write' do
let(:sftp) { double 'sftp' } let(:sftp) { double 'sftp' }
let(:path) { 'some_directory_path' } let(:file) { double 'file' }
let(:path) { 'some_file_path' }
let(:content) { 'some_content' }
before { allow(fs).to receive(:sftp) { sftp } }
it 'creates the directory' do
expect(sftp).to receive(:mkdir!).with(path)
fs.mkdir path
end
end
describe '#file_read' do
let(:sftp) { double 'sftp' }
let(:file) { double 'file' }
let(:f) { double 'f' }
let(:path) { 'some_file_path' }
let(:content) { 'some_content' }
before do
allow(fs).to receive(:sftp) { sftp }
allow(sftp).to receive(:file) { file }
allow(file).to receive(:open).and_yield(f)
allow(f).to receive(:read) { content }
end
it 'returns the file content' do
expect(fs.file_read(path)).to eq content
end
context 'when opening the file raises a Net::SFTP::StatusException' do
before do before do
response = double 'response', code: '42', message: 'some message' allow(fs).to receive(:sftp) { sftp }
ex = Net::SFTP::StatusException.new(response) allow(sftp).to receive(:file) { file }
allow(file).to receive(:open).and_raise(ex)
end end
it 'returns nil' do it 'opens the file' do
expect(fs.file_read(path)).to be nil expect(file).to receive(:open).with(path, 'w')
fs.file_write path, content
end end
end
end
describe '#file_write' do it 'writes the content' do
let(:sftp) { double 'sftp' } expect(file).to receive(:open).with(any_args) do |&b|
let(:file) { double 'file' } expect(file).to receive(:write).with(content)
let(:path) { 'some_file_path' } b.call file
let(:content) { 'some_content' } end
fs.file_write path, content
before do
allow(fs).to receive(:sftp) { sftp }
allow(sftp).to receive(:file) { file }
end
it 'opens the file' do
expect(file).to receive(:open).with(path, 'w')
fs.file_write path, content
end
it 'writes the content' do
expect(file).to receive(:open).with(any_args) do |&b|
expect(file).to receive(:write).with(content)
b.call file
end end
fs.file_write path, content
end end
end end
end end

View File

@ -1,113 +1,115 @@
require 'spec_helper' require 'spec_helper'
module Producer::Core module Producer::Core
describe Task::DSL do class Task
let(:block) { proc {} } describe DSL do
let(:env) { Env.new } let(:block) { proc {} }
subject(:dsl) { Task::DSL.new(env, &block) } let(:env) { Env.new }
subject(:dsl) { DSL.new(env, &block) }
%w[echo sh mkdir file_write].each do |action| %w[echo sh mkdir file_write].each do |action|
it "has `#{action}' action defined" do it "has `#{action}' action defined" do
expect(dsl).to respond_to action.to_sym expect(dsl).to respond_to action.to_sym
end
end
describe '.define_action' do
let(:some_action_class) { Class.new(Action) }
before { described_class.define_action(:some_action, some_action_class) }
it 'defines a new action keyword' do
expect(dsl).to respond_to :some_action
end
context 'when an action keyword is called' do
it 'registers the action' do
expect { dsl.some_action }.to change { dsl.actions.count }.by 1
end
it 'registers the action with current env' do
dsl.some_action
expect(dsl.actions.first.env).to be env
end
it 'registers the action with given arguments' do
dsl.some_action :some, :args
expect(dsl.actions.first.arguments).to eq [:some, :args]
end end
end end
end
describe '#initialize' do describe '.define_action' do
it 'assigns the given env' do let(:some_action_class) { Class.new(Action) }
expect(dsl.env).to be env
end
it 'assigns the given block' do before { described_class.define_action(:some_action, some_action_class) }
expect(dsl.block).to be block
end
it 'assigns no action' do it 'defines a new action keyword' do
expect(dsl.actions).to be_empty expect(dsl).to respond_to :some_action
end end
it 'assigns true as the condition' do context 'when an action keyword is called' do
expect(dsl.condition).to be true it 'registers the action' do
end expect { dsl.some_action }.to change { dsl.actions.count }.by 1
end end
describe '#evaluate' do it 'registers the action with current env' do
let(:block) { proc { throw :task_code } } dsl.some_action
expect(dsl.actions.first.env).to be env
end
it 'evaluates its code' do it 'registers the action with given arguments' do
expect { dsl.evaluate } dsl.some_action :some, :args
.to throw_symbol :task_code expect(dsl.actions.first.arguments).to eq [:some, :args]
end end
context 'when arguments are given' do
let(:block) { proc { |e| throw e } }
it 'passes arguments as block parameters' do
expect { dsl.evaluate :some_argument }
.to throw_symbol :some_argument
end end
end end
end
describe '#condition' do describe '#initialize' do
context 'when a block is given' do it 'assigns the given env' do
it 'assigns a new evaluated condition' do expect(dsl.env).to be env
dsl.condition { :some_return_value } end
expect(dsl.condition.return_value).to eq :some_return_value
it 'assigns the given block' do
expect(dsl.block).to be block
end
it 'assigns no action' do
expect(dsl.actions).to be_empty
end
it 'assigns true as the condition' do
expect(dsl.condition).to be true
end end
end end
end
describe '#ask' do describe '#evaluate' do
let(:question) { 'Which letter?' } let(:block) { proc { throw :task_code } }
let(:choices) { [[:a, ?A], [:b, ?B]] }
let(:prompter_class) { double('prompter class').as_null_object }
subject(:ask) { dsl.ask question, choices,
prompter: prompter_class }
it 'builds a prompter' do it 'evaluates its code' do
expect(prompter_class).to receive(:new).with(env.input, env.output) expect { dsl.evaluate }
ask .to throw_symbol :task_code
end
context 'when arguments are given' do
let(:block) { proc { |e| throw e } }
it 'passes arguments as block parameters' do
expect { dsl.evaluate :some_argument }
.to throw_symbol :some_argument
end
end
end end
it 'prompts and returns the choice' do describe '#condition' do
prompter = double 'prompter' context 'when a block is given' do
allow(prompter_class).to receive(:new) { prompter } it 'assigns a new evaluated condition' do
allow(prompter).to receive(:prompt) { :choice } dsl.condition { :some_return_value }
expect(ask).to eq :choice expect(dsl.condition.return_value).to eq :some_return_value
end
end
end end
end
describe '#get' do describe '#ask' do
let(:env) { Env.new(registry: { some_key: :some_value }) } let(:question) { 'Which letter?' }
let(:choices) { [[:a, ?A], [:b, ?B]] }
let(:prompter_class) { double('prompter class').as_null_object }
subject(:ask) { dsl.ask question, choices,
prompter: prompter_class }
it 'fetches a value from the registry at given index' do it 'builds a prompter' do
expect(dsl.get :some_key).to eq :some_value expect(prompter_class).to receive(:new).with(env.input, env.output)
ask
end
it 'prompts and returns the choice' do
prompter = double 'prompter'
allow(prompter_class).to receive(:new) { prompter }
allow(prompter).to receive(:prompt) { :choice }
expect(ask).to eq :choice
end
end
describe '#get' do
let(:env) { Env.new(registry: { some_key: :some_value }) }
it 'fetches a value from the registry at given index' do
expect(dsl.get :some_key).to eq :some_value
end
end end
end end
end end

View File

@ -49,7 +49,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) { described_class.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

@ -1,42 +1,49 @@
require 'spec_helper' require 'spec_helper'
module Producer::Core module Producer::Core
describe Tests::FileContains do module Tests
let(:env) { Env.new } describe FileContains do
let(:filepath) { 'some_file' } let(:env) { Env.new }
let(:content) { 'some_content' } let(:filepath) { 'some_file' }
subject(:test) { Tests::FileContains.new(env, filepath, content) } let(:content) { 'some_content' }
subject(:test) { FileContains.new(env, filepath, content) }
it 'is a kind of test' do it 'is a kind of test' do
expect(test).to be_a Test expect(test).to be_a Test
end
describe '#verify' do
let(:fs) { double 'fs' }
before { allow(test).to receive(:fs) { fs } }
context 'when file contains the content' do
before { allow(fs).to receive(:file_read).with(filepath) { "foo#{content}bar" } }
it 'returns true' do
expect(test.verify).to be true
end
end end
context 'when file does not contain the content' do describe '#verify' do
before { allow(fs).to receive(:file_read).with(filepath) { 'foo bar' } } let(:fs) { double 'fs' }
it 'returns false' do before { allow(test).to receive(:fs) { fs } }
expect(test.verify).to be false
context 'when file contains the content' do
before do
allow(fs)
.to receive(:file_read).with(filepath) { "foo#{content}bar" }
end
it 'returns true' do
expect(test.verify).to be true
end
end end
end
context 'when file does not exist' do context 'when file does not contain the content' do
before { allow(fs).to receive(:file_read).with(filepath) { nil } } before do
allow(fs).to receive(:file_read).with(filepath) { 'foo bar' }
end
it 'returns false' do it 'returns false' do
expect(test.verify).to be false expect(test.verify).to be false
end
end
context 'when file does not exist' do
before { allow(fs).to receive(:file_read).with(filepath) { nil } }
it 'returns false' do
expect(test.verify).to be false
end
end end
end end
end end

View File

@ -1,27 +1,29 @@
require 'spec_helper' require 'spec_helper'
module Producer::Core module Producer::Core
describe Tests::HasDir do module Tests
let(:env) { Env.new } describe HasDir do
let(:path) { 'some_directory' } let(:env) { Env.new }
subject(:has_dir) { Tests::HasDir.new(env, path) } let(:path) { 'some_directory' }
subject(:has_dir) { HasDir.new(env, path) }
it 'is a kind of test' do it 'is a kind of test' do
expect(has_dir).to be_a Test expect(has_dir).to be_a Test
end
describe '#verify', :ssh do
before { sftp_story }
it 'delegates the call on remote FS' do
expect(env.remote.fs).to receive(:dir?).with(path)
has_dir.verify
end end
it 'returns the dir existence' do describe '#verify', :ssh do
existence = double 'existence' before { sftp_story }
allow(env.remote.fs).to receive(:dir?) { existence }
expect(has_dir.verify).to be existence it 'delegates the call on remote FS' do
expect(env.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 }
expect(has_dir.verify).to be existence
end
end end
end end
end end

View File

@ -1,40 +1,42 @@
require 'spec_helper' require 'spec_helper'
module Producer::Core module Producer::Core
describe Tests::HasEnv do module Tests
let(:env) { Env.new } describe HasEnv do
let(:variable_name) { :some_variable_name } let(:env) { Env.new }
subject(:has_env) { Tests::HasEnv.new(env, variable_name) } let(:variable_name) { :some_variable_name }
subject(:has_env) { HasEnv.new(env, variable_name) }
it 'is a kind of test' do it 'is a kind of test' do
expect(has_env).to be_a Test expect(has_env).to be_a Test
end
describe '#verify' do
let(:environment) { double 'environment' }
before do
allow(env.remote).to receive(:environment) { environment }
end end
it 'stringifies the queried variable name' do describe '#verify' do
expect(environment).to receive(:key?).with(kind_of(String)) let(:environment) { double 'environment' }
has_env.verify
end
it 'upcases the queried variable name' do before do
expect(environment).to receive(:key?).with('SOME_VARIABLE_NAME') allow(env.remote).to receive(:environment) { environment }
has_env.verify end
end
it 'returns true when remote environment var is defined' do it 'stringifies the queried variable name' do
allow(environment).to receive(:key?) { true } expect(environment).to receive(:key?).with(kind_of(String))
expect(has_env.verify).to be true has_env.verify
end end
it 'returns false when remote environment var is not defined' do it 'upcases the queried variable name' do
allow(environment).to receive(:key?) { false } expect(environment).to receive(:key?).with('SOME_VARIABLE_NAME')
expect(has_env.verify).to be false has_env.verify
end
it 'returns true when remote environment var is defined' do
allow(environment).to receive(:key?) { true }
expect(has_env.verify).to be true
end
it 'returns false when remote environment var is not defined' do
allow(environment).to receive(:key?) { false }
expect(has_env.verify).to be false
end
end end
end end
end end

View File

@ -1,27 +1,29 @@
require 'spec_helper' require 'spec_helper'
module Producer::Core module Producer::Core
describe Tests::HasFile do module Tests
let(:env) { Env.new } describe HasFile do
let(:filepath) { 'some_file' } let(:env) { Env.new }
subject(:has_file) { Tests::HasFile.new(env, filepath) } let(:filepath) { 'some_file' }
subject(:has_file) { HasFile.new(env, filepath) }
it 'is a kind of test' do it 'is a kind of test' do
expect(has_file).to be_a Test expect(has_file).to be_a Test
end
describe '#verify', :ssh do
before { sftp_story }
it 'delegates the call on remote FS' do
expect(env.remote.fs).to receive(:file?).with(filepath)
has_file.verify
end end
it 'returns the file existence' do describe '#verify', :ssh do
existence = double 'existence' before { sftp_story }
allow(env.remote.fs).to receive(:file?) { existence }
expect(has_file.verify).to be existence it 'delegates the call on remote FS' do
expect(env.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 }
expect(has_file.verify).to be existence
end
end end
end end
end end