Fix coding standards (module nesting) in specs
This commit is contained in:
@@ -1,48 +1,50 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module Producer::Core
|
||||
describe Remote::Environment do
|
||||
let(:variables) { { 'FOO' => 'bar', 'BAZ' => 'qux' } }
|
||||
let(:string) { "FOO=bar\nBAZ=qux" }
|
||||
let(:argument) { variables }
|
||||
subject(:environment) { Remote::Environment.new(argument) }
|
||||
class Remote
|
||||
describe Environment do
|
||||
let(:variables) { { 'FOO' => 'bar', 'BAZ' => 'qux' } }
|
||||
let(:string) { "FOO=bar\nBAZ=qux" }
|
||||
let(:argument) { variables }
|
||||
subject(:environment) { Environment.new(argument) }
|
||||
|
||||
describe '.string_to_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
|
||||
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
|
||||
describe '.string_to_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
|
||||
end
|
||||
end
|
||||
|
||||
context 'when key is not defined' do
|
||||
it 'returns false' do
|
||||
expect(environment.key? 'INEXISTENT_KEY').to be false
|
||||
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
|
||||
end
|
||||
|
||||
describe '#[]' do
|
||||
it 'returns the value indexed by given key' do
|
||||
expect(environment['FOO']).to eq 'bar'
|
||||
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
|
||||
|
||||
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
|
||||
|
@@ -1,97 +1,111 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module Producer::Core
|
||||
describe Remote::FS do
|
||||
let(:remote) { Remote.new('some_host.example') }
|
||||
subject(:fs) { Remote::FS.new(remote) }
|
||||
class Remote
|
||||
describe FS do
|
||||
let(:remote) { Remote.new('some_host.example') }
|
||||
subject(:fs) { FS.new(remote) }
|
||||
|
||||
describe '#new' do
|
||||
it 'assigns the remote given as argument' do
|
||||
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
|
||||
describe '#new' do
|
||||
it 'assigns the remote given as argument' do
|
||||
expect(fs.remote).to be remote
|
||||
end
|
||||
end
|
||||
|
||||
context 'when path given as argument is not a directory' do
|
||||
before { allow(stat).to receive(:directory?) { false } }
|
||||
describe '#sftp', :ssh do
|
||||
before { sftp_story }
|
||||
|
||||
it 'returns false' do
|
||||
expect(fs.dir? path).to be false
|
||||
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
|
||||
|
||||
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
|
||||
response = double 'response', code: '42', message: 'some message'
|
||||
ex = Net::SFTP::StatusException.new(response)
|
||||
allow(sftp).to receive(:stat!).with(path).and_raise(ex)
|
||||
allow(fs).to receive(:sftp) { sftp }
|
||||
allow(sftp).to receive(:stat!).with(path) { stat }
|
||||
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 } }
|
||||
context 'when path given as argument is a directory' do
|
||||
before { allow(stat).to receive(:directory?) { true } }
|
||||
|
||||
it 'returns true' do
|
||||
expect(fs.file? file_path).to be true
|
||||
expect(fs.dir? path).to be true
|
||||
end
|
||||
end
|
||||
|
||||
context 'when path is not a file' do
|
||||
before { allow(stat).to receive(:file?) { false } }
|
||||
context 'when path given as argument is not a directory' do
|
||||
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
|
||||
expect(fs.file? file_path).to be false
|
||||
@@ -99,84 +113,72 @@ module Producer::Core
|
||||
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
|
||||
response = double 'response', code: '42', message: 'some message'
|
||||
ex = Net::SFTP::StatusException.new(response)
|
||||
allow(stat).to receive(:file?).and_raise(ex)
|
||||
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 false' do
|
||||
expect(fs.file? file_path).to be false
|
||||
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
|
||||
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
|
||||
|
||||
describe '#mkdir' do
|
||||
let(:sftp) { double 'sftp' }
|
||||
let(:path) { 'some_directory_path' }
|
||||
describe '#file_write' do
|
||||
let(:sftp) { double 'sftp' }
|
||||
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
|
||||
response = double 'response', code: '42', message: 'some message'
|
||||
ex = Net::SFTP::StatusException.new(response)
|
||||
allow(file).to receive(:open).and_raise(ex)
|
||||
allow(fs).to receive(:sftp) { sftp }
|
||||
allow(sftp).to receive(:file) { file }
|
||||
end
|
||||
|
||||
it 'returns nil' do
|
||||
expect(fs.file_read(path)).to be nil
|
||||
it 'opens the file' do
|
||||
expect(file).to receive(:open).with(path, 'w')
|
||||
fs.file_write path, content
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#file_write' do
|
||||
let(:sftp) { double 'sftp' }
|
||||
let(:file) { double 'file' }
|
||||
let(:path) { 'some_file_path' }
|
||||
let(:content) { 'some_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
|
||||
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
|
||||
fs.file_write path, content
|
||||
end
|
||||
fs.file_write path, content
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user