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

Allow File.read to be stubbed out without breaking ripper support #431

Merged
merged 1 commit into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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