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

Commit a7c1d35

Browse files
committed
Merge pull request #1360 from rspec/issue-1338
Move private methods from RSpec to RSpec.world.
2 parents eec1f5b + 0b42695 commit a7c1d35

File tree

8 files changed

+44
-54
lines changed

8 files changed

+44
-54
lines changed

lib/rspec/core.rb

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,6 @@ module RSpec
4040

4141
extend RSpec::Core::Warnings
4242

43-
# @private
44-
def self.wants_to_quit
45-
# Used internally to determine what to do when a SIGINT is received
46-
world.wants_to_quit
47-
end
48-
49-
# @private
50-
# Used internally to determine what to do when a SIGINT is received
51-
def self.wants_to_quit=(maybe)
52-
world.wants_to_quit=(maybe)
53-
end
54-
55-
# @private
56-
# Internal container for global non-configuration data
57-
def self.world
58-
@world ||= RSpec::Core::World.new
59-
end
60-
61-
# @private
62-
# Used internally to set the global object
63-
def self.world=(new_world)
64-
@world = new_world
65-
end
66-
6743
# @private
6844
# Used internally to ensure examples get reloaded between multiple runs in
6945
# the same process.
@@ -107,12 +83,6 @@ def self.configure
10783
yield configuration if block_given?
10884
end
10985

110-
# @private
111-
# Used internally to clear remaining groups when fail_fast is set
112-
def self.clear_remaining_example_groups
113-
world.example_groups.clear
114-
end
115-
11686
# The example being executed.
11787
#
11888
# The primary audience for this method is library authors who need access
@@ -145,8 +115,15 @@ def self.current_example=(example)
145115
end
146116

147117
# @private
148-
def self.windows_os?
149-
RbConfig::CONFIG['host_os'] =~ /cygwin|mswin|mingw|bccwin|wince|emx/
118+
# Internal container for global non-configuration data
119+
def self.world
120+
@world ||= RSpec::Core::World.new
121+
end
122+
123+
# @private
124+
# Used internally to set the global object
125+
def self.world=(new_world)
126+
@world = new_world
150127
end
151128

152129
module Core

lib/rspec/core/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ def color(output=output_stream)
524524

525525
def color=(bool)
526526
if bool
527-
if RSpec.windows_os? and not ENV['ANSICON']
527+
if RSpec.world.windows_os? and not ENV['ANSICON']
528528
RSpec.warning "You must use ANSICON 1.31 or later (http://adoxa.3eeweb.com/ansicon/) to use colour on Windows"
529529
@color = false
530530
else

lib/rspec/core/example_group.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ def self.run_after_all_hooks(example_group_instance)
420420

421421
# Runs all the examples in this group
422422
def self.run(reporter)
423-
if RSpec.wants_to_quit
424-
RSpec.clear_remaining_example_groups if top_level?
423+
if RSpec.world.wants_to_quit
424+
RSpec.world.clear_remaining_example_groups if top_level?
425425
return
426426
end
427427
reporter.example_group_started(self)
@@ -434,7 +434,7 @@ def self.run(reporter)
434434
rescue Pending::SkipDeclaredInExample => ex
435435
for_filtered_examples(reporter) {|example| example.skip_with_exception(reporter, ex) }
436436
rescue Exception => ex
437-
RSpec.wants_to_quit = true if fail_fast?
437+
RSpec.world.wants_to_quit = true if fail_fast?
438438
for_filtered_examples(reporter) {|example| example.fail_with_exception(reporter, ex) }
439439
ensure
440440
run_after_all_hooks(new)
@@ -462,11 +462,11 @@ def self.ordering_strategy
462462
# @private
463463
def self.run_examples(reporter)
464464
ordering_strategy.order(filtered_examples).map do |example|
465-
next if RSpec.wants_to_quit
465+
next if RSpec.world.wants_to_quit
466466
instance = new
467467
set_ivars(instance, before_all_ivars)
468468
succeeded = example.run(instance, reporter)
469-
RSpec.wants_to_quit = true if fail_fast? && !succeeded
469+
RSpec.world.wants_to_quit = true if fail_fast? && !succeeded
470470
succeeded
471471
end.all?
472472
end

lib/rspec/core/runner.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ def self.running_in_drb?
6262

6363
def self.trap_interrupt
6464
trap('INT') do
65-
exit!(1) if RSpec.wants_to_quit
66-
RSpec.wants_to_quit = true
65+
exit!(1) if RSpec.world.wants_to_quit
66+
RSpec.world.wants_to_quit = true
6767
STDERR.puts "\nExiting... Interrupt again to exit immediately."
6868
end
6969
end

lib/rspec/core/world.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class World
88
include RSpec::Core::Hooks
99

1010
attr_reader :example_groups, :filtered_examples
11+
12+
# Used internally to determine what to do when a SIGINT is received
1113
attr_accessor :wants_to_quit
1214

1315
def initialize(configuration=RSpec.configuration)
@@ -23,6 +25,17 @@ def initialize(configuration=RSpec.configuration)
2325
}
2426
end
2527

28+
# @private
29+
# Used internally to clear remaining groups when fail_fast is set
30+
def clear_remaining_example_groups
31+
example_groups.clear
32+
end
33+
34+
# @private
35+
def windows_os?
36+
RbConfig::CONFIG['host_os'] =~ /cygwin|mswin|mingw|bccwin|wince|emx/
37+
end
38+
2639
# @api private
2740
#
2841
# Apply ordering strategy from configuration to example groups

spec/rspec/core/backtrace_formatter_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def make_backtrace_formatter(exclusion_patterns=nil, inclusion_patterns=nil)
4141
end
4242

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

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

9797
context "when rspec is installed in the current working directory" do
98-
it "excludes lines from rspec libs by default", :unless => RSpec.windows_os? do
98+
it "excludes lines from rspec libs by default", :unless => RSpec.world.windows_os? do
9999
backtrace = [
100100
"#{Dir.getwd}/.bundle/path/to/rspec-expectations/lib/rspec/expectations/foo.rb:37",
101101
"#{Dir.getwd}/.bundle/path/to/rspec-expectations/lib/rspec/matchers/foo.rb:37",

spec/rspec/core/configuration_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,12 @@ def absolute_path_to(dir)
396396
expect(config.files_to_run).to eq([ "spec/rspec/core/resources/a_spec.rb"])
397397
end
398398

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

404-
it "loads files in Windows when directory is specified", :if => RSpec.windows_os? do
404+
it "loads files in Windows when directory is specified", :if => RSpec.world.windows_os? do
405405
assign_files_or_directories_to_run "spec\\rspec\\core\\resources"
406406
expect(config.files_to_run).to eq([ "spec/rspec/core/resources/a_spec.rb"])
407407
end

spec/rspec/core/example_group_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -621,13 +621,13 @@ def define_and_run_group(define_outer_example = false)
621621
expect(order).to eq([1,2,3])
622622
end
623623

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

629629
group.run
630-
expect(RSpec.wants_to_quit).to be_falsey
630+
expect(RSpec.world.wants_to_quit).to be_falsey
631631
end
632632

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

1263-
it "sets RSpec.wants_to_quit flag if encountering an exception in before(:all)" do
1263+
it "sets RSpec.world.wants_to_quit flag if encountering an exception in before(:all)" do
12641264
group.before(:all) { raise "error in before all" }
12651265
group.example("equality") { expect(1).to eq(2) }
12661266
expect(group.run).to be_falsey
1267-
expect(RSpec.wants_to_quit).to be_truthy
1267+
expect(RSpec.world.wants_to_quit).to be_truthy
12681268
end
12691269
end
12701270

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

12741274
before do
1275-
allow(RSpec).to receive(:wants_to_quit) { true }
1276-
allow(RSpec).to receive(:clear_remaining_example_groups)
1275+
allow(RSpec.world).to receive(:wants_to_quit) { true }
1276+
allow(RSpec.world).to receive(:clear_remaining_example_groups)
12771277
end
12781278

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

12841284
context "at top level" do
12851285
it "purges remaining groups" do
1286-
expect(RSpec).to receive(:clear_remaining_example_groups)
1286+
expect(RSpec.world).to receive(:clear_remaining_example_groups)
12871287
group.run(reporter)
12881288
end
12891289
end
12901290

12911291
context "in a nested group" do
12921292
it "does not purge remaining groups" do
12931293
nested_group = group.describe
1294-
expect(RSpec).not_to receive(:clear_remaining_example_groups)
1294+
expect(RSpec.world).not_to receive(:clear_remaining_example_groups)
12951295
nested_group.run(reporter)
12961296
end
12971297
end

0 commit comments

Comments
 (0)