Integrate a logger attached to our env

This commit is contained in:
Thibault Jouan 2014-05-19 23:01:18 +00:00
parent 4e6e92bf36
commit 024ab7eba0
2 changed files with 61 additions and 8 deletions

View File

@ -1,7 +1,7 @@
module Producer module Producer
module Core module Core
class Env class Env
attr_reader :input, :output, :registry attr_reader :input, :output, :registry, :logger
attr_accessor :target attr_accessor :target
def initialize(input: $stdin, output: $stdout, remote: nil, registry: {}) def initialize(input: $stdin, output: $stdout, remote: nil, registry: {})
@ -9,7 +9,9 @@ module Producer
@output = output @output = output
@registry = registry @registry = registry
@remote = remote @remote = remote
@target = nil @logger = Logger.new(output)
self.log_level = Logger::ERROR
end end
def remote def remote
@ -23,6 +25,18 @@ module Producer
def []=(key, value) def []=(key, value)
@registry[key] = value @registry[key] = value
end end
def log(message)
logger.info message
end
def log_level
logger.level
end
def log_level=(level)
logger.level = level
end
end end
end end
end end

View File

@ -2,17 +2,14 @@ require 'spec_helper'
module Producer::Core module Producer::Core
describe Env do describe Env do
subject(:env) { Env.new } let(:output) { StringIO.new }
subject(:env) { Env.new(output: output) }
describe '#initialize' do describe '#initialize' do
it 'assigns $stdin as the default output' do it 'assigns $stdin as the default output' do
expect(env.input).to be $stdin expect(env.input).to be $stdin
end end
it 'assigns $stdout as the default output' do
expect(env.output).to be $stdout
end
it 'assigns no default target' do it 'assigns no default target' do
expect(env.target).not_to be expect(env.target).not_to be
end end
@ -21,6 +18,14 @@ module Producer::Core
expect(env.registry).to be_empty expect(env.registry).to be_empty
end end
context 'when output is not given as argument' do
subject(:env) { Env.new }
it 'assigns $stdout as the default output' do
expect(env.output).to be $stdout
end
end
context 'when input is given as argument' do context 'when input is given as argument' do
let(:input) { double 'input' } let(:input) { double 'input' }
subject(:env) { described_class.new(input: input) } subject(:env) { described_class.new(input: input) }
@ -31,7 +36,6 @@ module Producer::Core
end end
context 'when output is given as argument' do context 'when output is given as argument' do
let(:output) { double 'output' }
subject(:env) { described_class.new(output: output) } subject(:env) { described_class.new(output: output) }
it 'assigns the given output' do it 'assigns the given output' do
@ -58,6 +62,21 @@ module Producer::Core
end end
end end
describe '#logger' do
it 'returns a logger' do
expect(env.logger).to be_a Logger
end
it 'uses env output' do
env.logger.error 'some message'
expect(output.string).to include 'some message'
end
it 'has a log level of ERROR' do
expect(env.log_level).to eq Logger::ERROR
end
end
describe '#remote' do describe '#remote' do
it 'builds a Remote with the current target' do it 'builds a Remote with the current target' do
env.target = 'some_hostname.example' env.target = 'some_hostname.example'
@ -90,5 +109,25 @@ module Producer::Core
expect(env[:some_key]).to eq :some_value expect(env[:some_key]).to eq :some_value
end end
end end
describe '#log' do
it 'logs an info message through the assigned logger' do
expect(env.logger).to receive(:info).with 'message'
env.log 'message'
end
end
describe '#log_level' do
it 'returns the logger level' do
expect(env.log_level).to eq env.logger.level
end
end
describe '#log_level=' do
it 'sets the logger level' do
env.log_level = Logger::DEBUG
expect(env.logger.level).to eq Logger::DEBUG
end
end
end end
end end