Skip to content

Commit 54058b0

Browse files
sdsShane da Silva
authored and
Shane da Silva
committed
Add specs for Logger
Change-Id: I7ff61d577923a8dfe0eb462408dd61b3335e6b1f Reviewed-on: http://gerrit.causes.com/35583 Reviewed-by: Shane da Silva <shane@causes.com> Tested-by: Shane da Silva <shane@causes.com>
1 parent dd4404a commit 54058b0

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

lib/overcommit/logger.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def notice(str)
4141
private
4242

4343
def color(code, str)
44-
log(@out.isatty ? "\033[#{code}m#{str}\033[0m" : str)
44+
log(@out.tty? ? "\033[#{code}m#{str}\033[0m" : str)
4545
end
4646
end
4747
end

spec/overcommit/logger_spec.rb

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Logger do
4+
let(:io) { StringIO.new }
5+
let(:output) { io.string }
6+
subject { described_class.new(io) }
7+
8+
describe '.silent' do
9+
subject { described_class.silent }
10+
11+
it 'does not output anything' do
12+
capture_stdout { subject.log('Something') }.should be_empty
13+
end
14+
end
15+
16+
describe '#partial' do
17+
subject { super().partial('Hello') }
18+
19+
it 'writes to the output stream' do
20+
subject
21+
output.should_not be_empty
22+
end
23+
24+
it 'does not append a newline' do
25+
subject
26+
output[-1].should_not == "\n"
27+
end
28+
end
29+
30+
describe '#log' do
31+
subject { super().log('Hello') }
32+
33+
it 'writes to the output stream' do
34+
subject
35+
output.should_not be_empty
36+
end
37+
38+
it 'appends a newline' do
39+
subject
40+
output[-1].should == "\n"
41+
end
42+
end
43+
44+
shared_examples_for 'colorized output' do
45+
subject { super().send(method, 'Hello') }
46+
47+
it 'writes to the output stream' do
48+
subject
49+
output.should_not be_empty
50+
end
51+
52+
it 'appends a newline' do
53+
subject
54+
output[-1].should == "\n"
55+
end
56+
57+
context 'when the output stream is a TTY' do
58+
before do
59+
io.stub(:tty?).and_return(true)
60+
end
61+
62+
it 'includes the color escape sequence' do
63+
subject
64+
output.should include "\033[#{color_code}m"
65+
end
66+
67+
it 'ends with the color reset sequence' do
68+
subject
69+
output.should end_with "[0m\n"
70+
end
71+
end
72+
73+
context 'when the output stream is not a TTY' do
74+
before do
75+
io.stub(:tty?).and_return(false)
76+
end
77+
78+
it 'omits the color escape sequence' do
79+
subject
80+
output.should_not include "\033"
81+
end
82+
end
83+
end
84+
85+
describe '#bold' do
86+
it_behaves_like 'colorized output' do
87+
let(:method) { :bold }
88+
let(:color_code) { '1;37' }
89+
end
90+
end
91+
92+
describe '#error' do
93+
it_behaves_like 'colorized output' do
94+
let(:method) { :error }
95+
let(:color_code) { 31 }
96+
end
97+
end
98+
99+
describe '#success' do
100+
it_behaves_like 'colorized output' do
101+
let(:method) { :success }
102+
let(:color_code) { 32 }
103+
end
104+
end
105+
106+
describe '#warning' do
107+
it_behaves_like 'colorized output' do
108+
let(:method) { :warning }
109+
let(:color_code) { 33 }
110+
end
111+
end
112+
113+
describe '#notice' do
114+
it_behaves_like 'colorized output' do
115+
let(:method) { :notice }
116+
let(:color_code) { '1;33' }
117+
end
118+
end
119+
end

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717

1818
RSpec.configure do |config|
1919
config.include GitSpecHelpers
20+
config.include OutputHelpers
2021
end

spec/support/output_helpers.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Helpers for capturing output streams in tests.
2+
module OutputHelpers
3+
module_function
4+
5+
def capture_stdout(&block)
6+
original = $stdout
7+
$stdout = output = StringIO.new
8+
9+
yield
10+
11+
output.string
12+
ensure
13+
$stdout = original
14+
end
15+
end

0 commit comments

Comments
 (0)