Skip to content

Commit

Permalink
Move private methods from RSpec to RSpec.world.
Browse files Browse the repository at this point in the history
Fixes #1338.
  • Loading branch information
xaviershay committed Mar 1, 2014
1 parent eec1f5b commit 0b42695
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 54 deletions.
41 changes: 9 additions & 32 deletions lib/rspec/core.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,30 +40,6 @@ module RSpec


extend RSpec::Core::Warnings extend RSpec::Core::Warnings


# @private
def self.wants_to_quit
# Used internally to determine what to do when a SIGINT is received
world.wants_to_quit
end

# @private
# Used internally to determine what to do when a SIGINT is received
def self.wants_to_quit=(maybe)
world.wants_to_quit=(maybe)
end

# @private
# Internal container for global non-configuration data
def self.world
@world ||= RSpec::Core::World.new
end

# @private
# Used internally to set the global object
def self.world=(new_world)
@world = new_world
end

# @private # @private
# Used internally to ensure examples get reloaded between multiple runs in # Used internally to ensure examples get reloaded between multiple runs in
# the same process. # the same process.
Expand Down Expand Up @@ -107,12 +83,6 @@ def self.configure
yield configuration if block_given? yield configuration if block_given?
end end


# @private
# Used internally to clear remaining groups when fail_fast is set
def self.clear_remaining_example_groups
world.example_groups.clear
end

# The example being executed. # The example being executed.
# #
# The primary audience for this method is library authors who need access # The primary audience for this method is library authors who need access
Expand Down Expand Up @@ -145,8 +115,15 @@ def self.current_example=(example)
end end


# @private # @private
def self.windows_os? # Internal container for global non-configuration data
RbConfig::CONFIG['host_os'] =~ /cygwin|mswin|mingw|bccwin|wince|emx/ def self.world
@world ||= RSpec::Core::World.new
end

# @private
# Used internally to set the global object
def self.world=(new_world)
@world = new_world
end end


module Core module Core
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/configuration.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ def color(output=output_stream)


def color=(bool) def color=(bool)
if bool if bool
if RSpec.windows_os? and not ENV['ANSICON'] if RSpec.world.windows_os? and not ENV['ANSICON']
RSpec.warning "You must use ANSICON 1.31 or later (http://adoxa.3eeweb.com/ansicon/) to use colour on Windows" RSpec.warning "You must use ANSICON 1.31 or later (http://adoxa.3eeweb.com/ansicon/) to use colour on Windows"
@color = false @color = false
else else
Expand Down
10 changes: 5 additions & 5 deletions lib/rspec/core/example_group.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ def self.run_after_all_hooks(example_group_instance)


# Runs all the examples in this group # Runs all the examples in this group
def self.run(reporter) def self.run(reporter)
if RSpec.wants_to_quit if RSpec.world.wants_to_quit
RSpec.clear_remaining_example_groups if top_level? RSpec.world.clear_remaining_example_groups if top_level?
return return
end end
reporter.example_group_started(self) reporter.example_group_started(self)
Expand All @@ -434,7 +434,7 @@ def self.run(reporter)
rescue Pending::SkipDeclaredInExample => ex rescue Pending::SkipDeclaredInExample => ex
for_filtered_examples(reporter) {|example| example.skip_with_exception(reporter, ex) } for_filtered_examples(reporter) {|example| example.skip_with_exception(reporter, ex) }
rescue Exception => ex rescue Exception => ex
RSpec.wants_to_quit = true if fail_fast? RSpec.world.wants_to_quit = true if fail_fast?
for_filtered_examples(reporter) {|example| example.fail_with_exception(reporter, ex) } for_filtered_examples(reporter) {|example| example.fail_with_exception(reporter, ex) }
ensure ensure
run_after_all_hooks(new) run_after_all_hooks(new)
Expand Down Expand Up @@ -462,11 +462,11 @@ def self.ordering_strategy
# @private # @private
def self.run_examples(reporter) def self.run_examples(reporter)
ordering_strategy.order(filtered_examples).map do |example| ordering_strategy.order(filtered_examples).map do |example|
next if RSpec.wants_to_quit next if RSpec.world.wants_to_quit
instance = new instance = new
set_ivars(instance, before_all_ivars) set_ivars(instance, before_all_ivars)
succeeded = example.run(instance, reporter) succeeded = example.run(instance, reporter)
RSpec.wants_to_quit = true if fail_fast? && !succeeded RSpec.world.wants_to_quit = true if fail_fast? && !succeeded
succeeded succeeded
end.all? end.all?
end end
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/core/runner.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def self.running_in_drb?


def self.trap_interrupt def self.trap_interrupt
trap('INT') do trap('INT') do
exit!(1) if RSpec.wants_to_quit exit!(1) if RSpec.world.wants_to_quit
RSpec.wants_to_quit = true RSpec.world.wants_to_quit = true
STDERR.puts "\nExiting... Interrupt again to exit immediately." STDERR.puts "\nExiting... Interrupt again to exit immediately."
end end
end end
Expand Down
13 changes: 13 additions & 0 deletions lib/rspec/core/world.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class World
include RSpec::Core::Hooks include RSpec::Core::Hooks


attr_reader :example_groups, :filtered_examples attr_reader :example_groups, :filtered_examples

# Used internally to determine what to do when a SIGINT is received
attr_accessor :wants_to_quit attr_accessor :wants_to_quit


def initialize(configuration=RSpec.configuration) def initialize(configuration=RSpec.configuration)
Expand All @@ -23,6 +25,17 @@ def initialize(configuration=RSpec.configuration)
} }
end end


# @private
# Used internally to clear remaining groups when fail_fast is set
def clear_remaining_example_groups
example_groups.clear
end

# @private
def windows_os?
RbConfig::CONFIG['host_os'] =~ /cygwin|mswin|mingw|bccwin|wince|emx/
end

# @api private # @api private
# #
# Apply ordering strategy from configuration to example groups # Apply ordering strategy from configuration to example groups
Expand Down
6 changes: 3 additions & 3 deletions spec/rspec/core/backtrace_formatter_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def make_backtrace_formatter(exclusion_patterns=nil, inclusion_patterns=nil)
end end


describe "#format_backtrace" do describe "#format_backtrace" do
it "excludes lines from rspec libs by default", :unless => RSpec.windows_os? do it "excludes lines from rspec libs by default", :unless => RSpec.world.windows_os? do
backtrace = [ backtrace = [
"/path/to/rspec-expectations/lib/rspec/expectations/foo.rb:37", "/path/to/rspec-expectations/lib/rspec/expectations/foo.rb:37",
"/path/to/rspec-expectations/lib/rspec/matchers/foo.rb:37", "/path/to/rspec-expectations/lib/rspec/matchers/foo.rb:37",
Expand All @@ -53,7 +53,7 @@ def make_backtrace_formatter(exclusion_patterns=nil, inclusion_patterns=nil)
expect(BacktraceFormatter.new.format_backtrace(backtrace)).to eq(["./my_spec.rb:5"]) expect(BacktraceFormatter.new.format_backtrace(backtrace)).to eq(["./my_spec.rb:5"])
end end


it "excludes lines from rspec libs by default", :if => RSpec.windows_os? do it "excludes lines from rspec libs by default", :if => RSpec.world.windows_os? do
backtrace = [ backtrace = [
"\\path\\to\\rspec-expectations\\lib\\rspec\\expectations\\foo.rb:37", "\\path\\to\\rspec-expectations\\lib\\rspec\\expectations\\foo.rb:37",
"\\path\\to\\rspec-expectations\\lib\\rspec\\matchers\\foo.rb:37", "\\path\\to\\rspec-expectations\\lib\\rspec\\matchers\\foo.rb:37",
Expand Down Expand Up @@ -95,7 +95,7 @@ def make_backtrace_formatter(exclusion_patterns=nil, inclusion_patterns=nil)
end end


context "when rspec is installed in the current working directory" do context "when rspec is installed in the current working directory" do
it "excludes lines from rspec libs by default", :unless => RSpec.windows_os? do it "excludes lines from rspec libs by default", :unless => RSpec.world.windows_os? do
backtrace = [ backtrace = [
"#{Dir.getwd}/.bundle/path/to/rspec-expectations/lib/rspec/expectations/foo.rb:37", "#{Dir.getwd}/.bundle/path/to/rspec-expectations/lib/rspec/expectations/foo.rb:37",
"#{Dir.getwd}/.bundle/path/to/rspec-expectations/lib/rspec/matchers/foo.rb:37", "#{Dir.getwd}/.bundle/path/to/rspec-expectations/lib/rspec/matchers/foo.rb:37",
Expand Down
4 changes: 2 additions & 2 deletions spec/rspec/core/configuration_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -396,12 +396,12 @@ def absolute_path_to(dir)
expect(config.files_to_run).to eq([ "spec/rspec/core/resources/a_spec.rb"]) expect(config.files_to_run).to eq([ "spec/rspec/core/resources/a_spec.rb"])
end end


it "loads files in Windows", :if => RSpec.windows_os? do it "loads files in Windows", :if => RSpec.world.windows_os? do
assign_files_or_directories_to_run "C:\\path\\to\\project\\spec\\sub\\foo_spec.rb" assign_files_or_directories_to_run "C:\\path\\to\\project\\spec\\sub\\foo_spec.rb"
expect(config.files_to_run).to eq([ "C:/path/to/project/spec/sub/foo_spec.rb"]) expect(config.files_to_run).to eq([ "C:/path/to/project/spec/sub/foo_spec.rb"])
end end


it "loads files in Windows when directory is specified", :if => RSpec.windows_os? do it "loads files in Windows when directory is specified", :if => RSpec.world.windows_os? do
assign_files_or_directories_to_run "spec\\rspec\\core\\resources" assign_files_or_directories_to_run "spec\\rspec\\core\\resources"
expect(config.files_to_run).to eq([ "spec/rspec/core/resources/a_spec.rb"]) expect(config.files_to_run).to eq([ "spec/rspec/core/resources/a_spec.rb"])
end end
Expand Down
18 changes: 9 additions & 9 deletions spec/rspec/core/example_group_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -621,13 +621,13 @@ def define_and_run_group(define_outer_example = false)
expect(order).to eq([1,2,3]) expect(order).to eq([1,2,3])
end end


it "does not set RSpec.wants_to_quit in case of an error in before all (without fail_fast?)" do it "does not set RSpec.world.wants_to_quit in case of an error in before all (without fail_fast?)" do
group = ExampleGroup.describe group = ExampleGroup.describe
group.before(:all) { raise "error in before all" } group.before(:all) { raise "error in before all" }
group.example("example") {} group.example("example") {}


group.run group.run
expect(RSpec.wants_to_quit).to be_falsey expect(RSpec.world.wants_to_quit).to be_falsey
end end


it "runs the before eachs in order" do it "runs the before eachs in order" do
Expand Down Expand Up @@ -1260,20 +1260,20 @@ def extract_execution_results(group)
expect(examples_run.length).to eq(2) expect(examples_run.length).to eq(2)
end end


it "sets RSpec.wants_to_quit flag if encountering an exception in before(:all)" do it "sets RSpec.world.wants_to_quit flag if encountering an exception in before(:all)" do
group.before(:all) { raise "error in before all" } group.before(:all) { raise "error in before all" }
group.example("equality") { expect(1).to eq(2) } group.example("equality") { expect(1).to eq(2) }
expect(group.run).to be_falsey expect(group.run).to be_falsey
expect(RSpec.wants_to_quit).to be_truthy expect(RSpec.world.wants_to_quit).to be_truthy
end end
end end


context "with RSpec.wants_to_quit=true" do context "with RSpec.world.wants_to_quit=true" do
let(:group) { RSpec::Core::ExampleGroup.describe } let(:group) { RSpec::Core::ExampleGroup.describe }


before do before do
allow(RSpec).to receive(:wants_to_quit) { true } allow(RSpec.world).to receive(:wants_to_quit) { true }
allow(RSpec).to receive(:clear_remaining_example_groups) allow(RSpec.world).to receive(:clear_remaining_example_groups)
end end


it "returns without starting the group" do it "returns without starting the group" do
Expand All @@ -1283,15 +1283,15 @@ def extract_execution_results(group)


context "at top level" do context "at top level" do
it "purges remaining groups" do it "purges remaining groups" do
expect(RSpec).to receive(:clear_remaining_example_groups) expect(RSpec.world).to receive(:clear_remaining_example_groups)
group.run(reporter) group.run(reporter)
end end
end end


context "in a nested group" do context "in a nested group" do
it "does not purge remaining groups" do it "does not purge remaining groups" do
nested_group = group.describe nested_group = group.describe
expect(RSpec).not_to receive(:clear_remaining_example_groups) expect(RSpec.world).not_to receive(:clear_remaining_example_groups)
nested_group.run(reporter) nested_group.run(reporter)
end end
end end
Expand Down

0 comments on commit 0b42695

Please sign in to comment.