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

Commit 327852f

Browse files
authored
Merge pull request #431 from rspec/fix-file-read-stub
Allow File.read to be stubbed out without breaking ripper support
2 parents e16dbca + d18bd38 commit 327852f

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/rspec/support/source.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ module Support
88
class Source
99
attr_reader :source, :path
1010

11+
# This class protects us against having File read and expand_path
12+
# stubbed out within tests.
13+
class File
14+
class << self
15+
[:read, :expand_path].each do |method_name|
16+
define_method(method_name, &::File.method(method_name))
17+
end
18+
end
19+
end
20+
1121
def self.from_file(path)
1222
source = File.read(path)
1323
new(source, path)

spec/rspec/support/source_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ module RSpec::Support
4141
expect(source.path).not_to eq(path)
4242
expect(source.path).to end_with(path)
4343
end
44+
45+
it 'continues to work if File.read is stubbed' do
46+
allow(::File).to receive(:read).and_raise
47+
expect(source.lines.first).to eq('2.times do')
48+
end
4449
end
4550

4651
describe '#lines' do

0 commit comments

Comments
 (0)