pax_global_header00006660000000000000000000000064137602046400014514gustar00rootroot0000000000000052 comment=2a3d73ba81e1b671fa943f63a36e35586da30ce2 zeitwerk-2.4.2/000077500000000000000000000000001376020464000133655ustar00rootroot00000000000000zeitwerk-2.4.2/.gitignore000066400000000000000000000000341376020464000153520ustar00rootroot00000000000000Gemfile.lock test/tmp *.gem zeitwerk-2.4.2/.travis.yml000066400000000000000000000003071376020464000154760ustar00rootroot00000000000000language: ruby rvm: - 2.4.4 - 2.4 - 2.5 - 2.6 - 2.7 - ruby-head - jruby-head - truffleruby - truffleruby-head matrix: allow_failures: - rvm: jruby-head - rvm: truffleruby zeitwerk-2.4.2/CHANGELOG.md000066400000000000000000000216541376020464000152060ustar00rootroot00000000000000# CHANGELOG ## 2.4.2 (27 November 2020) * Implements `Zeitwerk::Loader#on_load`, which allows you to configure blocks of code to be executed after a certain class or module have been loaded: ```ruby # config/environments/development.rb loader.on_load("SomeApiClient") do SomeApiClient.endpoint = "https://api.dev" end # config/environments/production.rb loader.on_load("SomeApiClient") do SomeApiClient.endpoint = "https://api.prod" end ``` See the [documentation](https://github.com/fxn/zeitwerk/blob/master/README.md#the-on_load-callback) for further details. ## 2.4.1 (29 October 2020) * Use `__send__` instead of `send` internally. ## 2.4.0 (15 July 2020) * `Zeitwerk::Loader#push_dir` supports an optional `namespace` keyword argument. Pass a class or module object if you want the given root directory to be associated with it instead of `Object`. Said class or module object cannot be reloadable. * The default inflector is even more performant. ## 2.3.1 (29 June 2020) * Saves some unnecessary allocations made internally by MRI. See [#125](https://github.com/fxn/zeitwerk/pull/125), by [@casperisfine](https://github.com/casperisfine). * Documentation improvements. * Internal code base maintenance. ## 2.3.0 (3 March 2020) * Adds support for collapsing directories. For example, if `booking/actions/create.rb` is meant to define `Booking::Create` because the subdirectory `actions` is there only for organizational purposes, you can tell Zeitwerk with `collapse`: ```ruby loader.collapse("booking/actions") ``` The method also accepts glob patterns to support standardized project structures: ```ruby loader.collapse("*/actions") ``` Please check the documentation for more details. * Eager loading is idempotent, but now you can eager load again after reloading. ## 2.2.2 (29 November 2019) * `Zeitwerk::NameError#name` has the name of the missing constant now. ## 2.2.1 (1 November 2019) * Zeitwerk raised `NameError` when a managed file did not define its expected constant. Now, it raises `Zeitwerk::NameError` instead, so it is possible for client code to distinguish that mismatch from a regular `NameError`. Regarding backwards compatibility, `Zeitwerk::NameError` is a subclass of `NameError`. ## 2.2.0 (9 October 2019) * The default inflectors have API to override how to camelize selected basenames: ```ruby loader.inflector.inflect "mysql_adapter" => "MySQLAdapter" ``` This addresses a common pattern, which is to use the basic inflectors with a few straightforward exceptions typically configured in a hash table or `case` expression. You no longer have to define a custom inflector if that is all you need. * Documentation improvements. ## 2.1.10 (6 September 2019) * Raises `Zeitwerk::NameError` with a better error message when a managed file or directory has a name that yields an invalid constant name when inflected. `Zeitwerk::NameError` is a subclass of `NameError`. ## 2.1.9 (16 July 2019) * Preloading is soft-deprecated. The use case it was thought for is no longer. Please, if you have a legit use case for it, drop me a line. * Root directory conflict detection among loaders takes ignored directories into account. * Supports classes and modules with overridden `name` methods. * Documentation improvements. ## 2.1.8 (29 June 2019) * Fixes eager loading nested root directories. The new approach in 2.1.7 introduced a regression. ## 2.1.7 (29 June 2019) * Prevent the inflector from deleting parts un multiword constants whose capitalization is the same. For example, `point_2d` should be inflected as `Point2d`, rather than `Point`. While the inflector is frozen, this seems to be just wrong, and the refinement should be backwards compatible, since those constants were not usable. * Make eager loading consistent with auto loading with regard to detecting namespaces that do not define the matching constant. * Documentation improvements. ## 2.1.6 (30 April 2019) * Fixed: If an eager load exclusion contained an autoload for a namespace also present in other branches that had to be eager loaded, they could be skipped. * `loader.log!` is a convenient shortcut to get traces to `$stdout`. * Allocates less strings. ## 2.1.5 (24 April 2019) * Failed autoloads raise `NameError` as always, but with a more user-friendly message instead of the original generic one from Ruby. * Eager loading uses `const_get` now rather than `require`. A file that does not define the expected constant could be eager loaded, but not autoloaded, which would be inconsistent. Thanks to @casperisfine for reporting this one and help testing the alternative. ## 2.1.4 (23 April 2019) * Supports deletion of root directories in disk after they've been configured. `push_dir` requires root directories to exist to prevent misconfigurations, but after that Zeitwerk no longer assumes they exist. This might be convenient if you removed one in a web application while a server was running. ## 2.1.3 (22 April 2019) * Documentation improvements. * Internal work. ## 2.1.2 (11 April 2019) * Calling `reload` with reloading disabled raises `Zeitwerk::ReloadingDisabledError`. ## 2.1.1 (10 April 2019) * Internal performance work. ## 2.1.0 (9 April 2019) * `loaded_cpaths` is gone, you can ask if a constant path is going to be unloaded instead with `loader.to_unload?(cpath)`. Thanks to this refinement, Zeitwerk is able to consume even less memory. (Change included in a minor upgrade because the introspection API is not documented, and it still isn't, needs some time to settle down). ## 2.0.0 (7 April 2019) * Reloading is disabled by default. In order to be able to reload you need to opt-in by calling `loader.enable_reloading` before setup. The motivation for this breaking change is twofold. On one hand, this is a design decision at the interface/usage level that reflects that the majority of use cases for Zeitwerk do not need reloading. On the other hand, if reloading is not enabled, Zeitwerk is able to use less memory. Notably, this is more optimal for large web applications in production. ## 1.4.3 (26 March 2019) * Faster reload. If you're using `bootsnap`, requires at least version 1.4.2. ## 1.4.2 (23 March 2019) * Includes an optimization. ## 1.4.1 (23 March 2019) * Fixes concurrent autovivifications. ## 1.4.0 (19 March 2019) * Trace point optimization for singleton classes by @casperisfine. See the use case, explanation, and patch in [#24](https://github.com/fxn/zeitwerk/pull/24). * `Zeitwerk::Loader#do_not_eager_load` provides a way to have autoloadable files and directories that should be skipped when eager loading. ## 1.3.4 (14 March 2019) * Files shadowed by previous occurrences defining the same constant path were being correctly skipped when autoloading, but not when eager loading. This has been fixed. This mimicks what happens when there are two files in `$LOAD_PATH` with the same relative name, only the first one is loaded by `require`. ## 1.3.3 (12 March 2019) * Bug fix by @casperisfine: If the superclass or one of the ancestors of an explicit namespace `N` has an autoload set for constant `C`, and `n/c.rb` exists, the autoload for `N::C` proper could be missed. ## 1.3.2 (6 March 2019) * Improved documentation. * Zeitwerk creates at most one trace point per process, instead of one per loader. This is more performant when there are multiple gems managed by Zeitwerk. ## 1.3.1 (23 February 2019) * After module vivification, the tracer could trigger one unnecessary autoload walk. ## 1.3.0 (21 February 2019) * In addition to callables, loggers can now also be any object that responds to `debug`, which accepts one string argument. ## 1.2.0 (14 February 2019) * Use `pretty_print` in the exception message for conflicting directories. ## 1.2.0.beta (14 February 2019) * Two different loaders cannot be managing the same files. Now, `Zeitwerk::Loader#push_dir` raises `Zeitwerk::ConflictingDirectory` if it detects a conflict. ## 1.1.0 (14 February 2019) * New class attribute `Zeitwerk::Loader.default_logger`, inherited by newly instantiated loaders. Default is `nil`. * Traces include the loader tag in the prefix to easily distinguish them. * Loaders now have a tag. ## 1.0.0 (12 February 2019) * Documentation improvements. ## 1.0.0.beta3 (4 February 2019) * Documentation improvements. * `Zeitwerk::Loader#ignore` accepts glob patterns. * New read-only introspection method `Zeitwerk::Loader.all_dirs`. * New read-only introspection method `Zeitwerk::Loader#dirs`. * New introspection predicate `Zeitwerk::Loader#loaded?(cpath)`. ## 1.0.0.beta2 (22 January 2019) * `do_not_eager_load` has been removed, please use `ignore` to opt-out. * Documentation improvements. * Pronunciation section in the README, linking to sample audio file. * All logged messages have a "Zeitwerk:" prefix for easy grepping. * On reload, the logger also traces constants and autoloads removed. ## 1.0.0.beta (18 January 2019) * Initial beta release. zeitwerk-2.4.2/Gemfile000066400000000000000000000001601376020464000146550ustar00rootroot00000000000000source 'https://rubygems.org' gemspec gem "rake" gem "minitest" gem "minitest-focus" gem "minitest-reporters" zeitwerk-2.4.2/MIT-LICENSE000066400000000000000000000020451376020464000150220ustar00rootroot00000000000000Copyright (c) 2019–ω Xavier Noria Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. zeitwerk-2.4.2/PROJECT_RULES.md000066400000000000000000000102371376020464000157720ustar00rootroot00000000000000# Project rules ## Notation It is very important that all the source code uses systematically the following naming conventions. ### Variables for constants * `cname`: A constant name, for example `:User`. Must be a symbol. * `cpath`: A constant path, for example `"User"` or `"Hotel::Pricing"`. Must be a string. * `cref`: A constant reference represented as an array of two elements. The first one a class or module object, and the second one a constant name as a symbol. For example `[Admin, :UsersController]`. ### Variables for paths You should pick always the most specific option: * `file`: Absolute path of a file. * `dir`: Absolute path of a directory. * `abspath`: Absolute path of a file or directory. * `realpath`: Absolute real path of a file or directory. Note that Zeitwerk does not deal with file or directory objects, only with paths. For brevity, we exploit this fact to adopt the convention `file`/`dir` instead of `filename`/`dirname` or somesuch. ## Paths * The only relative file names allowed in the project come from users. For example, public methods like `push_dir` should understand relative paths. * As soon as a relative file name comes from outside, it has to be converted to an absolute file name right away. * Internally, you have to use exclusively absolute file names. In particular, any `autoload` or `require` calls have to be issued using absolute paths to avoid `$LOAD_PATH` walks. * It is forbidden to do any sort of directory lookups resolving relative file names. * The only directory walks allowed are the one needed to set autoloads. One pass, and as lazy as possible (do not descend into subdirectories until necessary). * File and directory names should be kept as entered as much as possible so that logging prints what the user expects. Convert to real paths only in code that needs coordination with `Kernel#require`. ## Class and module names * Classes and modules may override the `name` method, therefore we cannot assume it returns their original constant path. Always use the helper `real_mod_name` on classes and modules coming from the user. ## Types * All methods should have a documented signature. * Use the most concise type always. Use a set when a set is the best choice, use `Module` when a class or module object is the natural data type (rather than its name). * Use always symbols for constant names. * Use always strings for constant paths. * Use always strings for paths, not pathnames. Pathnames are only accepted coming from the user, but internally everything is strings. ## Public interface definition Documented public methods conform the public interface. In particular: * Public methods tagged as `@private` do not belong to the public interface. * Undocumented public methods do not belong to the public interface. They are probably exploratory and may change or be deleted without warning. These are private interface in practice. * Undocumented public methods can be used in the Rails integration. We control both repositories, and Rails usage may help refine the actual public interface. Any release can change the private interface, including patch releases. ## Documentation Try to word the documentation in terms of classes, modules, and namespaces. Do that with extra care to avoid introducing leaking metaphors. We sacrifice there a bit of precision in order to communicate better. Some Ruby programmers do not have a deep understanding of constants, so better avoid being pedantic for didactic purposes. Those in the know understand what the documentation really says. ## Performance Zeitwerk is infrastructure, should have minimal cost both in speed and memory usage. Be extra careful, allocate as less as possible, store as less as possible. Use always absolute file names for `autoload` and `require`. Log always using this pattern: ```ruby log(message) if logger ``` to avoid unnecessary calls, and unnecessary computed values in the message. Some projects may have hundreds of root directories and hundreds of thousands of files, please remember that. However, do not write ugly code. Ugly code should be extremely justified in terms of performance. Instead, keep it simple, write simple performant code that reads well and is idiomatic. zeitwerk-2.4.2/README.md000066400000000000000000001000401376020464000146370ustar00rootroot00000000000000# Zeitwerk [![Gem Version](https://img.shields.io/gem/v/zeitwerk.svg?style=for-the-badge)](https://rubygems.org/gems/zeitwerk) [![Build Status](https://img.shields.io/travis/com/fxn/zeitwerk/master?style=for-the-badge)](https://travis-ci.com/fxn/zeitwerk) - [Introduction](#introduction) - [Synopsis](#synopsis) - [File structure](#file-structure) - [Implicit namespaces](#implicit-namespaces) - [Explicit namespaces](#explicit-namespaces) - [Collapsing directories](#collapsing-directories) - [Nested root directories](#nested-root-directories) - [Usage](#usage) - [Setup](#setup) - [Generic](#generic) - [for_gem](#for_gem) - [Autoloading](#autoloading) - [Eager loading](#eager-loading) - [Reloading](#reloading) - [Inflection](#inflection) - [Zeitwerk::Inflector](#zeitwerkinflector) - [Zeitwerk::GemInflector](#zeitwerkgeminflector) - [Custom inflector](#custom-inflector) - [The on_load callback](#the-on_load-callback) - [Logging](#logging) - [Loader tag](#loader-tag) - [Ignoring parts of the project](#ignoring-parts-of-the-project) - [Use case: Files that do not follow the conventions](#use-case-files-that-do-not-follow-the-conventions) - [Use case: The adapter pattern](#use-case-the-adapter-pattern) - [Use case: Test files mixed with implementation files](#use-case-test-files-mixed-with-implementation-files) - [Edge cases](#edge-cases) - [Reopening third-party namespaces](#reopening-third-party-namespaces) - [Rules of thumb](#rules-of-thumb) - [Debuggers](#debuggers) - [Break](#break) - [Byebug](#byebug) - [Pronunciation](#pronunciation) - [Supported Ruby versions](#supported-ruby-versions) - [Testing](#testing) - [Motivation](#motivation) - [Thanks](#thanks) - [License](#license) ## Introduction Zeitwerk is an efficient and thread-safe code loader for Ruby. Given a [conventional file structure](#file-structure), Zeitwerk is able to load your project's classes and modules on demand (autoloading), or upfront (eager loading). You don't need to write `require` calls for your own files, rather, you can streamline your programming knowing that your classes and modules are available everywhere. This feature is efficient, thread-safe, and matches Ruby's semantics for constants. Zeitwerk is also able to reload code, which may be handy while developing web applications. Coordination is needed to reload in a thread-safe manner. The documentation below explains how to do this. The gem is designed so that any project, gem dependency, application, etc. can have their own independent loader, coexisting in the same process, managing their own project trees, and independent of each other. Each loader has its own configuration, inflector, and optional logger. Internally, Zeitwerk issues `require` calls exclusively using absolute file names, so there are no costly file system lookups in `$LOAD_PATH`. Technically, the directories managed by Zeitwerk do not even need to be in `$LOAD_PATH`. Furthermore, Zeitwerk does at most one single scan of the project tree, and it descends into subdirectories lazily, only if their namespaces are used. ## Synopsis Main interface for gems: ```ruby # lib/my_gem.rb (main file) require "zeitwerk" loader = Zeitwerk::Loader.for_gem loader.setup # ready! module MyGem # ... end loader.eager_load # optionally ``` Main generic interface: ```ruby loader = Zeitwerk::Loader.new loader.push_dir(...) loader.setup # ready! ``` The `loader` variable can go out of scope. Zeitwerk keeps a registry with all of them, and so the object won't be garbage collected. You can reload if you want to: ```ruby loader = Zeitwerk::Loader.new loader.push_dir(...) loader.enable_reloading # you need to opt-in before setup loader.setup ... loader.reload ``` and you can eager load all the code: ```ruby loader.eager_load ``` It is also possible to broadcast `eager_load` to all instances: ```ruby Zeitwerk::Loader.eager_load_all ``` ## File structure To have a file structure Zeitwerk can work with, just name files and directories after the name of the classes and modules they define: ``` lib/my_gem.rb -> MyGem lib/my_gem/foo.rb -> MyGem::Foo lib/my_gem/bar_baz.rb -> MyGem::BarBaz lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo ``` Every directory configured with `push_dir` acts as root namespace. There can be several of them. For example, given ```ruby loader.push_dir(Rails.root.join("app/models")) loader.push_dir(Rails.root.join("app/controllers")) ``` Zeitwerk understands that their respective files and subdirectories belong to the root namespace: ``` app/models/user.rb -> User app/controllers/admin/users_controller.rb -> Admin::UsersController ``` Alternatively, you can associate a custom namespace to a root directory by passing a class or module object in the optional `namespace` keyword argument. For example, Active Job queue adapters have to define a constant after their name in `ActiveJob::QueueAdapters`. So, if you declare ```ruby require "active_job" require "active_job/queue_adapters" loader.push_dir("#{__dir__}/adapters", namespace: ActiveJob::QueueAdapters) ``` your adapter can be stored directly in that directory instead of the canonical `#{__dir__}/active_job/queue_adapters`. Please, note that the given namespace must be non-reloadable, though autoloaded constants in that namespace can be. That is, if you associate `app/api` with an existing `Api` module, that module should not be reloadable. However, if the project defines and autoloads the class `Api::V2::Deliveries`, that one can be reloaded. ### Implicit namespaces Directories without a matching Ruby file get modules autovivified automatically by Zeitwerk. For example, in ``` app/controllers/admin/users_controller.rb -> Admin::UsersController ``` `Admin` is autovivified as a module on demand, you do not need to define an `Admin` class or module in an `admin.rb` file explicitly. ### Explicit namespaces Classes and modules that act as namespaces can also be explicitly defined, though. For instance, consider ``` app/models/hotel.rb -> Hotel app/models/hotel/pricing.rb -> Hotel::Pricing ``` There, `app/models/hotel.rb` defines `Hotel`, and thus Zeitwerk does not autovivify a module. The classes and modules from the namespace are already available in the body of the class or module defining it: ```ruby class Hotel < ApplicationRecord include Pricing # works ... end ``` An explicit namespace must be managed by one single loader. Loaders that reopen namespaces owned by other projects are responsible for loading their constants before setup. ### Collapsing directories Say some directories in a project exist for organizational purposes only, and you prefer not to have them as namespaces. For example, the `actions` subdirectory in the next example is not meant to represent a namespace, it is there only to group all actions related to bookings: ``` booking.rb -> Booking booking/actions/create.rb -> Booking::Create ``` To make it work that way, configure Zeitwerk to collapse said directory: ```ruby loader.collapse("#{__dir__}/booking/actions") ``` This method accepts an arbitrary number of strings or `Pathname` objects, and also an array of them. You can pass directories and glob patterns. Glob patterns are expanded when they are added, and again on each reload. To illustrate usage of glob patterns, if `actions` in the example above is part of a standardized structure, you could use a wildcard: ```ruby loader.collapse("#{__dir__}/*/actions") ``` ### Nested root directories Root directories should not be ideally nested, but Zeitwerk supports them because in Rails, for example, both `app/models` and `app/models/concerns` belong to the autoload paths. Zeitwerk detects nested root directories, and treats them as roots only. In the example above, `concerns` is not considered to be a namespace below `app/models`. For example, the file: ``` app/models/concerns/geolocatable.rb ``` should define `Geolocatable`, not `Concerns::Geolocatable`. ## Usage ### Setup #### Generic Loaders are ready to load code right after calling `setup` on them: ```ruby loader.setup ``` This method is synchronized and idempotent. Customization should generally be done before that call. In particular, in the generic interface you may set the root directories from which you want to load files: ```ruby loader.push_dir(...) loader.push_dir(...) loader.setup ``` #### for_gem `Zeitwerk::Loader.for_gem` is a convenience shortcut for the common case in which a gem has its entry point directly under the `lib` directory: ``` lib/my_gem.rb # MyGem lib/my_gem/version.rb # MyGem::VERSION lib/my_gem/foo.rb # MyGem::Foo ``` Neither a gemspec nor a version file are technically required, this helper works as long as the code is organized using that standard structure. If the entry point of your gem lives in a subdirectory of `lib` because it is reopening a namespace defined somewhere else, please use the generic API to setup the loader, and make sure you check the section [_Reopening third-party namespaces_](https://github.com/fxn/zeitwerk#reopening-third-party-namespaces) down below. Conceptually, `for_gem` translates to: ```ruby # lib/my_gem.rb require "zeitwerk" loader = Zeitwerk::Loader.new loader.tag = File.basename(__FILE__, ".rb") loader.inflector = Zeitwerk::GemInflector.new(__FILE__) loader.push_dir(__dir__) ``` except that this method returns the same object in subsequent calls from the same file, in the unlikely case the gem wants to be able to reload. If the main module references project constants at the top-level, Zeitwerk has to be ready to load them. Their definitions, in turn, may reference other project constants. And this is recursive. Therefore, it is important that the `setup` call happens above the main module definition: ```ruby # lib/my_gem.rb (main file) require "zeitwerk" loader = Zeitwerk::Loader.for_gem loader.setup module MyGem # Since the setup has been performed, at this point we are already able # to reference project constants, in this case MyGem::MyLogger. include MyLogger end ``` ### Autoloading After `setup`, you are able to reference classes and modules from the project without issuing `require` calls for them. They are all available everywhere, autoloading loads them on demand. This works even if the reference to the class or module is first hit in client code, outside your project. Let's revisit the example above: ```ruby # lib/my_gem.rb (main file) require "zeitwerk" loader = Zeitwerk::Loader.for_gem loader.setup module MyGem include MyLogger # (*) end ``` That works, and there is no `require "my_gem/my_logger"`. When `(*)` is reached, Zeitwerk seamlessly autoloads `MyGem::MyLogger`. If autoloading a file does not define the expected class or module, Zeitwerk raises `Zeitwerk::NameError`, which is a subclass of `NameError`. ### Eager loading Zeitwerk instances are able to eager load their managed files: ```ruby loader.eager_load ``` That skips [ignored files and directories](#ignoring-parts-of-the-project), and you can also tell Zeitwerk that certain files or directories are autoloadable, but should not be eager loaded: ```ruby db_adapters = "#{__dir__}/my_gem/db_adapters" loader.do_not_eager_load(db_adapters) loader.setup loader.eager_load # won't eager load the database adapters ``` In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](https://github.com/fxn/zeitwerk#synopsis). Eager loading is synchronized and idempotent. If eager loading a file does not define the expected class or module, Zeitwerk raises `Zeitwerk::NameError`, which is a subclass of `NameError`. If you want to eager load yourself and all dependencies using Zeitwerk, you can broadcast the `eager_load` call to all instances: ```ruby Zeitwerk::Loader.eager_load_all ``` This may be handy in top-level services, like web applications. Note that thanks to idempotence `Zeitwerk::Loader.eager_load_all` won't eager load twice if any of the instances already eager loaded. ### Reloading Zeitwerk is able to reload code, but you need to enable this feature: ```ruby loader = Zeitwerk::Loader.new loader.push_dir(...) loader.enable_reloading # you need to opt-in before setup loader.setup ... loader.reload ``` There is no way to undo this, either you want to reload or you don't. Enabling reloading after setup raises `Zeitwerk::Error`. Attempting to reload without having it enabled raises `Zeitwerk::ReloadingDisabledError`. Generally speaking, reloading is useful while developing running services like web applications. Gems that implement regular libraries, so to speak, or services running in testing or production environments, won't normally have a use case for reloading. If reloading is not enabled, Zeitwerk is able to use less memory. Reloading removes the currently loaded classes and modules and resets the loader so that it will pick whatever is in the file system now. It is important to highlight that this is an instance method. Don't worry about project dependencies managed by Zeitwerk, their loaders are independent. In order for reloading to be thread-safe, you need to implement some coordination. For example, a web framework that serves each request with its own thread may have a globally accessible RW lock. When a request comes in, the framework acquires the lock for reading at the beginning, and the code in the framework that calls `loader.reload` needs to acquire the lock for writing. On reloading, client code has to update anything that would otherwise be storing a stale object. For example, if the routing layer of a web framework stores controller class objects or instances in internal structures, on reload it has to refresh them somehow, possibly reevaluating routes. ### Inflection Each individual loader needs an inflector to figure out which constant path would a given file or directory map to. Zeitwerk ships with two basic inflectors. #### Zeitwerk::Inflector This is a very basic inflector that converts snake case to camel case: ``` user -> User users_controller -> UsersController html_parser -> HtmlParser ``` The camelize logic can be overridden easily for individual basenames: ```ruby loader.inflector.inflect( "html_parser" => "HTMLParser", "mysql_adapter" => "MySQLAdapter" ) ``` The `inflect` method can be invoked several times if you prefer this other style: ```ruby loader.inflector.inflect "html_parser" => "HTMLParser" loader.inflector.inflect "mysql_adapter" => "MySQLAdapter" ``` Overrides need to be configured before calling `setup`. There are no inflection rules or global configuration that can affect this inflector. It is deterministic. Loaders instantiated with `Zeitwerk::Loader.new` have an inflector of this type, independent of each other. #### Zeitwerk::GemInflector This inflector is like the basic one, except it expects `lib/my_gem/version.rb` to define `MyGem::VERSION`. Loaders instantiated with `Zeitwerk::Loader.for_gem` have an inflector of this type, independent of each other. #### Custom inflector The inflectors that ship with Zeitwerk are deterministic and simple. But you can configure your own: ```ruby # frozen_string_literal: true class MyInflector < Zeitwerk::Inflector def camelize(basename, abspath) if basename =~ /\Ahtml_(.*)/ "HTML" + super($1, abspath) else super end end end ``` The first argument, `basename`, is a string with the basename of the file or directory to be inflected. In the case of a file, without extension. In the case of a directory, without trailing slash. The inflector needs to return this basename inflected. Therefore, a simple constant name without colons. The second argument, `abspath`, is a string with the absolute path to the file or directory in case you need it to decide how to inflect the basename. Paths to directories don't have trailing slashes. Then, assign the inflector: ```ruby loader.inflector = MyInflector.new ``` This needs to be done before calling `setup`. If a custom inflector definition in a gem takes too much space in the main file, you can extract it. For example, this is a simple pattern: ```ruby # lib/my_gem/inflector.rb module MyGem class Inflector < Zeitwerk::GemInflector ... end end # lib/my_gem.rb require "zeitwerk" require_relative "my_gem/inflector" loader = Zeitwerk::Loader.for_gem loader.inflector = MyGem::Inflector.new(__FILE__) loader.setup module MyGem # ... end ``` Since `MyGem` is referenced before the namespace is defined in the main file, it is important to use this style: ```ruby # Correct, effectively defines MyGem. module MyGem class Inflector < Zeitwerk::GemInflector # ... end end ``` instead of: ```ruby # Raises uninitialized constant MyGem (NameError). class MyGem::Inflector < Zeitwerk::GemInflector # ... end ``` ### The on_load callback The usual place to run something when a file is loaded is the file itself. However, sometimes you'd like to be called, and this is possible with the `on_load` callback. For example, let's imagine this class belongs to a Rails application: ```ruby class SomeApiClient class << self attr_accessor :endpoint end end ``` With `on_load`, it is easy to schedule code at boot time that initializes `endpoint` according to the configuration: ```ruby # config/environments/development.rb loader.on_load("SomeApiClient") do SomeApiClient.endpoint = "https://api.dev" end # config/environments/production.rb loader.on_load("SomeApiClient") do SomeApiClient.endpoint = "https://api.prod" end ``` Uses cases: * Doing something with an autoloadable class or module in a Rails application during initialization, in a way that plays well with reloading. As in the previous example. * Delaying the execution of the block until the class is loaded for performance. * Delaying the execution of the block until the class is loaded because it follows the adapter pattern and better not to load the class if the user does not need it. * Etc. However, let me stress that the easiest way to accomplish that is to write whatever you have to do in the actual target file. `on_load` use cases are edgy, use it only if appropriate. `on_load` receives the name of the target class or module as a string. The given block is executed every time its corresponding file is loaded. That includes reloads. Multiple callbacks on the same target are supported, and they run in order of definition. The block is executed once the loader has loaded the target. In particular, if the target was already loaded when the callback is defined, the block won't run. But if you reload and load the target again, then it will. Normally, you'll want to define `on_load` callbacks before `setup`. Defining a callback for a target not managed by the receiver is not an error, the block simply won't ever be executed. ### Logging Zeitwerk is silent by default, but you can ask loaders to trace their activity. Logging is meant just for troubleshooting, shouldn't normally be enabled. The `log!` method is a quick shortcut to let the loader log to `$stdout`: ``` loader.log! ``` If you want more control, a logger can be configured as a callable ```ruby loader.logger = method(:puts) loader.logger = ->(msg) { ... } ``` as well as anything that responds to `debug`: ```ruby loader.logger = Logger.new($stderr) loader.logger = Rails.logger ``` In both cases, the corresponding methods are going to be passed exactly one argument with the message to be logged. It is also possible to set a global default this way: ```ruby Zeitwerk::Loader.default_logger = method(:puts) ``` If there is a logger configured, you'll see traces when autoloads are set, files loaded, and modules autovivified. While reloading, removed autoloads and unloaded objects are also traced. As a curiosity, if your project has namespaces you'll notice in the traces Zeitwerk sets autoloads for _directories_. That's a technique used to be able to descend into subdirectories on demand, avoiding that way unnecessary tree walks. #### Loader tag Loaders have a tag that is printed in traces in order to be able to distinguish them in globally logged activity: ``` Zeitwerk@9fa54b: autoload set for User, to be loaded from ... ``` By default, a random tag like the one above is assigned, but you can change it: ``` loader.tag = "grep_me" ``` The tag of a loader returned by `for_gem` is the basename of the root file without extension: ``` Zeitwerk@my_gem: constant MyGem::Foo loaded from ... ``` ### Ignoring parts of the project Zeitwerk ignores automatically any file or directory whose name starts with a dot, and any files that do not have extension ".rb". However, sometimes it might still be convenient to tell Zeitwerk to completely ignore some particular Ruby file or directory. That is possible with `ignore`, which accepts an arbitrary number of strings or `Pathname` objects, and also an array of them. You can ignore file names, directory names, and glob patterns. Glob patterns are expanded when they are added and again on each reload. Let's see some use cases. #### Use case: Files that do not follow the conventions Let's suppose that your gem decorates something in `Kernel`: ```ruby # lib/my_gem/core_ext/kernel.rb Kernel.module_eval do # ... end ``` That file does not define a constant path after the path name and you need to tell Zeitwerk: ```ruby kernel_ext = "#{__dir__}/my_gem/core_ext/kernel.rb" loader.ignore(kernel_ext) loader.setup ``` You can also ignore the whole directory: ```ruby core_ext = "#{__dir__}/my_gem/core_ext" loader.ignore(core_ext) loader.setup ``` #### Use case: The adapter pattern Another use case for ignoring files is the adapter pattern. Let's imagine your project talks to databases, supports several, and has adapters for each one of them. Those adapters may have top-level `require` calls that load their respective drivers: ```ruby # my_gem/db_adapters/postgresql.rb require "pg" ``` but you don't want your users to install them all, only the one they are going to use. On the other hand, if your code is eager loaded by you or a parent project (with `Zeitwerk::Loader.eager_load_all`), those `require` calls are going to be executed. Ignoring the adapters prevents that: ```ruby db_adapters = "#{__dir__}/my_gem/db_adapters" loader.ignore(db_adapters) loader.setup ``` The chosen adapter, then, has to be loaded by hand somehow: ```ruby require "my_gem/db_adapters/#{config[:db_adapter]}" ``` Note that since the directory is ignored, the required adapter can instantiate another loader to manage its subtree, if desired. Such loader would coexist with the main one just fine. #### Use case: Test files mixed with implementation files There are project layouts that put implementation files and test files together. To ignore the test files, you can use a glob pattern like this: ```ruby tests = "#{__dir__}/**/*_test.rb" loader.ignore(tests) loader.setup ``` ### Edge cases A class or module that acts as a namespace: ```ruby # trip.rb class Trip include Geolocation end # trip/geolocation.rb module Trip::Geolocation ... end ``` has to be defined with the `class` or `module` keywords, as in the example above. For technical reasons, raw constant assignment is not supported: ```ruby # trip.rb Trip = Class.new { ... } # NOT SUPPORTED Trip = Struct.new { ... } # NOT SUPPORTED ``` This only affects explicit namespaces, those idioms work well for any other ordinary class or module. ### Reopening third-party namespaces Projects managed by Zeitwerk can work with namespaces defined by third-party libraries. However, they have to be loaded in memory before calling `setup`. For example, let's imagine you're writing a gem that implements an adapter for [Active Job](https://guides.rubyonrails.org/active_job_basics.html) that uses AwesomeQueue as backend. By convention, your gem has to define a class called `ActiveJob::QueueAdapters::AwesomeQueue`, and it has to do so in a file with a matching path: ```ruby # lib/active_job/queue_adapters/awesome_queue.rb module ActiveJob module QueueAdapters class AwesomeQueue # ... end end end ``` It is very important that your gem _reopens_ the modules `ActiveJob` and `ActiveJob::QueueAdapters` instead of _defining_ them. Because their proper definition lives in Active Job. Furthermore, if the project reloads, you do not want any of `ActiveJob` or `ActiveJob::QueueAdapters` to be reloaded. Bottom line, Zeitwerk should not be managing those namespaces. Active Job owns them and defines them. Your gem needs to _reopen_ them. In order to do so, you need to make sure those modules are loaded before calling `setup`. For instance, in the entry file for the gem: ```ruby # Ensure these namespaces are reopened, not defined. require "active_job" require "active_job/queue_adapters" require "zeitwerk" loader = Zeitwerk::Loader.for_gem loader.setup ``` With that, when Zeitwerk scans the file system and reaches the gem directories `lib/active_job` and `lib/active_job/queue_adapters`, it detects the corresponding modules already exist and therefore understands it does not have to manage them. The loader just descends into those directories. Eventually will reach `lib/active_job/queue_adapters/awesome_queue.rb`, and since `ActiveJob::QueueAdapters::AwesomeQueue` is unknown, Zeitwerk will manage it. Which is what happens regularly with the files in your gem. On reload, the namespaces are safe, won't be reloaded. The loader only reloads what it manages, which in this case is the adapter itself. ### Rules of thumb 1. Different loaders should manage different directory trees. It is an error condition to configure overlapping root directories in different loaders. 2. Think the mere existence of a file is effectively like writing a `require` call for them, which is executed on demand (autoload) or upfront (eager load). 3. In that line, if two loaders manage files that translate to the same constant in the same namespace, the first one wins, the rest are ignored. Similar to what happens with `require` and `$LOAD_PATH`, only the first occurrence matters. 4. Projects that reopen a namespace defined by some dependency have to ensure said namespace is loaded before setup. That is, the project has to make sure it reopens, rather than define. This is often accomplished just loading the dependency. 5. Objects stored in reloadable constants should not be cached in places that are not reloaded. For example, non-reloadable classes should not subclass a reloadable class, or mixin a reloadable module. Otherwise, after reloading, those classes or module objects would become stale. Referring to constants in dynamic places like method calls or lambdas is fine. 6. In a given process, ideally, there should be at most one loader with reloading enabled. Technically, you can have more, but it may get tricky if one refers to constants managed by the other one. Do that only if you know what you are doing. ### Debuggers #### Break Zeitwerk works fine with [@gsamokovarov](https://github.com/gsamokovarov)'s [Break](https://github.com/gsamokovarov/break) debugger. #### Byebug Zeitwerk and [Byebug](https://github.com/deivid-rodriguez/byebug) are incompatible, classes or modules that belong to [explicit namespaces](#explicit-namespaces) are not autoloaded inside a Byebug session. See [this issue](https://github.com/deivid-rodriguez/byebug/issues/564#issuecomment-499413606) for further details. ## Pronunciation "Zeitwerk" is pronounced [this way](http://share.hashref.com/zeitwerk/zeitwerk_pronunciation.mp3). ## Supported Ruby versions Zeitwerk works with MRI 2.4.4 and above. ## Testing In order to run the test suite of Zeitwerk, `cd` into the project root and execute ``` bin/test ``` To run one particular suite, pass its file name as an argument: ``` bin/test test/lib/zeitwerk/test_eager_load.rb ``` Furthermore, the project has a development dependency on [`minitest-focus`](https://github.com/seattlerb/minitest-focus). To run an individual test mark it with `focus`: ```ruby focus test "capitalizes the first letter" do assert_equal "User", camelize("user") end ``` and run `bin/test`. ## Motivation Since `require` has global side-effects, and there is no static way to verify that you have issued the `require` calls for code that your file depends on, in practice it is very easy to forget some. That introduces bugs that depend on the load order. Zeitwerk provides a way to forget about `require` in your own code, just name things following conventions and done. On the other hand, autoloading in Rails is based on `const_missing`, which lacks fundamental information like the nesting and the resolution algorithm that was being used. Because of that, Rails autoloading is not able to match Ruby's semantics and that introduces a series of gotchas. The original goal of this project was to bring a better autoloading mechanism for Rails 6. ## Thanks I'd like to thank [@matthewd](https://github.com/matthewd) for the discussions we've had about this topic in the past years, I learned a couple of tricks used in Zeitwerk from him. Also, would like to thank [@Shopify](https://github.com/Shopify), [@rafaelfranca](https://github.com/rafaelfranca), and [@dylanahsmith](https://github.com/dylanahsmith), for sharing [this PoC](https://github.com/Shopify/autoload_reloader). The technique Zeitwerk uses to support explicit namespaces was copied from that project. Jean Boussier ([@casperisfine](https://github.com/casperisfine), [@byroot](https://github.com/byroot)) deserves special mention. Jean migrated autoloading in Shopify when Zeitwerk integration in Rails was yet unreleased. His work and positive attitude have been outstanding, and thanks to his feedback the interface and performance of Zeitwerk are way, way better. Kudos man ❤️. Finally, many thanks to [@schurig](https://github.com/schurig) for recording an [audio file](http://share.hashref.com/zeitwerk/zeitwerk_pronunciation.mp3) with the pronunciation of "Zeitwerk" in perfect German. 💯 ## License Released under the MIT License, Copyright (c) 2019–ω Xavier Noria. zeitwerk-2.4.2/Rakefile000066400000000000000000000002251376020464000150310ustar00rootroot00000000000000require 'rake/testtask' task :default => :test Rake::TestTask.new do |t| t.test_files = Dir.glob('test/lib/**/test_*.rb') t.libs << "test" end zeitwerk-2.4.2/bin/000077500000000000000000000000001376020464000141355ustar00rootroot00000000000000zeitwerk-2.4.2/bin/test000077500000000000000000000001361376020464000150420ustar00rootroot00000000000000#!/bin/bash if [[ -z $1 ]]; then bundle exec rake else bundle exec rake TEST="$1" fi zeitwerk-2.4.2/extras/000077500000000000000000000000001376020464000146735ustar00rootroot00000000000000zeitwerk-2.4.2/extras/zeitwerk_pronunciation.mp3000066400000000000000000002511631376020464000221400ustar00rootroot00000000000000ID3vTSSLogic Pro X 10.4.4COMhengiTunNORM 00000537 000004F7 00001508 00001441 000003C6 000003C6 000082B0 00008327 00000392 00000392COMengiTunSMPB 00000000 00000210 000008E8 0000000000015888 00000000 000121CC 00000000 00000000 00000000 00000000 00000000 00000000@7@7@7@7@7@7@7@7@7@7@7@7@7@7@7@7@7@7@7@7@7@7@7>@Jc H0 AIhDy֙X,B$D ZMI7l`1`D LȮKȦ!!E (&bg"::+L"ȮEc w"Tb+ur+tr+u髑m|bCȒC@2k011D(,@=0"#Q: - SH@pg00E4M)',!xq])sJOLiL#y<$қx_sҚdW?3 @@͐</{pen:@ =;E4?6{=W%0X }/ H& Xc h'n%ܱ؉:62ޣHywm(as=8??؎5z9}mM@Oǒ<f4~.@x&ƈ/)އDC>Qr' g;L[XFjJQ>φ`U|8OY"i^]<,77"yw^@S9Gbb [1F|bBrX[MY>hVuz`1{uknԱ1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sǤ R}I/\Vj5&@?kפbO}ZL__پ+iǧ׽LR7Mbrc$E0@Y DD] Da(6ZΝx ֥nPW}c3+$dtJ.rCѪMNJ%wr=R ۵ {6h pק8yg[m[~ƫOIyJ<56_W7\:bͳ}Ҕ)o━k8JRkJ^7{zk{ҟ9)McY1JR@Ӑ:8Kppen ;A6-CIcD!B"1e]%t/pXg 0*U1.>.7r\?iiiar455NӔM^"4 8BHҊ"H 5hE"HR*&(YT "Y[k"ȥ-&jHBP 1JK !g%(T(YK[,^-$H5f%J1g?1zU cT1e-bdH-J "#(A(M :6Dy`" =D( :dzIlF"7AJ5Q!%ZrCMrNECx/XSr\XpX&ke)" BH1YT B`HR)%g%[RT(e+UB $H)Z""&D[),H"D}I$H1!B cPf1CPjR(JRVD$MJ_RD%YI$(Pzb ,?T(PcPDRT*'s(_@$7&` @X NQ\=Epzeitwerk-2.4.2/lib/000077500000000000000000000000001376020464000141335ustar00rootroot00000000000000zeitwerk-2.4.2/lib/zeitwerk.rb000066400000000000000000000005711376020464000163270ustar00rootroot00000000000000# frozen_string_literal: true module Zeitwerk require_relative "zeitwerk/real_mod_name" require_relative "zeitwerk/loader" require_relative "zeitwerk/registry" require_relative "zeitwerk/explicit_namespace" require_relative "zeitwerk/inflector" require_relative "zeitwerk/gem_inflector" require_relative "zeitwerk/kernel" require_relative "zeitwerk/error" end zeitwerk-2.4.2/lib/zeitwerk/000077500000000000000000000000001376020464000157775ustar00rootroot00000000000000zeitwerk-2.4.2/lib/zeitwerk/error.rb000066400000000000000000000002151376020464000174530ustar00rootroot00000000000000module Zeitwerk class Error < StandardError end class ReloadingDisabledError < Error end class NameError < ::NameError end end zeitwerk-2.4.2/lib/zeitwerk/explicit_namespace.rb000066400000000000000000000050541376020464000221650ustar00rootroot00000000000000module Zeitwerk # Centralizes the logic for the trace point used to detect the creation of # explicit namespaces, needed to descend into matching subdirectories right # after the constant has been defined. # # The implementation assumes an explicit namespace is managed by one loader. # Loaders that reopen namespaces owned by other projects are responsible for # loading their constant before setup. This is documented. module ExplicitNamespace # :nodoc: all class << self include RealModName # Maps constant paths that correspond to explicit namespaces according to # the file system, to the loader responsible for them. # # @private # @sig Hash[String, Zeitwerk::Loader] attr_reader :cpaths # @private # @sig Mutex attr_reader :mutex # @private # @sig TracePoint attr_reader :tracer # Asserts `cpath` corresponds to an explicit namespace for which `loader` # is responsible. # # @private # @sig (String, Zeitwerk::Loader) -> void def register(cpath, loader) mutex.synchronize do cpaths[cpath] = loader # We check enabled? because, looking at the C source code, enabling an # enabled tracer does not seem to be a simple no-op. tracer.enable unless tracer.enabled? end end # @private # @sig (Zeitwerk::Loader) -> void def unregister(loader) cpaths.delete_if { |_cpath, l| l == loader } disable_tracer_if_unneeded end private # @sig () -> void def disable_tracer_if_unneeded mutex.synchronize do tracer.disable if cpaths.empty? end end # @sig (TracePoint) -> void def tracepoint_class_callback(event) # If the class is a singleton class, we won't do anything with it so we # can bail out immediately. This is several orders of magnitude faster # than accessing its name. return if event.self.singleton_class? # Note that it makes sense to compute the hash code unconditionally, # because the trace point is disabled if cpaths is empty. if loader = cpaths.delete(real_mod_name(event.self)) loader.on_namespace_loaded(event.self) disable_tracer_if_unneeded end end end @cpaths = {} @mutex = Mutex.new # We go through a method instead of defining a block mainly to have a better # label when profiling. @tracer = TracePoint.new(:class, &method(:tracepoint_class_callback)) end end zeitwerk-2.4.2/lib/zeitwerk/gem_inflector.rb000066400000000000000000000007101376020464000211370ustar00rootroot00000000000000# frozen_string_literal: true module Zeitwerk class GemInflector < Inflector # @sig (String) -> void def initialize(root_file) namespace = File.basename(root_file, ".rb") lib_dir = File.dirname(root_file) @version_file = File.join(lib_dir, namespace, "version.rb") end # @sig (String, String) -> String def camelize(basename, abspath) abspath == @version_file ? "VERSION" : super end end end zeitwerk-2.4.2/lib/zeitwerk/inflector.rb000066400000000000000000000025751376020464000203220ustar00rootroot00000000000000# frozen_string_literal: true module Zeitwerk class Inflector # Very basic snake case -> camel case conversion. # # inflector = Zeitwerk::Inflector.new # inflector.camelize("post", ...) # => "Post" # inflector.camelize("users_controller", ...) # => "UsersController" # inflector.camelize("api", ...) # => "Api" # # Takes into account hard-coded mappings configured with `inflect`. # # @sig (String, String) -> String def camelize(basename, _abspath) overrides[basename] || basename.split('_').each(&:capitalize!).join end # Configures hard-coded inflections: # # inflector = Zeitwerk::Inflector.new # inflector.inflect( # "html_parser" => "HTMLParser", # "mysql_adapter" => "MySQLAdapter" # ) # # inflector.camelize("html_parser", abspath) # => "HTMLParser" # inflector.camelize("mysql_adapter", abspath) # => "MySQLAdapter" # inflector.camelize("users_controller", abspath) # => "UsersController" # # @sig (Hash[String, String]) -> void def inflect(inflections) overrides.merge!(inflections) end private # Hard-coded basename to constant name user maps that override the default # inflection logic. # # @sig () -> Hash[String, String] def overrides @overrides ||= {} end end end zeitwerk-2.4.2/lib/zeitwerk/kernel.rb000066400000000000000000000047701376020464000176140ustar00rootroot00000000000000# frozen_string_literal: true module Kernel module_function # We are going to decorate Kerner#require with two goals. # # First, by intercepting Kernel#require calls, we are able to autovivify # modules on required directories, and also do internal housekeeping when # managed files are loaded. # # On the other hand, if you publish a new version of a gem that is now managed # by Zeitwerk, client code can reference directly your classes and modules and # should not require anything. But if someone has legacy require calls around, # they will work as expected, and in a compatible way. # # We cannot decorate with prepend + super because Kernel has already been # included in Object, and changes in ancestors don't get propagated into # already existing ancestor chains. alias_method :zeitwerk_original_require, :require # @sig (String) -> true | false def require(path) if loader = Zeitwerk::Registry.loader_for(path) if path.end_with?(".rb") zeitwerk_original_require(path).tap do |required| loader.on_file_autoloaded(path) if required end else loader.on_dir_autoloaded(path) true end else zeitwerk_original_require(path).tap do |required| if required realpath = $LOADED_FEATURES.last if loader = Zeitwerk::Registry.loader_for(realpath) loader.on_file_autoloaded(realpath) end end end end end # By now, I have seen no way so far to decorate require_relative. # # For starters, at least in CRuby, require_relative does not delegate to # require. Both require and require_relative delegate the bulk of their work # to an internal C function called rb_require_safe. So, our require wrapper is # not executed. # # On the other hand, we cannot use the aliasing technique above because # require_relative receives a path relative to the directory of the file in # which the call is performed. If a wrapper here invoked the original method, # Ruby would resolve the relative path taking lib/zeitwerk as base directory. # # A workaround could be to extract the base directory from caller_locations, # but what if someone else decorated require_relative before us? You can't # really know with certainty where's the original call site in the stack. # # However, the main use case for require_relative is to load files from your # own project. Projects managed by Zeitwerk don't do this for files managed by # Zeitwerk, precisely. end zeitwerk-2.4.2/lib/zeitwerk/loader.rb000066400000000000000000000620451376020464000176010ustar00rootroot00000000000000# frozen_string_literal: true require "set" require "securerandom" module Zeitwerk class Loader require_relative "loader/callbacks" include Callbacks include RealModName # @sig String attr_reader :tag # @sig #camelize attr_accessor :inflector # @sig #call | #debug | nil attr_accessor :logger # Absolute paths of the root directories. Stored in a hash to preserve # order, easily handle duplicates, and also be able to have a fast lookup, # needed for detecting nested paths. # # "/Users/fxn/blog/app/assets" => true, # "/Users/fxn/blog/app/channels" => true, # ... # # This is a private collection maintained by the loader. The public # interface for it is `push_dir` and `dirs`. # # @private # @sig Hash[String, true] attr_reader :root_dirs # Absolute paths of files or directories that have to be preloaded. # # @private # @sig Array[String] attr_reader :preloads # Absolute paths of files, directories, or glob patterns to be totally # ignored. # # @private # @sig Set[String] attr_reader :ignored_glob_patterns # The actual collection of absolute file and directory names at the time the # ignored glob patterns were expanded. Computed on setup, and recomputed on # reload. # # @private # @sig Set[String] attr_reader :ignored_paths # Absolute paths of directories or glob patterns to be collapsed. # # @private # @sig Set[String] attr_reader :collapse_glob_patterns # The actual collection of absolute directory names at the time the collapse # glob patterns were expanded. Computed on setup, and recomputed on reload. # # @private # @sig Set[String] attr_reader :collapse_dirs # Maps real absolute paths for which an autoload has been set ---and not # executed--- to their corresponding parent class or module and constant # name. # # "/Users/fxn/blog/app/models/user.rb" => [Object, :User], # "/Users/fxn/blog/app/models/hotel/pricing.rb" => [Hotel, :Pricing] # ... # # @private # @sig Hash[String, [Module, Symbol]] attr_reader :autoloads # We keep track of autoloaded directories to remove them from the registry # at the end of eager loading. # # Files are removed as they are autoloaded, but directories need to wait due # to concurrency (see why in Zeitwerk::Loader::Callbacks#on_dir_autoloaded). # # @private # @sig Array[String] attr_reader :autoloaded_dirs # Stores metadata needed for unloading. Its entries look like this: # # "Admin::Role" => [".../admin/role.rb", [Admin, :Role]] # # The cpath as key helps implementing unloadable_cpath? The real file name # is stored in order to be able to delete it from $LOADED_FEATURES, and the # pair [Module, Symbol] is used to remove_const the constant from the class # or module object. # # If reloading is enabled, this hash is filled as constants are autoloaded # or eager loaded. Otherwise, the collection remains empty. # # @private # @sig Hash[String, [String, [Module, Symbol]]] attr_reader :to_unload # Maps constant paths of namespaces to arrays of corresponding directories. # # For example, given this mapping: # # "Admin" => [ # "/Users/fxn/blog/app/controllers/admin", # "/Users/fxn/blog/app/models/admin", # ... # ] # # when `Admin` gets defined we know that it plays the role of a namespace and # that its children are spread over those directories. We'll visit them to set # up the corresponding autoloads. # # @private # @sig Hash[String, Array[String]] attr_reader :lazy_subdirs # Absolute paths of files or directories not to be eager loaded. # # @private # @sig Set[String] attr_reader :eager_load_exclusions # User-oriented callbacks to be fired when a constant is loaded. attr_reader :on_load_callbacks # @private # @sig Mutex attr_reader :mutex # @private # @sig Mutex attr_reader :mutex2 def initialize @initialized_at = Time.now @tag = SecureRandom.hex(3) @inflector = Inflector.new @logger = self.class.default_logger @root_dirs = {} @preloads = [] @ignored_glob_patterns = Set.new @ignored_paths = Set.new @collapse_glob_patterns = Set.new @collapse_dirs = Set.new @autoloads = {} @autoloaded_dirs = [] @to_unload = {} @lazy_subdirs = {} @eager_load_exclusions = Set.new @on_load_callbacks = {} # TODO: find a better name for these mutexes. @mutex = Mutex.new @mutex2 = Mutex.new @setup = false @eager_loaded = false @reloading_enabled = false Registry.register_loader(self) end # Sets a tag for the loader, useful for logging. # # @param tag [#to_s] # @sig (#to_s) -> void def tag=(tag) @tag = tag.to_s end # Absolute paths of the root directories. This is a read-only collection, # please push here via `push_dir`. # # @sig () -> Array[String] def dirs root_dirs.keys.freeze end # Pushes `path` to the list of root directories. # # Raises `Zeitwerk::Error` if `path` does not exist, or if another loader in # the same process already manages that directory or one of its ascendants # or descendants. # # @raise [Zeitwerk::Error] # @sig (String | Pathname, Module) -> void def push_dir(path, namespace: Object) # Note that Class < Module. unless namespace.is_a?(Module) raise Error, "#{namespace.inspect} is not a class or module object, should be" end abspath = File.expand_path(path) if dir?(abspath) raise_if_conflicting_directory(abspath) root_dirs[abspath] = namespace else raise Error, "the root directory #{abspath} does not exist" end end # You need to call this method before setup in order to be able to reload. # There is no way to undo this, either you want to reload or you don't. # # @raise [Zeitwerk::Error] # @sig () -> void def enable_reloading mutex.synchronize do break if @reloading_enabled if @setup raise Error, "cannot enable reloading after setup" else @reloading_enabled = true end end end # @sig () -> bool def reloading_enabled? @reloading_enabled end # Files or directories to be preloaded instead of lazy loaded. # # @sig (*(String | Pathname | Array[String | Pathname])) -> void def preload(*paths) mutex.synchronize do expand_paths(paths).each do |abspath| preloads << abspath do_preload_abspath(abspath) if @setup end end end # Configure files, directories, or glob patterns to be totally ignored. # # @sig (*(String | Pathname | Array[String | Pathname])) -> void def ignore(*glob_patterns) glob_patterns = expand_paths(glob_patterns) mutex.synchronize do ignored_glob_patterns.merge(glob_patterns) ignored_paths.merge(expand_glob_patterns(glob_patterns)) end end # Configure directories or glob patterns to be collapsed. # # @sig (*(String | Pathname | Array[String | Pathname])) -> void def collapse(*glob_patterns) glob_patterns = expand_paths(glob_patterns) mutex.synchronize do collapse_glob_patterns.merge(glob_patterns) collapse_dirs.merge(expand_glob_patterns(glob_patterns)) end end # Configure a block to be invoked once a certain constant path is loaded. # Supports multiple callbacks, and if there are many, they are executed in # the order in which they were defined. # # loader.on_load("SomeApiClient") do # SomeApiClient.endpoint = "https://api.dev" # end # # @raise [TypeError] # @sig (String) { () -> void } -> void def on_load(cpath, &block) raise TypeError, "on_load only accepts strings" unless cpath.is_a?(String) mutex.synchronize do (on_load_callbacks[cpath] ||= []) << block end end # Sets autoloads in the root namespace and preloads files, if any. # # @sig () -> void def setup mutex.synchronize do break if @setup actual_root_dirs.each do |root_dir, namespace| set_autoloads_in_dir(root_dir, namespace) end do_preload @setup = true end end # Removes loaded constants and configured autoloads. # # The objects the constants stored are no longer reachable through them. In # addition, since said objects are normally not referenced from anywhere # else, they are eligible for garbage collection, which would effectively # unload them. # # @private # @sig () -> void def unload mutex.synchronize do # We are going to keep track of the files that were required by our # autoloads to later remove them from $LOADED_FEATURES, thus making them # loadable by Kernel#require again. # # Directories are not stored in $LOADED_FEATURES, keeping track of files # is enough. unloaded_files = Set.new autoloads.each do |realpath, (parent, cname)| if parent.autoload?(cname) unload_autoload(parent, cname) else # Could happen if loaded with require_relative. That is unsupported, # and the constant path would escape unloadable_cpath? This is just # defensive code to clean things up as much as we are able to. unload_cref(parent, cname) if cdef?(parent, cname) unloaded_files.add(realpath) if ruby?(realpath) end end to_unload.each_value do |(realpath, (parent, cname))| unload_cref(parent, cname) if cdef?(parent, cname) unloaded_files.add(realpath) if ruby?(realpath) end unless unloaded_files.empty? # Bootsnap decorates Kernel#require to speed it up using a cache and # this optimization does not check if $LOADED_FEATURES has the file. # # To make it aware of changes, the gem defines singleton methods in # $LOADED_FEATURES: # # https://github.com/Shopify/bootsnap/blob/master/lib/bootsnap/load_path_cache/core_ext/loaded_features.rb # # Rails applications may depend on bootsnap, so for unloading to work # in that setting it is preferable that we restrict our API choice to # one of those methods. $LOADED_FEATURES.reject! { |file| unloaded_files.member?(file) } end autoloads.clear autoloaded_dirs.clear to_unload.clear lazy_subdirs.clear Registry.on_unload(self) ExplicitNamespace.unregister(self) @setup = false @eager_loaded = false end end # Unloads all loaded code, and calls setup again so that the loader is able # to pick any changes in the file system. # # This method is not thread-safe, please see how this can be achieved by # client code in the README of the project. # # @raise [Zeitwerk::Error] # @sig () -> void def reload if reloading_enabled? unload recompute_ignored_paths recompute_collapse_dirs setup else raise ReloadingDisabledError, "can't reload, please call loader.enable_reloading before setup" end end # Eager loads all files in the root directories, recursively. Files do not # need to be in `$LOAD_PATH`, absolute file names are used. Ignored files # are not eager loaded. You can opt-out specifically in specific files and # directories with `do_not_eager_load`. # # @sig () -> void def eager_load mutex.synchronize do break if @eager_loaded queue = [] actual_root_dirs.each do |root_dir, namespace| queue << [namespace, root_dir] unless eager_load_exclusions.member?(root_dir) end while to_eager_load = queue.shift namespace, dir = to_eager_load ls(dir) do |basename, abspath| next if eager_load_exclusions.member?(abspath) if ruby?(abspath) if cref = autoloads[File.realpath(abspath)] cref[0].const_get(cref[1], false) end elsif dir?(abspath) && !root_dirs.key?(abspath) if collapse_dirs.member?(abspath) queue << [namespace, abspath] else cname = inflector.camelize(basename, abspath) queue << [namespace.const_get(cname, false), abspath] end end end end autoloaded_dirs.each do |autoloaded_dir| Registry.unregister_autoload(autoloaded_dir) end autoloaded_dirs.clear @eager_loaded = true end end # Let eager load ignore the given files or directories. The constants # defined in those files are still autoloadable. # # @sig (*(String | Pathname | Array[String | Pathname])) -> void def do_not_eager_load(*paths) mutex.synchronize { eager_load_exclusions.merge(expand_paths(paths)) } end # Says if the given constant path would be unloaded on reload. This # predicate returns `false` if reloading is disabled. # # @sig (String) -> bool def unloadable_cpath?(cpath) to_unload.key?(cpath) end # Returns an array with the constant paths that would be unloaded on reload. # This predicate returns an empty array if reloading is disabled. # # @sig () -> Array[String] def unloadable_cpaths to_unload.keys.freeze end # Logs to `$stdout`, handy shortcut for debugging. # # @sig () -> void def log! @logger = ->(msg) { puts msg } end # @private # @sig (String) -> bool def manages?(dir) dir = dir + "/" ignored_paths.each do |ignored_path| return false if dir.start_with?(ignored_path + "/") end root_dirs.each_key do |root_dir| return true if root_dir.start_with?(dir) || dir.start_with?(root_dir + "/") end false end # --- Class methods --------------------------------------------------------------------------- class << self # @sig #call | #debug | nil attr_accessor :default_logger # @private # @sig Mutex attr_accessor :mutex # This is a shortcut for # # require "zeitwerk" # loader = Zeitwerk::Loader.new # loader.tag = File.basename(__FILE__, ".rb") # loader.inflector = Zeitwerk::GemInflector.new(__FILE__) # loader.push_dir(__dir__) # # except that this method returns the same object in subsequent calls from # the same file, in the unlikely case the gem wants to be able to reload. # # @sig () -> Zeitwerk::Loader def for_gem called_from = caller_locations(1, 1).first.path Registry.loader_for_gem(called_from) end # Broadcasts `eager_load` to all loaders. # # @sig () -> void def eager_load_all Registry.loaders.each(&:eager_load) end # Returns an array with the absolute paths of the root directories of all # registered loaders. This is a read-only collection. # # @sig () -> Array[String] def all_dirs Registry.loaders.flat_map(&:dirs).freeze end end self.mutex = Mutex.new private # ------------------------------------------------------------------------------------- # @sig () -> Array[String] def actual_root_dirs root_dirs.reject do |root_dir, _namespace| !dir?(root_dir) || ignored_paths.member?(root_dir) end end # @sig (String, Module) -> void def set_autoloads_in_dir(dir, parent) ls(dir) do |basename, abspath| begin if ruby?(basename) basename[-3..-1] = '' cname = inflector.camelize(basename, abspath).to_sym autoload_file(parent, cname, abspath) elsif dir?(abspath) # In a Rails application, `app/models/concerns` is a subdirectory of # `app/models`, but both of them are root directories. # # To resolve the ambiguity file name -> constant path this introduces, # the `app/models/concerns` directory is totally ignored as a namespace, # it counts only as root. The guard checks that. unless root_dirs.key?(abspath) cname = inflector.camelize(basename, abspath).to_sym if collapse_dirs.member?(abspath) set_autoloads_in_dir(abspath, parent) else autoload_subdir(parent, cname, abspath) end end end rescue ::NameError => error path_type = ruby?(abspath) ? "file" : "directory" raise NameError.new(<<~MESSAGE, error.name) #{error.message} inferred by #{inflector.class} from #{path_type} #{abspath} Possible ways to address this: * Tell Zeitwerk to ignore this particular #{path_type}. * Tell Zeitwerk to ignore one of its parent directories. * Rename the #{path_type} to comply with the naming conventions. * Modify the inflector to handle this case. MESSAGE end end end # @sig (Module, Symbol, String) -> void def autoload_subdir(parent, cname, subdir) if autoload_path = autoload_for?(parent, cname) cpath = cpath(parent, cname) register_explicit_namespace(cpath) if ruby?(autoload_path) # We do not need to issue another autoload, the existing one is enough # no matter if it is for a file or a directory. Just remember the # subdirectory has to be visited if the namespace is used. (lazy_subdirs[cpath] ||= []) << subdir elsif !cdef?(parent, cname) # First time we find this namespace, set an autoload for it. (lazy_subdirs[cpath(parent, cname)] ||= []) << subdir set_autoload(parent, cname, subdir) else # For whatever reason the constant that corresponds to this namespace has # already been defined, we have to recurse. set_autoloads_in_dir(subdir, parent.const_get(cname)) end end # @sig (Module, Symbol, String) -> void def autoload_file(parent, cname, file) if autoload_path = autoload_for?(parent, cname) # First autoload for a Ruby file wins, just ignore subsequent ones. if ruby?(autoload_path) log("file #{file} is ignored because #{autoload_path} has precedence") if logger else promote_namespace_from_implicit_to_explicit( dir: autoload_path, file: file, parent: parent, cname: cname ) end elsif cdef?(parent, cname) log("file #{file} is ignored because #{cpath(parent, cname)} is already defined") if logger else set_autoload(parent, cname, file) end end # `dir` is the directory that would have autovivified a namespace. `file` is # the file where we've found the namespace is explicitly defined. # # @sig (dir: String, file: String, parent: Module, cname: Symbol) -> void def promote_namespace_from_implicit_to_explicit(dir:, file:, parent:, cname:) autoloads.delete(dir) Registry.unregister_autoload(dir) set_autoload(parent, cname, file) register_explicit_namespace(cpath(parent, cname)) end # @sig (Module, Symbol, String) -> void def set_autoload(parent, cname, abspath) # $LOADED_FEATURES stores real paths since Ruby 2.4.4. We set and save the # real path to be able to delete it from $LOADED_FEATURES on unload, and to # be able to do a lookup later in Kernel#require for manual require calls. # # We freeze realpath because that saves allocations in Module#autoload. # See #125. realpath = File.realpath(abspath).freeze parent.autoload(cname, realpath) if logger if ruby?(realpath) log("autoload set for #{cpath(parent, cname)}, to be loaded from #{realpath}") else log("autoload set for #{cpath(parent, cname)}, to be autovivified from #{realpath}") end end autoloads[realpath] = [parent, cname] Registry.register_autoload(self, realpath) # See why in the documentation of Zeitwerk::Registry.inceptions. unless parent.autoload?(cname) Registry.register_inception(cpath(parent, cname), realpath, self) end end # @sig (Module, Symbol) -> String? def autoload_for?(parent, cname) strict_autoload_path(parent, cname) || Registry.inception?(cpath(parent, cname)) end # The autoload? predicate takes into account the ancestor chain of the # receiver, like const_defined? and other methods in the constants API do. # # For example, given # # class A # autoload :X, "x.rb" # end # # class B < A # end # # B.autoload?(:X) returns "x.rb". # # We need a way to strictly check in parent ignoring ancestors. # # @sig (Module, Symbol) -> String? if method(:autoload?).arity == 1 def strict_autoload_path(parent, cname) parent.autoload?(cname) if cdef?(parent, cname) end else def strict_autoload_path(parent, cname) parent.autoload?(cname, false) end end # This method is called this way because I prefer `preload` to be the method # name to configure preloads in the public interface. # # @sig () -> void def do_preload preloads.each do |abspath| do_preload_abspath(abspath) end end # @sig (String) -> void def do_preload_abspath(abspath) if ruby?(abspath) do_preload_file(abspath) elsif dir?(abspath) do_preload_dir(abspath) end end # @sig (String) -> void def do_preload_dir(dir) ls(dir) do |_basename, abspath| do_preload_abspath(abspath) end end # @sig (String) -> bool def do_preload_file(file) log("preloading #{file}") if logger require file end # @sig (Module, Symbol) -> String def cpath(parent, cname) parent.equal?(Object) ? cname.to_s : "#{real_mod_name(parent)}::#{cname}" end # @sig (String) { (String, String) -> void } -> void def ls(dir) Dir.foreach(dir) do |basename| next if basename.start_with?(".") abspath = File.join(dir, basename) next if ignored_paths.member?(abspath) # We freeze abspath because that saves allocations when passed later to # File methods. See #125. yield basename, abspath.freeze end end # @sig (String) -> bool def ruby?(path) path.end_with?(".rb") end # @sig (String) -> bool def dir?(path) File.directory?(path) end # @sig (String | Pathname | Array[String | Pathname]) -> Array[String] def expand_paths(paths) paths.flatten.map! { |path| File.expand_path(path) } end # @sig (Array[String]) -> Array[String] def expand_glob_patterns(glob_patterns) # Note that Dir.glob works with regular file names just fine. That is, # glob patterns technically need no wildcards. glob_patterns.flat_map { |glob_pattern| Dir.glob(glob_pattern) } end # @sig () -> void def recompute_ignored_paths ignored_paths.replace(expand_glob_patterns(ignored_glob_patterns)) end # @sig () -> void def recompute_collapse_dirs collapse_dirs.replace(expand_glob_patterns(collapse_glob_patterns)) end # @sig (String) -> void def log(message) method_name = logger.respond_to?(:debug) ? :debug : :call logger.send(method_name, "Zeitwerk@#{tag}: #{message}") end # @sig (Module, Symbol) -> bool def cdef?(parent, cname) parent.const_defined?(cname, false) end # @sig (String) -> void def register_explicit_namespace(cpath) ExplicitNamespace.register(cpath, self) end # @sig (String) -> void def raise_if_conflicting_directory(dir) self.class.mutex.synchronize do Registry.loaders.each do |loader| if loader != self && loader.manages?(dir) require "pp" raise Error, "loader\n\n#{pretty_inspect}\n\nwants to manage directory #{dir}," \ " which is already managed by\n\n#{loader.pretty_inspect}\n" EOS end end end end # @sig (Module, Symbol) -> void def unload_autoload(parent, cname) parent.__send__(:remove_const, cname) log("autoload for #{cpath(parent, cname)} removed") if logger end # @sig (Module, Symbol) -> void def unload_cref(parent, cname) parent.__send__(:remove_const, cname) log("#{cpath(parent, cname)} unloaded") if logger end end end zeitwerk-2.4.2/lib/zeitwerk/loader/000077500000000000000000000000001376020464000172455ustar00rootroot00000000000000zeitwerk-2.4.2/lib/zeitwerk/loader/callbacks.rb000066400000000000000000000057251376020464000215220ustar00rootroot00000000000000module Zeitwerk::Loader::Callbacks include Zeitwerk::RealModName # Invoked from our decorated Kernel#require when a managed file is autoloaded. # # @private # @sig (String) -> void def on_file_autoloaded(file) cref = autoloads.delete(file) cpath = cpath(*cref) to_unload[cpath] = [file, cref] if reloading_enabled? Zeitwerk::Registry.unregister_autoload(file) if logger && cdef?(*cref) log("constant #{cpath} loaded from file #{file}") elsif !cdef?(*cref) raise Zeitwerk::NameError.new("expected file #{file} to define constant #{cpath}, but didn't", cref.last) end run_on_load_callbacks(cpath) end # Invoked from our decorated Kernel#require when a managed directory is # autoloaded. # # @private # @sig (String) -> void def on_dir_autoloaded(dir) # Module#autoload does not serialize concurrent requires, and we handle # directories ourselves, so the callback needs to account for concurrency. # # Multi-threading would introduce a race condition here in which thread t1 # autovivifies the module, and while autoloads for its children are being # set, thread t2 autoloads the same namespace. # # Without the mutex and subsequent delete call, t2 would reset the module. # That not only would reassign the constant (undesirable per se) but, worse, # the module object created by t2 wouldn't have any of the autoloads for its # children, since t1 would have correctly deleted its lazy_subdirs entry. mutex2.synchronize do if cref = autoloads.delete(dir) autovivified_module = cref[0].const_set(cref[1], Module.new) cpath = autovivified_module.name log("module #{cpath} autovivified from directory #{dir}") if logger to_unload[cpath] = [dir, cref] if reloading_enabled? # We don't unregister `dir` in the registry because concurrent threads # wouldn't find a loader associated to it in Kernel#require and would # try to require the directory. Instead, we are going to keep track of # these to be able to unregister later if eager loading. autoloaded_dirs << dir on_namespace_loaded(autovivified_module) run_on_load_callbacks(cpath) end end end # Invoked when a class or module is created or reopened, either from the # tracer or from module autovivification. If the namespace has matching # subdirectories, we descend into them now. # # @private # @sig (Module) -> void def on_namespace_loaded(namespace) if subdirs = lazy_subdirs.delete(real_mod_name(namespace)) subdirs.each do |subdir| set_autoloads_in_dir(subdir, namespace) end end end private # @sig (String) -> void def run_on_load_callbacks(cpath) # Very common, do not even compute a hash code. return if on_load_callbacks.empty? callbacks = reloading_enabled? ? on_load_callbacks[cpath] : on_load_callbacks.delete(cpath) callbacks.each(&:call) if callbacks end end zeitwerk-2.4.2/lib/zeitwerk/real_mod_name.rb000066400000000000000000000011331376020464000211040ustar00rootroot00000000000000module Zeitwerk::RealModName UNBOUND_METHOD_MODULE_NAME = Module.instance_method(:name) private_constant :UNBOUND_METHOD_MODULE_NAME # Returns the real name of the class or module, as set after the first # constant to which it was assigned (or nil). # # The name method can be overridden, hence the indirection in this method. # # @sig (Module) -> String? if UnboundMethod.method_defined?(:bind_call) def real_mod_name(mod) UNBOUND_METHOD_MODULE_NAME.bind_call(mod) end else def real_mod_name(mod) UNBOUND_METHOD_MODULE_NAME.bind(mod).call end end end zeitwerk-2.4.2/lib/zeitwerk/registry.rb000066400000000000000000000101071376020464000201730ustar00rootroot00000000000000# frozen_string_literal: true module Zeitwerk module Registry # :nodoc: all class << self # Keeps track of all loaders. Useful to broadcast messages and to prevent # them from being garbage collected. # # @private # @sig Array[Zeitwerk::Loader] attr_reader :loaders # Registers loaders created with `for_gem` to make the method idempotent # in case of reload. # # @private # @sig Hash[String, Zeitwerk::Loader] attr_reader :loaders_managing_gems # Maps real paths to the loaders responsible for them. # # This information is used by our decorated `Kernel#require` to be able to # invoke callbacks and autovivify modules. # # @private # @sig Hash[String, Zeitwerk::Loader] attr_reader :autoloads # This hash table addresses an edge case in which an autoload is ignored. # # For example, let's suppose we want to autoload in a gem like this: # # # lib/my_gem.rb # loader = Zeitwerk::Loader.new # loader.push_dir(__dir__) # loader.setup # # module MyGem # end # # if you require "my_gem", as Bundler would do, this happens while setting # up autoloads: # # 1. Object.autoload?(:MyGem) returns `nil` because the autoload for # the constant is issued by Zeitwerk while the same file is being # required. # 2. The constant `MyGem` is undefined while setup runs. # # Therefore, a directory `lib/my_gem` would autovivify a module according to # the existing information. But that would be wrong. # # To overcome this fundamental limitation, we keep track of the constant # paths that are in this situation ---in the example above, "MyGem"--- and # take this collection into account for the autovivification logic. # # Note that you cannot generally address this by moving the setup code # below the constant definition, because we want libraries to be able to # use managed constants in the module body: # # module MyGem # include MyConcern # end # # @private # @sig Hash[String, [String, Zeitwerk::Loader]] attr_reader :inceptions # Registers a loader. # # @private # @sig (Zeitwerk::Loader) -> void def register_loader(loader) loaders << loader end # This method returns always a loader, the same instance for the same root # file. That is how Zeitwerk::Loader.for_gem is idempotent. # # @private # @sig (String) -> Zeitwerk::Loader def loader_for_gem(root_file) loaders_managing_gems[root_file] ||= begin Loader.new.tap do |loader| loader.tag = File.basename(root_file, ".rb") loader.inflector = GemInflector.new(root_file) loader.push_dir(File.dirname(root_file)) end end end # @private # @sig (Zeitwerk::Loader, String) -> String def register_autoload(loader, realpath) autoloads[realpath] = loader end # @private # @sig (String) -> void def unregister_autoload(realpath) autoloads.delete(realpath) end # @private # @sig (String, String, Zeitwerk::Loader) -> void def register_inception(cpath, realpath, loader) inceptions[cpath] = [realpath, loader] end # @private # @sig (String) -> String? def inception?(cpath) if pair = inceptions[cpath] pair.first end end # @private # @sig (String) -> Zeitwerk::Loader? def loader_for(path) autoloads[path] end # @private # @sig (Zeitwerk::Loader) -> void def on_unload(loader) autoloads.delete_if { |_path, object| object == loader } inceptions.delete_if { |_cpath, (_path, object)| object == loader } end end @loaders = [] @loaders_managing_gems = {} @autoloads = {} @inceptions = {} end end zeitwerk-2.4.2/lib/zeitwerk/version.rb000066400000000000000000000001071376020464000200070ustar00rootroot00000000000000# frozen_string_literal: true module Zeitwerk VERSION = "2.4.2" end zeitwerk-2.4.2/test/000077500000000000000000000000001376020464000143445ustar00rootroot00000000000000zeitwerk-2.4.2/test/lib/000077500000000000000000000000001376020464000151125ustar00rootroot00000000000000zeitwerk-2.4.2/test/lib/test_gem_inflector.rb000066400000000000000000000015401376020464000213130ustar00rootroot00000000000000require "test_helper" class TestGemInflector < LoaderTest def with_setup files = [ ["lib/my_gem.rb", <<-EOS], loader = Zeitwerk::Loader.for_gem loader.enable_reloading loader.setup module MyGem end EOS ["lib/my_gem/foo.rb", "MyGem::Foo = true"], ["lib/my_gem/version.rb", "MyGem::VERSION = '1.0.0'"], ["lib/my_gem/ns/version.rb", "MyGem::Ns::Version = true"] ] with_files(files) do require "./lib/my_gem" yield end end test "the constant for my_gem/version.rb is inflected as VERSION" do with_setup { assert_equal "1.0.0", MyGem::VERSION } end test "other possible version.rb are inflected normally" do with_setup { assert MyGem::Ns::Version } end test "works as expected for other files" do with_setup { assert MyGem::Foo } end end zeitwerk-2.4.2/test/lib/test_inflector.rb000066400000000000000000000020371376020464000204650ustar00rootroot00000000000000require "test_helper" class TestInflector < Minitest::Test def camelize(str) Zeitwerk::Inflector.new.camelize(str, nil) end test "capitalizes the first letter" do assert_equal "User", camelize("user") end test "camelizes snake case basenames" do assert_equal "UsersController", camelize("users_controller") end test "supports segments that do not capitalize" do assert_equal "Point3dValue", camelize("point_3d_value") end test "knows nothing about acronyms" do assert_equal "HtmlParser", camelize("html_parser") end test "returns inflections defined using the inflect method" do inflections = { "html_parser" => "HTMLParser", "csv_controller" => "CSVController", "mysql_adapter" => "MySQLAdapter" } inflector = Zeitwerk::Inflector.new inflector.inflect(inflections) inflections.each do |basename, cname| assert_equal cname, inflector.camelize(basename, nil) end assert_equal "UsersController", inflector.camelize("users_controller", nil) end end zeitwerk-2.4.2/test/lib/test_real_mod_name.rb000066400000000000000000000023031376020464000212560ustar00rootroot00000000000000require "test_helper" class TestRealModName < Minitest::Test include Zeitwerk::RealModName test "returns nil for anonymous classes and modules" do [Class.new, Module.new].each do |mod| assert_nil real_mod_name(mod) end end test "returns nil for anonymous classes and modules that override #name" do [Class.new, Module.new].each do |mod| def mod.name; "X"; end assert_equal "X", mod.name assert_nil real_mod_name(mod) end end test "returns the name of regular classes an modules" do on_teardown do remove_const :C, from: self.class remove_const :M, from: self.class end C = Class.new M = Module.new [C, M].each do |mod| assert_equal mod.name, real_mod_name(mod) end end test "returns the real name of class and modules that override #name" do on_teardown do remove_const :C, from: self.class remove_const :M, from: self.class end C = Class.new { def self.name; "X"; end } M = Module.new { def self.name; "X"; end } [[C, "#{self.class}::C"], [M, "#{self.class}::M"]].each do |mod, real| assert_equal "X", mod.name assert_equal real, real_mod_name(mod) end end end zeitwerk-2.4.2/test/lib/zeitwerk/000077500000000000000000000000001376020464000167565ustar00rootroot00000000000000zeitwerk-2.4.2/test/lib/zeitwerk/test_all_dirs.rb000066400000000000000000000012631376020464000221350ustar00rootroot00000000000000require "test_helper" class TestAllDirs < LoaderTest test "returns an empty array if no loaders are instantiated" do assert_empty Zeitwerk::Loader.all_dirs end test "returns an empty array if there are loaders but they have no root dirs" do 2.times { new_loader } assert_empty Zeitwerk::Loader.all_dirs end test "returns the root directories of the registered loaders" do files = [ ["loaderA/a.rb", "A = true"], ["loaderB/b.rb", "B = true"] ] with_files(files) do new_loader(dirs: "loaderA") new_loader(dirs: "loaderB") assert_equal ["#{Dir.pwd}/loaderA", "#{Dir.pwd}/loaderB"], Zeitwerk::Loader.all_dirs end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_ancestors.rb000066400000000000000000000023001376020464000223360ustar00rootroot00000000000000require "test_helper" # The following properties are not supported by the classic Rails autoloader. class TestAncestors < LoaderTest test "autoloads a constant from an ancestor" do files = [ ["a.rb", "class A; end"], ["a/x.rb", "class A::X; end"], ["b.rb", "class B < A; end"], ["c.rb", "class C < B; end"] ] with_setup(files) do assert C::X end end test "autoloads a constant from an ancenstor, even if present above" do files = [ ["a.rb", "class A; X = :A; end"], ["b.rb", "class B < A; end"], ["b/x.rb", "class B; X = :B; end"], ["c.rb", "class C < B; end"] ] with_setup(files) do assert_equal :A, A::X assert_equal :B, C::X end end # See https://github.com/rails/rails/issues/28997. test "autoloads a constant from an ancestor that has some nesting going on" do files = [ ["test_class.rb", "class TestClass; include IncludeModule; end"], ["include_module.rb", "module IncludeModule; include ContainerModule; end"], ["container_module/child_class.rb", "class ContainerModule::ChildClass; end"] ] with_setup(files) do assert TestClass::ChildClass end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_autovivification.rb000066400000000000000000000035371376020464000237350ustar00rootroot00000000000000require "test_helper" class TestAutovivification < LoaderTest module Namespace; end test "autoloads a simple constant in an autovivified module (Object)" do files = [["admin/x.rb", "Admin::X = true"]] with_setup(files) do assert_kind_of Module, Admin assert Admin::X end end test "autoloads a simple constant in an autovivified module (Namespace)" do files = [["admin/x.rb", "#{Namespace}::Admin::X = true"]] with_setup(files, namespace: Namespace) do assert_kind_of Module, Namespace::Admin assert Namespace::Admin::X end end test "autovivifies several levels in a row (Object)" do files = [["foo/bar/baz/woo.rb", "Foo::Bar::Baz::Woo = true"]] with_setup(files) do assert Foo::Bar::Baz::Woo end end test "autovivifies several levels in a row (Namespace)" do files = [["foo/bar/baz/woo.rb", "#{Namespace}::Foo::Bar::Baz::Woo = true"]] with_setup(files, namespace: Namespace) do assert Namespace::Foo::Bar::Baz::Woo end end test "autoloads several constants from the same namespace (Object)" do files = [ ["app/models/admin/hotel.rb", "class Admin::Hotel; end"], ["app/controllers/admin/hotels_controller.rb", "class Admin::HotelsController; end"] ] with_setup(files, dirs: %w(app/models app/controllers)) do assert Admin::Hotel assert Admin::HotelsController end end test "autoloads several constants from the same namespace (Namespace)" do files = [ ["app/models/admin/hotel.rb", "class #{Namespace}::Admin::Hotel; end"], ["app/controllers/admin/hotels_controller.rb", "class #{Namespace}::Admin::HotelsController; end"] ] with_setup(files, namespace: Namespace, dirs: %w(app/models app/controllers)) do assert Namespace::Admin::Hotel assert Namespace::Admin::HotelsController end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_callbacks.rb000066400000000000000000000053341376020464000222660ustar00rootroot00000000000000require "test_helper" class TestCallbacks < LoaderTest module Namespace; end test "autoloading a file triggers on_file_autoloaded (Object)" do def loader.on_file_autoloaded(file) if file == File.realpath("x.rb") $on_file_autoloaded_called = true end super end files = [["x.rb", "X = true"]] with_setup(files) do $on_file_autoloaded_called = false assert X assert $on_file_autoloaded_called end end test "autoloading a file triggers on_file_autoloaded (Namespace)" do def loader.on_file_autoloaded(file) if file == File.realpath("x.rb") $on_file_autoloaded_called = true end super end files = [["x.rb", "#{Namespace}::X = true"]] with_setup(files, namespace: Namespace) do $on_file_autoloaded_called = false assert Namespace::X assert $on_file_autoloaded_called end end test "requiring an autoloadable file triggers on_file_autoloaded (Object)" do def loader.on_file_autoloaded(file) if file == File.realpath("y.rb") $on_file_autoloaded_called = true end super end files = [ ["x.rb", "X = true"], ["y.rb", "Y = X"] ] with_setup(files, load_path: ".") do $on_file_autoloaded_called = false require "y" assert Y assert $on_file_autoloaded_called end end test "requiring an autoloadable file triggers on_file_autoloaded (Namespace)" do def loader.on_file_autoloaded(file) if file == File.realpath("y.rb") $on_file_autoloaded_called = true end super end files = [ ["x.rb", "#{Namespace}::X = true"], ["y.rb", "#{Namespace}::Y = #{Namespace}::X"] ] with_setup(files, namespace: Namespace, load_path: ".") do $on_file_autoloaded_called = false require "y" assert Namespace::Y assert $on_file_autoloaded_called end end test "autoloading a directory triggers on_dir_autoloaded (Object)" do def loader.on_dir_autoloaded(dir) if dir == File.realpath("m") $on_dir_autoloaded_called = true end super end files = [["m/x.rb", "M::X = true"]] with_setup(files) do $on_dir_autoloaded_called = false assert M::X assert $on_dir_autoloaded_called end end test "autoloading a directory triggers on_dir_autoloaded (Namespace)" do def loader.on_dir_autoloaded(dir) if dir == File.realpath("m") $on_dir_autoloaded_called = true end super end files = [["m/x.rb", "#{Namespace}::M::X = true"]] with_setup(files, namespace: Namespace) do $on_dir_autoloaded_called = false assert Namespace::M::X assert $on_dir_autoloaded_called end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_collapse.rb000066400000000000000000000052051376020464000221460ustar00rootroot00000000000000require "test_helper" require "set" class TestCollapse < LoaderTest test "collapsed directories are ignored as namespaces" do files = [["foo/bar/x.rb", "Foo::X = true"]] with_files(files) do loader.push_dir(".") loader.collapse("foo/bar") loader.setup assert Foo::X end end test "top-level directories can be collapsed" do files = [["foo/bar/x.rb", "Bar::X = true"]] with_files(files) do loader.push_dir(".") loader.collapse("foo") loader.setup assert Bar::X end end test "accepts several arguments" do files = [ ["foo/bar/x.rb", "Foo::X = true"], ["zoo/bar/x.rb", "Zoo::X = true"] ] with_files(files) do loader.push_dir(".") loader.collapse("foo/bar", "zoo/bar") loader.setup assert Foo::X assert Zoo::X end end test "accepts an array" do files = [ ["foo/bar/x.rb", "Foo::X = true"], ["zoo/bar/x.rb", "Zoo::X = true"] ] with_files(files) do loader.push_dir(".") loader.collapse(["foo/bar", "zoo/bar"]) loader.setup assert Foo::X assert Zoo::X end end test "supports glob patterns" do files = [ ["foo/bar/x.rb", "Foo::X = true"], ["zoo/bar/x.rb", "Zoo::X = true"] ] with_files(files) do loader.push_dir(".") loader.collapse("*/bar") loader.setup assert Foo::X assert Zoo::X end end test "collapse directories are recomputed on reload" do files = [["foo/bar/x.rb", "Foo::X = true"]] with_files(files) do loader.push_dir(".") loader.collapse("*/bar") loader.setup assert Foo::X assert_raises(NameError) { Zoo::X } FileUtils.mkdir_p("zoo/bar") File.write("zoo/bar/x.rb", "Zoo::X = true") loader.reload assert Foo::X assert Zoo::X end end test "collapse directories are honored when eager loading" do $collapse_honored_when_eager_loading = false files = [["foo/bar/x.rb", "Foo::X = true; $collapse_honored_when_eager_loading = true"]] with_files(files) do loader.push_dir(".") loader.collapse("foo/bar") loader.setup loader.eager_load assert $collapse_honored_when_eager_loading end end test "collapsed top-level directories are eager loaded too" do $collapse_honored_when_eager_loading = false files = [["foo/bar/x.rb", "Bar::X = true; $collapse_honored_when_eager_loading = true"]] with_files(files) do loader.push_dir(".") loader.collapse("foo") loader.setup loader.eager_load assert $collapse_honored_when_eager_loading end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_concurrency.rb000066400000000000000000000014101376020464000226700ustar00rootroot00000000000000require "test_helper" class TestConcurrency < LoaderTest test "constant definition is synchronized" do files = [["m.rb", <<-EOS]] module M sleep 0.5 def self.works? true end end EOS with_setup(files) do t = Thread.new { M } assert M.works? t.join end end test "module autovivification" do $test_admin_const_set_calls = 0 files = [["admin/v2/user.rb", "class Admin::V2::User; end"]] with_setup(files) do assert Admin def Admin.const_set(cname, mod) $test_admin_const_set_calls += 1 sleep 0.5 super end Array.new(2) { Thread.new { Admin::V2 } }.each(&:join) assert_equal 1, $test_admin_const_set_calls end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_conflicting_directory.rb000066400000000000000000000045411376020464000247310ustar00rootroot00000000000000require "test_helper" class TestConflictingDirectory < LoaderTest def dir __dir__ end def parent File.expand_path("..", dir) end def existing_loader @existing_loader ||= new_loader(setup: false) end def loader @loader ||= new_loader(setup: false) end def conflicting_directory_message(dir) require "pp" "loader\n\n#{loader.pretty_inspect}\n\nwants to manage directory #{dir}," \ " which is already managed by\n\n#{existing_loader.pretty_inspect}\n" end test "raises if an existing loader manages the same root dir" do existing_loader.push_dir(dir) e = assert_raises(Zeitwerk::Error) { loader.push_dir(dir) } assert_equal conflicting_directory_message(dir), e.message end test "raises if an existing loader manages a parent directory" do existing_loader.push_dir(parent) e = assert_raises(Zeitwerk::Error) { loader.push_dir(dir) } assert_equal conflicting_directory_message(dir), e.message end test "raises if an existing loader manages a subdirectory" do existing_loader.push_dir(dir) e = assert_raises(Zeitwerk::Error) { loader.push_dir(parent) } assert_equal conflicting_directory_message(parent), e.message end test "does not raise if an existing loader manages a directory with a matching prefix" do files = [["foo/x.rb", "X = 1"], ["foobar/y.rb", "Y = 1"]] with_files(files) do existing_loader.push_dir("foo") assert loader.push_dir("foobar") end end test "does not raise if an existing loader ignores the directory (dir)" do existing_loader.push_dir(parent) existing_loader.ignore(dir) assert loader.push_dir(dir) end test "does not raise if an existing loader ignores the directory (glob pattern)" do existing_loader.push_dir(parent) existing_loader.ignore("#{parent}/*") assert loader.push_dir(dir) end test "raises if an existing loader ignores a directory with a matching prefix" do files = [["foo/x.rb", "X = 1"], ["foobar/y.rb", "Y = 1"]] with_files(files) do ignored = File.expand_path("foo") conflicting_dir = File.expand_path("foobar") existing_loader.push_dir(".") existing_loader.ignore(ignored) e = assert_raises(Zeitwerk::Error) { loader.push_dir(conflicting_dir) } assert_equal conflicting_directory_message(conflicting_dir), e.message end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_eager_load.rb000066400000000000000000000173141376020464000224320ustar00rootroot00000000000000require "test_helper" require "fileutils" class TestEagerLoad < LoaderTest module Namespace; end test "eager loads independent files (Object)" do loaders = [loader, new_loader(setup: false)] $tel0 = $tel1 = false files = [ ["lib0/app0.rb", "module App0; end"], ["lib0/app0/foo.rb", "class App0::Foo; $tel0 = true; end"], ["lib1/app1/foo.rb", "class App1::Foo; end"], ["lib1/app1/foo/bar/baz.rb", "class App1::Foo::Bar::Baz; $tel1 = true; end"] ] with_files(files) do loaders[0].push_dir("lib0") loaders[0].setup loaders[1].push_dir("lib1") loaders[1].setup Zeitwerk::Loader.eager_load_all assert $tel0 assert $tel1 end end test "eager loads independent files (Namespace)" do loaders = [loader, new_loader(setup: false)] $tel0 = $tel1 = false files = [ ["lib0/app0.rb", "module #{Namespace}::App0; end"], ["lib0/app0/foo.rb", "class #{Namespace}::App0::Foo; $tel0 = true; end"], ["lib1/app1/foo.rb", "class App1::Foo; end"], ["lib1/app1/foo/bar/baz.rb", "class App1::Foo::Bar::Baz; $tel1 = true; end"] ] with_files(files) do loaders[0].push_dir("lib0", namespace: Namespace) loaders[0].setup loaders[1].push_dir("lib1") loaders[1].setup Zeitwerk::Loader.eager_load_all assert $tel0 assert $tel1 end end test "eager loads dependent loaders" do loaders = [loader, new_loader(setup: false)] $tel0 = $tel1 = false files = [ ["lib0/app0.rb", <<-EOS], module App0 App1 end EOS ["lib0/app0/foo.rb", <<-EOS], class App0::Foo $tel0 = App1::Foo end EOS ["lib1/app1/foo.rb", <<-EOS], class App1::Foo App0 end EOS ["lib1/app1/foo/bar/baz.rb", <<-EOS] class App1::Foo::Bar::Baz $tel1 = App0::Foo end EOS ] with_files(files) do loaders[0].push_dir("lib0") loaders[0].setup loaders[1].push_dir("lib1") loaders[1].setup Zeitwerk::Loader.eager_load_all assert $tel0 assert $tel1 end end test "eager loads gems" do on_teardown do remove_const :MyGem delete_loaded_feature "my_gem.rb" delete_loaded_feature "my_gem/foo.rb" delete_loaded_feature "my_gem/foo/bar.rb" delete_loaded_feature "my_gem/foo/baz.rb" end $my_gem_foo_bar_eager_loaded = false files = [ ["my_gem.rb", <<-EOS], $for_gem_test_loader = Zeitwerk::Loader.for_gem $for_gem_test_loader.setup class MyGem Foo::Baz # autoloads fine end $for_gem_test_loader.eager_load EOS ["my_gem/foo.rb", "class MyGem::Foo; end"], ["my_gem/foo/bar.rb", "class MyGem::Foo::Bar; end; $my_gem_foo_bar_eager_loaded = true"], ["my_gem/foo/baz.rb", "class MyGem::Foo::Baz; end"], ] with_files(files) do with_load_path(".") do require "my_gem" assert $my_gem_foo_bar_eager_loaded end end end [false, true].each do |enable_reloading| test "we can opt-out of entire root directories, and still autoload (enable_autoloading #{enable_reloading})" do on_teardown do remove_const :Foo delete_loaded_feature "foo.rb" end $test_eager_load_eager_loaded_p = false files = [["foo.rb", "Foo = true; $test_eager_load_eager_loaded_p = true"]] with_files(files) do loader = new_loader(dirs: ".", enable_reloading: enable_reloading) loader.do_not_eager_load(".") loader.eager_load assert !$test_eager_load_eager_loaded_p assert Foo end end test "we can opt-out of sudirectories, and still autoload (enable_autoloading #{enable_reloading})" do on_teardown do remove_const :Foo delete_loaded_feature "foo.rb" remove_const :DbAdapters delete_loaded_feature "db_adapters/mysql_adapter.rb" end $test_eager_load_eager_loaded_p = false files = [ ["db_adapters/mysql_adapter.rb", <<-EOS], module DbAdapters::MysqlAdapter end $test_eager_load_eager_loaded_p = true EOS ["foo.rb", "Foo = true"] ] with_files(files) do loader = new_loader(dirs: ".", enable_reloading: enable_reloading) loader.do_not_eager_load("db_adapters") loader.eager_load assert Foo assert !$test_eager_load_eager_loaded_p assert DbAdapters::MysqlAdapter end end test "we can opt-out of files, and still autoload (enable_autoloading #{enable_reloading})" do on_teardown do remove_const :Foo delete_loaded_feature "foo.rb" remove_const :Bar delete_loaded_feature "bar.rb" end $test_eager_load_eager_loaded_p = false files = [ ["foo.rb", "Foo = true"], ["bar.rb", "Bar = true; $test_eager_load_eager_loaded_p = true"] ] with_files(files) do loader = new_loader(dirs: ".", enable_reloading: enable_reloading) loader.do_not_eager_load("bar.rb") loader.eager_load assert Foo assert !$test_eager_load_eager_loaded_p assert Bar end end test "opt-ed out root directories sharing a namespace don't prevent autoload (enable_autoloading #{enable_reloading})" do on_teardown do remove_const :Ns delete_loaded_feature "ns/foo.rb" delete_loaded_feature "ns/bar.rb" end $test_eager_load_eager_loaded_p = false files = [ ["lazylib/ns/foo.rb", "module Ns::Foo; end"], ["eagerlib/ns/bar.rb", "module Ns::Bar; $test_eager_load_eager_loaded_p = true; end"] ] with_files(files) do loader = new_loader(dirs: %w(lazylib eagerlib), enable_reloading: enable_reloading) loader.do_not_eager_load('lazylib') loader.eager_load assert $test_eager_load_eager_loaded_p end end test "opt-ed out subdirectories don't prevent autoloading shared namespaces (enable_autoloading #{enable_reloading})" do on_teardown do remove_const :Ns delete_loaded_feature "ns/foo.rb" delete_loaded_feature "ns/bar.rb" end $test_eager_load_eager_loaded_p = false files = [ ["lazylib/ns/foo.rb", "module Ns::Foo; end"], ["eagerlib/ns/bar.rb", "module Ns::Bar; $test_eager_load_eager_loaded_p = true; end"] ] with_files(files) do loader = new_loader(dirs: %w(lazylib eagerlib), enable_reloading: enable_reloading) loader.do_not_eager_load('lazylib/namespace') loader.eager_load assert $test_eager_load_eager_loaded_p end end end test "eager loading skips repeated files" do $test_eager_loaded_file = nil files = [ ["a/foo.rb", "Foo = 1; $test_eager_loaded_file = :a"], ["b/foo.rb", "Foo = 1; $test_eager_loaded_file = :b"] ] with_files(files) do la = new_loader(dirs: "a") lb = new_loader(dirs: "b") la.eager_load lb.eager_load assert_equal :a, $test_eager_loaded_file end end test "eager loading skips files that would map to already loaded constants" do on_teardown { remove_const :X } $test_eager_loaded_file = false files = [["x.rb", "X = 1; $test_eager_loaded_file = true"]] ::X = 1 with_setup(files) do loader.eager_load assert !$test_eager_loaded_file end end test "eager loading works with symbolic links" do files = [["real/x.rb", "X = true"]] with_files(files) do FileUtils.ln_s("real", "symlink") loader.push_dir("symlink") loader.setup loader.eager_load assert_nil Object.autoload?(:X) end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_exceptions.rb000066400000000000000000000047561376020464000225370ustar00rootroot00000000000000require "test_helper" class TestExceptions < LoaderTest test "raises NameError if the expected constant is not defined" do files = [["typo.rb", "TyPo = 1"]] with_setup(files) do typo_rb = File.realpath("typo.rb") error = assert_raises(Zeitwerk::NameError) { Typo } assert_equal "expected file #{typo_rb} to define constant Typo, but didn't", error.message assert_equal :Typo, error.name end end test "eager loading raises NameError if files do not define the expected constants" do on_teardown do remove_const :X # should be unnecessary, but $LOADED_FEATURES.reject! redefines it remove_const :Y end files = [["x.rb", "Y = 1"]] with_setup(files) do x_rb = File.realpath("x.rb") error = assert_raises(Zeitwerk::NameError) { loader.eager_load } assert_equal "expected file #{x_rb} to define constant X, but didn't", error.message assert_equal :X, error.name end end test "eager loading raises NameError if a namespace has not been loaded yet" do on_teardown do remove_const :CLI delete_loaded_feature 'cli/x.rb' end files = [["cli/x.rb", "module CLI; X = 1; end"]] with_setup(files) do cli_x_rb = File.realpath("cli/x.rb") error = assert_raises(Zeitwerk::NameError) { loader.eager_load } assert_equal "expected file #{cli_x_rb} to define constant Cli::X, but didn't", error.message assert_equal :X, error.name end end test "raises if the file does" do files = [["raises.rb", "Raises = 1; raise 'foo'"]] with_setup(files, rm: false) do assert_raises(RuntimeError, "foo") { Raises } end end test "raises Zeitwerk::NameError if the inflector returns an invalid constant name for a file" do files = [["foo-bar.rb", "FooBar = 1"]] error = assert_raises Zeitwerk::NameError do with_setup(files) {} end assert_equal :"Foo-bar", error.name assert_includes error.message, "wrong constant name Foo-bar" assert_includes error.message, "Tell Zeitwerk to ignore this particular file." end test "raises Zeitwerk::NameError if the inflector returns an invalid constant name for a directory" do files = [["foo-bar/baz.rb", "FooBar::Baz = 1"]] error = assert_raises Zeitwerk::NameError do with_setup(files) {} end assert_equal :"Foo-bar", error.name assert_includes error.message, "wrong constant name Foo-bar" assert_includes error.message, "Tell Zeitwerk to ignore this particular directory." end end zeitwerk-2.4.2/test/lib/zeitwerk/test_explicit_namespace.rb000066400000000000000000000116351376020464000242050ustar00rootroot00000000000000require "test_helper" class TestExplicitNamespace < LoaderTest module Namespace; end test "explicit namespaces are loaded correctly (Object)" do files = [ ["app/models/hotel.rb", "class Hotel; X = 1; end"], ["app/models/hotel/pricing.rb", "class Hotel::Pricing; end"] ] with_setup(files, dirs: "app/models") do assert_kind_of Class, Hotel assert Hotel::X assert Hotel::Pricing end end test "explicit namespaces are loaded correctly (Namespace)" do files = [ ["app/models/hotel.rb", "class #{Namespace}::Hotel; X = 1; end"], ["app/models/hotel/pricing.rb", "class #{Namespace}::Hotel::Pricing; end"] ] with_setup(files, namespace: Namespace, dirs: "app/models") do assert_kind_of Class, Namespace::Hotel assert Namespace::Hotel::X assert Namespace::Hotel::Pricing end end test "explicit namespaces are loaded correctly even if #name is overridden" do files = [ ["app/models/hotel.rb", <<~RUBY], class Hotel def self.name "X" end end RUBY ["app/models/hotel/pricing.rb", "class Hotel::Pricing; end"] ] with_setup(files, dirs: "app/models") do assert Hotel::Pricing end end test "explicit namespaces managed by different instances" do files = [ ["a/m.rb", "module M; end"], ["a/m/n.rb", "M::N = true"], ["b/x.rb", "module X; end"], ["b/x/y.rb", "X::Y = true"], ] with_files(files) do new_loader(dirs: "a") new_loader(dirs: "b") assert M::N assert X::Y end end test "autoloads are set correctly, even if there are autoloads for the same cname in the superclass" do files = [ ["a.rb", "class A; end"], ["a/x.rb", "A::X = :A"], ["b.rb", "class B < A; end"], ["b/x.rb", "B::X = :B"] ] with_setup(files) do assert_kind_of Class, A assert_kind_of Class, B assert_equal :B, B::X end end test "autoloads are set correctly, even if there are autoloads for the same cname in a module prepended to the superclass" do files = [ ["m/x.rb", "M::X = :M"], ["a.rb", "class A; prepend M; end"], ["b.rb", "class B < A; end"], ["b/x.rb", "B::X = :B"] ] with_setup(files) do assert_kind_of Class, A assert_kind_of Class, B assert_equal :B, B::X end end test "autoloads are set correctly, even if there are autoloads for the same cname in other ancestors" do files = [ ["m/x.rb", "M::X = :M"], ["a.rb", "class A; include M; end"], ["b.rb", "class B < A; end"], ["b/x.rb", "B::X = :B"] ] with_setup(files) do assert_kind_of Class, A assert_kind_of Class, B assert_equal :B, B::X end end # As of this writing, a tracer on the :class event does not seem to have any # performance penalty in an ordinary code base. But I prefer to precisely # control that we use a tracer only if needed in case this issue # # https://bugs.ruby-lang.org/issues/14104 # # goes forward. def tracer Zeitwerk::ExplicitNamespace.tracer end test "the tracer starts disabled" do assert !tracer.enabled? end test "simple autoloading does not enable the tracer" do files = [["x.rb", "X = true"]] with_setup(files) do assert !tracer.enabled? assert X assert !tracer.enabled? end end test "autovivification does not enable the tracer" do files = [["foo/bar.rb", "module Foo::Bar; end"]] with_setup(files) do assert !tracer.enabled? assert Foo::Bar assert !tracer.enabled? end end test "explicit namespaces enable the tracer until loaded" do files = [ ["hotel.rb", "class Hotel; end"], ["hotel/pricing.rb", "class Hotel::Pricing; end"] ] with_setup(files) do assert tracer.enabled? assert Hotel assert !tracer.enabled? assert Hotel::Pricing assert !tracer.enabled? end end test "the tracer is enabled until everything is loaded" do files = [ ["a/m.rb", "module M; end"], ["a/m/n.rb", "M::N = true"], ["b/x.rb", "module X; end"], ["b/x/y.rb", "X::Y = true"], ] with_files(files) do new_loader(dirs: "a") assert tracer.enabled? new_loader(dirs: "b") assert tracer.enabled? assert M assert tracer.enabled? assert X assert !tracer.enabled? end end # This is a regression test. test "the tracer handles singleton classes" do files = [ ["hotel.rb", <<-EOS], class Hotel class << self def x 1 end end end EOS ["hotel/pricing.rb", "class Hotel::Pricing; end"], ["car.rb", "class Car; end"], ["car/pricing.rb", "class Car::Pricing; end"], ] with_setup(files) do assert tracer.enabled? assert_equal 1, Hotel.x assert tracer.enabled? end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_for_gem.rb000066400000000000000000000051051376020464000217610ustar00rootroot00000000000000require "test_helper" class TestForGem < LoaderTest test "sets things correctly" do files = [ ["my_gem.rb", <<-EOS], $for_gem_test_loader = Zeitwerk::Loader.for_gem $for_gem_test_loader.enable_reloading $for_gem_test_loader.setup class MyGem end EOS ["my_gem/foo.rb", "class MyGem::Foo; end"], ["my_gem/foo/bar.rb", "class MyGem::Foo::Bar; end"] ] with_files(files) do with_load_path(".") do assert require "my_gem" # what bundler is going to do assert MyGem::Foo::Bar $for_gem_test_loader.unload assert !Object.const_defined?(:MyGem) $for_gem_test_loader.setup assert MyGem::Foo::Bar end end end test "is idempotent" do files = [ ["my_gem.rb", <<-EOS], $for_gem_test_zs << Zeitwerk::Loader.for_gem $for_gem_test_zs.last.enable_reloading $for_gem_test_zs.last.setup class MyGem end EOS ["my_gem/foo.rb", "class MyGem::Foo; end"] ] with_files(files) do with_load_path(".") do $for_gem_test_zs = [] assert require "my_gem" # what bundler is going to do assert MyGem::Foo $for_gem_test_zs.first.unload assert !Object.const_defined?(:MyGem) $for_gem_test_zs.first.setup assert MyGem::Foo assert_equal 2, $for_gem_test_zs.size assert_same $for_gem_test_zs.first, $for_gem_test_zs.last end end end test "configures the gem inflector by default" do on_teardown do remove_const :MyGem delete_loaded_feature "my_gem.rb" end files = [ ["my_gem.rb", <<-EOS], $for_gem_test_loader = Zeitwerk::Loader.for_gem $for_gem_test_loader.setup class MyGem end EOS ["my_gem/foo.rb", "class MyGem::Foo; end"] ] with_files(files) do with_load_path(".") do require "my_gem" assert_instance_of Zeitwerk::GemInflector, $for_gem_test_loader.inflector end end end test "configures the basename of the root file as loader name" do on_teardown do remove_const :MyGem delete_loaded_feature "my_gem.rb" end files = [ ["my_gem.rb", <<-EOS], $for_gem_test_loader = Zeitwerk::Loader.for_gem $for_gem_test_loader.setup class MyGem end EOS ["my_gem/foo.rb", "class MyGem::Foo; end"] ] with_files(files) do with_load_path(".") do require "my_gem" assert_equal "my_gem", $for_gem_test_loader.tag end end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_ignore.rb000066400000000000000000000057641376020464000216410ustar00rootroot00000000000000require "test_helper" require "set" class TestIgnore < LoaderTest test "ignored root directories are ignored" do files = [["x.rb", "X = true"]] with_files(files) do loader.push_dir(".") loader.ignore(".") assert_empty loader.autoloads assert_raises(NameError) { ::X } end end test "ignored files are ignored" do files = [ ["x.rb", "X = true"], ["y.rb", "Y = true"] ] with_files(files) do loader.push_dir(".") loader.ignore("y.rb") loader.setup assert_equal 1, loader.autoloads.size assert ::X assert_raises(NameError) { ::Y } end end test "ignored directories are ignored" do files = [ ["x.rb", "X = true"], ["m/a.rb", "M::A = true"], ["m/b.rb", "M::B = true"], ["m/c.rb", "M::C = true"] ] with_files(files) do loader.push_dir(".") loader.ignore("m") loader.setup assert_equal 1, loader.autoloads.size assert ::X assert_raises(NameError) { ::M } end end test "ignored files are not eager loaded" do files = [ ["x.rb", "X = true"], ["y.rb", "Y = true"] ] with_files(files) do loader.push_dir(".") loader.ignore("y.rb") loader.setup loader.eager_load assert ::X assert_raises(NameError) { ::Y } end end test "ignored directories are not eager loaded" do files = [ ["x.rb", "X = true"], ["m/a.rb", "M::A = true"], ["m/b.rb", "M::B = true"], ["m/c.rb", "M::C = true"] ] with_files(files) do loader.push_dir(".") loader.ignore("m") loader.setup loader.eager_load assert ::X assert_raises(NameError) { ::M } end end test "supports several arguments" do a = "#{Dir.pwd}/a.rb" b = "#{Dir.pwd}/b.rb" loader.ignore(a, b) assert_equal [a, b].to_set, loader.ignored_glob_patterns end test "supports an array" do a = "#{Dir.pwd}/a.rb" b = "#{Dir.pwd}/b.rb" loader.ignore([a, b]) assert_equal [a, b].to_set, loader.ignored_glob_patterns end test "supports glob patterns" do files = [ ["admin/user.rb", "class Admin::User; end"], ["admin/user_test.rb", "class Admin::UserTest < Minitest::Test; end"] ] with_files(files) do loader.push_dir(".") loader.ignore("**/*_test.rb") loader.setup assert Admin::User assert_raises(NameError) { Admin::UserTest } end end test "ignored paths are recomputed on reload" do files = [ ["user.rb", "class User; end"], ["user_test.rb", "class UserTest < Minitest::Test; end"], ] with_files(files) do loader.push_dir(".") loader.ignore("*_test.rb") loader.setup assert User assert_raises(NameError) { UserTest } File.write("post.rb", "class Post; end") File.write("post_test.rb", "class PostTest < Minitest::Test; end") loader.reload assert Post assert_raises(NameError) { PostTest } end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_logging.rb000066400000000000000000000106441376020464000217750ustar00rootroot00000000000000require "test_helper" class TestLogging < LoaderTest def setup super loader.logger = method(:print) end def teardown Zeitwerk::Loader.default_logger = nil loader.logger = nil super end def tagged_message(message) "Zeitwerk@#{loader.tag}: #{message}" end def assert_logged(expected) case expected when String assert_output(tagged_message(expected)) { yield } when Regexp assert_output(/#{tagged_message(expected)}/) { yield } end end test "log! just prints to $stdout" do loader.logger = nil # make sure we are setting something loader.log! message = "test log!" assert_logged(/#{message}\n/) { loader.send(:log, message) } end test "accepts objects that respond to :call" do logger = Object.new def logger.call(message) print message end loader.logger = logger message = "test message :call" assert_logged(message) { loader.send(:log, message) } end test "accepts objects that respond to :debug" do logger = Object.new def logger.debug(message) print message end loader.logger = logger message = "test message :debug" assert_logged(message) { loader.send(:log, message) } end test "new loaders get assigned the default global logger" do assert_nil Zeitwerk::Loader.new.logger Zeitwerk::Loader.default_logger = Object.new assert_same Zeitwerk::Loader.default_logger, Zeitwerk::Loader.new.logger end test "logs loaded files" do files = [["x.rb", "X = true"]] with_files(files) do with_load_path(".") do assert_logged(/constant X loaded from file #{File.realpath("x.rb")}/) do loader.push_dir(".") loader.setup assert X end end end end test "logs required managed files" do files = [["x.rb", "X = true"]] with_files(files) do with_load_path(".") do assert_logged(/constant X loaded from file #{File.realpath("x.rb")}/) do loader.push_dir(".") loader.setup assert require "x" end end end end test "logs autovivified modules" do files = [["admin/user.rb", "class Admin::User; end"]] with_files(files) do with_load_path(".") do assert_logged(/module Admin autovivified from directory #{File.realpath("admin")}/) do loader.push_dir(".") loader.setup assert Admin end end end end test "logs autoload configured for files" do files = [["x.rb", "X = true"]] with_files(files) do assert_logged("autoload set for X, to be loaded from #{File.realpath("x.rb")}") do loader.push_dir(".") loader.setup end end end test "logs autoload configured for directories" do files = [["admin/user.rb", "class Admin::User; end"]] with_files(files) do assert_logged("autoload set for Admin, to be autovivified from #{File.realpath("admin")}") do loader.push_dir(".") loader.setup end end end test "logs preloads" do files = [["x.rb", "X = true"]] with_files(files) do loader.push_dir(".") loader.preload("x.rb") assert_logged(/preloading #{File.realpath("x.rb")}/) do loader.setup end end end test "logs unloads for autoloads" do files = [["x.rb", "X = true"]] with_files(files) do assert_logged(/autoload for X removed/) do loader.push_dir(".") loader.setup loader.reload end end end test "logs unloads for loaded objects" do files = [["x.rb", "X = true"]] with_files(files) do assert_logged(/X unloaded/) do loader.push_dir(".") loader.setup assert X loader.reload end end end test "logs files shadowed by autoloads" do files = [ ["a/foo.rb", "Foo = :a"], ["b/foo.rb", "Foo = :b"] ] with_files(files) do new_loader(dirs: "a") assert_logged(%r(file .*?/b/foo\.rb is ignored because .*?/a/foo\.rb has precedence)) do loader.push_dir("b") loader.setup end end end test "eager loading skips files that would map to already loaded constants" do on_teardown { remove_const :X } ::X = 1 files = [["x.rb", "X = 1"]] with_files(files) do loader.push_dir(".") assert_logged(%r(file .*?/x\.rb is ignored because X is already defined)) do loader.setup end end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_multiple.rb000066400000000000000000000021401376020464000221720ustar00rootroot00000000000000require "test_helper" class TestMultiple < LoaderTest test "multiple independent loaders" do files = [ ["lib0/app0.rb", "module App0; end"], ["lib0/app0/foo.rb", "class App0::Foo; end"], ["lib1/app1/foo.rb", "class App1::Foo; end"], ["lib1/app1/foo/bar/baz.rb", "class App1::Foo::Bar::Baz; end"] ] with_files(files) do new_loader(dirs: "lib0") new_loader(dirs: "lib1") assert App0::Foo assert App1::Foo::Bar::Baz end end test "multiple dependent loaders" do files = [ ["lib0/app0.rb", <<-EOS], module App0 App1 end EOS ["lib0/app0/foo.rb", <<-EOS], class App0::Foo App1::Foo end EOS ["lib1/app1/foo.rb", <<-EOS], class App1::Foo App0 end EOS ["lib1/app1/foo/bar/baz.rb", <<-EOS] class App1::Foo::Bar::Baz App0::Foo end EOS ] with_files(files) do new_loader(dirs: "lib0") new_loader(dirs: "lib1") assert App0::Foo assert App1::Foo::Bar::Baz end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_nested_root_directories.rb000066400000000000000000000023041376020464000252620ustar00rootroot00000000000000require "test_helper" class TestNestedRootDirectories < LoaderTest test "nested root directories do not autovivify modules" do files = [["app/models/concerns/pricing.rb", "module Pricing; end"]] with_setup(files, dirs: %w(app/models app/models/concerns)) do assert_raises(NameError) { Concerns } end end test "nested root directories are ignored even if there is a matching file" do files = [ ["app/models/hotel.rb", "class Hotel; include GeoLoc; end"], ["app/models/concerns/geo_loc.rb", "module GeoLoc; end"], ["app/models/concerns.rb", "module Concerns; end"] ] with_setup(files, dirs: %w(app/models app/models/concerns)) do assert Concerns assert Hotel end end test "eager loading handles nested root directories correctly" do $airplane_eager_loaded = $locatable_eager_loaded = false files = [ ["airplane.rb", "class Airplane; $airplane_eager_loaded = true; end"], ["concerns/locatable.rb", "module Locatable; $locatable_eager_loaded = true; end"] ] with_setup(files, dirs: [".", "concerns"]) do loader.eager_load assert $airplane_eager_loaded assert $locatable_eager_loaded end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_on_load.rb000066400000000000000000000040771376020464000217650ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestOnLoad < LoaderTest test "on_load checks its argument type" do assert_raises(TypeError, "on_load only accepts strings") do loader.on_load(:X) {} end assert_raises(TypeError, "on_load only accepts strings") do loader.on_load(Object) {} end end test "on_load is called in the expected order, no namespace" do files = [ ["a.rb", "class A; end"], ["b.rb", "class B; end"] ] with_setup(files) do x = [] loader.on_load("A") { x << 1 } loader.on_load("B") { x << 2 } loader.on_load("A") { x << 3 } loader.on_load("B") { x << 4 } assert A assert B assert_equal [1, 3, 2, 4], x end end test "on_load is called in the expected order, implicit namespace" do files = [["x/a.rb", "class X::A; end"]] with_setup(files) do x = [] loader.on_load("X") { x << 1 } loader.on_load("X::A") { x << 2 } assert X::A assert_equal [1, 2], x end end test "on_load is called in the expected order, explicit namespace" do files = [["x.rb", "module X; end"], ["x/a.rb", "class X::A; end"]] with_setup(files) do x = [] loader.on_load("X") { x << 1 } loader.on_load("X::A") { x << 2 } assert X::A assert_equal [1, 2], x end end test "on_load survives reloads" do with_setup([["a.rb", "class A; end"]]) do x = 0; loader.on_load("A") { x += 1 } assert A assert_equal 1, x loader.reload assert A assert_equal 2, x end end test "if reloading is disabled, we deplete the hash (performance test)" do on_teardown do remove_const :A delete_loaded_feature "a.rb" end with_files([["a.rb", "class A; end"]]) do loader = new_loader(dirs: ".", enable_reloading: false, setup: false) x = 0; loader.on_load("A") { x = 1 } loader.setup assert !loader.on_load_callbacks.empty? assert A assert_equal 1, x assert loader.on_load_callbacks.empty? end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_preload.rb000066400000000000000000000022461376020464000217740ustar00rootroot00000000000000require "test_helper" class TestPreload < LoaderTest def preloads @preloads ||= ["a.rb", "m/n"] end def assert_preload $a_preloaded = $b_preoloaded = $c_preloaded = $d_preloaded = false files = [ ["a.rb", "A = 1; $a_preloaded = true"], ["m/n/b.rb", "M::N::B = 1; $b_preloaded = true"], ["m/c.rb", "M::C = 1; $c_preloaded = true"], ["d.rb", "D = 1; $d_preloaded = true"] ] with_files(files) do loader.push_dir(".") yield # preload here loader.setup assert $a_preloaded assert $b_preloaded assert !$c_preloaded assert !$d_preloaded end end test "preloads files and directories (multiple args)" do assert_preload do loader.preload(*preloads) end end test "preloads files and directories (array)" do assert_preload do loader.preload(preloads) end end test "preloads files and directories (multiple calls)" do assert_preload do loader.preload(preloads.first) loader.preload(preloads.last) end end test "preloads files after setup too" do assert_preload do loader.setup loader.preload(preloads) end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_push_dir.rb000066400000000000000000000026761376020464000221720ustar00rootroot00000000000000require "test_helper" require "pathname" class TesPushDir < LoaderTest module Namespace; end test "accepts dirs as strings and associates them to the Object namespace" do loader.push_dir(".") assert loader.root_dirs == { Dir.pwd => Object } assert loader.dirs.include?(Dir.pwd) end test "accepts dirs as pathnames and associates them to the Object namespace" do loader.push_dir(Pathname.new(".")) assert loader.root_dirs == { Dir.pwd => Object } assert loader.dirs.include?(Dir.pwd) end test "accepts dirs as strings and associates them to the given namespace" do loader.push_dir(".", namespace: Namespace) assert loader.root_dirs == { Dir.pwd => Namespace } assert loader.dirs.include?(Dir.pwd) end test "accepts dirs as pathnames and associates them to the given namespace" do loader.push_dir(Pathname.new("."), namespace: Namespace) assert loader.root_dirs == { Dir.pwd => Namespace } assert loader.dirs.include?(Dir.pwd) end test "raises on non-existing directories" do dir = File.expand_path("non-existing") e = assert_raises(Zeitwerk::Error) { loader.push_dir(dir) } assert_equal "the root directory #{dir} does not exist", e.message end test "raises if the namespace is not a class or module object" do e = assert_raises(Zeitwerk::Error) { loader.push_dir(".", namespace: :foo) } assert_equal ":foo is not a class or module object, should be", e.message end end zeitwerk-2.4.2/test/lib/zeitwerk/test_reloading.rb000066400000000000000000000115541376020464000223140ustar00rootroot00000000000000require "test_helper" require "fileutils" class TestReloading < LoaderTest module Namespace; end test "enabling reloading after setup raises" do e = assert_raises(Zeitwerk::Error) do loader = Zeitwerk::Loader.new loader.setup loader.enable_reloading end assert_equal "cannot enable reloading after setup", e.message end test "enabling reloading is idempotent, even after setup" do assert loader.reloading_enabled? # precondition loader.setup loader.enable_reloading # should not raise assert loader.reloading_enabled? end test "reloading works if the flag is set (Object)" do files = [ ["x.rb", "X = 1"], # top-level ["y.rb", "module Y; end"], # explicit namespace ["y/a.rb", "Y::A = 1"], ["z/a.rb", "Z::A = 1"] # implicit namespace ] with_setup(files) do assert_equal 1, X assert_equal 1, Y::A assert_equal 1, Z::A y_object_id = Y.object_id z_object_id = Z.object_id File.write("x.rb", "X = 2") File.write("y/a.rb", "Y::A = 2") File.write("z/a.rb", "Z::A = 2") loader.reload assert_equal 2, X assert_equal 2, Y::A assert_equal 2, Z::A assert Y.object_id != y_object_id assert Z.object_id != z_object_id assert_equal 2, X end end test "reloading works if the flag is set (Namespace)" do files = [ ["x.rb", "#{Namespace}::X = 1"], # top-level ["y.rb", "module #{Namespace}::Y; end"], # explicit namespace ["y/a.rb", "#{Namespace}::Y::A = 1"], ["z/a.rb", "#{Namespace}::Z::A = 1"] # implicit namespace ] with_setup(files, namespace: Namespace) do assert_equal 1, Namespace::X assert_equal 1, Namespace::Y::A assert_equal 1, Namespace::Z::A ns_object_id = Namespace.object_id y_object_id = Namespace::Y.object_id z_object_id = Namespace::Z.object_id File.write("x.rb", "#{Namespace}::X = 2") File.write("y/a.rb", "#{Namespace}::Y::A = 2") File.write("z/a.rb", "#{Namespace}::Z::A = 2") loader.reload assert_equal 2, Namespace::X assert_equal 2, Namespace::Y::A assert_equal 2, Namespace::Z::A assert Namespace.object_id == ns_object_id assert Namespace::Y.object_id != y_object_id assert Namespace::Z.object_id != z_object_id assert_equal 2, Namespace::X end end test "reloading raises if the flag is not set" do e = assert_raises(Zeitwerk::ReloadingDisabledError) do loader = Zeitwerk::Loader.new loader.setup loader.reload end assert_equal "can't reload, please call loader.enable_reloading before setup", e.message end test "if reloading is disabled, autoloading metadata shrinks while autoloading (performance test)" do on_teardown do remove_const :X delete_loaded_feature "x.rb" remove_const :Y delete_loaded_feature "y.rb" delete_loaded_feature "y/a.rb" remove_const :Z delete_loaded_feature "z/a.rb" end files = [ ["x.rb", "X = 1"], ["y.rb", "module Y; end"], ["y/a.rb", "Y::A = 1"], ["z/a.rb", "Z::A = 1"] ] with_files(files) do loader = new_loader(dirs: ".", enable_reloading: false) assert !loader.autoloads.empty? assert_equal 1, X assert_equal 1, Y::A assert_equal 1, Z::A assert loader.autoloads.empty? assert loader.to_unload.empty? end end test "if reloading is disabled, autoloading metadata shrinks while eager loading (performance test)" do on_teardown do remove_const :X delete_loaded_feature "x.rb" remove_const :Y delete_loaded_feature "y.rb" delete_loaded_feature "y/a.rb" remove_const :Z delete_loaded_feature "z/a.rb" end files = [ ["x.rb", "X = 1"], ["y.rb", "module Y; end"], ["y/a.rb", "Y::A = 1"], ["z/a.rb", "Z::A = 1"] ] with_files(files) do loader = new_loader(dirs: ".", enable_reloading: false) assert !loader.autoloads.empty? assert !Zeitwerk::Registry.autoloads.empty? loader.eager_load assert loader.autoloads.empty? assert Zeitwerk::Registry.autoloads.empty? assert loader.to_unload.empty? end end test "reloading supports deleted root directories" do files = [["a/x.rb", "X = 1"], ["b/y.rb", "Y = 1"]] with_setup(files, dirs: %w(a b)) do assert X assert Y FileUtils.rm_rf("b") loader.reload assert X end end test "you can eager load again after reloading" do $test_eager_load_after_reload = 0 files = [["x.rb", "$test_eager_load_after_reload += 1; X = 1"]] with_setup(files) do loader.eager_load assert_equal 1, $test_eager_load_after_reload loader.reload loader.eager_load assert_equal 2, $test_eager_load_after_reload end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_require_interaction.rb000066400000000000000000000145201376020464000244170ustar00rootroot00000000000000require "test_helper" require "pathname" class TestRequireInteraction < LoaderTest def assert_required(str) assert_equal true, require(str) end def assert_not_required(str) assert_equal false, require(str) end test "our decorated require returns true or false as expected" do on_teardown do remove_const :User delete_loaded_feature "user.rb" end files = [["user.rb", "class User; end"]] with_files(files) do with_load_path(".") do assert_required "user" assert_not_required "user" end end end test "our decorated require returns true or false as expected (Pathname)" do on_teardown do remove_const :User delete_loaded_feature "user.rb" end files = [["user.rb", "class User; end"]] pathname_for_user = Pathname.new("user") with_files(files) do with_load_path(".") do assert_required pathname_for_user assert_not_required pathname_for_user end end end test "autoloading makes require idempotent even with a relative path" do files = [["user.rb", "class User; end"]] with_setup(files, load_path: ".") do assert User assert_not_required "user" end end test "a required top-level file is still detected as autoloadable" do files = [["user.rb", "class User; end"]] with_setup(files, load_path: ".") do assert_required "user" loader.unload assert !Object.const_defined?(:User, false) loader.setup assert User end end test "a required top-level file is still detected as autoloadable (Pathname)" do files = [["user.rb", "class User; end"]] with_setup(files, load_path: ".") do assert_required Pathname.new("user") assert User loader.unload assert !Object.const_defined?(:User, false) loader.setup assert User end end test "require autovivifies as needed" do files = [ ["app/models/admin/user.rb", "class Admin::User; end"], ["app/controllers/admin/users_controller.rb", "class Admin::UsersController; end"] ] dirs = %w(app/models app/controllers) with_setup(files, dirs: dirs, load_path: dirs) do assert_required "admin/user" assert Admin::User assert Admin::UsersController loader.unload assert !Object.const_defined?(:Admin) end end test "files deep down the current visited level are recognized as managed (implicit)" do files = [["foo/bar/baz/zoo/woo.rb", "Foo::Bar::Baz::Zoo::Woo = 1"]] with_setup(files, load_path: ".") do assert_required "foo/bar/baz/zoo/woo" assert loader.unloadable_cpath?("Foo::Bar::Baz::Zoo::Woo") end end test "files deep down the current visited level are recognized as managed (explicit)" do files = [ ["foo/bar/baz/zoo.rb", "module Foo::Bar::Baz::Zoo; include Wadus; end"], ["foo/bar/baz/zoo/wadus.rb", "module Foo::Bar::Baz::Zoo::Wadus; end"], ["foo/bar/baz/zoo/woo.rb", "Foo::Bar::Baz::Zoo::Woo = 1"] ] with_setup(files, load_path: ".") do assert_required "foo/bar/baz/zoo/woo" assert loader.unloadable_cpath?("Foo::Bar::Baz::Zoo::Wadus") assert loader.unloadable_cpath?("Foo::Bar::Baz::Zoo::Woo") end end test "require works well with explicit namespaces" do files = [ ["hotel.rb", "class Hotel; X = true; end"], ["hotel/pricing.rb", "class Hotel::Pricing; end"] ] with_setup(files, load_path: ".") do assert_required "hotel/pricing" assert Hotel::Pricing assert Hotel::X end end test "you can autoload yourself in a required file" do files = [ ["my_gem.rb", <<-EOS], loader = Zeitwerk::Loader.new loader.push_dir(__dir__) loader.enable_reloading loader.setup module MyGem; end EOS ["my_gem/foo.rb", "class MyGem::Foo; end"] ] with_files(files) do with_load_path(Dir.pwd) do assert_required "my_gem" end end end test "does not autovivify while loading an explicit namespace, constant is not yet defined - file first" do files = [ ["hotel.rb", <<-EOS], loader = Zeitwerk::Loader.new loader.push_dir(__dir__) loader.enable_reloading loader.setup Hotel.name class Hotel end EOS ["hotel/pricing.rb", "class Hotel::Pricing; end"] ] with_files(files) do iter = ->(dir, &block) do if dir == Dir.pwd block.call("hotel.rb") block.call("hotel") end end Dir.stub :foreach, iter do e = assert_raises(NameError) do with_load_path(Dir.pwd) do assert_required "hotel" end end assert_match %r/Hotel/, e.message end end end test "does not autovivify while loading an explicit namespace, constant is not yet defined - file last" do files = [ ["hotel.rb", <<-EOS], loader = Zeitwerk::Loader.new loader.push_dir(__dir__) loader.enable_reloading loader.setup Hotel.name class Hotel end EOS ["hotel/pricing.rb", "class Hotel::Pricing; end"] ] with_files(files) do iter = ->(dir, &block) do if dir == Dir.pwd block.call("hotel") block.call("hotel.rb") end end Dir.stub :foreach, iter do e = assert_raises(NameError) do with_load_path(Dir.pwd) do assert_required "hotel" end end assert_match %r/Hotel/, e.message end end end test "symlinks in autoloaded files set by Zeitwerk" do files = [["real/app/models/user.rb", "class User; end"]] with_files(files) do FileUtils.ln_s("real", "symlink") loader.push_dir("symlink/app/models") loader.setup with_load_path("symlink/app/models") do assert User assert_not_required "user" loader.reload assert_required "user" end end end test "symlinks in autoloaded files resolved by Ruby" do files = [["real/app/models/user.rb", "class User; end"]] with_files(files) do FileUtils.ln_s("real", "symlink") loader.push_dir("symlink/app/models") loader.setup with_load_path("symlink/app/models") do assert_required "user" loader.reload assert_required "user" end end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_ruby_compatibility.rb000066400000000000000000000220371376020464000242600ustar00rootroot00000000000000require "test_helper" require "pathname" class TestRubyCompatibility < LoaderTest # We decorate Kernel#require in lib/zeitwerk/kernel.rb to be able to log # autoloads and to record what has been autoloaded so far. test "autoload calls Kernel#require" do files = [["x.rb", "X = true"]] with_files(files) do loader.push_dir(".") loader.setup $trc_require_has_been_called = false $trc_autoload_path = File.expand_path("x.rb") begin Kernel.module_eval do alias_method :trc_original_require, :require def require(path) $trc_require_has_been_called = true if path == $trc_autoload_path trc_original_require(path) end end assert X assert $trc_require_has_been_called ensure Kernel.module_eval do remove_method :require define_method :require, instance_method(:trc_original_require) remove_method :trc_original_require end end end end # Zeitwerk has to be called as soon as explicit namespaces are defined, to be # able to configure autoloads for their children before the class or module # body is interpreted. If explicit namespaces are found, Zeitwerk sets a trace # point on the :class event with that purpose. # # This is key because the body could reference child constants at the # top-level, mixins are a common use case. test "TracePoint emits :class events" do on_teardown do @tp.disable remove_const :C, from: self.class end called = false @tp = TracePoint.new(:class) { called = true } @tp.enable class C; end assert called end # We configure autoloads on directories to autovivify modules on demand, and # lazily descend to set autoloads for their children. This is more efficient, # specially for large code bases. test "you can set autoloads on directories" do files = ["admin/users_controller.rb", "class UsersController; end"] with_setup(files) do assert_equal "#{Dir.pwd}/admin", Object.autoload?(:Admin) end end # While unloading constants we leverage this property to avoid lookups in # $LOADED_FEATURES for strings that we know are not going to be there. test "directories are not included in $LOADED_FEATURES" do with_files([]) do FileUtils.mkdir("admin") loader.push_dir(".") loader.setup assert Admin assert !$LOADED_FEATURES.include?(File.realpath("admin")) end end # We exploit this one to simplify the detection of explicit namespaces. # # Let's suppose `Admin` is an explicit namespace and scanning finds first a # directory called `admin`. We set at that point an autoload for `Admin` and # that will require that directory. If later on, scanning finds `admin.rb`, we # just set the autoload again, and change the target file. # # This way, we do not need to keep state or do an a posteriori pass, can set # autoloads linearly as scanning progresses. test "an autoload can be overridden" do on_teardown { remove_const :X } files = [ ["x0/x.rb", "X = 0"], ["x1/x.rb", "X = 1"] ] with_files(files) do Object.autoload(:X, File.expand_path("x0/x.rb")) Object.autoload(:X, File.expand_path("x1/x.rb")) assert_equal 1, X end end # I believe Zeitwerk does not exploit this one now. Let's leave it here to # keep track of undocumented corner cases anyway. test "const_defined? is true for autoloads and does not load the file, if the file exists" do on_teardown { remove_const :X } files = [["x.rb", "$const_defined_does_not_trigger_autoload = false; X = true"]] with_files(files) do $const_defined_does_not_trigger_autoload = true Object.autoload(:X, File.expand_path("x.rb")) assert Object.const_defined?(:X, false) assert $const_defined_does_not_trigger_autoload end end # Unloading removes autoloads by calling remove_const. It is convenient that # remove_const does not execute the autoload because it would be surprising, # and slower, that those unused files got loaded precisely while unloading. test "remove_const does not trigger an autoload" do files = [["x.rb", "$remove_const_does_not_trigger_autoload = false; X = 1"]] with_files(files) do $remove_const_does_not_trigger_autoload = true Object.autoload(:X, File.expand_path("x.rb")) remove_const :X assert $remove_const_does_not_trigger_autoload end end # Zeitwerk uses this property when unloading to be able to differentiate when # it is removing and autoload, and when it is unloading an actual loaded # object. test "autoloading removes the autoload configuration in the parent" do on_teardown do remove_const :X delete_loaded_feature "x.rb" end files = [["x.rb", "X = true"]] with_files(files) do Object.autoload(:X, File.expand_path("x.rb")) assert Object.autoload?(:X) assert X assert !Object.autoload?(:X) end end # We use remove_const to delete autoload configurations while unloading. # Otherwise, the configured files or directories could become stale. test "autoload configuration can be deleted with remove_const" do files = [["x.rb", "X = true"]] with_files(files) do Object.autoload(:X, File.expand_path("x.rb")) assert Object.autoload?(:X) remove_const :X assert !Object.autoload?(:X) end end # Thanks to this the code that unloads can just blindly issue remove_const # calls without catching exceptions. test "remove_const works on constants with an autoload even if the file did not define them" do on_teardown do remove_const :Foo remove_const :NOT_FOO delete_loaded_feature "foo.rb" end files = [["foo.rb", "NOT_FOO = 1"]] with_files(files) do with_load_path(Dir.pwd) do begin Object.autoload(:Foo, "foo") assert_raises(NameError) { Foo } end end end end # This edge case justifies the need for the inceptions collection in the # registry. test "an autoload on yourself is ignored" do files = [["foo.rb", <<-EOS]] Object.autoload(:Foo, __FILE__) $trc_inception = !Object.autoload?(:Foo) Foo = 1 EOS with_files(files) do loader.push_dir(".") loader.setup with_load_path do $trc_inception = false require "foo" end assert $trc_inception end end # Same as above, adding some depth. test "an autoload on a file being required at some point up in the call chain is also ignored" do files = [ ["foo.rb", <<-EOS], require 'bar' Foo = 1 EOS ["bar.rb", <<-EOS] Bar = true Object.autoload(:Foo, File.realpath('foo.rb')) $trc_inception = !Object.autoload?(:Foo) EOS ] with_files(files) do loader.push_dir(".") loader.setup with_load_path do $trc_inception = false require "foo" end assert $trc_inception end end # This is why we issue a lazy_subdirs.delete call in the tracer block, to # ignore events triggered by reopenings. test "tracing :class calls you back on creation and on reopening" do on_teardown do @tracer.disable remove_const :C, from: self.class remove_const :M, from: self.class end traced = [] @tracer = TracePoint.trace(:class) do |tp| traced << tp.self end 2.times do class C; end module M; end end assert_equal [C, M, C, M], traced end # Computing hash codes is costly and we want the tracer to be as efficient as # possible. The callback doesn't short-circuit anonymous classes and modules # because Class.new and Module.new do not trigger it, but if in the future # they do we could benchmark if we should change event.self.name before the # deletion call. test "trace points on the :class events don't get called on Class.new and Module.new" do on_teardown { @tracer.disable } $tracer_for_anonymous_class_and_modules_called = false @tracer = TracePoint.trace(:class) { $tracer_for_anonymous_class_and_modules_called = true } Class.new Module.new assert !$tracer_for_anonymous_class_and_modules_called end # If the user issues a require call with a Pathname object for a path that is # autoloadable, we are able to autoload because $LOADED_FEATURES.last returns # the real path as a string and loader_for is able to find its loader. During # unloading, we find and delete strings in $LOADED_FEATURES too. # # This is not a hard requirement, we could work around it if $LOADED_FEATURES # stored pathnames. But the code is simpler if this property holds. test "required pathnames end up as strings in $LOADED_FEATURES" do on_teardown do remove_const :X $LOADED_FEATURES.pop end files = [["x.rb", "X = 1"]] with_files(files) do with_load_path(".") do assert_equal true, require(Pathname.new("x")) assert_equal 1, X assert_equal File.realpath("x.rb"), $LOADED_FEATURES.last end end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_shadowed.rb000066400000000000000000000016411376020464000221420ustar00rootroot00000000000000require "test_helper" class TestShadowed < LoaderTest test "does not autoload from a shadowed file" do on_teardown { remove_const :X } ::X = 1 files = [["x.rb", "X = 2"]] with_setup(files) do assert_equal 1, ::X loader.reload assert_equal 1, ::X end end test "autoloads from a shadowed implicit namespace" do on_teardown { remove_const :M } mod = Module.new ::M = mod files = [["m/x.rb", "M::X = true"]] with_setup(files) do assert M::X loader.reload assert_same mod, M assert M::X end end test "autoloads from a shadowed explicit namespace" do on_teardown { remove_const :M } mod = Module.new ::M = mod files = [ ["m.rb", "class M; end"], ["m/x.rb", "M::X = true"] ] with_setup(files) do assert M::X loader.reload assert_same mod, M assert M::X end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_sti_old_school_workaround.rb000066400000000000000000000031371376020464000256250ustar00rootroot00000000000000require "test_helper" # Rails applications are expected to preload tree leafs in STIs. Using requires # is the old school way to address this and it is somewhat tricky. Let's have a # test to make sure the circularity works. class TestOldSchoolWorkaroundSTI < LoaderTest def files [ ["a.rb", <<-EOS], class A require 'b' end $test_sti_loaded << 'A' EOS ["b.rb", <<-EOS], class B < A require 'c' end $test_sti_loaded << 'B' EOS ["c.rb", <<-EOS], class C < B require 'd1' require 'd2' end $test_sti_loaded << 'C' EOS ["d1.rb", "class D1 < C; end; $test_sti_loaded << 'D1'"], ["d2.rb", "class D2 < C; end; $test_sti_loaded << 'D2'"] ] end def with_setup original_verbose = $VERBOSE $VERBOSE = nil # To avoid circular require warnings. $test_sti_loaded = [] super(files, load_path: ".") do yield end ensure $VERBOSE = original_verbose end def assert_all_loaded assert_equal %w(A B C D1 D2), $test_sti_loaded.sort end test "loading the root loads everything" do with_setup do assert A assert_all_loaded end end test "loading a root child loads everything" do with_setup do assert B assert_all_loaded end end test "loading an intermediate descendant loads everything" do with_setup do assert C assert_all_loaded end end test "loading a leaf loads everything" do with_setup do assert D1 assert_all_loaded end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_top_level.rb000066400000000000000000000043221376020464000223340ustar00rootroot00000000000000require "test_helper" class TestTopLevel < LoaderTest module Namespace; end test "autoloads a simple constant in a top-level file (Object)" do files = [["x.rb", "X = true"]] with_setup(files) do assert X end end test "autoloads a simple constant in a top-level file (Namespace)" do files = [["x.rb", "#{Namespace}::X = true"]] with_setup(files, namespace: Namespace) do assert Namespace::X end end test "autoloads a simple class in a top-level file (Object)" do files = [["app/models/user.rb", "class User; end"]] with_setup(files, dirs: "app/models") do assert User end end test "autoloads a simple class in a top-level file (Namespace)" do files = [["app/models/user.rb", "class #{Namespace}::User; end"]] with_setup(files, namespace: Namespace, dirs: "app/models") do assert Namespace::User end end test "autoloads several top-level classes" do files = [ ["app/models/user.rb", "class User; end"], ["app/controllers/users_controller.rb", "class UsersController; User; end"] ] with_setup(files, dirs: %w(app/models app/controllers)) do assert UsersController end end test "autoloads only the first of multiple occurrences" do files = [ ["app/models/user.rb", "User = :model"], ["app/decorators/user.rb", "User = :decorator"], ] with_setup(files, dirs: %w(app/models app/decorators)) do assert_equal :model, User end end test "anything other than Ruby and visible directories is ignored" do files = [ ["x.txt", ""], # Programmer notes ["x.lua", ""], # Lua files for Redis ["x.yaml", ""], # Included configuration ["x.json", ""], # Included configuration ["x.erb", ""], # Included template ["x.jpg", ""], # Included image ["x.rb~", ""], # Emacs auto backup ["#x.rb#", ""], # Emacs auto save [".filename.swp", ""], # Vim swap file ["4913", ""], # May be created by Vim [".idea/workspace.xml", ""] # RubyMine ] with_setup(files) do assert_empty loader.autoloads end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_unload.rb000066400000000000000000000072611376020464000216320ustar00rootroot00000000000000require "test_helper" class TestUnload < LoaderTest module Namespace; end test "unload removes all autoloaded constants (Object)" do files = [ ["user.rb", "class User; end"], ["admin/root.rb", "class Admin::Root; end"] ] with_setup(files) do assert User assert Admin::Root admin = Admin loader.unload assert !Object.const_defined?(:User) assert !Object.const_defined?(:Admin) assert !admin.const_defined?(:Root) end end test "unload removes all autoloaded constants (Namespace)" do files = [ ["user.rb", "class #{Namespace}::User; end"], ["admin/root.rb", "class #{Namespace}::Admin::Root; end"] ] with_setup(files, namespace: Namespace) do assert Namespace::User assert Namespace::Admin::Root admin = Namespace::Admin loader.unload assert !Namespace.const_defined?(:User) assert !Namespace.const_defined?(:Admin) assert !admin.const_defined?(:Root) end end test "unload removes autoloaded constants, even if #name is overridden" do files = [["x.rb", <<~RUBY]] module X def self.name "Y" end end RUBY with_setup(files) do assert X loader.unload assert !Object.const_defined?(:X) end end test "unload removes non-executed autoloads" do files = [["x.rb", "X = true"]] with_setup(files) do # This does not autolaod, see the compatibility test. assert Object.const_defined?(:X) loader.unload assert !Object.const_defined?(:X) end end test "unload clears internal caches" do files = [ ["app/user.rb", "class User; end"], ["app/api/v1/users_controller.rb", "class Api::V1::UsersController; end"], ["app/admin/root.rb", "class Admin::Root; end"], ["lib/user.rb", "class User; end"] ] with_setup(files, dirs: %w(app lib)) do assert User assert Api::V1::UsersController assert !loader.autoloads.empty? assert !loader.autoloaded_dirs.empty? assert !loader.to_unload.empty? assert !loader.lazy_subdirs.empty? loader.unload assert loader.autoloads.empty? assert loader.autoloaded_dirs.empty? assert loader.to_unload.empty? assert loader.lazy_subdirs.empty? end end test "unload does not assume autoloaded constants are still there" do files = [["x.rb", "X = true"]] with_setup(files) do assert X assert remove_const(:X) # user removed the constant by hand loader.unload # should not raise end end test "already existing namespaces are not reset" do on_teardown do remove_const :ActiveStorage delete_loaded_feature "active_storage.rb" end files = [ ["lib/active_storage.rb", "module ActiveStorage; end"], ["app/models/active_storage/blob.rb", "class ActiveStorage::Blob; end"] ] with_files(files) do with_load_path("lib") do require "active_storage" loader.push_dir("app/models") loader.setup assert ActiveStorage::Blob loader.unload assert ActiveStorage end end end test "unload clears explicit namespaces associated" do files = [ ["a/m.rb", "module M; end"], ["a/m/n.rb", "M::N = true"], ["b/x.rb", "module X; end"], ["b/x/y.rb", "X::Y = true"], ] with_files(files) do la = new_loader(dirs: "a") assert Zeitwerk::ExplicitNamespace.cpaths["M"] == la lb = new_loader(dirs: "b") assert Zeitwerk::ExplicitNamespace.cpaths["X"] == lb la.unload assert_nil Zeitwerk::ExplicitNamespace.cpaths["M"] assert Zeitwerk::ExplicitNamespace.cpaths["X"] == lb end end end zeitwerk-2.4.2/test/lib/zeitwerk/test_unloadable_cpath.rb000066400000000000000000000033561376020464000236360ustar00rootroot00000000000000require "test_helper" require "set" class TestUnloadableCpath < LoaderTest test "a loader that has loading nothing, has nothing to unload" do files = [["x.rb", "X = true"]] with_setup(files) do assert_empty loader.unloadable_cpaths assert !loader.unloadable_cpath?("X") end end test "a loader that loaded some stuff has that stuff to be unloaded if reloading is enabled" do files = [ ["m/x.rb", "M::X = true"], ["m/y.rb", "M::Y = true"], ["z.rb", "Z = true"] ] with_setup(files) do assert M::X assert_equal %w(M M::X), loader.unloadable_cpaths assert loader.unloadable_cpath?("M") assert loader.unloadable_cpath?("M::X") assert !loader.unloadable_cpath?("M::Y") assert !loader.unloadable_cpath?("Z") end end test "unloadable_cpaths returns actual constant paths even if #name is overridden" do files = [["m.rb", <<~RUBY], ["m/c.rb", "M::C = true"]] module M def self.name "X" end end RUBY with_setup(files) do assert M::C assert loader.unloadable_cpath?("M::C") end end test "a loader that loaded some stuff has nothing to unload if reloading is disabled" do on_teardown do remove_const :M delete_loaded_feature "m/x.rb" delete_loaded_feature "m/y.rb" remove_const :Z delete_loaded_feature "z.rb" end files = [ ["m/x.rb", "M::X = true"], ["m/y.rb", "M::Y = true"], ["z.rb", "Z = true"] ] with_files(files) do loader = new_loader(dirs: ".", enable_reloading: false) assert M::X assert M::Y assert Z assert_empty loader.unloadable_cpaths assert loader.to_unload.empty? end end end zeitwerk-2.4.2/test/support/000077500000000000000000000000001376020464000160605ustar00rootroot00000000000000zeitwerk-2.4.2/test/support/delete_loaded_feature.rb000066400000000000000000000002331376020464000226700ustar00rootroot00000000000000module DeleteLoadedFeature def delete_loaded_feature(path) $LOADED_FEATURES.delete_if do |realpath| realpath.end_with?(path) end end end zeitwerk-2.4.2/test/support/loader_test.rb000066400000000000000000000040011376020464000207050ustar00rootroot00000000000000class LoaderTest < Minitest::Test TMP_DIR = File.expand_path("../tmp", __dir__) attr_reader :loader def setup @loader = new_loader(setup: false) end # We enable reloading in the reloaders of the test suite to have a robust # cleanup of constants. # # There are gems that allow you to run tests in forked processes and you do # not need to care, but JRuby does not support forking, and I prefer to be # ready for the day in which Zeitwerk runs on JRuby. def new_loader(dirs: [], enable_reloading: true, setup: true) Zeitwerk::Loader.new.tap do |loader| Array(dirs).each do |dir| loader.push_dir(dir) end loader.enable_reloading if enable_reloading loader.setup if setup end end def reset_constants Zeitwerk::Registry.loaders.each(&:unload) end def reset_registry Zeitwerk::Registry.loaders.clear Zeitwerk::Registry.loaders_managing_gems.clear end def reset_explicit_namespace Zeitwerk::ExplicitNamespace.cpaths.clear Zeitwerk::ExplicitNamespace.tracer.disable end def teardown reset_constants reset_registry reset_explicit_namespace end def mkdir_test FileUtils.rm_rf(TMP_DIR) FileUtils.mkdir_p(TMP_DIR) end def with_files(files, rm: true) mkdir_test Dir.chdir(TMP_DIR) do files.each do |fname, contents| FileUtils.mkdir_p(File.dirname(fname)) File.write(fname, contents) end begin yield ensure mkdir_test if rm end end end def with_load_path(dirs = loader.dirs) Array(dirs).each { |dir| $LOAD_PATH.push(dir) } yield ensure Array(dirs).each { |dir| $LOAD_PATH.delete(dir) } end def with_setup(files, dirs: ".", namespace: Object, load_path: nil, rm: true) with_files(files, rm: rm) do Array(dirs).each { |dir| loader.push_dir(dir, namespace: namespace) } loader.setup if load_path with_load_path(load_path) { yield } else yield end end end end zeitwerk-2.4.2/test/support/on_teardown.rb000066400000000000000000000001721376020464000207240ustar00rootroot00000000000000module OnTeardown def on_teardown define_singleton_method(:teardown) do yield super() end end end zeitwerk-2.4.2/test/support/remove_const.rb000066400000000000000000000001511376020464000211050ustar00rootroot00000000000000module RemoveConst def remove_const(cname, from: Object) from.send(:remove_const, cname) end end zeitwerk-2.4.2/test/support/test_macro.rb000066400000000000000000000002321376020464000205420ustar00rootroot00000000000000module TestMacro def test(description, &block) method_name = "test_#{description}".gsub(/\W/, "_") define_method(method_name, &block) end end zeitwerk-2.4.2/test/test_helper.rb000066400000000000000000000007101376020464000172050ustar00rootroot00000000000000require "minitest/autorun" require "minitest/focus" require "minitest/reporters" Minitest::Reporters.use!(Minitest::Reporters::DefaultReporter.new) require "zeitwerk" require "support/test_macro" require "support/delete_loaded_feature" require "support/loader_test" require "support/remove_const" require "support/on_teardown" Minitest::Test.class_eval do extend TestMacro include DeleteLoadedFeature include RemoveConst include OnTeardown end zeitwerk-2.4.2/zeitwerk.gemspec000066400000000000000000000020271376020464000165770ustar00rootroot00000000000000require_relative "lib/zeitwerk/version" Gem::Specification.new do |spec| spec.name = "zeitwerk" spec.summary = "Efficient and thread-safe constant autoloader" spec.description = <<-EOS Zeitwerk implements constant autoloading with Ruby semantics. Each gem and application may have their own independent autoloader, with its own configuration, inflector, and logger. Supports autoloading, reloading, and eager loading. EOS spec.author = "Xavier Noria" spec.email = 'fxn@hashref.com' spec.license = "MIT" spec.homepage = "https://github.com/fxn/zeitwerk" spec.files = Dir["README.md", "MIT-LICENSE", "lib/**/*.rb"] spec.version = Zeitwerk::VERSION spec.metadata = { "homepage_uri" => "https://github.com/fxn/zeitwerk", "changelog_uri" => "https://github.com/fxn/zeitwerk/blob/master/CHANGELOG.md", "source_code_uri" => "https://github.com/fxn/zeitwerk", "bug_tracker_uri" => "https://github.com/fxn/zeitwerk/issues" } spec.required_ruby_version = ">= 2.4.4" end