1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
# frozen_string_literal: true
require 'fuubar'
require 'stringio'
require 'ostruct'
# rubocop:disable RSpec/MultipleMemoizedHelpers
describe ::Fuubar do
let(:output) do
io = ::StringIO.new
allow(io).to receive(:tty?).
and_return(true)
io
end
let(:formatter) { ::Fuubar.new(output) }
let(:example) { self.class.example }
let(:example_count) { 2 }
let(:start_notification) { ::RSpec::Core::Notifications::StartNotification.new(example_count, ::Time.now) }
let(:message_notification) { ::RSpec::Core::Notifications::MessageNotification.new('My Message') }
let(:example_notification) { ::RSpec::Core::Notifications::ExampleNotification.for(example) }
let(:pending_notification) { ::RSpec::Core::Notifications::ExampleNotification.for(pending_example) }
let(:failed_notification) { ::RSpec::Core::Notifications::ExampleNotification.for(failed_example) }
let(:failed_example) do
exception = ::RuntimeError.new('Test Fuubar Error')
exception.set_backtrace [
"/my/filename.rb:4:in `some_method'"
]
example = self.class.example
example.metadata[:file_path] = '/my/example/spec.rb'
example.metadata[:execution_result].status = :failed
example.metadata[:execution_result].exception = exception
example
end
let(:pending_example) do
example = self.class.example
example.metadata[:execution_result].pending_fixed = true
example
end
let(:fuubar_results) do
output.rewind
output.read
end
before(:each) do
::RSpec.configuration.fuubar_progress_bar_options = {
:length => 40,
:throttle_rate => 0.0
}
ENV.delete('CONTINUOUS_INTEGRATION')
end
context 'when it is created' do
it 'does not start the bar until the formatter is started' do
expect(formatter.progress).not_to be_started
formatter.start(start_notification)
expect(formatter.progress).to be_started
end
it 'creates a new ProgressBar' do
expect(formatter.progress).to be_instance_of ::ProgressBar::Base
end
it 'sets the format of the bar to the default' do
expect(formatter.progress.instance_variable_get(:@format)).to eql ' %c/%C |%w>%i| %e '
end
it 'sets the total to the number of examples' do
expect(formatter.progress.total).to be_zero
end
it 'sets the bar\'s output' do
expect(formatter.progress.send(:output).stream).to be_a ::Fuubar::Output
expect(formatter.progress.send(:output).stream.__getobj__).to eql output
end
context 'and continuous integration is enabled' do
before(:each) do
allow(::RSpec.configuration).to receive(:color_mode).and_return(:on)
::RSpec.configuration.fuubar_progress_bar_options = { :length => 40 }
ENV['CONTINUOUS_INTEGRATION'] = 'true'
end
it 'throttles the progress bar at one second' do
throttle = formatter.progress.__send__(:output).__send__(:throttle)
throttle_rate = throttle.__send__(:rate)
expect(throttle_rate).to eq(1.0)
end
context 'when processing an example' do
before(:each) do
formatter.start(start_notification)
throttle = formatter.progress.__send__(:output).__send__(:throttle)
_throttle_rate = throttle.__send__(:rate=, 0.0)
output.rewind
formatter.example_passed(example)
end
it 'does not output color codes' do
expect(fuubar_results).to start_with " 1/2 |== 50 ==> | ETA: 00:00:00 \r"
end
end
end
context 'and continuous integration is not enabled' do
before(:each) do
allow(::RSpec.configuration).to receive(:color_mode).and_return(:on)
::RSpec.configuration.fuubar_progress_bar_options = { :length => 40 }
ENV['CONTINUOUS_INTEGRATION'] = 'false'
end
it 'throttles the progress bar at the default rate' do
throttle = formatter.progress.__send__(:output).__send__(:throttle)
throttle_rate = throttle.__send__(:rate)
expect(throttle_rate).to eq(0.01)
end
context 'when processing an example' do
before(:each) do
formatter.start(start_notification)
throttle = formatter.progress.__send__(:output).__send__(:throttle)
_throttle_rate = throttle.__send__(:rate=, 0.0)
output.rewind
formatter.example_passed(example)
end
it 'outputs color codes' do
expect(fuubar_results).to start_with "\e[32m 1/2 |== 50 ==> | ETA: 00:00:00 \r\e[0m"
end
end
end
end
context 'when custom options are set after the formatter is created' do
before(:each) do
formatter
::RSpec.configuration.fuubar_progress_bar_options = {
:length => 40,
:throttle_rate => 0.0,
:format => '%c'
}
end
context 'when the bar is started' do
before(:each) { formatter.start(start_notification) }
it 'properly creates the bar' do
expect(formatter.progress.instance_variable_get(:@format)).to eql '%c'
end
end
end
context 'when it is started' do
before(:each) do
allow(::RSpec.configuration).to receive(:color_mode).and_return(:on)
formatter.start(start_notification)
end
it 'sets the total to the number of examples' do
expect(formatter.progress.total).to be 2
end
context 'and no custom options are passed in' do
it 'sets the format of the bar to the default' do
expect(formatter.progress.instance_variable_get(:@format)).to eql ' %c/%C |%w>%i| %e '
end
end
context 'and an example passes' do
before(:each) do
output.rewind
formatter.example_passed(example)
end
it 'outputs the proper bar information' do
expect(fuubar_results).to start_with "\e[32m 1/2 |== 50 ==> | ETA: 00:00:00 \r\e[0m"
end
end
context 'and an example pends' do
before(:each) do
output.rewind
formatter.example_pending(pending_example)
end
it 'outputs the proper bar information' do
formatter.progress.increment
expect(fuubar_results).to start_with "\e[33m 1/2 |== 50 ==> | ETA: 00:00:00 \r\e[0m"
end
context 'and then an example succeeds' do
before(:each) do
output.rewind
formatter.example_pending(pending_notification)
end
it 'outputs the pending bar' do
expect(fuubar_results).to start_with "\e[33m 2/2 |===== 100 ======>| Time: 00:00:00 \n\e[0m"
end
end
end
context 'and an example fails' do
it 'outputs the proper bar information' do
output.rewind
formatter.example_failed(failed_notification)
expect(fuubar_results).to end_with "\e[31m 1/2 |== 50 ==> | ETA: 00:00:00 \r\e[0m"
end
context 'and then an example succeeds' do
before(:each) do
formatter.example_failed(failed_notification)
output.rewind
formatter.example_passed(example)
end
it 'outputs the failed bar' do
expect(fuubar_results).to start_with "\e[31m 2/2 |===== 100 ======>| Time: 00:00:00 \n\e[0m"
end
end
context 'and then an example pends' do
before(:each) do
formatter.example_failed(failed_notification)
output.rewind
formatter.example_pending(example_notification)
end
it 'outputs the failed bar' do
expect(fuubar_results).to start_with "\e[31m 2/2 |===== 100 ======>| Time: 00:00:00 \n\e[0m"
end
end
end
it 'can properly log messages' do
formatter.message message_notification
expect(fuubar_results).to end_with "My Message\n 0/2 |> | ETA: ??:??:?? \r"
end
end
end
# rubocop:enable RSpec/MultipleMemoizedHelpers
|