Skip to content

Commit

Permalink
Extract logger to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Hunt committed Jan 4, 2014
1 parent edf1c6c commit 38ffdf6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/hi.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'hi/version'
require 'hi/logger'
require 'hi/request'
require 'hi/app'
require 'hi/server'
15 changes: 15 additions & 0 deletions lib/hi/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'awesome_print'

module Hi
class Logger
def log(message)
ap message unless testing
end

private

def testing
ENV['RACK_ENV'] == 'test'
end
end
end
48 changes: 48 additions & 0 deletions spec/hi/logger_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'spec_helper'
require 'hi/logger'

describe Hi::Logger do
let(:logger) { described_class.new }
let(:message) { 'Hello logger' }

context "when RACK_ENV is not 'test'" do
before do
ENV['RACK_ENV'] = 'the real deal'
end

it 'prints message to standard out' do
stdout = capture_stdout do
logger.log message
end

expect(stdout).to include message
end
end

context "when RACK_ENV is 'test'" do
before do
ENV['RACK_ENV'] = 'test'
end

it 'does not print message to standard out' do
stdout = capture_stdout do
logger.log message
end

expect(stdout).to be_empty
end
end

private

def capture_stdout
captured = StringIO.new
stdout = $stdout
$stdout = captured

yield

$stdout = stdout
captured.string
end
end

0 comments on commit 38ffdf6

Please sign in to comment.