From c67164c60ede7075101a40b28438a4357121b3ab Mon Sep 17 00:00:00 2001 From: Beyhan Veli Date: Wed, 2 Nov 2016 10:45:00 +0100 Subject: [PATCH] Print example name when started and result at the end [#130739823](https://www.pivotaltracker.com/story/show/130739823) Signed-off-by: Cornelius Schumacher Signed-off-by: Felix Riegger --- lib/formatter.rb | 20 ++++++- lib/validator/cli/cf_openstack_validator.rb | 5 +- spec/unit/formatter_spec.rb | 57 +++++++++++++++++++ .../cli/cf_openstack_validator_spec.rb | 6 +- src/specs/cpi_lifecycle_spec.rb | 4 +- 5 files changed, 83 insertions(+), 9 deletions(-) diff --git a/lib/formatter.rb b/lib/formatter.rb index 491790b..78b2bac 100644 --- a/lib/formatter.rb +++ b/lib/formatter.rb @@ -3,13 +3,31 @@ require_relative 'cli' class TestsuiteFormatter < RSpec::Core::Formatters::DocumentationFormatter - RSpec::Core::Formatters.register self, :dump_failures, :dump_pending, :dump_summary + RSpec::Core::Formatters.register self, :dump_failures, :dump_pending, :dump_summary, + :example_started, :example_pending, :example_failed def initialize(output) super @env = Cli.new(ENV) end + def example_started(notification) + output.print "#{current_indentation}#{notification.example.description}... " + end + + def example_passed(notification) + output.puts RSpec::Core::Formatters::ConsoleCodes.wrap("passed", :success) + end + + def example_failed(notification) + output.puts RSpec::Core::Formatters::ConsoleCodes.wrap("failed", :failure) + end + + def example_pending(pending) + pending_msg = pending.example.execution_result.pending_message + output.puts RSpec::Core::Formatters::ConsoleCodes.wrap("skipped: #{pending_msg}", :pending) + end + def dump_failures(notification) return if notification.failure_notifications.empty? formatted = "\nFailures:\n" diff --git a/lib/validator/cli/cf_openstack_validator.rb b/lib/validator/cli/cf_openstack_validator.rb index 83a16f0..22cfe43 100644 --- a/lib/validator/cli/cf_openstack_validator.rb +++ b/lib/validator/cli/cf_openstack_validator.rb @@ -182,9 +182,8 @@ def execute_specs "2> #{log_path}" ] Open3.popen3(env, rspec_command.join(' '), :unsetenv_others => true) do |_, stdout_out, _, wait_thr| - stdout_out.each do |line| - puts line - end + stdout_out.each_char {|c| print c } + unless wait_thr.value == 0 raise ErrorWithLogDetails.new("Executing '#{rspec_command}' failed", log_path) end diff --git a/spec/unit/formatter_spec.rb b/spec/unit/formatter_spec.rb index 08232da..95a7d35 100644 --- a/spec/unit/formatter_spec.rb +++ b/spec/unit/formatter_spec.rb @@ -14,6 +14,63 @@ ENV['VERBOSE_FORMATTER'] = 'false' end + describe '#example_started' do + let(:notification) { + instance_double(RSpec::Core::Notifications::ExampleNotification) + } + + let(:example) { double('example', description: 'example_name') } + + it 'prints the identation and example name' do + allow(notification).to receive(:example).and_return(example) + allow(subject).to receive(:current_indentation).and_return('IDENTATION') + + subject.example_started(notification) + + expect(output.string).to eq('IDENTATIONexample_name... ') + end + end + + describe '#example_passed' do + it 'it prints `passed` in success color' do + allow(RSpec::Core::Formatters::ConsoleCodes).to receive(:wrap).and_call_original + + subject.example_passed(nil) + + expect(RSpec::Core::Formatters::ConsoleCodes).to have_received(:wrap).with(anything, :success) + expect(output.string).to eq("passed\n") + end + end + + describe '#example_failed' do + it 'it prints `failed` in failure color' do + allow(RSpec::Core::Formatters::ConsoleCodes).to receive(:wrap).and_call_original + + subject.example_failed(nil) + + expect(RSpec::Core::Formatters::ConsoleCodes).to have_received(:wrap).with(anything, :failure) + expect(output.string).to eq("failed\n") + end + end + + describe '#example_pending' do + + let(:notification) { + execution_result = double('execution_result', pending_message: 'pending_message') + example = double('example', execution_result: execution_result) + instance_double(RSpec::Core::Notifications::FailedExampleNotification, example:example) + } + + it 'it prints the skipping reason in pending color' do + allow(RSpec::Core::Formatters::ConsoleCodes).to receive(:wrap).and_call_original + + subject.example_pending(notification) + + expect(RSpec::Core::Formatters::ConsoleCodes).to have_received(:wrap).with(anything, :pending) + expect(output.string).to eq("skipped: pending_message\n") + end + end + describe '#dump_failures' do let(:notification) { diff --git a/spec/unit/validator/cli/cf_openstack_validator_spec.rb b/spec/unit/validator/cli/cf_openstack_validator_spec.rb index d5d56dc..783bed7 100644 --- a/spec/unit/validator/cli/cf_openstack_validator_spec.rb +++ b/spec/unit/validator/cli/cf_openstack_validator_spec.rb @@ -335,16 +335,16 @@ module Validator::Cli end it 'should write the stdout to stdout' do - allow(Open3).to receive(:popen3).and_yield('', ['we write stdout to stdout'], [''], OpenStruct.new(:value => 0)) + allow(Open3).to receive(:popen3).and_yield('', 'we write stdout to stdout', [''], OpenStruct.new(:value => 0)) expect{ subject.execute_specs - }.to output("we write stdout to stdout\n").to_stdout + }.to output("we write stdout to stdout").to_stdout end context 'when execution fails' do it 'raises an error' do - allow(Open3).to receive(:popen3).and_yield('', [''], [''], OpenStruct.new(:value => 1)) + allow(Open3).to receive(:popen3).and_yield('', '', '', OpenStruct.new(:value => 1)) expect{ subject.execute_specs diff --git a/src/specs/cpi_lifecycle_spec.rb b/src/specs/cpi_lifecycle_spec.rb index c80b194..9f19a54 100644 --- a/src/specs/cpi_lifecycle_spec.rb +++ b/src/specs/cpi_lifecycle_spec.rb @@ -191,7 +191,7 @@ def with_cpi(error_message) end it 'can save and retrieve user-data from metadata service' do - Validator::Api::skip_test('Skipped optional metadata test. `config_drive` is configured in validator.yml.') if CfValidator.configuration.openstack['config_drive'] + Validator::Api::skip_test('`config_drive` is configured in validator.yml.') if CfValidator.configuration.openstack['config_drive'] @resources.consumes(:vm_cid_with_floating_ip, 'No VM to use') @@ -210,7 +210,7 @@ def with_cpi(error_message) end it 'can save and retrieve user-data from config_drive' do - Validator::Api::skip_test('Skipped optional config_drive test. `config_drive` is not configured in validator.yml.') unless CfValidator.configuration.openstack['config_drive'] + Validator::Api::skip_test('`config_drive` is not configured in validator.yml.') unless CfValidator.configuration.openstack['config_drive'] @resources.consumes(:vm_cid_with_floating_ip, 'No VM to use') vcap_password = 'c1oudc0w'