Skip to content

Commit

Permalink
feat: Increase compatibility of the require patch
Browse files Browse the repository at this point in the history
The previous patch did not declare the overwritten require method as
module_function. This led to subtle changes when calling
respond_to?(:require) on object later, which could return true when it
should not.

This commit changes the code to use module_function. It further moves
the loader into a module that is prepended, to avoid the addition of
method aliases.
  • Loading branch information
jgraichen committed Dec 13, 2024
1 parent eb6e922 commit f8c1441
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Added

- Increase compatibility of Kernel#require patch

## [2.0.0] - 2024-02-29

### Removed
Expand Down
20 changes: 9 additions & 11 deletions lib/mnemosyne/probes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@ def registry
@registry ||= ::Mnemosyne::Registry.new
end
end
end
end

module Kernel
alias require_without_mn require
module Loader
module_function

def require(name)
ret = require_without_mn(name)

begin
::Mnemosyne::Probes.required(name)
rescue Exception # rubocop:disable Lint/RescueException,Lint/SuppressedException
def require(name)
super(name).tap do
::Mnemosyne::Probes.required(name)
rescue Exception # rubocop:disable Lint/RescueException,Lint/SuppressedException
end
end
end

ret
::Kernel.prepend(Loader)
end
end
14 changes: 14 additions & 0 deletions spec/mnemosyne/probes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mnemosyne::Probes do
it 'injects the loader into Kernel#require' do
expect(Mnemosyne::Probes).to receive(:required).with('mnemosyne/probes')
require 'mnemosyne/probes'
end

it 'does not make objects respond to require' do
expect(Object.new).not_to respond_to(:require)
end
end

0 comments on commit f8c1441

Please sign in to comment.