Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2379 from rspec/myron/pr-2368-rebased
Browse files Browse the repository at this point in the history
PR 2368 rebased
  • Loading branch information
JonRowe committed Mar 2, 2017
1 parent 0452d36 commit 450552a
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 39 deletions.
2 changes: 1 addition & 1 deletion lib/rspec/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def self.reset
# same process.
def self.clear_examples
world.reset
configuration.reporter.reset
configuration.reset_reporter
configuration.start_time = ::RSpec::Core::Time.now
configuration.reset_filters
end
Expand Down
6 changes: 6 additions & 0 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ def reset
@formatter_loader = nil
end

# @private
def reset_reporter
@reporter = nil
@formatter_loader = nil
end

# @private
def reset_filters
self.filter_manager = FilterManager.new
Expand Down
6 changes: 3 additions & 3 deletions lib/rspec/core/formatters/base_text_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ def seed(notification)

# @api public
#
# Invoked at the very end, `close` allows the formatter to clean
# up resources, e.g. open streams, etc.
# Invoked at the end of a suite run. Allows the formatter to do any
# tidying up, but be aware that formatter output streams may be used
# elsewhere so don't actually close them.
#
# @param _notification [NullNotification] (Ignored)
def close(_notification)
Expand All @@ -69,7 +70,6 @@ def close(_notification)
output.puts

output.flush
output.close unless output == $stdout
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/rspec/core/formatters/json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def stop(notification)

def close(_notification)
output.write @output_hash.to_json
output.close if IO === output && output != $stdout
end

def dump_profile(profile)
Expand Down
5 changes: 3 additions & 2 deletions lib/rspec/core/formatters/protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ class Protocol
# @api public
# @group Suite Notifications
#
# Invoked at the very end, `close` allows the formatter to clean
# up resources, e.g. open streams, etc.
# Invoked at the end of a suite run. Allows the formatter to do any
# tidying up, but be aware that formatter output streams may be used
# elsewhere so don't actually close them.
#
# @param notification [NullNotification]
end
Expand Down
8 changes: 0 additions & 8 deletions lib/rspec/core/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ def initialize(configuration)
# @private
attr_reader :examples, :failed_examples, :pending_examples

# @private
def reset
@examples = []
@failed_examples = []
@pending_examples = []
@profiler = Profiler.new if defined?(@profiler)
end

# @private
def setup_profiler
@profiler = Profiler.new
Expand Down
1 change: 0 additions & 1 deletion lib/rspec/core/world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def ordered_example_groups
def reset
RSpec::ExampleGroups.remove_all_constants
example_groups.clear
@shared_example_group_registry = nil
end

# @private
Expand Down
35 changes: 18 additions & 17 deletions spec/rspec/core/bisect/coordinator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,25 @@ def find_minimal_repro(output, formatter=Formatters::BisectProgressFormatter)

it 'notifies the bisect progress formatter of progress and closes the output' do
tempfile = Tempfile.new("bisect")
output_file = File.open(tempfile.path, "w")
expect { find_minimal_repro(output_file) }.to change(output_file, :closed?).from(false).to(true)
output = normalize_durations(File.read(tempfile.path)).chomp
File.open(tempfile.path, "w") do |output_file|
find_minimal_repro(output_file)
output = normalize_durations(File.read(tempfile.path)).chomp

expect(output).to eq(<<-EOS.gsub(/^\s+\|/, ''))
|Bisect started using options: ""
|Running suite to find failures... (n.nnnn seconds)
|Starting bisect with 2 failing examples and 6 non-failing examples.
|Checking that failure(s) are order-dependent... failure appears to be order-dependent
|
|Round 1: bisecting over non-failing examples 1-6 .. ignoring examples 4-6 (n.nnnn seconds)
|Round 2: bisecting over non-failing examples 1-3 .. multiple culprits detected - splitting candidates (n.nnnn seconds)
|Round 3: bisecting over non-failing examples 1-2 .. ignoring example 2 (n.nnnn seconds)
|Bisect complete! Reduced necessary non-failing examples from 6 to 2 in n.nnnn seconds.
|
|The minimal reproduction command is:
| rspec 1.rb[1:1] 2.rb[1:1] 4.rb[1:1] 5.rb[1:1]
EOS
expect(output).to eq(<<-EOS.gsub(/^\s+\|/, ''))
|Bisect started using options: ""
|Running suite to find failures... (n.nnnn seconds)
|Starting bisect with 2 failing examples and 6 non-failing examples.
|Checking that failure(s) are order-dependent... failure appears to be order-dependent
|
|Round 1: bisecting over non-failing examples 1-6 .. ignoring examples 4-6 (n.nnnn seconds)
|Round 2: bisecting over non-failing examples 1-3 .. multiple culprits detected - splitting candidates (n.nnnn seconds)
|Round 3: bisecting over non-failing examples 1-2 .. ignoring example 2 (n.nnnn seconds)
|Bisect complete! Reduced necessary non-failing examples from 6 to 2 in n.nnnn seconds.
|
|The minimal reproduction command is:
| rspec 1.rb[1:1] 2.rb[1:1] 4.rb[1:1] 5.rb[1:1]
EOS
end
end

it 'can use the bisect debug formatter to get detailed progress' do
Expand Down
16 changes: 12 additions & 4 deletions spec/rspec/core/formatters/base_text_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@
let(:output_to_close) { File.new("./output_to_close", "w") }
let(:formatter) { described_class.new(output_to_close) }

it 'does not close an already closed output stream' do
after do
# Windows appears to not let the `:isolated_directory` shared group
# cleanup if the file isn't closed.
output_to_close.close unless output_to_close.closed?
end

it 'does not error on an already closed output stream' do
output_to_close.close

expect { formatter.close(RSpec::Core::Notifications::NullNotification) }.not_to raise_error
end

it "flushes output before closing the stream so buffered bytes are not lost if we exit right away" do
expect(output_to_close).to receive(:flush).ordered.and_call_original
# Windows appears to not let the `:isolated_directory` shared group cleanup if
# the file isn't closed, so we need to use `and_call_original` here.
expect(output_to_close).to receive(:close).ordered.and_call_original

formatter.close(RSpec::Core::Notifications::NullNotification)
end

it "does not close the stream so that it can be reused within a process" do
formatter.close(RSpec::Core::Notifications::NullNotification)
expect(output_to_close.closed?).to be(false)
end
end

describe "#dump_summary" do
Expand Down
5 changes: 5 additions & 0 deletions spec/rspec/core/formatters/json_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
:version => RSpec::Core::Version::STRING
}.to_json)
end

it "does not close the stream so that it can be reused within a process" do
formatter.close(RSpec::Core::Notifications::NullNotification)
expect(formatter_output.closed?).to be(false)
end
end

describe "#message" do
Expand Down
42 changes: 40 additions & 2 deletions spec/rspec/core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@

describe ".clear_examples" do
let(:listener) { double("listener") }
let(:reporter) { RSpec.configuration.reporter }

def reporter
RSpec.configuration.reporter
end

before do
RSpec.configuration.output_stream = StringIO.new
Expand Down Expand Up @@ -175,6 +178,8 @@
reporter.example_pending(pending_ex)
reporter.finish

RSpec.clear_examples

reporter.register_listener(listener, :dump_summary)

expect(listener).to receive(:dump_summary) do |notification|
Expand All @@ -183,7 +188,6 @@
expect(notification.pending_examples).to be_empty
end

RSpec.clear_examples
reporter.start(0)
reporter.finish
end
Expand Down Expand Up @@ -224,6 +228,40 @@
RSpec.configuration.filter_manager.exclusions.rules
).to eq(:slow => true)
end

it 'clears the deprecation buffer' do
RSpec.configuration.deprecation_stream = StringIO.new

group = RSpec.describe do
example { RSpec.deprecate("first deprecation") }
end.run

reporter.start(1)
reporter.finish

RSpec.clear_examples

RSpec.configuration.deprecation_stream = StringIO.new(deprecations = "")

group = RSpec.describe do
example { RSpec.deprecate("second deprecation") }
end.run

reporter.start(1)
reporter.finish

expect(deprecations).to include("second deprecation")
expect(deprecations).to_not include("first deprecation")
end

it 'does not clear shared examples' do
RSpec.shared_examples_for("shared") { }

RSpec.clear_examples

registry = RSpec.world.shared_example_group_registry
expect(registry.find([:main], "shared")).to_not be_nil
end
end

it 'uses only one thread local variable', :run_last do
Expand Down

0 comments on commit 450552a

Please sign in to comment.