|
| 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 |
0 commit comments