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

Commit 973ba24

Browse files
committed
Prevent invalid encoding for files blowing up
1 parent c316afd commit 973ba24

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/rspec/support/source.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@ class Source
1414
# stubbed out within tests.
1515
class File
1616
class << self
17-
[:read, :expand_path].each do |method_name|
18-
define_method(method_name, &::File.method(method_name))
17+
define_method(:expand_path, &::File.method(:expand_path))
18+
19+
if RUBY_VERSION.to_f > 1.9
20+
define_method(:binread, &::File.method(:binread))
21+
else
22+
define_method(:binread, &::File.method(:read))
1923
end
2024
end
2125
end
2226

2327
def self.from_file(path)
24-
source = File.read(path)
28+
# We must use `binread` here, there is no spec for this behaviour
29+
# as its proven troublesome to replicate within our spec suite, but
30+
# to manually verify run:
31+
# `bundle exec rspec spec/support/source_broken_example`
32+
source = File.binread(path)
2533
new(source, path)
2634
end
2735

spec/support/source_broken_example

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Encoding.default_internal = Encoding::BINARY
2+
3+
describe UndeclaredModule do
4+
# the missing constant can be anything
5+
it 'crashes and does not even parse this' do
6+
'привет'
7+
end
8+
end

0 commit comments

Comments
 (0)