Simplify Remote::Environment

This commit is contained in:
Thibault Jouan 2014-07-01 20:02:35 +00:00
parent 336003d2d7
commit 8375890c1b
3 changed files with 5 additions and 50 deletions

View File

@ -42,7 +42,7 @@ module Producer
end
def environment
Environment.new_from_string(execute 'env')
Environment.string_to_hash(execute 'env')
end
def cleanup

View File

@ -6,19 +6,6 @@ module Producer
def string_to_hash(str)
Hash[str.each_line.map { |l| l.chomp.split '=', 2 }]
end
def new_from_string(str)
new string_to_hash str
end
end
extend Forwardable
def_delegators :@variables, :[], :key?
attr_reader :variables
def initialize(variables)
@variables = variables
end
end
end

View File

@ -3,47 +3,15 @@ require 'spec_helper'
module Producer::Core
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
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'
expect(described_class.string_to_hash(string)).to eq ({
'FOO' => 'bar',
'BAZ' => 'qux'
})
end
end
end