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

Commit 850b904

Browse files
committed
Merge pull request #1880 from rspec/allowed-stdlibs
Allowed stdlibs
2 parents 0ec31db + 08c4c7b commit 850b904

File tree

18 files changed

+105
-21
lines changed

18 files changed

+105
-21
lines changed

lib/rspec/core.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
$_rspec_core_load_started_at = Time.now
33
# rubocop:enable Style/GlobalVars
44

5-
require 'rbconfig'
6-
75
require "rspec/support"
86
RSpec::Support.require_rspec_support "caller_filter"
97

spec/rspec/core/backtrace_formatter_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def make_backtrace_formatter(exclusion_patterns=nil, inclusion_patterns=nil)
112112

113113
expect(BacktraceFormatter.new.format_backtrace(backtrace)).to eq(["./my_spec.rb:5"])
114114
end
115-
115+
116116
context "when every line is filtered out" do
117117
let(:backtrace) do
118118
[

spec/rspec/core_spec.rb

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,65 @@
1-
require 'rspec/support/spec/prevent_load_time_warnings'
1+
require 'rspec/support/spec/library_wide_checks'
22

33
RSpec.describe RSpec do
4-
fake_minitest = File.expand_path('../../support/fake_minitest', __FILE__)
5-
it_behaves_like 'a library that issues no warnings when loaded', 'rspec-core',
6-
# Loading minitest issues warnings, so we put our fake minitest on the load
7-
# path to prevent the real minitest from being loaded.
8-
"$LOAD_PATH.unshift '#{fake_minitest}'", 'require "rspec/core"', 'RSpec::Core::Runner.disable_autorun!' do
9-
10-
pending_when = {
11-
'1.9.2' => { :description => "issues no warnings when loaded" },
12-
'1.8.7' => { :description => "issues no warnings when the spec files are loaded" },
13-
'2.0.0' => { }
14-
}
15-
16-
if RUBY_VERSION == '1.9.2' || RUBY_VERSION == '1.8.7'
17-
before(:example, pending_when.fetch(RUBY_VERSION)) do
4+
fake_libs = File.expand_path('../../support/fake_libs', __FILE__)
5+
allowed_loaded_features = [
6+
/optparse\.rb/, # Used by OptionParser.
7+
/set\.rb/, # used in a few places but being removed in #1870.
8+
/rbconfig\.rb/, # loaded by rspec-support for OS detection.
9+
/shellwords\.rb/, # used by ConfigurationOptions and RakeTask.
10+
/stringio/, # Used by BaseFormatter.
11+
%r{/fake_libs/}, # ignore these, obviously
12+
]
13+
14+
# JRuby appears to not respect `--disable=gem` so rubygems also gets loaded.
15+
allowed_loaded_features << /rubygems/ if RSpec::Support::Ruby.jruby?
16+
17+
it_behaves_like 'library wide checks', 'rspec-core',
18+
:preamble_for_lib => [
19+
# rspec-core loads a number of external libraries. We don't want them loaded
20+
# as part of loading all of rspec-core for these specs, for a few reasons:
21+
#
22+
# * Some external libraries issue warnings, which we can't do anything about.
23+
# Since we are trying to prevent _any_ warnings from loading RSpec, it's
24+
# easiest to avoid loading those libraries entirely.
25+
# * Some external libraries load many stdlibs. Here we allow a known set of
26+
# directly loaded stdlibs, and we're not directly concerned with transitive
27+
# dependencies.
28+
# * We're really only concerned with these issues w.r.t. rspec-mocks and
29+
# rspec-expectations from within their spec suites. Here we care only about
30+
# rspec-core, so avoiding loading them helps keep the spec suites independent.
31+
# * These are some of the slowest specs we have, and cutting out the loading
32+
# of external libraries cuts down on how long these specs take.
33+
#
34+
# To facilitate the avoidance of loading certain libraries, we have a bunch
35+
# of files in `support/fake_libs` that substitute for the real things when
36+
# we put that directory on the load path. Here's the list:
37+
#
38+
# * coderay -- loaded by the HTML formatter if availble for syntax highlighting.
39+
# * drb -- loaded when `--drb` is used. Loads other stdlibs (socket, thread, fcntl).
40+
# * erb -- loaded by `ConfigurationOptions` so `.rspec` can use ERB. Loads other stdlibs (strscan, cgi/util).
41+
# * flexmock -- loaded by our Flexmock mocking adapter.
42+
# * json -- loaded by the JSON formatter, loads other stdlibs (ostruct, enc/utf_16le.bundle, etc).
43+
# * minitest -- loaded by our Minitest assertions adapter.
44+
# * mocha -- loaded by our Mocha mocking adapter.
45+
# * rake -- loaded by our Rake task. Loads other stdlibs (fileutils, ostruct, thread, monitor, etc).
46+
# * rr -- loaded by our RR mocking adapter.
47+
# * rspec-mocks -- loaded by our RSpec mocking adapter.
48+
# * rspec-expectations -- loaded by the generated `spec_helper` (defined in project_init).
49+
# * test-unit -- loaded by our T::U assertions adapter.
50+
#
51+
"$LOAD_PATH.unshift '#{fake_libs}'",
52+
# Many files assume this has already been loaded and will have errors if it has not.
53+
'require "rspec/core"',
54+
# Prevent rspec/autorun from trying to run RSpec.
55+
'RSpec::Core::Runner.disable_autorun!'
56+
], :skip_spec_files => %r{/fake_libs/}, :allowed_loaded_feature_regexps => allowed_loaded_features do
57+
if RUBY_VERSION == '1.8.7'
58+
before(:example, :description => /(issues no warnings when the spec files are loaded|stdlibs)/) do
1859
pending "Not working on #{RUBY_DESCRIPTION}"
1960
end
20-
end
21-
if (RUBY_PLATFORM == 'java' && RUBY_VERSION == '2.0.0')
22-
before(:example, pending_when.fetch(RUBY_VERSION)) do
61+
elsif RUBY_VERSION == '2.0.0' && RSpec::Support::Ruby.jruby?
62+
before(:example) do
2363
skip "Not reliably working on #{RUBY_DESCRIPTION}"
2464
end
2565
end

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def self.new(*args, &block)
2222
end
2323

2424
Dir['./spec/support/**/*.rb'].map do |file|
25+
# fake libs aren't intended to be loaded except by some specific specs
26+
# that shell out and run a new process.
27+
next if file =~ /fake_libs/
28+
2529
# Ensure requires are relative to `spec`, which is on the
2630
# load path. This helps prevent double requires on 1.8.7.
2731
require file.gsub("./spec/support", "support")

spec/support/fake_libs/drb/drb.rb

Whitespace-only changes.

spec/support/fake_libs/erb.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ERB
2+
module Util
3+
end
4+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module FlexMock
2+
module MockContainer
3+
end
4+
end

spec/support/fake_libs/json.rb

Whitespace-only changes.

spec/support/fake_libs/minitest.rb

Whitespace-only changes.

spec/support/fake_libs/mocha/api.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Mocha
2+
module API
3+
end
4+
end

spec/support/fake_libs/rake.rb

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Rake
2+
class TaskLib
3+
end
4+
end

spec/support/fake_libs/rr.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module RR
2+
module Errors
3+
BACKTRACE_IDENTIFIER = /doesn't matter/
4+
end
5+
6+
module Extensions
7+
module InstanceMethods
8+
end
9+
end
10+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module RSpec
2+
module Expectations
3+
end
4+
5+
module Matchers
6+
def self.configuration; RSpec::Core::NullReporter; end
7+
end
8+
end

spec/support/fake_libs/rspec/mocks.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module RSpec
2+
module Mocks
3+
module ExampleMethods
4+
end
5+
6+
def self.configuration; RSpec::Core::NullReporter; end
7+
end
8+
end

0 commit comments

Comments
 (0)