-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix: Faker::Config.lazy_loading configuration must be respected (WIP)
#3256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thdaraujo
wants to merge
5
commits into
main
Choose a base branch
from
ta/fix-lazy-loading-bug
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+279
−46
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c97a688
fix: lazy loading
thdaraujo d754091
fix: private method call
thdaraujo 7def9ec
refactor: extract loading strategies to new class
thdaraujo f1db978
test: `Faker::Loader` tests
thdaraujo e00e385
fix: private method error on `resolve_const`
thdaraujo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Faker | ||
| class Loader | ||
| INFLECTIONS = { 'DnD' => 'dnd' }.freeze | ||
|
|
||
| def initialize(base_dir, config) | ||
| @base_dir = base_dir | ||
| @config = config | ||
| @eager_loaded = false | ||
| @mutex = Mutex.new | ||
| end | ||
|
|
||
| def load_const(context_name, class_name) | ||
| @mutex.synchronize do | ||
| if loading_strategy == :lazy | ||
| resolve_const(context_name, class_name) | ||
| else | ||
| eager_load! | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def install_on(klass) | ||
| loader = self | ||
|
|
||
| klass.define_singleton_method(:const_missing) do |class_name| | ||
| loader.resolve_const(name, class_name) | ||
|
|
||
| const_get(class_name) | ||
| end | ||
| end | ||
|
|
||
| def loading_strategy | ||
| @loading_strategy ||= if @config.lazy_loading? | ||
| :lazy | ||
| else | ||
| :eager | ||
| end | ||
| end | ||
|
|
||
| def resolve_const(context_name, class_name) | ||
| load_path = build_path(context_name, class_name) | ||
|
|
||
| require load_path | ||
| rescue LoadError | ||
| # try to load default generators | ||
| require load_path.gsub('faker/', 'faker/default/') | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def eager_load! | ||
| return if @eager_loaded | ||
|
|
||
| @eager_loaded = true | ||
|
|
||
| paths = [ | ||
| "#{@base_dir}/faker/*.rb", | ||
| "#{@base_dir}/faker/**/*.rb" | ||
| ] | ||
|
|
||
| Dir.glob(paths).uniq.each { |f| require f } | ||
| end | ||
|
|
||
| def build_path(*constants) | ||
| constants.map do |c| | ||
| INFLECTIONS | ||
| .reduce(c.to_s) { |s, (word, replacement)| s.gsub(word, replacement) } | ||
| .gsub('::', '/') | ||
| .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') | ||
| .gsub(/([a-z\d])([A-Z])/, '\1_\2') | ||
| .tr('-', '_') | ||
| .downcase | ||
| end.join('/') | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative '../test_helper' | ||
|
|
||
| class TestLoader < Test::Unit::TestCase | ||
| FIXTURES_DIR = File.expand_path('../fixtures', __dir__) | ||
|
|
||
| FakeConfig = Struct.new(:lazy_loading?) | ||
|
|
||
| def eager_loader = Faker::Loader.new(FIXTURES_DIR, FakeConfig.new(false)) | ||
| def lazy_loader = Faker::Loader.new(FIXTURES_DIR, FakeConfig.new(true)) | ||
|
|
||
| def with_require_spy(fail_if: nil) | ||
| original = Kernel.instance_method(:require) | ||
| loaded_files = [] | ||
| mutex = Mutex.new | ||
|
|
||
| Kernel.define_method(:require) do |f| | ||
| if fail_if&.call(f) | ||
| raise LoadError | ||
| end | ||
|
|
||
| mutex.synchronize { loaded_files << f } | ||
| end | ||
|
|
||
| yield loaded_files | ||
| ensure | ||
| Kernel.define_method(:require, original) | ||
| end | ||
|
|
||
| def test_strategy_does_not_change_after_first_use | ||
| config = FakeConfig.new(false) | ||
| loader = Faker::Loader.new(FIXTURES_DIR, config) | ||
|
|
||
| with_require_spy do | ||
| loader.load_const('Faker', :Gadget) | ||
|
|
||
| config[:lazy_loading?] = true | ||
|
|
||
| assert_equal :eager, loader.loading_strategy | ||
| end | ||
| end | ||
|
|
||
| def test_falls_back_to_default_path_on_load_error | ||
| loader = lazy_loader | ||
|
|
||
| fail_when_requiring_non_default_path = ->(f) { f.end_with?('faker/gadget') } | ||
|
|
||
| with_require_spy(fail_if: fail_when_requiring_non_default_path) do |loaded_files| | ||
| loader.load_const('Faker', :Gadget) | ||
|
|
||
| assert loaded_files.any? { |f| f.include?('faker/default/gadget') } | ||
| end | ||
| end | ||
|
|
||
| def test_inflection_resolves_correctly | ||
| loader = lazy_loader | ||
|
|
||
| with_require_spy do |loaded_files| | ||
| loader.load_const('Faker::Games', :DnD) | ||
|
|
||
| assert_includes loaded_files.first, 'faker/games/dnd' | ||
| refute_includes loaded_files.first, 'dn_d' | ||
| end | ||
| end | ||
|
|
||
| def test_install_on_installs_const_missing | ||
| loader = lazy_loader | ||
| klass = Class.new | ||
|
|
||
| loader.install_on(klass) | ||
|
|
||
| assert_equal klass.singleton_class, klass.method(:const_missing).owner | ||
| end | ||
|
|
||
| def test_eager_loads_all_files_on_first_const_access | ||
| loader = eager_loader | ||
|
|
||
| with_require_spy do |loaded_files| | ||
| loader.load_const('Faker', :Gadget) | ||
|
|
||
| actual_files = loaded_files.map do |loaded| | ||
| loaded.match(/fixtures\/(?<path>.*)/)[:path] | ||
| end.uniq | ||
|
|
||
| expected_files = %w[ | ||
| faker/gadget.rb | ||
| faker/default/widget.rb | ||
| faker/games/dnd.rb | ||
| ] | ||
|
|
||
| expected_files.each do |file| | ||
| assert_includes actual_files, file, "expected #{file} to be loaded" | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def test_eager_loads_only_once | ||
| loader = eager_loader | ||
|
|
||
| with_require_spy do |loaded_files| | ||
| loader.load_const('Faker', :Gadget) | ||
|
|
||
| count = loaded_files.size | ||
|
|
||
| loader.load_const('Faker', :Gadget) | ||
|
|
||
| assert_equal count, loaded_files.size | ||
| end | ||
| end | ||
|
|
||
| def test_lazy_loads_single_file_on_const_access | ||
| loader = lazy_loader | ||
|
|
||
| with_require_spy do |loaded_files| | ||
| loader.load_const('Faker', :Gadget) | ||
|
|
||
| assert_equal 1, loaded_files.size | ||
| assert_includes loaded_files.first, 'faker/gadget' | ||
| end | ||
| end | ||
|
|
||
| def test_eager_loads_only_once_across_threads | ||
| loader = eager_loader | ||
|
|
||
| with_require_spy do |loaded_files| | ||
| threads = 10.times.map do | ||
| Thread.new { loader.load_const('Faker', :Gadget) } | ||
| end | ||
|
|
||
| threads.each(&:join) | ||
|
|
||
| actual_files = loaded_files.map do |loaded| | ||
| loaded.match(/fixtures\/(?<path>.*)/)[:path] | ||
| end.compact | ||
|
|
||
| assert_equal actual_files.uniq, actual_files | ||
| end | ||
| end | ||
|
|
||
| def test_raises_on_unknown_const | ||
| loader = lazy_loader | ||
|
|
||
| assert_raises(LoadError) { loader.load_const('Faker', :NonExistent) } | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Faker | ||
| class Default | ||
| # rubocop:disable Lint/EmptyClass | ||
| class Widget | ||
| end | ||
| # rubocop:enable Lint/EmptyClass | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Faker | ||
| # rubocop:disable Lint/EmptyClass | ||
| class Gadget | ||
| end | ||
| # rubocop:enable Lint/EmptyClass | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Faker | ||
| class Games | ||
| # rubocop:disable Lint/EmptyClass | ||
| class DnD | ||
| end | ||
| # rubocop:enable Lint/EmptyClass | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stub
Kernel#requireto spy on files being required/loadedUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe I could pass
requireorKernelvia dependency injection to make it easier to test :/It would also get rid of the warnings.
Could be a future improvement!