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 #431 from rspec/fix-file-read-stub
Browse files Browse the repository at this point in the history
Allow File.read to be stubbed out without breaking ripper support
  • Loading branch information
pirj authored and JonRowe committed Oct 23, 2020
1 parent babc386 commit bccb1b7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/rspec/support/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ module Support
class Source
attr_reader :source, :path

# This class protects us against having File read and expand_path
# stubbed out within tests.
class File
class << self
[:read, :expand_path].each do |method_name|
define_method(method_name, &::File.method(method_name))
end
end
end

def self.from_file(path)
source = File.read(path)
new(source, path)
Expand Down
5 changes: 5 additions & 0 deletions spec/rspec/support/source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ module RSpec::Support
expect(source.path).not_to eq(path)
expect(source.path).to end_with(path)
end

it 'continues to work if File.read is stubbed' do
allow(::File).to receive(:read).and_raise
expect(source.lines.first).to eq('2.times do')
end
end

describe '#lines' do
Expand Down

0 comments on commit bccb1b7

Please sign in to comment.