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

Prevent invalid encoding for files blowing up Source.from_file #364

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ jobs:
JRUBY_OPTS: ${{ matrix.container.jruby_opts || '--dev' }}
NO_COVERAGE: true
steps:
- uses: actions/checkout@v3
- run: git config --global --add safe.directory $GITHUB_WORKSPACE
- run: git init $GITHUB_WORKSPACE
- run: git remote add origin https://github.com/rspec/rspec-support
- run: git config --local gc.auto 0
- run: git fetch --no-tags --prune --no-recurse-submodules --depth=1 origin +$GITHUB_SHA:$GITHUB_REF
- run: git checkout --progress --force $GITHUB_REF
- run: ${{ matrix.container.pre }}
- run: script/legacy_setup.sh
- run: ${{ matrix.container.post }}
Expand Down
14 changes: 11 additions & 3 deletions lib/rspec/support/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ class Source
# stubbed out within tests.
class File
class << self
[:read, :expand_path].each do |method_name|
define_method(method_name, &::File.method(method_name))
define_method(:expand_path, &::File.method(:expand_path))

if RUBY_VERSION.to_f > 1.9
define_method(:binread, &::File.method(:binread))
else
define_method(:binread, &::File.method(:read))
end
end
end

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

Expand Down
8 changes: 8 additions & 0 deletions spec/support/source_broken_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Encoding.default_internal = Encoding::BINARY

describe UndeclaredModule do
# the missing constant can be anything
it 'crashes and does not even parse this' do
'привет'
end
end
Loading