pax_global_header00006660000000000000000000000064144226700210014510gustar00rootroot0000000000000052 comment=c3c0ecbd1b7fdbd11e26aac85630ac5b42aeb337 zeitwerk-2.6.8/000077500000000000000000000000001442267002100133715ustar00rootroot00000000000000zeitwerk-2.6.8/.github/000077500000000000000000000000001442267002100147315ustar00rootroot00000000000000zeitwerk-2.6.8/.github/workflows/000077500000000000000000000000001442267002100167665ustar00rootroot00000000000000zeitwerk-2.6.8/.github/workflows/ci.yml000066400000000000000000000011511442267002100201020ustar00rootroot00000000000000name: "CI" on: push: pull_request: branches: - "main" jobs: test: strategy: fail-fast: false matrix: os: - "ubuntu-latest" - "macos-latest" - "windows-latest" ruby-version: - "2.5" - "2.6" - "2.7" - "3.0" - "3.1" - "3.2" - "head" runs-on: ${{ matrix.os }} steps: - uses: "actions/checkout@v3" - uses: "ruby/setup-ruby@v1" with: ruby-version: ${{ matrix.ruby-version }} bundler-cache: true - run: "bundle exec rake" zeitwerk-2.6.8/.github/workflows/keep-an-eye-on-ruby-head.yml000066400000000000000000000010301442267002100240730ustar00rootroot00000000000000name: "Keep an eye on Ruby HEAD" on: workflow_dispatch: schedule: - cron: "11 9 * * *" jobs: test: strategy: fail-fast: false matrix: os: - "ubuntu-latest" - "macos-latest" - "windows-latest" ruby-version: - "head" runs-on: ${{ matrix.os }} steps: - uses: "actions/checkout@v3" - uses: "ruby/setup-ruby@v1" with: ruby-version: ${{ matrix.ruby-version }} bundler-cache: true - run: "bundle exec rake" zeitwerk-2.6.8/.gitignore000066400000000000000000000000341442267002100153560ustar00rootroot00000000000000Gemfile.lock test/tmp *.gem zeitwerk-2.6.8/CHANGELOG.md000066400000000000000000000503761442267002100152150ustar00rootroot00000000000000# CHANGELOG ## 2.6.8 (28 April 2023) * The new `Zeitwerk::Loader.for_gem_extension` gives you a loader configured according to the conventions of a [gem extension](https://guides.rubygems.org/name-your-gem/). Please check its [documentation](https://github.com/fxn/zeitwerk#for_gem_extension) for further details. ## 2.6.7 (10 February 2023) * Reset module state on `Zeitwerk::NameError`. If an autoload is triggered, the file is loaded successfully, but the expected constant does not get defined, Ruby resets the state of the module. In particular, `autoload?` returns `nil` for that constant name, and `constants` does not include the constant name (starting with Ruby 3.1). Zeitwerk is more strict, not defining the expected constant is an error condition and the loader raises `Zeitwerk::NameError`. But this happens during the `require` call and the exception prevents Ruby from doing that cleanup. With this change, the parent module is left in a state that makes more sense and is consistent with what Ruby does. * A message is logged if an autoload did not define the expected constant. When that happens, `Zeitwerk::NameError` is raised and you normally see the exception. But if the error is shallowed, and you are inspecting the logs to investigate something, this new message may be helpful. * By default, `Zeitwerk::Loader#dirs` filters ignored root directories out. Please, pass `ignored: true` if you want them included. It is very strange to configure a root directory and also ignore it, the edge case is supported only for completeness. However, in that case, client code listing root directories rarely needs the ignored ones. * Documentation improvements. * Enforcement of private interfaces continues with another gradual patch. ## 2.6.6 (8 November 2022) * The new `eager_load_namespace` had a bug when eager loading certain namespaces with collapsed directories. This has been fixed. ## 2.6.5 (6 November 2022) * Controlled errors in a couple of situations: - Attempting to eager load or reload without previously invoking `setup` now raises `Zeitwerk::SetupRequired`. - The method `Zeitwerk::Loader#push_dir` raises `Zeitwerk::Error` if it gets an anonymous custom namespace. These should be backwards compatible, because they raise in circumstances that didn't work anyway. The goal here is to provide a meaningful error upfront. * Enforcement of private interfaces continues with another gradual patch. ## 2.6.4 (1 November 2022) Ruby does not have gem-level visibility, so sometimes you need things to be `public` for them to be accessible internally. But they do not belong to the public interface of the gem. A method that is undocumented and marked as `@private` in the source code is clearly private API, regardless of its formal Ruby visibility. This release starts a series of gradual patches in which private interface is enforced with stricter formal visibility. ## 2.6.3 (31 October 2022) * `v2.6.2` introduced a regression in the logic that checks whether two loaders want to manage the same root directories. It has been fixed. ## 2.6.2 (31 October 2022) * `Zeitwerk::Loader#load_file` allows you to load an individual Ruby file. Check its [documentation](https://github.com/fxn/zeitwerk#loading-individual-files) for details. * `Zeitwerk::Loader#eager_load_dir` allows you to eager load a directory, recursively. Check its [documentation](https://github.com/fxn/zeitwerk#eager-load-directories) for details. * `Zeitwerk::Loader#eager_load_namespace` allows you to eager a namespace, recursively. Namespaces are global, this method loads only what the receiver manages from that namespace, if anything. Check its [documentation](https://github.com/fxn/zeitwerk#eager-load-namespaces) for details. * `Zeitwerk::Loader.eager_load_namespace` broadcasts `eager_load_namespace` to all registered loaders. Check its [documentation](https://github.com/fxn/zeitwerk#eager-load-namespaces-shared-by-several-loaders) for details. * Documents [shadowed files](https://github.com/fxn/zeitwerk#shadowed-files). They always existed, but were not covered by the documentation. * Other assorted documentation improvements. ## 2.6.1 (1 October 2022) * `Zeitwerk::Loader#dirs` allows you to instrospect the root directories configured in the receiver. Please check its [documentation](https://github.com/fxn/zeitwerk#introspection) for details. ## 2.6.0 (13 June 2022) * Directories are processed in lexicographic order. Different file systems may list directories in different order, and with this change we ensure that client code eager loads consistently across platforms, for example. * Before this release, subdirectories of root directories always represented namespaces (unless ignored or collapsed). From now on, to be considered namespaces they also have to contain at least one non-ignored Ruby file with extension `.rb`, directly or recursively. If you know beforehand a certain directory or directory pattern does not represent a namespace, it is intentional and more efficient to tell Zeitwerk to [ignore](https://github.com/fxn/zeitwerk#ignoring-parts-of-the-project) it. However, if you don't do so and have a directory `tasks` that only contains Rake files, arguably that directory is not meant to represent a Ruby module. Before, Zeitwerk would define a top-level `Tasks` module after it; now, it does not. This feature is also handy for projects that have directories with auxiliary resources mixed in the project tree in a way that is too dynamic for an ignore pattern to be practical. See https://github.com/fxn/zeitwerk/issues/216. In the unlikely case that an existing project has an empty directory for the sole purpose of defining a totally empty module (no code, and no nested classes or modules), such module has now to be defined in a file. Directories are scanned again on reloads. * On setup, loaders created with `Zeitwerk::Loader.for_gem` issue warnings if `lib` has extra, non-ignored Ruby files or directories. This is motivated by existing gems with directories under `lib` that are not meant to define Ruby modules, like directories for Rails generators, for instance. This warning can be silenced in the unlikely case that the extra stuff is actually autoloadable and has to be managed by Zeitwerk. Please, check the [documentation](https://github.com/fxn/zeitwerk#for_gem) for further details. This method returns an instance of a private subclass of `Zeitwerk::Loader` now, but you cannot rely on the type, just on the interface. ## 2.5.4 (28 January 2022) * If a file did not define the expected constant, there was a reload, and there were `on_unload` callbacks, Zeitwerk still tried to access the constant during reload, which raised. This has been corrected. ## 2.5.3 (30 December 2021) * The change introduced in 2.5.2 implied a performance regression that was particularly dramatic in Ruby 3.1. We'll address [#198](https://github.com/fxn/zeitwerk/issues/198) in a different way. ## 2.5.2 (27 December 2021) * When `Module#autoload` triggers the autovivification of an implicit namespace, `$LOADED_FEATURES` now gets the correspoding directory pushed. This is just a tweak to Zeitwerk's `Kernel#require` decoration. That way it acts more like the original, and cooperates better with other potential `Kernel#require` wrappers, like Bootsnap's. ## 2.5.1 (20 October 2021) * Restores support for namespaces that are not hashable. For example namespaces that override the `hash` method with a different arity as shown in [#188](https://github.com/fxn/zeitwerk/issues/188). ## 2.5.0 (20 October 2021) ### Breaking changes * Requires Ruby 2.5. * Deletes the long time deprecated preload API. Instead of: ```ruby loader.preload("app/models/user.rb") ``` just reference the constant on setup: ```ruby loader.on_setup { User } ``` If you want to eager load a namespace, use the constants API: ```ruby loader.on_setup do Admin.constants(false).each { |cname| Admin.const_get(cname) } end ``` ### Bug fixes * Fixes a bug in which a certain valid combination of overlapping trees managed by different loaders and ignored directories was mistakenly reported as having conflicting directories. * Detects external namespaces defined with `Module#autoload`. If your project reopens a 3rd party namespace, Zeitwerk already detected it and did not consider the namespace to be managed by the loader (automatically descends, ignored for reloads, etc.). However, the loader did not do that if the namespace had only an autoload in the 3rd party code yet to be executed. Now it does. ### Callbacks * Implements `Zeitwerk::Loader#on_setup`, which allows you to configure blocks of code to be executed on setup and on each reload. When the callback is fired, the loader is ready, you can refer to project constants in the block. See the [documentation](https://github.com/fxn/zeitwerk#the-on_setup-callback) for further details. * There is a new catch-all `Zeitwerk::Loader#on_load` that takes no argument and is triggered for all loaded objects: ```ruby loader.on_load do |cpath, value, abspath| # ... end ``` Please, remember that if you want to trace the activity of a loader, `Zeitwerk::Loader#log!` logs plenty of information. See the [documentation](https://github.com/fxn/zeitwerk#the-on_load-callback) for further details. * The block of the existing `Zeitwerk::Loader#on_load` receives also the value stored in the constant, and the absolute path to its corresponding file or directory: ```ruby loader.on_load("Service::NotificationsGateway") do |klass, abspath| # ... end ``` Remember that blocks can be defined to take less arguments than passed. So this change is backwards compatible. If you had ```ruby loader.on_load("Service::NotificationsGateway") do Service::NotificationsGateway.endpoint = ... end ``` That works. * Implements `Zeitwerk::Loader#on_unload`, which allows you to configure blocks of code to be executed before a certain class or module gets unloaded: ```ruby loader.on_unload("Country") do |klass, _abspath| klass.clear_cache end ``` These callbacks are invoked during unloading, which happens in an unspecified order. Therefore, they should not refer to reloadable constants. You can also be called for all unloaded objects: ```ruby loader.on_unload do |cpath, value, abspath| # ... end ``` Please, remember that if you want to trace the activity of a loader, `Zeitwerk::Loader#log!` logs plenty of information. See the [documentation](https://github.com/fxn/zeitwerk/blob/master/README.md#the-on_unload-callback) for further details. ### Assorted * Performance improvements. * Documentation improvements. * The method `Zeitwerk::Loader#eager_load` accepts a `force` flag: ```ruby loader.eager_load(force: true) ``` If passed, eager load exclusions configured with `do_not_eager_load` are not honoured (but ignored files and directories are). This may be handy for test suites that eager load in order to ensure all files define the expected constant. * Eliminates internal use of `File.realpath`. One visible consequence is that in logs root dirs are shown as configured if they contain symlinks. * When an autoloaded file does not define the expected constant, Ruby clears state differently starting with Ruby 3.1. Unloading has been revised to be compatible with both behaviours. * Logging prints a few new traces. ## 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" # 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.6.8/Gemfile000066400000000000000000000002641442267002100146660ustar00rootroot00000000000000# frozen_string_literal: true source 'https://rubygems.org' gemspec gem "rake" gem "minitest" gem "minitest-focus" gem "minitest-proveit" gem "minitest-reporters" gem "warning" zeitwerk-2.6.8/MIT-LICENSE000066400000000000000000000020451442267002100150260ustar00rootroot00000000000000Copyright (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.6.8/PROJECT_RULES.md000066400000000000000000000102721442267002100157750ustar00rootroot00000000000000# 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 always pick the most specific option: * `file`: Absolute path of a file. * `dir`: Absolute path of a directory. * `abspath`: Absolute 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 when resolving relative file names. * The only directory walks allowed are the ones 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 APIs return them as they were, and logging matches too. This is specially relevant if there are symlinks involved. ## 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 using [RBS syntax](https://github.com/ruby/rbs/blob/master/docs/syntax.md). * 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 leaky 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, so it should have minimal cost both in speed and memory usage. Be extra careful, allocate as little as possible, store as little as possible. Always use absolute file names for `autoload` and `require`. Always log 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.6.8/README.md000066400000000000000000001563521442267002100146640ustar00rootroot00000000000000# 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/github/actions/workflow/status/fxn/zeitwerk/ci.yml?branch=main&event=push&style=for-the-badge)](https://github.com/fxn/zeitwerk/actions/workflows/ci.yml?query=branch%3main) - [Introduction](#introduction) - [Synopsis](#synopsis) - [File structure](#file-structure) - [The idea: File paths match constant paths](#the-idea-file-paths-match-constant-paths) - [Inner simple constants](#inner-simple-constants) - [Root directories and root namespaces](#root-directories-and-root-namespaces) - [The default root namespace is `Object`](#the-default-root-namespace-is-object) - [Custom root namespaces](#custom-root-namespaces) - [Nested root directories](#nested-root-directories) - [Implicit namespaces](#implicit-namespaces) - [Explicit namespaces](#explicit-namespaces) - [Collapsing directories](#collapsing-directories) - [Testing compliance](#testing-compliance) - [Usage](#usage) - [Setup](#setup) - [Generic](#generic) - [for_gem](#for_gem) - [for_gem_extension](#for_gem_extension) - [Autoloading](#autoloading) - [Eager loading](#eager-loading) - [Eager load exclusions](#eager-load-exclusions) - [Eager load directories](#eager-load-directories) - [Eager load namespaces](#eager-load-namespaces) - [Eager load namespaces shared by several loaders](#eager-load-namespaces-shared-by-several-loaders) - [Global eager load](#global-eager-load) - [Loading individual files](#loading-individual-files) - [Reloading](#reloading) - [Configuration and usage](#configuration-and-usage) - [Thread-safety](#thread-safety) - [Inflection](#inflection) - [Zeitwerk::Inflector](#zeitwerkinflector) - [Zeitwerk::GemInflector](#zeitwerkgeminflector) - [Custom inflector](#custom-inflector) - [Callbacks](#callbacks) - [The on_setup callback](#the-on_setup-callback) - [The on_load callback](#the-on_load-callback) - [The on_unload callback](#the-on_unload-callback) - [Technical details](#technical-details) - [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) - [Shadowed files](#shadowed-files) - [Edge cases](#edge-cases) - [Beware of circular dependencies](#beware-of-circular-dependencies) - [Reopening third-party namespaces](#reopening-third-party-namespaces) - [Introspection](#introspection) - [Encodings](#encodings) - [Rules of thumb](#rules-of-thumb) - [Debuggers](#debuggers) - [Pronunciation](#pronunciation) - [Supported Ruby versions](#supported-ruby-versions) - [Testing](#testing) - [Motivation](#motivation) - [Kernel#require is brittle](#kernelrequire-is-brittle) - [Rails autoloading was brittle](#rails-autoloading-was-brittle) - [Awards](#awards) - [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 ### The idea: File paths match constant paths 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 ``` You can tune that a bit by [collapsing directories](#collapsing-directories), or by [ignoring parts of the project](#ignoring-parts-of-the-project), but that is the main idea. ### Inner simple constants While a simple constant like `HttpCrawler::MAX_RETRIES` can be defined in its own file: ```ruby # http_crawler/max_retries.rb HttpCrawler::MAX_RETRIES = 10 ``` that is not required, you can also define it the regular way: ```ruby # http_crawler.rb class HttpCrawler MAX_RETRIES = 10 end ``` The first example needs a custom [inflection](#inflection) rule: ```ruby loader.inflector.inflect("max_retries" => "MAX_RETRIES") ``` Otherwise, Zeitwerk would expect the file to define `MaxRetries`. In the second example, no custom rule is needed. ### Root directories and root namespaces Every directory configured with `push_dir` is called a _root directory_, and they represent _root namespaces_. #### The default root namespace is `Object` By default, the namespace associated to a root directory is the top-level one: `Object`. For example, given ```ruby loader.push_dir("#{__dir__}/models") loader.push_dir("#{__dir__}/serializers")) ``` these are the expected classes and modules being defined by these files: ``` models/user.rb -> User serializers/user_serializer.rb -> UserSerializer ``` #### Custom root namespaces While `Object` is by far the most common root namespace, you can associate a different one to a particular root directory. The method `push_dir` accepts a non-anonymous class or module object in the optional `namespace` keyword argument. For example, given: ```ruby require "active_job" require "active_job/queue_adapters" loader.push_dir("#{__dir__}/adapters", namespace: ActiveJob::QueueAdapters) ``` a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the conventional parent directories, you can (and have to) store the file directly below `adapters`: ``` adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter ``` Please, note that the given root 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::Deliveries`, that one can be reloaded. #### 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`. ### Implicit namespaces If a namespace is just a simple module with no code, you do not need to define it in a file: Directories without a matching Ruby file get modules created automatically on your behalf. For example, if a project has an `admin` directory: ``` app/controllers/admin/users_controller.rb -> Admin::UsersController ``` and does not have a file called `admin.rb`, Zeitwerk automatically creates an `Admin` module on your behalf the first time `Admin` is used. For this to happen, the directory has to contain non-ignored Ruby files with extension `.rb`, directly or recursively, otherwise it is ignored. This condition is evaluated again on reloads. ### 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") ``` ### Testing compliance When a managed file is loaded, Zeitwerk verifies the expected constant is defined. If it is not, `Zeitwerk::NameError` is raised. So, an easy way to ensure compliance in the test suite is to eager load the project: ```ruby begin loader.eager_load(force: true) rescue Zeitwerk::NameError => e flunk e.message else assert true end ``` ## 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. 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__) ``` 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 ``` Due to technical reasons, the entry point of the gem has to be loaded with `Kernel#require`, which is the standard way to load a gem. Loading that file with `Kernel#load` or `Kernel#require_relative` won't generally work. `Zeitwerk::Loader.for_gem` is idempotent when invoked from the same file, to support gems that want to reload (unlikely). 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_](#reopening-third-party-namespaces) down below. Loaders returned by `Zeitwerk::Loader.for_gem` issue warnings if `lib` has extra Ruby files or directories. For example, if the gem has Rails generators under `lib/generators`, by convention that directory defines a `Generators` Ruby module. If `generators` is just a container for non-autoloadable code and templates, not acting as a project namespace, you need to setup things accordingly. If the warning is legit, just tell the loader to ignore the offending file or directory: ```ruby loader.ignore("#{__dir__}/generators") ``` Otherwise, there's a flag to say the extra stuff is OK: ```ruby Zeitwerk::Loader.for_gem(warn_on_extra_files: false) ``` #### for_gem_extension Let's suppose you are writing a gem to extend `Net::HTTP` with some niche feature. By [convention](https://guides.rubygems.org/name-your-gem/): * The gem should be called `net-http-niche_feature`. That is, hyphens for the extended part, a hyphen, and underscores for yours. * The namespace should be `Net::HTTP::NicheFeature`. * The entry point should be `lib/net/http/niche_feature.rb`. * Optionally, the gem could have a top-level `lib/net-http-niche_feature.rb`, but, if defined, that one should have just a `require` call for the entry point. The top-level file mentioned in the last point is optional. In particular, from ```ruby gem "net-http-niche_feature" ``` if the hyphenated file does not exist, Bundler notes the conventional hyphenated pattern and issues a `require` for `net/http/niche_feature`. Gem extensions following the conventions above have a dedicated loader constructor: `Zeitwerk::Loader.for_gem_extension`. The structure of the gem would be like this: ```ruby # lib/net-http-niche_feature.rb (optional) # For technical reasons, this cannot be require_relative. require "net/http/niche_feature" # lib/net/http/niche_feature.rb require "net/http" require "zeitwerk" loader = Zeitwerk::Loader.for_gem_extension(Net::HTTP) loader.setup module Net::HTTP::NicheFeature # Since the setup has been performed, at this point we are already able # to reference project constants, in this case Net::HTTP::NicheFeature::MyMixin. include MyMixin end # lib/net/http/niche_feature/version.rb module Net::HTTP::NicheFeature VERSION = "1.0.0" end ``` `Zeitwerk::Loader.for_gem_extension` expects as argument the namespace being extended, which has to be a non-anonymous class or module object. If it exists, `lib/net/http/niche_feature/version.rb` is expected to define `Net::HTTP::NicheFeature::VERSION`. Due to technical reasons, the entry point of the gem has to be loaded with `Kernel#require`. Loading that file with `Kernel#load` or `Kernel#require_relative` won't generally work. This is important if you load the entry point from the optional hyphenated top-level file. `Zeitwerk::Loader.for_gem_extension` is idempotent when invoked from the same file, to support gems that want to reload (unlikely). ### 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). In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](#synopsis). Eager loading is synchronized and idempotent. Attempting to eager load without previously calling `setup` raises `Zeitwerk::SetupRequired`. #### Eager load exclusions You can 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 ``` However, that can be overridden with `force`: ```ruby loader.eager_load(force: true) # database adapters are eager loaded ``` Which may be handy if the project eager loads in the test suite to [ensure project layout compliance](#testing-compliance). The `force` flag does not affect ignored files and directories, those are still ignored. #### Eager load directories The method `Zeitwerk::Loader#eager_load_dir` eager loads a given directory, recursively: ```ruby loader.eager_load_dir("#{__dir__}/custom_web_app/routes") ``` This is useful when the loader is not eager loading the entire project, but you still need some subtree to be loaded for things to function properly. Both strings and `Pathname` objects are supported as arguments. If the argument is not a directory managed by the receiver, the method raises `Zeitwerk::Error`. [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded. `Zeitwerk::Loader#eager_load_dir` is idempotent, but compatible with reloading. If you eager load a directory and then reload, eager loading that directory will load its (current) contents again. The method checks if a regular eager load was already executed, in which case it returns fast. Nested root directories which are descendants of the argument are skipped. Those subtrees are considered to be conceptually apart. Attempting to eager load a directory without previously calling `setup` raises `Zeitwerk::SetupRequired`. #### Eager load namespaces The method `Zeitwerk::Loader#eager_load_namespace` eager loads a given namespace, recursively: ```ruby loader.eager_load_namespace(MyApp::Routes) ``` This is useful when the loader is not eager loading the entire project, but you still need some namespace to be loaded for things to function properly. The argument has to be a class or module object and the method raises `Zeitwerk::Error` otherwise. If the namespace is spread over multiple directories in the receiver's source tree, they are all eager loaded. For example, if you have a structure like ``` root_dir1/my_app/routes root_dir2/my_app/routes root_dir3/my_app/routes ``` where `root_directory{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories. There might exist external source trees implementing part of the namespace. This happens routinely, because top-level constants are stored in the globally shared `Object`. It happens also when deliberately [reopening third-party namespaces](#reopening-third-party-namespaces). Such external code is not eager loaded, the implementation is carefully scoped to what the receiver manages to avoid side-effects elsewhere. This method is flexible about what it accepts. Its semantics have to be interpreted as: "_If_ you manage this namespace, or part of this namespace, please eager load what you got". In particular, if the receiver does not manage the namespace, it will simply do nothing, this is not an error condition. [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded. `Zeitwerk::Loader#eager_load_namespace` is idempotent, but compatible with reloading. If you eager load a namespace and then reload, eager loading that namespace will load its (current) descendants again. The method checks if a regular eager load was already executed, in which case it returns fast. If root directories are assigned to custom namespaces, the method behaves as you'd expect, according to the namespacing relationship between the custom namespace and the argument. Attempting to eager load a namespace without previously calling `setup` raises `Zeitwerk::SetupRequired`. #### Eager load namespaces shared by several loaders The method `Zeitwerk::Loader.eager_load_namespace` broadcasts `eager_load_namespace` to all loaders. ```ruby Zeitwerk::Loader.eager_load_namespace(MyFramework::Routes) ``` This may be handy, for example, if a framework supports plugins and a shared namespace needs to be eager loaded for the framework to function properly. Please, note that loaders only eager load namespaces they manage, as documented above. Therefore, this method does not allow you to eager load namespaces not managed by Zeitwerk loaders. This method does not require that all registered loaders have `setup` already invoked, since that is out of your control. If there's any in that state, it is simply skipped. #### Global eager load If you want to eager load yourself and all dependencies that use 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. This method does not accept the `force` flag, since in general it wouldn't be a good idea to force eager loading in 3rd party code. This method does not require that all registered loaders have `setup` already invoked, since that is out of your control. If there's any in that state, it is simply skipped. ### Loading individual files The method `Zeitwerk::Loader#load_file` loads an individual Ruby file: ```ruby loader.load_file("#{__dir__}/custom_web_app/routes.rb") ``` This is useful when the loader is not eager loading the entire project, but you still need an individual file to be loaded for things to function properly. Both strings and `Pathname` objects are supported as arguments. The method raises `Zeitwerk::Error` if the argument is not a Ruby file, is [ignored](#ignoring-parts-of-the-project), is [shadowed](#shadowed-files), or is not managed by the receiver. `Zeitwerk::Loader#load_file` is idempotent, but compatible with reloading. If you load a file and then reload, a new call will load its (current) contents again. If you want to eager load a directory, `Zeitwerk::Loader#eager_load_dir` is more efficient than invoking `Zeitwerk::Loader#load_file` on its files. ### Reloading #### Configuration and usage 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`. Attempting to reload without previously calling `setup` raises `Zeitwerk::SetupRequired`. 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. #### Thread-safety In order to reload safely, no other thread can be autoloading or reloading concurrently. Client code is responsible for this coordination. For example, a web framework that serves each request in its own thread and has reloading enabled could create a read-write lock on boot like this: ```ruby require "concurrent/atomic/read_write_lock" MyFramework::RELOAD_RW_LOCK = Concurrent::ReadWriteLock.new ``` You acquire the lock for reading for serving each individual request: ```ruby MyFramework::RELOAD_RW_LOCK.with_read_lock do serve(request) end ``` Then, when a reload is triggered, just acquire the lock for writing in order to execute the method call safely: ```ruby MyFramework::RELOAD_RW_LOCK.with_write_lock do loader.reload end ``` 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 reloadable 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, and you can define your own. #### Zeitwerk::Inflector Each loader instantiated with `Zeitwerk::Loader.new` has an inflector of this type by default. 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`. The inflectors of different loaders are independent of each other. There are no global inflection rules or global configuration that can affect this inflector. It is deterministic. #### Zeitwerk::GemInflector Each loader instantiated with `Zeitwerk::Loader.for_gem` has an inflector of this type by default. This inflector is like the basic one, except it expects `lib/my_gem/version.rb` to define `MyGem::VERSION`. The inflectors of different loaders are independent of each other. There are no global inflection rules or global configuration that can affect this inflector. It is deterministic. #### 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 ``` ### Callbacks #### The on_setup callback The `on_setup` callback is fired on setup and on each reload: ```ruby loader.on_setup do # Ready to autoload here. end ``` Multiple `on_setup` callbacks are supported, and they run in order of definition. If `setup` was already executed, the callback is fired immediately. #### 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 |klass, _abspath| klass.endpoint = "https://api.dev" end # config/environments/production.rb loader.on_load("SomeApiClient") do |klass, _abspath| klass.endpoint = "https://api.prod" end ``` Some uses cases: * Doing something with a reloadable 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. `on_load` gets a target constant path as a string (e.g., "User", or "Service::NotificationsGateway"). When fired, its block receives the stored value, and the absolute path to the corresponding file or directory as a string. The callback is executed every time the target 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. It is also possible to be called when any constant managed by the loader is loaded: ```ruby loader.on_load do |cpath, value, abspath| # ... end ``` The block gets the constant path as a string (e.g., "User", or "Foo::VERSION"), the value it stores (e.g., the class object stored in `User`, or "2.5.0"), and the absolute path to the corresponding file or directory as a string. Multiple callbacks like these are supported, and they run in order of definition. There are use cases for this last catch-all callback, but they are rare. If you just need to understand how things are being loaded for debugging purposes, please remember that `Zeitwerk::Loader#log!` logs plenty of information. If both types of callbacks are defined, the specific ones run first. Since `on_load` callbacks are executed right after files are loaded, even if the loading context seems to be far away, in practice **the block is subject to [circular dependencies](#beware-of-circular-dependencies)**. As a rule of thumb, as far as loading order and its interdependencies is concerned, you have to program as if the block was executed at the bottom of the file just loaded. #### The on_unload callback When reloading is enabled, you may occasionally need to execute something before a certain autoloaded class or module is unloaded. The `on_unload` callback allows you to do that. For example, let's imagine that a `Country` class fetches a list of countries and caches them when it is loaded. You might want to clear that cache if unloaded: ```ruby loader.on_unload("Country") do |klass, _abspath| klass.clear_cache end ``` `on_unload` gets a target constant path as a string (e.g., "User", or "Service::NotificationsGateway"). When fired, its block receives the stored value, and the absolute path to the corresponding file or directory as a string. The callback is executed every time the target is unloaded. `on_unload` blocks are executed before the class is unloaded, but in the middle of unloading, which happens in an unspecified order. Therefore, **that callback should not refer to any reloadable constant because there is no guarantee the constant works there**. Those blocks should rely on objects only, as in the example above, or regular constants not managed by the loader. This remark is transitive, applies to any methods invoked within the block. Multiple callbacks on the same target are supported, and they run in order of definition. Defining a callback for a target not managed by the receiver is not an error, the block simply won't ever be executed. It is also possible to be called when any constant managed by the loader is unloaded: ```ruby loader.on_unload do |cpath, value, abspath| # ... end ``` The block gets the constant path as a string (e.g., "User", or "Foo::VERSION"), the value it stores (e.g., the class object stored in `User`, or "2.5.0"), and the absolute path to the corresponding file or directory as a string. Multiple callbacks like these are supported, and they run in order of definition. If both types of callbacks are defined, the specific ones run first. #### Technical details Zeitwerk uses the word "unload" to ease communication and for symmetry with `on_load`. However, in Ruby you cannot unload things for real. So, when does `on_unload` technically happen? When unloading, Zeitwerk issues `Module#remove_const` calls. Classes and modules are no longer reachable through their constants, and `on_unload` callbacks are executed right before those calls. Technically, though, the objects themselves are still alive, but if everything is used as expected and they are not stored in any non-reloadable place (don't do that), they are ready for garbage collection, which is when the real unloading happens. ### 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. There is an edge case related to nested root directories. Conceptually, root directories are independent source trees. If you ignore a parent of a nested root directory, the nested root directory is not affected. You need to ignore it explictly if you want it ignored too. 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 ``` `Kernel` is already defined by Ruby so the module cannot be autoloaded. Also, that file does not define a constant path after the path name. Therefore, Zeitwerk should not process it at all. The extension can still coexist with the rest of the project, you only need to tell Zeitwerk to ignore it: ```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 ``` Now, that file has to be loaded manually with `require` or `require_relative`: ```ruby require_relative "my_gem/core_ext/kernel" ``` and you can do that anytime, before configuring the loader, or after configuring the loader, does not matter. #### 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 ``` ### Shadowed files In Ruby, if you have several files called `foo.rb` in different directories of `$LOAD_PATH` and execute ```ruby require "foo" ``` the first one found gets loaded, and the rest are ignored. Zeitwerk behaves in a similar way. If `foo.rb` is present in several root directories (at the same namespace level), the constant `Foo` is autoloaded from the first one, and the rest of the files are not evaluated. If logging is enabled, you'll see something like ``` file #{file} is ignored because #{previous_occurrence} has precedence ``` (This message is not public interface and may change, you cannot rely on that exact wording.) Even if there's only one `foo.rb`, if the constant `Foo` is already defined when Zeitwerk finds `foo.rb`, then the file is ignored too. This could happen if `Foo` was defined by a dependency, for example. If logging is enabled, you'll see something like ``` file #{file} is ignored because #{constant_path} is already defined ``` (This message is not public interface and may change, you cannot rely on that exact wording.) Shadowing only applies to Ruby files, namespace definition can be spread over multiple directories. And you can also reopen third-party namespaces if done [orderly](#reopening-third-party-namespaces). ### Edge cases [Explicit namespaces](#explicit-namespaces) like `Trip` here: ```ruby # trip.rb class Trip include Geolocation end # trip/geolocation.rb module Trip::Geolocation ... end ``` have to be defined with the `class`/`module` keywords, as in the example above. For technical reasons, raw constant assignment is not supported: ```ruby # trip.rb Trip = Class { ...} # NOT SUPPORTED Trip = Struct.new { ... } # NOT SUPPORTED Trip = Data.define { ... } # NOT SUPPORTED ``` This only affects explicit namespaces, those idioms work well for any other ordinary class or module. ### Beware of circular dependencies In Ruby, you can't have certain top-level circular dependencies. Take for example: ```ruby # c.rb class C < D end # d.rb class D C end ``` In order to define `C`, you need to load `D`. However, the body of `D` refers to `C`. Circular dependencies like those do not work in plain Ruby, and therefore do not work in projects managed by Zeitwerk either. ### 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" # By passing the flag, we acknowledge the extra directory lib/active_job # has to be managed by the loader and no warning has to be issued for it. loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false) 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. ### Introspection The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings: ```ruby loader = Zeitwerk::Loader.new loader.push_dir(Pathname.new("/foo")) loader.dirs # => ["/foo"] ``` This method accepts an optional `namespaces` keyword argument. If truthy, the method returns a hash table instead. Keys are the absolute paths of the root directories as strings. Values are their corresponding namespaces, class or module objects: ```ruby loader = Zeitwerk::Loader.new loader.push_dir(Pathname.new("/foo")) loader.push_dir(Pathname.new("/bar"), namespace: Bar) loader.dirs(namespaces: true) # => { "/foo" => Object, "/bar" => Bar } ``` By default, ignored root directories are filtered out. If you want them included, please pass `ignored: true`. These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`. ### Encodings Zeitwerk supports projects whose files and file system are in UTF-8. The encoding of the file system can be checked this way: ``` % ruby -e "puts Encoding.find('filesystem')" UTF-8 ``` The test suite passes on Windows with codepage `Windows-1252` if all the involved absolute paths are ASCII. Other supersets of ASCII may work too, but you have to try. ### 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 Zeitwerk works fine with [debug.rb](https://github.com/ruby/debug) and [Break](https://github.com/gsamokovarov/break). [Byebug](https://github.com/deivid-rodriguez/byebug) is compatible except for an edge case explained in [deivid-rodriguez/byebug#564](https://github.com/deivid-rodriguez/byebug/issues/564). ## Pronunciation "Zeitwerk" is pronounced [this way](http://share.hashref.com/zeitwerk/zeitwerk_pronunciation.mp3). ## Supported Ruby versions Zeitwerk works with CRuby 2.5 and above. On TruffleRuby all is good except for thread-safety. Right now, in TruffleRuby `Module#autoload` does not block threads accessing a constant that is being autoloaded. CRuby prevents such access to avoid concurrent threads from seeing partial evaluations of the corresponding file. Zeitwerk inherits autoloading thread-safety from this property. This is not an issue if your project gets eager loaded, or if you lazy load in single-threaded environments. (See https://github.com/oracle/truffleruby/issues/2431.) JRuby 9.3.0.0 is almost there. As of this writing, the test suite of Zeitwerk passes on JRuby except for three tests. (See https://github.com/jruby/jruby/issues/6781.) ## 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 ### Kernel#require is brittle 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. Also, if the project has namespaces, setting things up and getting client code to load things in a consistent way needs discipline. For example, `require "foo/bar"` may define `Foo`, instead of reopen it. That may be a broken window, giving place to superclass mismatches or partially-defined namespaces. With Zeitwerk, you just name things following conventions and done. Things are available everywhere, and descend is always orderly. Without effort and without broken windows. ### Rails autoloading was brittle Autoloading in Rails was based on `const_missing` up to Rails 5. That callback lacks fundamental information like the nesting or the resolution algorithm being used. Because of that, Rails autoloading was not able to match Ruby's semantics, and that introduced a [series of issues](https://guides.rubyonrails.org/v5.2/autoloading_and_reloading_constants.html#common-gotchas). Zeitwerk is based on a different technique and fixed Rails autoloading starting with Rails 6. ## Awards Zeitwerk has been awarded an "Outstanding Performance Award" Fukuoka Ruby Award 2022. ## 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.6.8/Rakefile000066400000000000000000000003071442267002100150360ustar00rootroot00000000000000# frozen_string_literal: true require 'rake/testtask' task :default => :test Rake::TestTask.new do |t| t.test_files = Dir.glob('test/lib/**/test_*.rb') t.libs << "test" t.warning = true end zeitwerk-2.6.8/bin/000077500000000000000000000000001442267002100141415ustar00rootroot00000000000000zeitwerk-2.6.8/bin/test000077500000000000000000000001361442267002100150460ustar00rootroot00000000000000#!/bin/bash if [[ -z $1 ]]; then bundle exec rake else bundle exec rake TEST="$1" fi zeitwerk-2.6.8/extras/000077500000000000000000000000001442267002100146775ustar00rootroot00000000000000zeitwerk-2.6.8/extras/zeitwerk_pronunciation.mp3000066400000000000000000002511631442267002100221440ustar00rootroot00000000000000ID3vTSSLogic 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Ա1k^?_^ck{׷uook1\ͩ|)@R;O{p`in@ Ú6cOYH|SF"NQѥKG8OTvDqMz[7`_ٶhշ󘸃zגُXXyV%lbe13XJ ֈ%3D!>@* .s^"̗J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[~ƫ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.6.8/lib/000077500000000000000000000000001442267002100141375ustar00rootroot00000000000000zeitwerk-2.6.8/lib/zeitwerk.rb000066400000000000000000000012511442267002100163270ustar00rootroot00000000000000# frozen_string_literal: true module Zeitwerk require_relative "zeitwerk/real_mod_name" require_relative "zeitwerk/internal" require_relative "zeitwerk/loader" require_relative "zeitwerk/gem_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" require_relative "zeitwerk/version" # This is a dangerous method. # # @experimental # @sig () -> void def self.with_loader loader = Zeitwerk::Loader.new yield loader ensure loader.unregister end end zeitwerk-2.6.8/lib/zeitwerk/000077500000000000000000000000001442267002100160035ustar00rootroot00000000000000zeitwerk-2.6.8/lib/zeitwerk/error.rb000066400000000000000000000006701442267002100174640ustar00rootroot00000000000000# frozen_string_literal: true module Zeitwerk class Error < StandardError end class ReloadingDisabledError < Error def initialize super("can't reload, please call loader.enable_reloading before setup") end end class NameError < ::NameError end class SetupRequired < Error def initialize super("please, finish your configuration and call Zeitwerk::Loader#setup once all is ready") end end end zeitwerk-2.6.8/lib/zeitwerk/explicit_namespace.rb000066400000000000000000000061401442267002100221660ustar00rootroot00000000000000# frozen_string_literal: true module 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 extend Internal # Maps constant paths that correspond to explicit namespaces according to # the file system, to the loader responsible for them. # # @sig Hash[String, Zeitwerk::Loader] attr_reader :cpaths private :cpaths # @sig Mutex attr_reader :mutex private :mutex # @sig TracePoint attr_reader :tracer private :tracer # Asserts `cpath` corresponds to an explicit namespace for which `loader` # is responsible. # # @sig (String, Zeitwerk::Loader) -> void internal 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 # @sig (Zeitwerk::Loader) -> void internal def unregister_loader(loader) cpaths.delete_if { |_cpath, l| l == loader } disable_tracer_if_unneeded end # This is an internal method only used by the test suite. # # @sig (String) -> bool internal def registered?(cpath) cpaths.key?(cpath) end # @sig () -> void private def disable_tracer_if_unneeded mutex.synchronize do tracer.disable if cpaths.empty? end end # @sig (TracePoint) -> void private 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? # It might be tempting to return if name.nil?, to avoid the computation # of a hash code and delete call. But Ruby does not trigger the :class # event on Class.new or Module.new, so that would incur in an extra call # for nothing. # # On the other hand, if we were called, cpaths is not empty. Otherwise # the tracer is disabled. So we do need to go ahead with the hash code # computation and delete call. 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.6.8/lib/zeitwerk/gem_inflector.rb000066400000000000000000000007111442267002100211440ustar00rootroot00000000000000# frozen_string_literal: true module Zeitwerk class GemInflector < Inflector # @sig (String) -> void def initialize(root_file) namespace = File.basename(root_file, ".rb") root_dir = File.dirname(root_file) @version_file = File.join(root_dir, namespace, "version.rb") end # @sig (String, String) -> String def camelize(basename, abspath) abspath == @version_file ? "VERSION" : super end end end zeitwerk-2.6.8/lib/zeitwerk/gem_loader.rb000066400000000000000000000036541442267002100204360ustar00rootroot00000000000000# frozen_string_literal: true module Zeitwerk # @private class GemLoader < Loader include RealModName # Users should not create instances directly, the public interface is # `Zeitwerk::Loader.for_gem`. private_class_method :new # @private # @sig (String, bool) -> Zeitwerk::GemLoader def self.__new(root_file, namespace:, warn_on_extra_files:) new(root_file, namespace: namespace, warn_on_extra_files: warn_on_extra_files) end # @sig (String, bool) -> void def initialize(root_file, namespace:, warn_on_extra_files:) super() @tag = File.basename(root_file, ".rb") @tag = real_mod_name(namespace) + "-" + @tag unless namespace.equal?(Object) @inflector = GemInflector.new(root_file) @root_file = File.expand_path(root_file) @root_dir = File.dirname(root_file) @warn_on_extra_files = warn_on_extra_files push_dir(@root_dir, namespace: namespace) end # @sig () -> void def setup warn_on_extra_files if @warn_on_extra_files super end private # @sig () -> void def warn_on_extra_files expected_namespace_dir = @root_file.delete_suffix(".rb") ls(@root_dir) do |basename, abspath| next if abspath == @root_file next if abspath == expected_namespace_dir basename_without_ext = basename.delete_suffix(".rb") cname = inflector.camelize(basename_without_ext, abspath).to_sym ftype = dir?(abspath) ? "directory" : "file" warn(<<~EOS) WARNING: Zeitwerk defines the constant #{cname} after the #{ftype} #{abspath} To prevent that, please configure the loader to ignore it: loader.ignore("\#{__dir__}/#{basename}") Otherwise, there is a flag to silence this warning: Zeitwerk::Loader.for_gem(warn_on_extra_files: false) EOS end end end end zeitwerk-2.6.8/lib/zeitwerk/inflector.rb000066400000000000000000000025751442267002100203260ustar00rootroot00000000000000# 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.6.8/lib/zeitwerk/internal.rb000066400000000000000000000003561442267002100201500ustar00rootroot00000000000000# frozen_string_literal: true # This is a private module. module Zeitwerk::Internal def internal(method_name) private method_name mangled = "__#{method_name}" alias_method mangled, method_name public mangled end end zeitwerk-2.6.8/lib/zeitwerk/kernel.rb000066400000000000000000000053011442267002100176070ustar00rootroot00000000000000# frozen_string_literal: true module Kernel module_function # Zeitwerk's main idea is to define autoloads for project constants, and then # intercept them when triggered in this thin `Kernel#require` wrapper. # # That allows us to complete the circle, invoke callbacks, autovivify modules, # define autoloads for just autoloaded namespaces, update internal state, etc. # # 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. This feature is by now # EXPERIMENTAL and UNDOCUMENTED. # # 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 on Ruby < 3.0. alias_method :zeitwerk_original_require, :require class << self alias_method :zeitwerk_original_require, :require end # @sig (String) -> true | false def require(path) if loader = Zeitwerk::Registry.loader_for(path) if path.end_with?(".rb") required = zeitwerk_original_require(path) loader.on_file_autoloaded(path) if required required else loader.on_dir_autoloaded(path) true end else required = zeitwerk_original_require(path) if required abspath = $LOADED_FEATURES.last if loader = Zeitwerk::Registry.loader_for(abspath) loader.on_file_autoloaded(abspath) end end required 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.6.8/lib/zeitwerk/loader.rb000066400000000000000000000447111442267002100176050ustar00rootroot00000000000000# frozen_string_literal: true require "set" module Zeitwerk class Loader require_relative "loader/helpers" require_relative "loader/callbacks" require_relative "loader/config" require_relative "loader/eager_load" extend Internal include RealModName include Callbacks include Helpers include Config include EagerLoad MUTEX = Mutex.new private_constant :MUTEX # Maps 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] # ... # # @sig Hash[String, [Module, Symbol]] attr_reader :autoloads internal :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). # # @sig Array[String] attr_reader :autoloaded_dirs internal :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 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. # # @sig Hash[String, [String, [Module, Symbol]]] attr_reader :to_unload internal :to_unload # Maps namespace constant paths to their respective 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. # # @sig Hash[String, Array[String]] attr_reader :namespace_dirs internal :namespace_dirs # A shadowed file is a file managed by this loader that is ignored when # setting autoloads because its matching constant is already taken. # # This private set is populated as we descend. For example, if the loader # has only scanned the top-level, `shadowed_files` does not have shadowed # files that may exist deep in the project tree yet. # # @sig Set[String] attr_reader :shadowed_files internal :shadowed_files # @sig Mutex attr_reader :mutex private :mutex # @sig Mutex attr_reader :mutex2 private :mutex2 def initialize super @autoloads = {} @autoloaded_dirs = [] @to_unload = {} @namespace_dirs = Hash.new { |h, cpath| h[cpath] = [] } @shadowed_files = Set.new @mutex = Mutex.new @mutex2 = Mutex.new @setup = false @eager_loaded = false Registry.register_loader(self) end # Sets autoloads in the root namespaces. # # @sig () -> void def setup mutex.synchronize do break if @setup actual_roots.each do |root_dir, root_namespace| set_autoloads_in_dir(root_dir, root_namespace) end on_setup_callbacks.each(&:call) @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. # # This method is public but undocumented. Main interface is `reload`, which # means `unload` + `setup`. This one is available to be used together with # `unregister`, which is undocumented too. # # @sig () -> void def unload mutex.synchronize do raise SetupRequired unless @setup # 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 |abspath, (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) unloaded_files.add(abspath) if ruby?(abspath) end end to_unload.each do |cpath, (abspath, (parent, cname))| unless on_unload_callbacks.empty? begin value = cget(parent, cname) rescue ::NameError # Perhaps the user deleted the constant by hand, or perhaps an # autoload failed to define the expected constant but the user # rescued the exception. else run_on_unload_callbacks(cpath, value, abspath) end end unload_cref(parent, cname) unloaded_files.add(abspath) if ruby?(abspath) 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 namespace_dirs.clear shadowed_files.clear Registry.on_unload(self) ExplicitNamespace.__unregister_loader(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 raise ReloadingDisabledError unless reloading_enabled? raise SetupRequired unless @setup unload recompute_ignored_paths recompute_collapse_dirs setup 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 # This is a dangerous method. # # @experimental # @sig () -> void def unregister Registry.unregister_loader(self) ExplicitNamespace.__unregister_loader(self) end # The return value of this predicate is only meaningful if the loader has # scanned the file. This is the case in the spots where we use it. # # @sig (String) -> Boolean internal def shadowed_file?(file) shadowed_files.member?(file) end # --- Class methods --------------------------------------------------------------------------- class << self include RealModName # @sig #call | #debug | nil attr_accessor :default_logger # 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. # # This method returns a subclass of Zeitwerk::Loader, but the exact type # is private, client code can only rely on the interface. # # @sig (bool) -> Zeitwerk::GemLoader def for_gem(warn_on_extra_files: true) called_from = caller_locations(1, 1).first.path Registry.loader_for_gem(called_from, namespace: Object, warn_on_extra_files: warn_on_extra_files) end # This is a shortcut for # # require "zeitwerk" # # loader = Zeitwerk::Loader.new # loader.tag = namespace.name + "-" + File.basename(__FILE__, ".rb") # loader.inflector = Zeitwerk::GemInflector.new(__FILE__) # loader.push_dir(__dir__, namespace: namespace) # # 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. # # This method returns a subclass of Zeitwerk::Loader, but the exact type # is private, client code can only rely on the interface. # # @sig (bool) -> Zeitwerk::GemLoader def for_gem_extension(namespace) unless namespace.is_a?(Module) # Note that Class < Module. raise Zeitwerk::Error, "#{namespace.inspect} is not a class or module object, should be" end unless real_mod_name(namespace) raise Zeitwerk::Error, "extending anonymous namespaces is unsupported" end called_from = caller_locations(1, 1).first.path Registry.loader_for_gem(called_from, namespace: namespace, warn_on_extra_files: false) end # Broadcasts `eager_load` to all loaders. Those that have not been setup # are skipped. # # @sig () -> void def eager_load_all Registry.loaders.each do |loader| begin loader.eager_load rescue SetupRequired # This is fine, we eager load what can be eager loaded. end end end # Broadcasts `eager_load_namespace` to all loaders. Those that have not # been setup are skipped. # # @sig (Module) -> void def eager_load_namespace(mod) Registry.loaders.each do |loader| begin loader.eager_load_namespace(mod) rescue SetupRequired # This is fine, we eager load what can be eager loaded. end end 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 # @sig (String, Module) -> void private def set_autoloads_in_dir(dir, parent) ls(dir) do |basename, abspath| begin if ruby?(basename) basename.delete_suffix!(".rb") cname = inflector.camelize(basename, abspath).to_sym autoload_file(parent, cname, abspath) else if collapse?(abspath) set_autoloads_in_dir(abspath, parent) else cname = inflector.camelize(basename, abspath).to_sym autoload_subdir(parent, cname, abspath) 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 private def autoload_subdir(parent, cname, subdir) if autoload_path = autoload_path_set_by_me_for?(parent, cname) cpath = cpath(parent, cname) if ruby?(autoload_path) # Scanning visited a Ruby file first, and now a directory for the same # constant has been found. This means we are dealing with an explicit # namespace whose definition was seen first. # # Registering is idempotent, and we have to keep the autoload pointing # to the file. This may run again if more directories are found later # on, no big deal. register_explicit_namespace(cpath) end # If the existing autoload points to a file, it has to be preserved, if # not, it is fine as it is. In either case, we do not need to override. # Just remember the subdirectory conforms this namespace. namespace_dirs[cpath] << subdir elsif !cdef?(parent, cname) # First time we find this namespace, set an autoload for it. namespace_dirs[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. log("the namespace #{cpath(parent, cname)} already exists, descending into #{subdir}") if logger set_autoloads_in_dir(subdir, cget(parent, cname)) end end # @sig (Module, Symbol, String) -> void private def autoload_file(parent, cname, file) if autoload_path = strict_autoload_path(parent, cname) || Registry.inception?(cpath(parent, cname)) # First autoload for a Ruby file wins, just ignore subsequent ones. if ruby?(autoload_path) shadowed_files << file 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) shadowed_files << file 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 private def promote_namespace_from_implicit_to_explicit(dir:, file:, parent:, cname:) autoloads.delete(dir) Registry.unregister_autoload(dir) log("earlier autoload for #{cpath(parent, cname)} discarded, it is actually an explicit namespace defined in #{file}") if logger set_autoload(parent, cname, file) register_explicit_namespace(cpath(parent, cname)) end # @sig (Module, Symbol, String) -> void private def set_autoload(parent, cname, abspath) parent.autoload(cname, abspath) if logger if ruby?(abspath) log("autoload set for #{cpath(parent, cname)}, to be loaded from #{abspath}") else log("autoload set for #{cpath(parent, cname)}, to be autovivified from #{abspath}") end end autoloads[abspath] = [parent, cname] Registry.register_autoload(self, abspath) # See why in the documentation of Zeitwerk::Registry.inceptions. unless parent.autoload?(cname) Registry.register_inception(cpath(parent, cname), abspath, self) end end # @sig (Module, Symbol) -> String? private def autoload_path_set_by_me_for?(parent, cname) if autoload_path = strict_autoload_path(parent, cname) autoload_path if autoloads.key?(autoload_path) else Registry.inception?(cpath(parent, cname)) end end # @sig (String) -> void private def register_explicit_namespace(cpath) ExplicitNamespace.__register(cpath, self) end # @sig (String) -> void private def raise_if_conflicting_directory(dir) MUTEX.synchronize do dir_slash = dir + "/" Registry.loaders.each do |loader| next if loader == self next if loader.__ignores?(dir) loader.__roots.each_key do |root_dir| next if ignores?(root_dir) root_dir_slash = root_dir + "/" if dir_slash.start_with?(root_dir_slash) || root_dir_slash.start_with?(dir_slash) require "pp" # Needed for pretty_inspect, even in Ruby 2.5. 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 end # @sig (String, Object, String) -> void private def run_on_unload_callbacks(cpath, value, abspath) # Order matters. If present, run the most specific one. on_unload_callbacks[cpath]&.each { |c| c.call(value, abspath) } on_unload_callbacks[:ANY]&.each { |c| c.call(cpath, value, abspath) } end # @sig (Module, Symbol) -> void private def unload_autoload(parent, cname) crem(parent, cname) log("autoload for #{cpath(parent, cname)} removed") if logger end # @sig (Module, Symbol) -> void private def unload_cref(parent, cname) # Let's optimistically remove_const. The way we use it, this is going to # succeed always if all is good. crem(parent, cname) rescue ::NameError # There are a few edge scenarios in which this may happen. If the constant # is gone, that is OK, anyway. else log("#{cpath(parent, cname)} unloaded") if logger end end end zeitwerk-2.6.8/lib/zeitwerk/loader/000077500000000000000000000000001442267002100172515ustar00rootroot00000000000000zeitwerk-2.6.8/lib/zeitwerk/loader/callbacks.rb000066400000000000000000000064521442267002100215240ustar00rootroot00000000000000# frozen_string_literal: true module 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) Zeitwerk::Registry.unregister_autoload(file) if cdef?(*cref) log("constant #{cpath} loaded from file #{file}") if logger to_unload[cpath] = [file, cref] if reloading_enabled? run_on_load_callbacks(cpath, cget(*cref), file) unless on_load_callbacks.empty? else msg = "expected file #{file} to define constant #{cpath}, but didn't" log(msg) if logger crem(*cref) to_unload[cpath] = [file, cref] if reloading_enabled? raise Zeitwerk::NameError.new(msg, cref.last) end 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 namespace_dirs 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, autovivified_module, dir) unless on_load_callbacks.empty? 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 dirs = namespace_dirs.delete(real_mod_name(namespace)) dirs.each do |dir| set_autoloads_in_dir(dir, namespace) end end end private # @sig (String, Object) -> void def run_on_load_callbacks(cpath, value, abspath) # Order matters. If present, run the most specific one. callbacks = reloading_enabled? ? on_load_callbacks[cpath] : on_load_callbacks.delete(cpath) callbacks&.each { |c| c.call(value, abspath) } callbacks = on_load_callbacks[:ANY] callbacks&.each { |c| c.call(cpath, value, abspath) } end end zeitwerk-2.6.8/lib/zeitwerk/loader/config.rb000066400000000000000000000245501442267002100210510ustar00rootroot00000000000000# frozen_string_literal: true require "set" require "securerandom" module Zeitwerk::Loader::Config extend Zeitwerk::Internal include Zeitwerk::RealModName # @sig #camelize attr_accessor :inflector # @sig #call | #debug | nil attr_accessor :logger # Absolute paths of the root directories, mapped to their respective root namespaces: # # "/Users/fxn/blog/app/channels" => Object, # "/Users/fxn/blog/app/adapters" => ActiveJob::QueueAdapters, # ... # # Stored in a hash to preserve order, easily handle duplicates, and have a # fast lookup by directory. # # This is a private collection maintained by the loader. The public # interface for it is `push_dir` and `dirs`. # # @sig Hash[String, Module] attr_reader :roots internal :roots # Absolute paths of files, directories, or glob patterns to be totally # ignored. # # @sig Set[String] attr_reader :ignored_glob_patterns private :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. # # @sig Set[String] attr_reader :ignored_paths private :ignored_paths # Absolute paths of directories or glob patterns to be collapsed. # # @sig Set[String] attr_reader :collapse_glob_patterns private :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. # # @sig Set[String] attr_reader :collapse_dirs private :collapse_dirs # Absolute paths of files or directories not to be eager loaded. # # @sig Set[String] attr_reader :eager_load_exclusions private :eager_load_exclusions # User-oriented callbacks to be fired on setup and on reload. # # @sig Array[{ () -> void }] attr_reader :on_setup_callbacks private :on_setup_callbacks # User-oriented callbacks to be fired when a constant is loaded. # # @sig Hash[String, Array[{ (Object, String) -> void }]] # Hash[Symbol, Array[{ (String, Object, String) -> void }]] attr_reader :on_load_callbacks private :on_load_callbacks # User-oriented callbacks to be fired before constants are removed. # # @sig Hash[String, Array[{ (Object, String) -> void }]] # Hash[Symbol, Array[{ (String, Object, String) -> void }]] attr_reader :on_unload_callbacks private :on_unload_callbacks def initialize @inflector = Zeitwerk::Inflector.new @logger = self.class.default_logger @tag = SecureRandom.hex(3) @initialized_at = Time.now @roots = {} @ignored_glob_patterns = Set.new @ignored_paths = Set.new @collapse_glob_patterns = Set.new @collapse_dirs = Set.new @eager_load_exclusions = Set.new @reloading_enabled = false @on_setup_callbacks = [] @on_load_callbacks = {} @on_unload_callbacks = {} 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) unless namespace.is_a?(Module) # Note that Class < Module. raise Zeitwerk::Error, "#{namespace.inspect} is not a class or module object, should be" end unless real_mod_name(namespace) raise Zeitwerk::Error, "root namespaces cannot be anonymous" end abspath = File.expand_path(path) if dir?(abspath) raise_if_conflicting_directory(abspath) roots[abspath] = namespace else raise Zeitwerk::Error, "the root directory #{abspath} does not exist" end end # Returns the loader's tag. # # Implemented as a method instead of via attr_reader for symmetry with the # writer below. # # @sig () -> String def tag @tag end # Sets a tag for the loader, useful for logging. # # @sig (#to_s) -> void def tag=(tag) @tag = tag.to_s end # If `namespaces` is falsey (default), returns an array with the absolute # paths of the root directories as strings. If truthy, returns a hash table # instead. Keys are the absolute paths of the root directories as strings, # values are their corresponding namespaces, class or module objects. # # If `ignored` is falsey (default), ignored root directories are filtered out. # # These are read-only collections, please add to them with `push_dir`. # # @sig () -> Array[String] | Hash[String, Module] def dirs(namespaces: false, ignored: false) if namespaces if ignored || ignored_paths.empty? roots.clone else roots.reject { |root_dir, _namespace| ignored_path?(root_dir) } end else if ignored || ignored_paths.empty? roots.keys else roots.keys.reject { |root_dir| ignored_path?(root_dir) } end end.freeze 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 Zeitwerk::Error, "cannot enable reloading after setup" else @reloading_enabled = true end end end # @sig () -> bool def reloading_enabled? @reloading_enabled 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 # 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 called after setup and on each reload. # If setup was already done, the block runs immediately. # # @sig () { () -> void } -> void def on_setup(&block) mutex.synchronize do on_setup_callbacks << block block.call if @setup 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 |klass, _abspath| # klass.endpoint = "https://api.dev" # end # # Can also be configured for any constant loaded: # # loader.on_load do |cpath, value, abspath| # # ... # end # # @raise [TypeError] # @sig (String) { (Object, String) -> void } -> void # (:ANY) { (String, Object, String) -> void } -> void def on_load(cpath = :ANY, &block) raise TypeError, "on_load only accepts strings" unless cpath.is_a?(String) || cpath == :ANY mutex.synchronize do (on_load_callbacks[cpath] ||= []) << block end end # Configure a block to be invoked right before a certain constant is removed. # Supports multiple callbacks, and if there are many, they are executed in the # order in which they were defined. # # loader.on_unload("Country") do |klass, _abspath| # klass.clear_cache # end # # Can also be configured for any removed constant: # # loader.on_unload do |cpath, value, abspath| # # ... # end # # @raise [TypeError] # @sig (String) { (Object) -> void } -> void # (:ANY) { (String, Object) -> void } -> void def on_unload(cpath = :ANY, &block) raise TypeError, "on_unload only accepts strings" unless cpath.is_a?(String) || cpath == :ANY mutex.synchronize do (on_unload_callbacks[cpath] ||= []) << block end end # Logs to `$stdout`, handy shortcut for debugging. # # @sig () -> void def log! @logger = ->(msg) { puts msg } end # Returns true if the argument has been configured to be ignored, or is a # descendant of an ignored directory. # # @sig (String) -> bool internal def ignores?(abspath) # Common use case. return false if ignored_paths.empty? walk_up(abspath) do |path| return true if ignored_path?(path) return false if roots.key?(path) end false end # @sig (String) -> bool private def ignored_path?(abspath) ignored_paths.member?(abspath) end # @sig () -> Array[String] private def actual_roots roots.reject do |root_dir, _root_namespace| !dir?(root_dir) || ignored_path?(root_dir) end end # @sig (String) -> bool private def root_dir?(dir) roots.key?(dir) end # @sig (String) -> bool private def excluded_from_eager_load?(abspath) # Optimize this common use case. return false if eager_load_exclusions.empty? walk_up(abspath) do |path| return true if eager_load_exclusions.member?(path) return false if roots.key?(path) end false end # @sig (String) -> bool private def collapse?(dir) collapse_dirs.member?(dir) end # @sig (String | Pathname | Array[String | Pathname]) -> Array[String] private def expand_paths(paths) paths.flatten.map! { |path| File.expand_path(path) } end # @sig (Array[String]) -> Array[String] private 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 private def recompute_ignored_paths ignored_paths.replace(expand_glob_patterns(ignored_glob_patterns)) end # @sig () -> void private def recompute_collapse_dirs collapse_dirs.replace(expand_glob_patterns(collapse_glob_patterns)) end end zeitwerk-2.6.8/lib/zeitwerk/loader/eager_load.rb000066400000000000000000000160531442267002100216650ustar00rootroot00000000000000module Zeitwerk::Loader::EagerLoad # Eager loads all files in the root directories, recursively. Files do not # need to be in `$LOAD_PATH`, absolute file names are used. Ignored and # shadowed files are not eager loaded. You can opt-out specifically in # specific files and directories with `do_not_eager_load`, and that can be # overridden passing `force: true`. # # @sig (true | false) -> void def eager_load(force: false) mutex.synchronize do break if @eager_loaded raise Zeitwerk::SetupRequired unless @setup log("eager load start") if logger actual_roots.each do |root_dir, root_namespace| actual_eager_load_dir(root_dir, root_namespace, force: force) end autoloaded_dirs.each do |autoloaded_dir| Zeitwerk::Registry.unregister_autoload(autoloaded_dir) end autoloaded_dirs.clear @eager_loaded = true log("eager load end") if logger end end # @sig (String | Pathname) -> void def eager_load_dir(path) raise Zeitwerk::SetupRequired unless @setup abspath = File.expand_path(path) raise Zeitwerk::Error.new("#{abspath} is not a directory") unless dir?(abspath) cnames = [] root_namespace = nil walk_up(abspath) do |dir| return if ignored_path?(dir) return if eager_load_exclusions.member?(dir) break if root_namespace = roots[dir] unless collapse?(dir) basename = File.basename(dir) cnames << inflector.camelize(basename, dir).to_sym end end raise Zeitwerk::Error.new("I do not manage #{abspath}") unless root_namespace return if @eager_loaded namespace = root_namespace cnames.reverse_each do |cname| # Can happen if there are no Ruby files. This is not an error condition, # the directory is actually managed. Could have Ruby files later. return unless cdef?(namespace, cname) namespace = cget(namespace, cname) end # A shortcircuiting test depends on the invocation of this method. Please # keep them in sync if refactored. actual_eager_load_dir(abspath, namespace) end # @sig (Module) -> void def eager_load_namespace(mod) raise Zeitwerk::SetupRequired unless @setup unless mod.is_a?(Module) raise Zeitwerk::Error, "#{mod.inspect} is not a class or module object" end return if @eager_loaded mod_name = real_mod_name(mod) return unless mod_name actual_roots.each do |root_dir, root_namespace| if mod.equal?(Object) # A shortcircuiting test depends on the invocation of this method. # Please keep them in sync if refactored. actual_eager_load_dir(root_dir, root_namespace) elsif root_namespace.equal?(Object) eager_load_child_namespace(mod, mod_name, root_dir, root_namespace) else root_namespace_name = real_mod_name(root_namespace) if root_namespace_name.start_with?(mod_name + "::") actual_eager_load_dir(root_dir, root_namespace) elsif mod_name == root_namespace_name actual_eager_load_dir(root_dir, root_namespace) elsif mod_name.start_with?(root_namespace_name + "::") eager_load_child_namespace(mod, mod_name, root_dir, root_namespace) else # Unrelated constant hierarchies, do nothing. end end end end # Loads the given Ruby file. # # Raises if the argument is ignored, shadowed, or not managed by the receiver. # # The method is implemented as `constantize` for files, in a sense, to be able # to descend orderly and make sure the file is loadable. # # @sig (String | Pathname) -> void def load_file(path) abspath = File.expand_path(path) raise Zeitwerk::Error.new("#{abspath} does not exist") unless File.exist?(abspath) raise Zeitwerk::Error.new("#{abspath} is not a Ruby file") if dir?(abspath) || !ruby?(abspath) raise Zeitwerk::Error.new("#{abspath} is ignored") if ignored_path?(abspath) basename = File.basename(abspath, ".rb") base_cname = inflector.camelize(basename, abspath).to_sym root_namespace = nil cnames = [] walk_up(File.dirname(abspath)) do |dir| raise Zeitwerk::Error.new("#{abspath} is ignored") if ignored_path?(dir) break if root_namespace = roots[dir] unless collapse?(dir) basename = File.basename(dir) cnames << inflector.camelize(basename, dir).to_sym end end raise Zeitwerk::Error.new("I do not manage #{abspath}") unless root_namespace namespace = root_namespace cnames.reverse_each do |cname| namespace = cget(namespace, cname) end raise Zeitwerk::Error.new("#{abspath} is shadowed") if shadowed_file?(abspath) cget(namespace, base_cname) end # The caller is responsible for making sure `namespace` is the namespace that # corresponds to `dir`. # # @sig (String, Module, Boolean) -> void private def actual_eager_load_dir(dir, namespace, force: false) honour_exclusions = !force return if honour_exclusions && excluded_from_eager_load?(dir) log("eager load directory #{dir} start") if logger queue = [[dir, namespace]] while to_eager_load = queue.shift dir, namespace = to_eager_load ls(dir) do |basename, abspath| next if honour_exclusions && eager_load_exclusions.member?(abspath) if ruby?(abspath) if (cref = autoloads[abspath]) && !shadowed_file?(abspath) cget(*cref) end else if collapse?(abspath) queue << [abspath, namespace] else cname = inflector.camelize(basename, abspath).to_sym queue << [abspath, cget(namespace, cname)] end end end end log("eager load directory #{dir} end") if logger end # In order to invoke this method, the caller has to ensure `child` is a # strict namespace descendant of `root_namespace`. # # @sig (Module, String, Module, Boolean) -> void private def eager_load_child_namespace(child, child_name, root_dir, root_namespace) suffix = child_name unless root_namespace.equal?(Object) suffix = suffix.delete_prefix(real_mod_name(root_namespace) + "::") end # These directories are at the same namespace level, there may be more if # we find collapsed ones. As we scan, we look for matches for the first # segment, and store them in `next_dirs`. If there are any, we look for # the next segments in those matches. Repeat. # # If we exhaust the search locating directories that match all segments, # we just need to eager load those ones. dirs = [root_dir] next_dirs = [] suffix.split("::").each do |segment| while dir = dirs.shift ls(dir) do |basename, abspath| next unless dir?(abspath) if collapse?(abspath) dirs << abspath elsif segment == inflector.camelize(basename, abspath) next_dirs << abspath end end end return if next_dirs.empty? dirs.replace(next_dirs) next_dirs.clear end dirs.each do |dir| actual_eager_load_dir(dir, child) end end end zeitwerk-2.6.8/lib/zeitwerk/loader/helpers.rb000066400000000000000000000072201442267002100212410ustar00rootroot00000000000000# frozen_string_literal: true module Zeitwerk::Loader::Helpers # --- Logging ----------------------------------------------------------------------------------- # @sig (String) -> void private def log(message) method_name = logger.respond_to?(:debug) ? :debug : :call logger.send(method_name, "Zeitwerk@#{tag}: #{message}") end # --- Files and directories --------------------------------------------------------------------- # @sig (String) { (String, String) -> void } -> void private def ls(dir) children = Dir.children(dir) # The order in which a directory is listed depends on the file system. # # Since client code may run in different platforms, it seems convenient to # order directory entries. This provides consistent eager loading across # platforms, for example. children.sort! children.each do |basename| next if hidden?(basename) abspath = File.join(dir, basename) next if ignored_path?(abspath) if dir?(abspath) next if roots.key?(abspath) next if !has_at_least_one_ruby_file?(abspath) else next unless ruby?(abspath) end # We freeze abspath because that saves allocations when passed later to # File methods. See #125. yield basename, abspath.freeze end end # @sig (String) -> bool private def has_at_least_one_ruby_file?(dir) to_visit = [dir] while dir = to_visit.shift ls(dir) do |_basename, abspath| if dir?(abspath) to_visit << abspath else return true end end end false end # @sig (String) -> bool private def ruby?(path) path.end_with?(".rb") end # @sig (String) -> bool private def dir?(path) File.directory?(path) end # @sig (String) -> bool private def hidden?(basename) basename.start_with?(".") end # @sig (String) { (String) -> void } -> void private def walk_up(abspath) loop do yield abspath abspath, basename = File.split(abspath) break if basename == "/" end end # --- Constants --------------------------------------------------------------------------------- # 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 private def strict_autoload_path(parent, cname) parent.autoload?(cname) if cdef?(parent, cname) end else private def strict_autoload_path(parent, cname) parent.autoload?(cname, false) end end # @sig (Module, Symbol) -> String if Symbol.method_defined?(:name) # Symbol#name was introduced in Ruby 3.0. It returns always the same # frozen object, so we may save a few string allocations. private def cpath(parent, cname) Object == parent ? cname.name : "#{real_mod_name(parent)}::#{cname.name}" end else private def cpath(parent, cname) Object == parent ? cname.to_s : "#{real_mod_name(parent)}::#{cname}" end end # @sig (Module, Symbol) -> bool private def cdef?(parent, cname) parent.const_defined?(cname, false) end # @raise [NameError] # @sig (Module, Symbol) -> Object private def cget(parent, cname) parent.const_get(cname, false) end # @raise [NameError] # @sig (Module, Symbol) -> Object private def crem(parent, cname) parent.__send__(:remove_const, cname) end end zeitwerk-2.6.8/lib/zeitwerk/real_mod_name.rb000066400000000000000000000011721442267002100211130ustar00rootroot00000000000000# frozen_string_literal: true module 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.6.8/lib/zeitwerk/registry.rb000066400000000000000000000104011442267002100201740ustar00rootroot00000000000000# 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 gem loaders to let `for_gem` be idempotent in case of reload. # # @private # @sig Hash[String, Zeitwerk::Loader] attr_reader :gem_loaders_by_root_file # Maps absolute 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 # @private # @sig (Zeitwerk::Loader) -> void def unregister_loader(loader) loaders.delete(loader) gem_loaders_by_root_file.delete_if { |_, l| l == loader } autoloads.delete_if { |_, l| l == loader } inceptions.delete_if { |_, (_, l)| l == 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, namespace:, warn_on_extra_files:) gem_loaders_by_root_file[root_file] ||= GemLoader.__new(root_file, namespace: namespace, warn_on_extra_files: warn_on_extra_files) end # @private # @sig (Zeitwerk::Loader, String) -> String def register_autoload(loader, abspath) autoloads[abspath] = loader end # @private # @sig (String) -> void def unregister_autoload(abspath) autoloads.delete(abspath) end # @private # @sig (String, String, Zeitwerk::Loader) -> void def register_inception(cpath, abspath, loader) inceptions[cpath] = [abspath, 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 = [] @gem_loaders_by_root_file = {} @autoloads = {} @inceptions = {} end end zeitwerk-2.6.8/lib/zeitwerk/version.rb000066400000000000000000000001071442267002100200130ustar00rootroot00000000000000# frozen_string_literal: true module Zeitwerk VERSION = "2.6.8" end zeitwerk-2.6.8/test/000077500000000000000000000000001442267002100143505ustar00rootroot00000000000000zeitwerk-2.6.8/test/lib/000077500000000000000000000000001442267002100151165ustar00rootroot00000000000000zeitwerk-2.6.8/test/lib/test_gem_inflector.rb000066400000000000000000000016561442267002100213270ustar00rootroot00000000000000# frozen_string_literal: true require "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 with_load_path("lib") do assert require("my_gem") yield end 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.6.8/test/lib/test_inflector.rb000066400000000000000000000020761442267002100204740ustar00rootroot00000000000000# frozen_string_literal: true require "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.6.8/test/lib/test_real_mod_name.rb000066400000000000000000000023421442267002100212650ustar00rootroot00000000000000# frozen_string_literal: true require "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.6.8/test/lib/zeitwerk/000077500000000000000000000000001442267002100167625ustar00rootroot00000000000000zeitwerk-2.6.8/test/lib/zeitwerk/test_all_dirs.rb000066400000000000000000000013221442267002100221350ustar00rootroot00000000000000# frozen_string_literal: true require "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.6.8/test/lib/zeitwerk/test_ancestors.rb000066400000000000000000000023371442267002100223540ustar00rootroot00000000000000# frozen_string_literal: true require "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.6.8/test/lib/zeitwerk/test_autovivification.rb000066400000000000000000000101151442267002100237270ustar00rootroot00000000000000# frozen_string_literal: true require "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 = [ ["rd1/admin/hotel.rb", "class Admin::Hotel; end"], ["rd2/admin/hotels_controller.rb", "class Admin::HotelsController; end"] ] with_setup(files) do assert Admin::Hotel assert Admin::HotelsController end end test "autoloads several constants from the same namespace (Namespace)" do files = [ ["rd1/admin/hotel.rb", "class #{Namespace}::Admin::Hotel; end"], ["rd2/admin/hotels_controller.rb", "class #{Namespace}::Admin::HotelsController; end"] ] with_setup(files, namespace: Namespace) do assert Namespace::Admin::Hotel assert Namespace::Admin::HotelsController end end test "does not register the namespace as explicit" do files = [ ["rd1/admin/x.rb", "Admin::X = true"], ["rd2/admin/y.rb", "Admin::Y = true"] ] with_setup(files) do assert !Zeitwerk::ExplicitNamespace.__registered?("Admin") end end test "autovivification is synchronized" do $test_admin_const_set_calls = 0 $test_admin_const_set_queue = Queue.new 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 $test_admin_const_set_queue << true sleep 0.1 super end concurrent_autovivifications = [ Thread.new { Admin::V2 }, Thread.new { $test_admin_const_set_queue.pop() Admin::V2 } ] concurrent_autovivifications.each(&:join) assert $test_admin_const_set_queue.empty? assert_equal 1, $test_admin_const_set_calls end end test "defines no namespace for empty directories" do with_files([]) do FileUtils.mkdir("foo") loader.push_dir(".") loader.setup assert !Object.autoload?(:Foo) end end test "defines no namespace for empty directories (recursively)" do with_files([]) do FileUtils.mkdir_p("foo/bar/baz") loader.push_dir(".") loader.setup assert !Object.autoload?(:Foo) end end test "defines no namespace for directories whose files are all non-Ruby" do with_setup([["tasks/newsletter.rake", ""], ["assets/.keep", ""]]) do assert !Object.autoload?(:Tasks) assert !Object.autoload?(:Assets) end end test "defines no namespace for directories whose files are all non-Ruby (recursively)" do with_setup([["tasks/product/newsletter.rake", ""], ["assets/css/.keep", ""]]) do assert !Object.autoload?(:Tasks) assert !Object.autoload?(:Assets) end end test "defines no namespace for directories whose Ruby files are all ignored" do with_setup([["foo/bar/ignored.rb", "IGNORED"]]) do assert !Object.autoload?(:Foo) end end test "defines no namespace for directories that have Ruby files below ignored directories" do with_setup([["foo/ignored/baz.rb", "IGNORED"]]) do assert !Object.autoload?(:Foo) end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_callbacks.rb000066400000000000000000000054151442267002100222720ustar00rootroot00000000000000# frozen_string_literal: true require "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.expand_path("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.expand_path("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.expand_path("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.expand_path("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.expand_path("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.expand_path("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.6.8/test/lib/zeitwerk/test_collapse.rb000066400000000000000000000061231442267002100221520ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" require "set" class TestCollapse < LoaderTest test "top-level directories can be collapsed" do files = [["collapsed/bar/x.rb", "Bar::X = true"]] with_setup(files) do assert Bar::X end end test "collapsed directories are ignored as namespaces" do files = [["foo/collapsed/x.rb", "Foo::X = true"]] with_setup(files) do assert Foo::X end end test "collapsed directories are ignored as explicit namespaces" do files = [ ["collapsed.rb", "Collapsed = true"], ["collapsed/x.rb", "X = true"] ] with_setup(files) do assert Collapsed assert X end end test "explicit namespaces are honored downwards" do files = [ ["foo.rb", "module Foo; end"], ["foo/foo/x.rb", "Foo::X = true"] ] with_files(files) do loader.push_dir(".") loader.collapse("foo") loader.setup assert Foo::X end end test "explicit namespaces are honored downwards, deeper" do files = [ ["foo.rb", "module Foo; end"], ["foo/bar/foo/x.rb", "Foo::X = true"] ] with_files(files) do loader.push_dir(".") loader.collapse(["foo", "foo/bar"]) loader.setup assert Foo::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 glob patterns 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/collapsed/x.rb", "Foo::X = true"]] with_setup(files) do loader.eager_load assert required?(files) end end test "collapsed top-level directories are eager loaded too" do $collapse_honored_when_eager_loading = false files = [["collapsed/bar/x.rb", "Bar::X = true"]] with_setup(files) do loader.eager_load assert required?(files) end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_conflicting_directory.rb000066400000000000000000000061261442267002100247360ustar00rootroot00000000000000# frozen_string_literal: true require "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 a second existing loader ignores the directory (dir)" do # Ensure this loader is loaded existing_loader second_existing_loader = new_loader(setup: false) second_existing_loader.push_dir(parent) second_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 "does not raise if the loader ignores a directory managed by an existing loader (dir)" do existing_loader.push_dir(dir) loader.ignore(dir) assert loader.push_dir(parent) end test "does not raise if the loader ignores a directory managed by an existing loader (glob pattern)" do existing_loader.push_dir(dir) loader.ignore("#{parent}/*") assert loader.push_dir(parent) 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.6.8/test/lib/zeitwerk/test_eager_load.rb000066400000000000000000000230521442267002100224320ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" require "fileutils" class TestEagerLoad < LoaderTest module Namespace; end test "eager loads independent files (Object)" do loaders = [loader, new_loader(setup: false)] 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 loaders[0].push_dir("lib0") loaders[0].setup loaders[1].push_dir("lib1") loaders[1].setup Zeitwerk::Loader.eager_load_all assert required?(files) end end test "eager loads independent files (Namespace)" do loaders = [loader, new_loader(setup: false)] files = [ ["lib0/app0.rb", "module #{Namespace}::App0; end"], ["lib0/app0/foo.rb", "class #{Namespace}::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 loaders[0].push_dir("lib0", namespace: Namespace) loaders[0].setup loaders[1].push_dir("lib1") loaders[1].setup Zeitwerk::Loader.eager_load_all assert required?(files) end end test "eager loads dependent loaders" do loaders = [loader, new_loader(setup: false)] 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 loaders[0].push_dir("lib0") loaders[0].setup loaders[1].push_dir("lib1") loaders[1].setup Zeitwerk::Loader.eager_load_all assert required?(files) end end test "skips loaders that are not ready" do files = [["x.rb", "X = 1"]] with_setup(files) do new_loader(setup: false) # should be skipped Zeitwerk::Loader.eager_load_all assert required?(files) 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 files = [ ["my_gem.rb", <<-EOS], loader = Zeitwerk::Loader.for_gem loader.setup class MyGem Foo::Baz # autoloads fine end 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/baz.rb", "class MyGem::Foo::Baz; end"], ] with_files(files) do with_load_path(".") do require "my_gem" assert required?(files) 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 files = [["foo.rb", "Foo = true"]] with_files(files) do loader = new_loader(dirs: ".", enable_reloading: enable_reloading) loader.do_not_eager_load(".") loader.eager_load assert !required?(files[0]) 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 files = [ ["db_adapters/mysql_adapter.rb", <<-EOS], module DbAdapters::MysqlAdapter end 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 !required?(files[0]) assert required?(files[1]) 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 files = [ ["foo.rb", "Foo = true"], ["bar.rb", "Bar = true"] ] with_files(files) do loader = new_loader(dirs: ".", enable_reloading: enable_reloading) loader.do_not_eager_load("bar.rb") loader.eager_load assert required?(files[0]) assert !required?(files[1]) 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 files = [ ["lazylib/ns/foo.rb", "module Ns::Foo; end"], ["eagerlib/ns/bar.rb", "module Ns::Bar; 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 !required?(files[0]) assert required?(files[1]) assert Ns::Foo 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 files = [ ["lazylib/ns/foo.rb", "module Ns::Foo; end"], ["eagerlib/ns/bar.rb", "module Ns::Bar; end"] ] with_files(files) do loader = new_loader(dirs: %w(lazylib eagerlib), enable_reloading: enable_reloading) loader.do_not_eager_load('lazylib/ns') loader.eager_load assert !required?(files[0]) assert required?(files[1]) assert Ns::Foo end end end test "eager loading skips shadowed files" do files = [ ["a/foo.rb", "Foo = 1"], ["b/foo.rb", "Foo = 1"] ] with_files(files) do la = new_loader(dirs: "a") lb = new_loader(dirs: "b") la.eager_load lb.eager_load assert required?(files[0]) assert !required?(files[1]) end end test "eager loading skips files that would map to already loaded constants" do on_teardown { remove_const :X } files = [["x.rb", "X = 1"]] ::X = 1 with_setup(files) do loader.eager_load assert !required?(files[0]) 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 test "force eager load for root directories" do files = [["foo.rb", "Foo = true"]] with_setup(files) do loader.do_not_eager_load(".") loader.eager_load(force: true) assert required?(files) end end test "force eager load for sudirectories" do files = [ ["db_adapters/mysql_adapter.rb", <<-EOS], module DbAdapters::MysqlAdapter end EOS ] with_setup(files) do loader.do_not_eager_load("db_adapters") loader.eager_load(force: true) assert required?(files) assert DbAdapters::MysqlAdapter end end test "force eager load for root files" do files = [["foo.rb", "Foo = true"]] with_setup(files) do loader.do_not_eager_load("foo.rb") loader.eager_load(force: true) assert required?(files) end end test "force eager load for namespaced files" do files = [["m/foo.rb", "M::Foo = true"]] with_setup(files) do loader.do_not_eager_load("m/foo.rb") loader.eager_load(force: true) assert required?(files) end end test "force eager load honours ignored root directories" do files = [["ignored/foo.rb", "Foo = true"]] with_setup(files, dirs: %w(ignored)) do loader.eager_load(force: true) assert !required?(files) end end test "force eager load honours ignored subdirectories" do files = [["ignored/foo.rb", "IGNORED"]] with_setup(files) do loader.eager_load(force: true) assert !required?(files) end end test "force eager load honours root files" do files = [["ignored.rb", "IGNORED"]] with_setup(files) do loader.eager_load(force: true) assert !required?(files) end end test "force eager load honours namespaced files" do files = [["m/ignored.rb", "IGNORED"]] with_setup(files) do loader.eager_load(force: true) assert !required?(files) end end test "files are eager loaded in lexicographic order" do files = [["x.rb", "X = 1"], ["y.rb", "Y = 1"]] with_setup(files) do loaded = [] loader.on_load do |cpath, _value, _abspath| loaded << cpath end Dir.stub :children, ["y.rb", "x.rb"] do loader.eager_load end assert_equal ["X", "Y"], loaded end end test "raises if called before setup" do assert_raises(Zeitwerk::SetupRequired) do loader.eager_load end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_eager_load_dir.rb000066400000000000000000000171731442267002100232770ustar00rootroot00000000000000# frozen_string_literal: true require "pathname" require "test_helper" class TestEagerLoadDir < LoaderTest test "eager loads all files" do files = [ ["x.rb", "X = 1"], ["y.rb", "Y = 1"], ["m/n/p.rb", "module M::N::P; end"], ["m/n/a.rb", "M::N::A = 1"], ["m/n/p/q/z.rb", "M::N::P::Q::Z = 1"] ] with_setup(files) do loader.eager_load_dir(".") files.each do |file| assert required?(file) end end end test "eager loads all files (Pathname)" do files = [ ["x.rb", "X = 1"], ["y.rb", "Y = 1"], ["m/n/p.rb", "module M::N::P; end"], ["m/n/a.rb", "M::N::A = 1"], ["m/n/p/q/z.rb", "M::N::P::Q::Z = 1"] ] with_setup(files) do loader.eager_load_dir(Pathname.new(".")) assert required?(files) end end test "does not eager load excluded files or directories" do files = [ ["x.rb", "X = 1"], ["y.rb", "Y = 1"], ["m/n.rb", "module M::N; end"], ["m/n/a.rb", "M::N::A = 1"] ] with_setup(files) do loader.do_not_eager_load("y.rb") loader.do_not_eager_load("m/n") loader.eager_load_dir(".") assert required?(files[0]) assert !required?(files[1]) assert required?(files[2]) assert !required?(files[3]) end end test "does not eager load excluded files or directories (descendants)" do files = [ ["excluded/m/n.rb", "module M::N; end"], ["excluded/m/n/a.rb", "M::N::A = 1"] ] with_setup(files) do loader.do_not_eager_load("excluded") loader.eager_load_dir(".") assert files.none? { |file| required?(file) } end end # This is a file system-based interface. test "eager loads excluded explicit namespaces if some subtree is not excluded" do files = [ ["x.rb", "X = 1"], ["m/n.rb", "module M::N; end"], ["m/n/a.rb", "M::N::A = 1"] ] with_setup(files) do loader.do_not_eager_load("m/n.rb") loader.eager_load_dir(".") assert required?(files) end end test "does not load intermediate files if the target is an excluded descendant" do files = [["excluded.rb", "module Excluded; end"], ["excluded/n/x.rb", "EXCLUDED"]] with_setup(files) do loader.do_not_eager_load("excluded") loader.eager_load_dir("excluded/n") assert !required?(files[0]) end end test "does not eager load descendant ignored files or directories" do files = [ ["x.rb", "X = 1"], ["y.rb", "IGNORED"], ["m/n.rb", "module M::N; end"], ["m/n/a.rb", "IGNORED"] ] with_files(files) do loader.push_dir(".") loader.ignore("y.rb") loader.ignore("m/n") loader.setup loader.eager_load_dir(".") assert required?(files[0]) assert !required?(files[1]) assert required?(files[2]) assert !required?(files[3]) end end test "does not eager load shadowed files" do files = [ ["rd1/x.rb", "X = 1"], ["rd2/x.rb", "SHADOWED"] ] with_setup(files) do loader.eager_load_dir("rd2") assert !required?(files[0]) assert !required?(files[1]) end end test "eager loads all files in a subdirectory, ignoring what is above" do files = [ ["x.rb", "IGNORED"], ["m/k/x.rb", "IGNORED"], ["m/n/p.rb", "module M::N::P; end"], ["m/n/a.rb", "M::N::A = 1"], ["m/n/p/q/z.rb", "M::N::P::Q::Z = 1"] ] with_setup(files) do loader.eager_load_dir("m/n") assert !required?(files[0]) assert !required?(files[1]) assert required?(files[2]) assert required?(files[3]) assert required?(files[4]) end end test "eager loads all files, ignoring other directories (different namespace)" do files = [ ["a/x.rb", "A::X = 1"], ["b/y.rb", "B::Y = 1"], ["c/z.rb", "C::Z = 1"] ] with_setup(files) do loader.eager_load_dir("a") assert required?(files[0]) assert !required?(files[1]) assert !required?(files[2]) end end test "eager loads all files, ignoring other directories (same namespace)" do files = [ ["rd1/m/x.rb", "M::X = 1"], ["rd2/m/y.rb", "M::Y = 1"], ] with_setup(files) do loader.eager_load_dir("rd1/m") assert required?(files[0]) assert !required?(files[1]) end end # This is a file system-based interface. test "eager loads collapsed directories, ignoring the rest of the namespace" do files = [["x.rb", "X = 1"], ["collapsed/y.rb", "Y = 1"]] with_setup(files) do loader.eager_load_dir("collapsed") assert !required?(files[0]) assert required?(files[1]) end end test "does not eager load ignored directories (same)" do files = [["ignored/x.rb", "IGNORED"]] with_setup(files) do loader.eager_load_dir("ignored") assert !required?(files[0]) end end test "does not eager load if the argument is an ignored directory (descendant)" do files = [["ignored/m/x.rb", "IGNORED"]] with_setup(files) do loader.eager_load_dir("ignored/m") assert !required?(files[0]) end end test "files under nested root directories are ignored" do files = [ ["x.rb", "X = 1"], ["nested_root/y.rb", "Y = 1"] ] with_setup(files, dirs: %w(. nested_root)) do loader.eager_load_dir(".") assert required?(files[0]) assert !required?(files[1]) end end test "files under a root directory are loaded even if it has an ignored ascendant" do files = [ ["x.rb", "X = 1"], ["ignored/x.rb", "IGNORED"], ["ignored/nested-rd/y.rb", "Y = 1"] ] with_setup(files, dirs: %w(. ignored/nested-rd)) do loader.eager_load_dir("ignored/nested-rd") assert !required?(files[0]) assert !required?(files[1]) assert required?(files[2]) end end test "can be called recursively" do $test_loader = loader files = [ ["a/x.rb", "A::X = 1; $test_loader.eager_load_dir('b')"], ["b/x.rb", "B::X = 1"] ] with_setup(files) do loader.eager_load_dir("a") assert files.all? { |file| required?(file) } end end test "does not prevent reload" do $test_loaded_count = 0 files = [["m/x.rb", "$test_loaded_count += 1; M::X = 1"]] with_setup(files) do loader.eager_load_dir("m") assert_equal 1, $test_loaded_count loader.reload loader.eager_load_dir("m") assert_equal 2, $test_loaded_count end end test "non-Ruby files are just ignored" do files = [ ["x.rb", "X = 1"], ["README.md", ""], ["TODO.txt", ""], [".config", ""], ] with_setup(files) do loader.eager_load_dir(".") assert required?(files[0]) end end test "shortcircuits if eager loaded" do files = [["x.rb", "X = 1"]] with_setup(files) do loader.eager_load # Dirty way to prove we shortcircuit. def loader.actual_eager_load_dir(*) raise end begin loader.eager_load_dir(".") rescue flunk else pass end end end test "raises Zeitwerk::Error if the argument is not a directory" do with_setup do e = assert_raises(Zeitwerk::Error) { loader.eager_load_dir(__FILE__) } assert_equal "#{__FILE__} is not a directory", e.message end end test "raises if the argument is not managed by the loader" do with_setup do e = assert_raises(Zeitwerk::Error) { loader.eager_load_dir(__dir__) } assert_equal "I do not manage #{__dir__}", e.message end end test "raises if called before setup" do assert_raises(Zeitwerk::SetupRequired) do loader.eager_load_dir(__dir__) end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_eager_load_namespace.rb000066400000000000000000000277351442267002100244620ustar00rootroot00000000000000require "test_helper" class TestEagerLoadNamespaceWithObjectRootNamespace < LoaderTest test "eager loads everything" do files = [["x.rb", "X = 1"], ["m/x.rb", "M::X = 1"]] with_setup(files) do loader.eager_load_namespace(Object) assert required?(files) end end test "shortcircuits if eager loaded" do with_setup do loader.eager_load # Dirty way to prove we shortcircuit. def loader.actual_eager_load_dir(*) raise end begin loader.eager_load_namespace(Object) rescue flunk else pass end end end test "does not assume the namespace has a name" do files = [["x.rb", "X = 1"]] with_setup(files) do loader.eager_load_namespace(Module.new) assert !required?(files[0]) end end test "eager loads everything (multiple root directories)" do files = [ ["rd1/x.rb", "X = 1"], ["rd1/m/x.rb", "M::X = 1"], ["rd2/y.rb", "Y = 1"], ["rd2/m/y.rb", "M::Y = 1"] ] with_setup(files) do loader.eager_load_namespace(Object) assert required?(files) end end test "supports collapsed directories" do files = [ ["rd1/collapsed/m/x.rb", "M::X = 1"], ["rd2/y.rb", "Y = 1"], ["rd2/m/y.rb", "M::Y = 1"] ] with_setup(files) do loader.eager_load_namespace(M) assert required?(files[0]) assert !required?(files[1]) assert required?(files[2]) end end test "eager loads everything (nested root directories)" do files = [ ["x.rb", "X = 1"], ["m/x.rb", "M::X = 1"], ["nested/y.rb", "Y = 1"], ["nested/m/y.rb", "M::Y = 1"] ] with_setup(files, dirs: %w(. nested)) do loader.eager_load_namespace(Object) assert required?(files) end end test "eager loads a managed namespace" do files = [["x.rb", "X = 1"], ["m/x.rb", "M::X = 1"]] with_setup(files) do loader.eager_load_namespace(M) assert !required?(files[0]) assert required?(files[1]) end end test "eager loading a non-managed namespace does not raise" do files = [["x.rb", "X = 1"]] with_setup(files) do loader.eager_load_namespace(self.class) assert !required?(files[0]) end end test "does not eager load ignored files" do files = [["x.rb", "X = 1"], ["ignored.rb", "IGNORED"]] with_setup(files) do loader.eager_load_namespace(Object) assert required?(files[0]) assert !required?(files[1]) end end test "does not eager load shadowed files" do files = [["rd1/x.rb", "X = 1"], ["rd2/x.rb", "X = 1"]] with_setup(files) do loader.eager_load_namespace(Object) assert required?(files[0]) assert !required?(files[1]) end end test "skips root directories which are excluded from eager loading (Object)" do files = [["rd1/a.rb", "A = 1"], ["rd2/b.rb", "B = 1"]] with_setup(files) do loader.do_not_eager_load("rd1") loader.eager_load_namespace(Object) assert !required?(files[0]) assert required?(files[1]) end end test "skips directories which are excluded from eager loading (namespace, ancestor)" do files = [["rd1/m/a.rb", "M::A = 1"], ["rd2/m/b.rb", "M::B = 1"]] with_setup(files) do loader.do_not_eager_load("rd1/m") loader.eager_load_namespace(Object) assert !required?(files[0]) assert required?(files[1]) end end test "skips directories which are excluded from eager loading (namespace, self)" do files = [["rd1/m/a.rb", "M::A = 1"], ["rd2/m/b.rb", "M::B = 1"]] with_setup(files) do loader.do_not_eager_load("rd1/m") loader.eager_load_namespace(M) assert !required?(files[0]) assert required?(files[1]) end end test "skips directories which are excluded from eager loading (namespace, descendant)" do files = [["rd1/m/n/a.rb", "M::N::A = 1"], ["rd2/m/n/b.rb", "M::N::B = 1"]] with_setup(files) do loader.do_not_eager_load("rd1/m") loader.eager_load_namespace(M::N) assert !required?(files[0]) assert required?(files[1]) end end test "does not eager load namespaces from other loaders" do files = [["a/m/x.rb", "M::X = 1"], ["b/m/y.rb", "M::Y = 1"]] with_files(files) do loader.push_dir("a") loader.setup new_loader(dirs: "b").eager_load_namespace(M) assert !required?(files[0]) assert required?(files[1]) end end test "raises if the argument is not a class or module object" do with_setup do e = assert_raises(Zeitwerk::Error) do loader.eager_load_namespace(self.class.name) end assert_equal %Q("#{self.class.name}" is not a class or module object), e.message end end test "raises if the argument is not a class or module object, even if eager loaded" do with_setup do loader.eager_load e = assert_raises(Zeitwerk::Error) do loader.eager_load_namespace(self.class.name) end assert_equal %Q("#{self.class.name}" is not a class or module object), e.message end end end class TestEagerLoadNamespaceWithCustomRootNamespace < LoaderTest module CN; end ancestors = [Object, self, CN] ancestors.each do |ancestor| test "eager loads everything #{ancestor}" do files = [["x.rb", "#{CN}::X = 1"], ["m/x.rb", "#{CN}::M::X = 1"]] with_setup(files, namespace: CN) do loader.eager_load_namespace(ancestor) assert required?(files) end end test "eager loads everything (multiple root directories) #{ancestor}" do files = [ ["rd1/x.rb", "#{CN}::X = 1"], ["rd1/m/x.rb", "#{CN}::M::X = 1"], ["rd2/y.rb", "#{CN}::Y = 1"], ["rd2/m/y.rb", "#{CN}::M::Y = 1"] ] with_setup(files, namespace: CN) do loader.eager_load_namespace(ancestor) assert required?(files) end end test "eager loads everything (nested root directories) #{ancestor}" do files = [ ["x.rb", "#{CN}::X = 1"], ["m/x.rb", "#{CN}::M::X = 1"], ["nested/y.rb", "#{CN}::Y = 1"], ["nested/m/y.rb", "#{CN}::M::Y = 1"] ] with_setup(files, dirs: %w(. nested), namespace: CN) do loader.eager_load_namespace(ancestor) assert required?(files) end end test "eager loads everything (nested root directories, different namespaces 1) #{ancestor}" do files = [ ["x.rb", "#{CN}::X = 1"], ["m/x.rb", "#{CN}::M::X = 1"], ["nested/y.rb", "Y = 1"], ["nested/m/y.rb", "M::Y = 1"] ] with_files(files) do loader.push_dir(".", namespace: CN) loader.push_dir("nested") loader.setup loader.eager_load_namespace(ancestor) if ancestor.equal?(Object) assert required?(files) else assert required?(files[0..1]) assert !required?(files[2]) assert !required?(files[3]) end end end test "eager loads everything (nested root directories, different namespaces 2) #{ancestor}" do files = [ ["x.rb", "X = 1"], ["m/x.rb", "M::X = 1"], ["nested/y.rb", "#{CN}::Y = 1"], ["nested/m/y.rb", "#{CN}::M::Y = 1"] ] with_files(files) do loader.push_dir(".") loader.push_dir("nested", namespace: CN) loader.setup loader.eager_load_namespace(ancestor) if ancestor.equal?(Object) assert required?(files) else assert !required?(files[0]) assert !required?(files[1]) assert required?(files[2..3]) end end end end test "does not assume the namespace has a name" do files = [["x.rb", "X = 1"]] with_setup(files, namespace: CN) do loader.eager_load_namespace(Module.new) assert !required?(files[0]) end end test "eager loads a managed namespace" do files = [["x.rb", "#{CN}::X = 1"], ["m/x.rb", "#{CN}::M::X = 1"]] with_setup(files, namespace: CN) do loader.eager_load_namespace(CN::M) assert !required?(files[0]) assert required?(files[1]) end end test "eager loading a non-managed namespace does not raise" do files = [["x.rb", "#{CN}::X = 1"]] with_setup(files, namespace: CN) do loader.eager_load_namespace(String) assert !required?(files[0]) end end test "does not eager load ignored files" do files = [["x.rb", "#{CN}::X = 1"], ["ignored.rb", "IGNORED"]] with_setup(files, namespace: CN) do loader.eager_load_namespace(CN) assert required?(files[0]) assert !required?(files[1]) end end test "does not eager load shadowed files" do files = [["rd1/x.rb", "#{CN}::X = 1"], ["rd2/x.rb", "#{CN}::X = 1"]] with_setup(files, namespace: CN) do loader.eager_load_namespace(CN) assert required?(files[0]) assert !required?(files[1]) end end test "skips root directories which are excluded from eager loading (Object)" do files = [["rd1/a.rb", "#{CN}::A = 1"], ["rd2/b.rb", "#{CN}::B = 1"]] with_setup(files, namespace: CN) do loader.do_not_eager_load("rd1") loader.eager_load_namespace(Object) assert !required?(files[0]) assert required?(files[1]) end end test "skips root directories which are excluded from eager loading (self)" do files = [["rd1/a.rb", "#{CN}::A = 1"], ["rd2/b.rb", "#{CN}::B = 1"]] with_setup(files, namespace: CN) do loader.do_not_eager_load("rd1") loader.eager_load_namespace(CN) assert !required?(files[0]) assert required?(files[1]) end end test "skips directories which are excluded from eager loading (namespace, ancestor)" do files = [["rd1/m/a.rb", "#{CN}::M::A = 1"], ["rd2/m/b.rb", "#{CN}::M::B = 1"]] with_setup(files, namespace: CN) do loader.do_not_eager_load("rd1/m") loader.eager_load_namespace(Object) assert !required?(files[0]) assert required?(files[1]) end end test "skips directories which are excluded from eager loading (namespace, self)" do files = [["rd1/m/a.rb", "#{CN}::M::A = 1"], ["rd2/m/b.rb", "#{CN}::M::B = 1"]] with_setup(files, namespace: CN) do loader.do_not_eager_load("rd1/m") loader.eager_load_namespace(CN::M) assert !required?(files[0]) assert required?(files[1]) end end test "skips directories which are excluded from eager loading (namespace, descendant)" do files = [["rd1/m/n/a.rb", "#{CN}::M::N::A = 1"], ["rd2/m/n/b.rb", "#{CN}::M::N::B = 1"]] with_setup(files, namespace: CN) do loader.do_not_eager_load("rd1/m") loader.eager_load_namespace(CN::M::N) assert !required?(files[0]) assert required?(files[1]) end end test "does not eager load namespaces from other loaders" do files = [["a/m/x.rb", "#{CN}::M::X = 1"], ["b/m/y.rb", "#{CN}::M::Y = 1"]] with_files(files) do loader.push_dir("a", namespace: CN) loader.setup new_loader(dirs: "b", namespace: CN).eager_load_namespace(CN::M) assert !required?(files[0]) assert required?(files[1]) end end test "raises if called before setup" do assert_raises(Zeitwerk::SetupRequired) do loader.eager_load_namespace(self.class) end end test "the class method broadcasts the call to all registered loaders" do files = [ ["a/m/x.rb", "M::X = 1"], ["a/x.rb", "X = 1"], ["b/m/y.rb", "M::Y = 1"], ["b/y.rb", "Y = 1"], ["c/z.rb", "Z = 1"], ] with_files(files) do new_loader(dirs: "a") new_loader(dirs: "b") new_loader(dirs: "c") Zeitwerk::Loader.eager_load_namespace(M) assert required?(files[0]) assert !required?(files[1]) assert required?(files[2]) assert !required?(files[3]) assert !required?(files[4]) end end test "the class method skips loaders that are not ready" do files = [["m/x.rb", "M::X = 1"]] with_setup(files) do new_loader(setup: false) # should be skipped Zeitwerk::Loader.eager_load_namespace(M) assert required?(files) end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_exceptions.rb000066400000000000000000000053421442267002100225330ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestExceptions < LoaderTest # We cannot test error.message only because after # # https://github.com/ruby/ruby/commit/e94604966572bb43fc887856d54aa54b8e9f7719 # # error.message includes the line of code that raised. def assert_error_message(message, error) assert_equal message, error.message.lines.first.chomp end test "raises NameError if the expected constant is not defined" do on_teardown { remove_const :TyPo } files = [["typo.rb", "TyPo = 1"]] with_setup(files) do typo_rb = File.expand_path("typo.rb") error = assert_raises(Zeitwerk::NameError) { Typo } assert_error_message "expected file #{typo_rb} to define constant Typo, but didn't", error assert_equal :Typo, error.name end end test "eager loading raises NameError if files do not define the expected constants" do files = [["x.rb", ""]] with_setup(files) do x_rb = File.expand_path("x.rb") error = assert_raises(Zeitwerk::NameError) { loader.eager_load } assert_error_message "expected file #{x_rb} to define constant X, but didn't", error 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.expand_path("cli/x.rb") error = assert_raises(Zeitwerk::NameError) { loader.eager_load } assert_error_message "expected file #{cli_x_rb} to define constant Cli::X, but didn't", error 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.6.8/test/lib/zeitwerk/test_explicit_namespace.rb000066400000000000000000000154411442267002100242100ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestExplicitNamespace < LoaderTest module Namespace; end test "explicit namespaces are loaded correctly (directory first, Object)" do files = [ ["hotel.rb", "class Hotel; X = 1; end"], ["hotel/pricing.rb", "class Hotel::Pricing; end"] ] with_setup(files) do assert_kind_of Class, Hotel assert Hotel::X assert Hotel::Pricing end end test "explicit namespaces are loaded correctly (directory first, Namespace)" do files = [ ["hotel.rb", "class #{Namespace}::Hotel; X = 1; end"], ["hotel/pricing.rb", "class #{Namespace}::Hotel::Pricing; end"] ] with_setup(files, namespace: Namespace) do assert_kind_of Class, Namespace::Hotel assert Namespace::Hotel::X assert Namespace::Hotel::Pricing end end test "explicit namespaces are loaded correctly (file first, Object)" do files = [ ["rd1/hotel.rb", "class Hotel; X = 1; end"], ["rd2/hotel/pricing.rb", "class Hotel::Pricing; end"] ] with_setup(files) do assert_kind_of Class, Hotel assert Hotel::X assert Hotel::Pricing end end test "explicit namespaces are loaded correctly (file first, Namespace)" do files = [ ["rd1/hotel.rb", "class #{Namespace}::Hotel; X = 1; end"], ["rd2/hotel/pricing.rb", "class #{Namespace}::Hotel::Pricing; end"] ] with_setup(files, namespace: Namespace) 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 = [ ["hotel.rb", <<~RUBY], class Hotel def self.name "X" end end RUBY ["hotel/pricing.rb", "class Hotel::Pricing; end"] ] with_setup(files) 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 test "namespace promotion updates the registry" do # We use two root directories to make sure the loader visits the implicit # rd1/m first, and the explicit rd2/m.rb after it. files = [ ["rd1/m/x.rb", "M::X = true"], ["rd2/m.rb", "module M; end"] ] with_setup(files) do assert_nil Zeitwerk::Registry.loader_for(File.expand_path("rd1/m")) assert_same loader, Zeitwerk::Registry.loader_for(File.expand_path("rd2/m.rb")) 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.send(: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, one directory" 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 "autovivification does not enable the tracer, two directories" do files = [ ["rd1/foo/bar.rb", "module Foo::Bar; end"], ["rd2/foo/baz.rb", "module Foo::Baz; 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 test "non-hashable explicit namespaces are supported" do files = [ ["m.rb", <<~EOS], module M # This method is overridden with a different arity. Therefore, M is # not hashable. See https://github.com/fxn/zeitwerk/issues/188. def self.hash(_) end end EOS ["m/x.rb", "M::X = true"] ] with_setup(files) do assert M::X end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_for_gem.rb000066400000000000000000000147611442267002100217750ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestForGem < LoaderTest MY_GEM = ["lib/my_gem.rb", <<~EOS] $for_gem_test_loader = Zeitwerk::Loader.for_gem $for_gem_test_loader.enable_reloading $for_gem_test_loader.setup module MyGem end EOS def with_my_gem(files = [MY_GEM], rq = true) with_files(files) do with_load_path("lib") do if rq assert require("my_gem") assert MyGem end yield end end end test "sets things correctly" do files = [ MY_GEM, ["lib/my_gem/foo.rb", "class MyGem::Foo; end"], ["lib/my_gem/foo/bar.rb", "MyGem::Foo::Bar = true"] ] with_my_gem(files) 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 test "is idempotent" do $for_gem_test_zs = [] files = [ ["lib/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 module MyGem end EOS ] with_my_gem(files) do $for_gem_test_zs.first.unload assert !Object.const_defined?(:MyGem) $for_gem_test_zs.first.setup assert MyGem assert_equal 2, $for_gem_test_zs.size assert_same $for_gem_test_zs.first, $for_gem_test_zs.last end end test "configures the gem inflector by default" do with_my_gem do assert_instance_of Zeitwerk::GemInflector, $for_gem_test_loader.inflector end end test "configures the basename of the root file as loader tag" do with_my_gem do assert_equal "my_gem", $for_gem_test_loader.tag end end test "does not warn if lib only has expected files" do with_my_gem([MY_GEM], false) do assert_silent do assert require("my_gem") end end end test "does not warn if lib only has extra, non-hidden, non-Ruby files" do files = [MY_GEM, ["lib/i18n.yml", ""], ["lib/.vscode", ""]] with_my_gem(files, false) do assert_silent do assert require("my_gem") end end end test "warns if the lib has an extra Ruby file" do files = [MY_GEM, ["lib/foo.rb", ""]] with_my_gem(files, false) do _out, err = capture_io do assert require("my_gem") end assert_includes err, "Zeitwerk defines the constant Foo after the file" assert_includes err, File.expand_path("lib/foo.rb") assert_includes err, "Zeitwerk::Loader.for_gem(warn_on_extra_files: false)" end end test "does not warn if lib has an extra Ruby file, but it is ignored" do files = [["lib/my_gem.rb", <<~EOS], ["lib/foo.rb", ""]] loader = Zeitwerk::Loader.for_gem loader.ignore("\#{__dir__}/foo.rb") loader.enable_reloading loader.setup module MyGem end EOS with_my_gem(files, false) do _out, err = capture_io do assert require("my_gem") end assert_empty err end end test "does not warn if lib has an extra Ruby file, but warnings are disabled" do files = [["lib/my_gem.rb", <<~EOS], ["lib/foo.rb", ""]] loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false) loader.enable_reloading loader.setup module MyGem end EOS with_my_gem(files, false) do _out, err = capture_io do assert require("my_gem") end assert_empty err end end test "warns if lib has an extra directory" do files = [MY_GEM, ["lib/foo/bar.rb", "Foo::Bar = true"]] with_my_gem(files, false) do _out, err = capture_io do assert require("my_gem") end assert_includes err, "Zeitwerk defines the constant Foo after the directory" assert_includes err, File.expand_path("lib/foo") assert_includes err, "Zeitwerk::Loader.for_gem(warn_on_extra_files: false)" end end test "does not warn if lib has an extra directory, but it is ignored" do files = [["lib/my_gem.rb", <<~EOS], ["lib/foo/bar.rb", "Foo::Bar = true"]] loader = Zeitwerk::Loader.for_gem loader.ignore("\#{__dir__}/foo") loader.enable_reloading loader.setup module MyGem end EOS with_my_gem(files, false) do _out, err = capture_io do assert require("my_gem") end assert_empty err end end test "does not warn if lib has an extra directory, but it has no Ruby files" do files = [["lib/my_gem.rb", <<~EOS], ["lib/tasks/newsletter.rake", ""]] loader = Zeitwerk::Loader.for_gem loader.enable_reloading loader.setup module MyGem end EOS with_my_gem(files, false) do _out, err = capture_io do assert require("my_gem") end assert_empty err end end test "does not warn if lib has an extra directory, but warnings are disabled" do files = [["lib/my_gem.rb", <<~EOS], ["lib/foo/bar.rb", "Foo::Bar = true"]] loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false) loader.enable_reloading loader.setup module MyGem end EOS with_my_gem(files, false) do _out, err = capture_io do assert require("my_gem") end assert_empty err end end test "warnings do not assume the namespace directory is the tag" do files = [["lib/my_gem.rb", <<~EOS], ["lib/foo/bar.rb", "Foo::Bar = true"]] loader = Zeitwerk::Loader.for_gem loader.tag = "foo" loader.enable_reloading loader.setup module MyGem end EOS with_my_gem(files, false) do _out, err = capture_io do assert require("my_gem") end assert_includes err, "Zeitwerk defines the constant Foo after the directory" assert_includes err, File.expand_path("lib/foo") assert_includes err, "Zeitwerk::Loader.for_gem(warn_on_extra_files: false)" end end test "warnings use the gem inflector" do files = [["lib/my_gem.rb", <<~EOS], ["lib/foo/bar.rb", "Foo::Bar = true"]] loader = Zeitwerk::Loader.for_gem loader.inflector.inflect("foo" => "BAR") loader.enable_reloading loader.setup module MyGem end EOS with_my_gem(files, false) do _out, err = capture_io do assert require("my_gem") end assert_includes err, "Zeitwerk defines the constant BAR after the directory" assert_includes err, File.expand_path("lib/foo") assert_includes err, "Zeitwerk::Loader.for_gem(warn_on_extra_files: false)" end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_for_gem_extension.rb000066400000000000000000000065411442267002100240660ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestForGemExtension < LoaderTest ROOT_DIR = "lib/test_for_gem_extension" MY_GEM_EXTENSION = ["#{ROOT_DIR}/my_gem_extension.rb", <<~EOS] $for_gem_extension_test_loader = Zeitwerk::Loader.for_gem_extension(#{self}) $for_gem_extension_test_loader.enable_reloading $for_gem_extension_test_loader.setup module #{self}::MyGemExtension end EOS def with_my_gem_extension(files = [MY_GEM_EXTENSION], rq = true) with_files(files) do with_load_path("lib") do if rq assert require("test_for_gem_extension/my_gem_extension") assert self.class::MyGemExtension end yield end end end test "sets things correctly" do files = [ MY_GEM_EXTENSION, ["#{ROOT_DIR}/my_gem_extension/foo.rb", "class #{self.class}::MyGemExtension::Foo; end"], ["#{ROOT_DIR}/my_gem_extension/foo/bar.rb", "#{self.class}::MyGemExtension::Foo::Bar = true"] ] with_my_gem_extension(files) do assert self.class::MyGemExtension::Foo::Bar $for_gem_extension_test_loader.unload assert !self.class.const_defined?(:MyGemExtension) $for_gem_extension_test_loader.setup assert self.class::MyGemExtension::Foo::Bar end end test "is idempotent" do $for_gem_extension_zs = [] files = [ ["#{ROOT_DIR}/my_gem_extension.rb", <<-EOS], $for_gem_extension_zs << Zeitwerk::Loader.for_gem_extension(#{self.class}) $for_gem_extension_zs.last.enable_reloading $for_gem_extension_zs.last.setup module #{self.class}::MyGemExtension end EOS ] with_my_gem_extension(files) do $for_gem_extension_zs.first.unload assert !self.class.const_defined?(:MyGemExtension) $for_gem_extension_zs.first.setup assert self.class::MyGemExtension assert_equal 2, $for_gem_extension_zs.size assert_same $for_gem_extension_zs.first, $for_gem_extension_zs.last end end test "configures the gem inflector by default" do files = [MY_GEM_EXTENSION, ["#{ROOT_DIR}/my_gem_extension/version.rb", "#{self.class}::MyGemExtension::VERSION = '1.0'"]] with_my_gem_extension(files) do assert_instance_of Zeitwerk::GemInflector, $for_gem_extension_test_loader.inflector assert_equal "1.0", self.class::MyGemExtension::VERSION end end test "configures the namespace plus basename of the root file as loader tag" do with_my_gem_extension do assert_equal "#{self.class}-my_gem_extension", $for_gem_extension_test_loader.tag end end test "works too if going through a hyphenated entry point (require)" do files = [ ["lib/my-gem-extension.rb", "require 'test_for_gem_extension/my_gem_extension'"], MY_GEM_EXTENSION, ] with_my_gem_extension(files, false) do assert require("my-gem-extension") assert self.class::MyGemExtension end end test "raises if the namespace is not a class or module object" do e = assert_raises(Zeitwerk::Error) { Zeitwerk::Loader.for_gem_extension(:foo) } assert_equal ":foo is not a class or module object, should be", e.message end test "raises if the namespace is anonymous" do e = assert_raises(Zeitwerk::Error) { Zeitwerk::Loader.for_gem_extension(Module.new) } assert_equal "extending anonymous namespaces is unsupported", e.message end end zeitwerk-2.6.8/test/lib/zeitwerk/test_ignore.rb000066400000000000000000000111721442267002100216330ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" require "set" class TestIgnore < LoaderTest def this_dir @this_dir ||= __dir__ end def this_file @this_file ||= File.expand_path(__FILE__, this_dir) end def ascendant @dir_up ||= File.expand_path("#{this_dir}/../..") end test "ignored root directories are ignored" do files = [["x.rb", "X = true"]] with_files(files) do loader.push_dir(".") loader.ignore(".") loader.setup assert !Object.autoload?(:X) assert_raises(NameError) { ::X } end end test "ignored root directories are ignored, but nested ones are not" do files = [["x.rb", "X = true"], ["nested/y.rb", "Y = true"]] with_files(files) do loader.push_dir(".") loader.push_dir("nested") loader.ignore(".") loader.setup assert !Object.autoload?(:X) assert_raises(NameError) { ::X } assert Y 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 Object.autoload?(:X) assert !Object.autoload?(:Y) 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 Object.autoload?(:X) assert !Object.autoload?(:M) 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.send(: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.send(: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 test "returns true if a directory is ignored as is" do loader.ignore(this_dir) assert loader.__ignores?(this_dir) end test "returns true if a file is ignored as is" do loader.ignore(this_file) assert loader.__ignores?(this_file) end test "returns true for a descendant of an ignored directory" do loader.ignore(ascendant) assert loader.__ignores?(this_dir) end test "returns true for a file in a descendant of an ignored directory" do loader.ignore(ascendant) assert loader.__ignores?(this_file) end test "returns false for the directory of an ignored file" do loader.ignore(this_file) assert !loader.__ignores?(this_dir) end test "returns false for an ascendant directory of an ignored directory" do loader.ignore(this_dir) assert !loader.__ignores?(ascendant) end test "returns false if nothing is ignored" do assert !loader.__ignores?(this_dir) assert !loader.__ignores?(this_file) end end zeitwerk-2.6.8/test/lib/zeitwerk/test_load_file.rb000066400000000000000000000067121442267002100222720ustar00rootroot00000000000000require "pathname" require "test_helper" class TestLoadFile < LoaderTest test "loads a top-level file" do files = [["x.rb", "X = 1"]] with_setup(files) do loader.load_file("x.rb") assert required?(files[0]) end end test "loads a top-level file (Pathname)" do files = [["x.rb", "X = 1"]] with_setup(files) do loader.load_file(Pathname.new("x.rb")) assert required?(files[0]) end end test "loads a top-level file (custom root namespace)" do files = [["x.rb", "#{self.class}::X = 1"]] with_setup(files, namespace: self.class) do loader.load_file("x.rb") assert required?(files[0]) end end test "loads a namespaced file" do files = [["m/x.rb", "M::X = 1"]] with_setup(files) do loader.load_file("m/x.rb") assert required?(files[0]) end end test "loads a namespaced file (custom root namespace)" do files = [["m/x.rb", "#{self.class}::M::X = 1"]] with_setup(files, namespace: self.class) do loader.load_file("m/x.rb") assert required?(files[0]) end end test "supports collapsed directories" do files = [["m/collapsed/x.rb", "M::X = 1"]] with_setup(files) do loader.load_file("m/collapsed/x.rb") assert required?(files[0]) end end end class TestLoadFileErrors < LoaderTest test "raises if the argument does not exist" do with_setup do e = assert_raises { loader.load_file("foo.rb") } assert_equal "#{File.expand_path('foo.rb')} does not exist", e.message end end test "raises if the argument is a directory" do with_setup([["m/x.rb", "M::X = 1"]]) do e = assert_raises { loader.load_file("m") } assert_equal "#{File.expand_path('m')} is not a Ruby file", e.message end end test "raises if the argument is a file, but does not have .rb extension" do with_setup([["README.md", ""]]) do e = assert_raises { loader.load_file("README.md") } assert_equal "#{File.expand_path('README.md')} is not a Ruby file", e.message end end test "raises if the argument is ignored" do with_setup([["ignored.rb", "IGNORED"]]) do e = assert_raises { loader.load_file("ignored.rb") } assert_equal "#{File.expand_path('ignored.rb')} is ignored", e.message end end test "raises if the argument is a descendant of an ignored directory" do with_setup([["ignored/n/x.rb", "IGNORED"]]) do e = assert_raises { loader.load_file("ignored/n/x.rb") } assert_equal "#{File.expand_path('ignored/n/x.rb')} is ignored", e.message end end test "raises if the argument lives in an ignored root directory" do files = [["ignored/n/x.rb", "IGNORED"]] with_setup(files, dirs: %w(ignored)) do e = assert_raises { loader.load_file("ignored/n/x.rb") } assert_equal "#{File.expand_path('ignored/n/x.rb')} is ignored", e.message end end test "raises if the file exists, but it is not managed by this loader" do files = [["rd1/x.rb", "X = 1"], ["external/x.rb", ""]] with_setup(files, dirs: %w(rd1)) do e = assert_raises { loader.load_file("external/x.rb") } assert_equal "I do not manage #{File.expand_path('external/x.rb')}", e.message end end test "raises if the file is shadowed" do files = [["rd1/x.rb", "X = 1"], ["rd2/x.rb", "SHADOWED"]] with_setup(files) do e = assert_raises { loader.load_file("rd2/x.rb") } assert_equal "#{File.expand_path('rd2/x.rb')} is shadowed", e.message end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_logging.rb000066400000000000000000000125671442267002100220070ustar00rootroot00000000000000# frozen_string_literal: true require "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.expand_path("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.expand_path("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.expand_path("admin")}/) do loader.push_dir(".") loader.setup assert Admin end end end end test "logs implicit to explicit promotions" do # We use two root directories to make sure the loader visits the implicit # a/m first, and the explicit b/m.rb after it. files = [ ["a/m/x.rb", "M::X = true"], ["b/m.rb", "module M; end"] ] with_files(files) do loader.push_dir("a") loader.push_dir("b") assert_logged(/earlier autoload for M discarded, it is actually an explicit namespace defined in #{File.expand_path("b/m.rb")}/) do loader.setup 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.expand_path("x.rb")}") do loader.push_dir(".") loader.setup end end end test "logs failed autoloads, provided the require call succeeded" do files = [["x.rb", ""]] with_files(files) do assert_logged(/expected file #{File.expand_path("x.rb")} to define constant X, but didn't/) do loader.push_dir(".") loader.setup assert_raises(Zeitwerk::NameError) { X } 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.expand_path("admin")}") do loader.push_dir(".") 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 "logs when eager loading starts" do with_setup do assert_logged(/eager load start/) do loader.eager_load end end end test "logs when eager loading ends" do with_setup do assert_logged(/eager load end/) do loader.eager_load 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.6.8/test/lib/zeitwerk/test_marshal.rb000066400000000000000000000023231442267002100217750ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestMarshal < LoaderTest test "Marshal.load autoloads a top-level class" do files = [["c.rb", "class C; end"]] with_setup(files) do str = Marshal.dump(C.new) loader.reload assert_instance_of C, Marshal.load(str) end end test "Marshal.load autoloads a namespaced class (implicit)" do files = [["m/n/c.rb", "class M::N::C; end"]] with_setup(files) do str = Marshal.dump(M::N::C.new) loader.reload assert_instance_of M::N::C, Marshal.load(str) end end test "Marshal.load autoloads a namespaced class (explicit)" do files = [ ["m.rb", "module M; end"], ["m/n/c.rb", "class M::N::C; end"] ] with_setup(files) do str = Marshal.dump(M::N::C.new) loader.reload assert_instance_of M::N::C, Marshal.load(str) end end test "Marshal.load autoloads several classes" do files = [ ["c.rb", "class C; end"], ["d.rb", "class D; end"] ] with_setup(files) do str = Marshal.dump([C.new, D.new]) loader.reload loaded = Marshal.load(str) assert_instance_of C, loaded[0] assert_instance_of D, loaded[1] end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_multiple_loaders.rb000066400000000000000000000022061442267002100237120ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestMultipleLoaders < 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.6.8/test/lib/zeitwerk/test_nested_root_directories.rb000066400000000000000000000022141442267002100252660ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestNestedRootDirectories < LoaderTest test "nested root directories do not autovivify modules" do files = [["concerns/pricing.rb", "module Pricing; end"]] with_setup(files, dirs: %w(. concerns)) do assert_raises(NameError) { Concerns } end end test "nested root directories are ignored even if there is a matching file" do files = [ ["hotel.rb", "class Hotel; include GeoLoc; end"], ["concerns/geo_loc.rb", "module GeoLoc; end"], ["concerns.rb", "module Concerns; end"] ] with_setup(files, dirs: %w(. 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: %w(. concerns)) do loader.eager_load assert $airplane_eager_loaded assert $locatable_eager_loaded end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_on_load.rb000066400000000000000000000114661442267002100217710ustar00rootroot00000000000000# 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 gets the expected arguments passed" do with_setup([["x.rb", "X = 1"]]) do args = []; loader.on_load("X") { |*a| args = a } assert X assert_equal 1, args[0] assert_abspath "x.rb", args[1] end end test "on_load for :ANY is called for files with the expected arguments" do with_setup([["x.rb", "X = 1"]]) do args = []; loader.on_load { |*a| args = a } assert X assert_equal "X", args[0] assert_equal 1, args[1] assert_abspath "x.rb", args[2] end end test "on_load for :ANY is called for autovivified modules with the expected arguments" do with_setup([["x/a.rb", "X::A = 1"]]) do args = []; loader.on_load { |*a| args = a } assert X assert_equal "X", args[0] assert_equal X, args[1] assert_abspath "x", args[2] end end test "on_load for :ANY is called for namespaced constants with the expected arguments" do with_setup([["x/a.rb", "X::A = 1"]]) do args = []; loader.on_load { |*a| args = a } assert X::A assert_equal "X::A", args[0] assert_equal X::A, args[1] assert_abspath "x/a.rb", args[2] end end test "multiple on_load for :ANY are called in order" do with_setup([["x.rb", "X = 1"]]) do x = [] loader.on_load { x << 1 } loader.on_load { x << 2 } assert X assert_equal [1, 2], x end end test "if there are specific and :ANY on_loads, the specific one runs first" do with_setup([["x.rb", "X = 1"]]) do x = [] loader.on_load { x << 2 } loader.on_load("X") { x << 1 } assert X 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 "on_load for namespaces gets called with child constants available (implicit)" do with_setup([["x/a.rb", "X::A = 1"]]) do ok = false loader.on_load("X") { ok = X.const_defined?(:A) } assert X assert ok end end test "on_load for namespaces gets called with child constants available (explicit)" do with_setup([["x.rb", "module X; end"], ["x/a.rb", "X::A = 1"]]) do ok = false loader.on_load("X") { ok = X.const_defined?(:A) } assert X assert ok end end test "on_load :ANY for namespaces gets called with child constants available (implicit)" do with_setup([["x/a.rb", "X::A = 1"]]) do ok = false loader.on_load { |cpath| ok = X.const_defined?(:A) if cpath == "X" } assert X assert ok end end test "on_load :ANY for namespaces gets called with child constants available (explicit)" do with_setup([["x.rb", "module X; end"], ["x/a.rb", "X::A = 1"]]) do ok = false loader.on_load { |cpath| ok = X.const_defined?(:A) if cpath == "X" } assert X assert ok 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.send(:on_load_callbacks).empty? assert A assert_equal 1, x assert loader.send(:on_load_callbacks).empty? end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_on_setup.rb000066400000000000000000000016601442267002100222050ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestOnSetup < LoaderTest test "on_setup callbacks are fired on setup, in order" do x = [] loader.on_setup { x << 0 } loader.on_setup { x << 1 } loader.setup assert_equal [0, 1], x end test "on_setup callbacks are fired if setup was already done" do loader.setup x = [] loader.on_setup { x << 0 } loader.on_setup { x << 1 } assert_equal [0, 1], x end test "on_setup callbacks are fired again on reload" do loader.enable_reloading x = [] loader.on_setup { x << 0 } loader.on_setup { x << 1 } loader.setup assert_equal [0, 1], x loader.reload assert_equal [0, 1, 0, 1], x end test "on_setup is able to autoload" do files = [["x.rb", "X = true"]] with_files(files) do loader.push_dir(".") loader.on_setup do assert X end loader.setup end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_on_unload.rb000066400000000000000000000111541442267002100223260ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestOnUnload < LoaderTest test "on_unload checks its argument type" do assert_raises(TypeError, "on_unload only accepts strings") do loader.on_unload(:X) {} end assert_raises(TypeError, "on_unload only accepts strings") do loader.on_unload(Object) {} end end test "multiple on_unload on cpaths are called in order of definition" do with_setup([["x.rb", "X = 1"]]) do x = [] loader.on_unload("X") { x << 1 } loader.on_unload("X") { x << 2 } assert X loader.reload assert_equal [1, 2], x end end test "on_unload blocks for cpaths get the expected arguments passed" do with_setup([["x.rb", "X = 1"]]) do args = []; loader.on_unload("X") { |*a| args = a } assert X loader.reload assert_equal 1, args[0] assert_abspath "x.rb", args[1] end end test "on_unload for cpaths is called before the constant is removed" do with_setup([["x.rb", "X = 1"]]) do defined_X = false loader.on_unload("X") { defined_X = Object.const_defined?(:X) } assert X loader.reload assert defined_X end end test "on_unload on cpaths is not called for other constants" do files = [ ["x.rb", "X = 1"], ["y.rb", "Y = 1"] ] with_setup(files) do on_unload_for_Y = false loader.on_unload("Y") { on_unload_for_Y = true } assert X loader.reload assert !on_unload_for_Y end end test "on_unload on cpaths is resilient to manually removed constants" do with_setup([["x.rb", "X = 1"]]) do on_unload_for_X = false loader.on_unload("X") { on_unload_for_X = true } assert X remove_const :X loader.reload assert !on_unload_for_X end end test "on_unload on cpaths is resilient to failed autoloads" do on_teardown { remove_const :Y } with_setup([["x.rb", "Y = 1"]]) do on_unload_for_X = false loader.on_unload("X") { on_unload_for_X = true } assert_raises(Zeitwerk::NameError) { X } loader.reload assert !on_unload_for_X end end test "on_unload on cpaths does not trigger a failed autoload twice" do on_teardown { remove_const :Y } $failed_autoloads = 0 with_setup([["x.rb", "$failed_autoloads += 1; Y = 1"]]) do loader.on_unload("X") {} assert_raises(Zeitwerk::NameError) { X } loader.reload assert_equal 1, $failed_autoloads end end test "on_unload for :ANY is called with the expected arguments" do with_setup([["x.rb", "X = 1"]]) do args = []; loader.on_unload { |*a| args << a } assert X loader.reload assert_equal 1, args.length assert_equal "X", args[0][0] assert_equal 1, args[0][1] assert_abspath "x.rb", args[0][2] end end test "on_unload for :ANY is called before the constant is removed" do with_setup([["x.rb", "X = 1"]]) do defined_X = false loader.on_unload { defined_X = Object.const_defined?(:X) } assert X loader.reload assert defined_X end end test "multiple on_unload for :ANY are called in order of definition" do with_setup([["x.rb", "X = 1"]]) do x = [] loader.on_unload { x << 1 } loader.on_unload { x << 2 } assert X loader.reload assert_equal [1, 2], x end end test "if there are specific and :ANY on_unloads, the specific one runs first" do with_setup([["x.rb", "X = 1"]]) do x = [] loader.on_unload { x << 2 } loader.on_unload("X") { x << 1 } assert X loader.reload assert_equal [1, 2], x end end test "on_unload for :ANY is is resilient to manually removed constants" do with_setup([["x.rb", "X = 1"]]) do on_unload_for_X = false loader.on_unload { on_unload_for_X = true } assert X remove_const :X loader.reload assert !on_unload_for_X end end test "on_unload for :ANY is is resilient to failed autoloads" do on_teardown { remove_const :Y } with_setup([["x.rb", "Y = 1"]]) do on_unload_for_X = false loader.on_unload { on_unload_for_X = true } assert_raises(Zeitwerk::NameError) { X } loader.reload assert !on_unload_for_X end end test "on_unload on :ANY does not trigger a failed autoload twice" do on_teardown { remove_const :Y } $failed_autoloads = 0 with_setup([["x.rb", "$failed_autoloads += 1; Y = 1"]]) do loader.on_unload {} assert_raises(Zeitwerk::NameError) { X } loader.reload assert_equal 1, $failed_autoloads end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_private_constants.rb000066400000000000000000000023621442267002100241170ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestPrivateConstants < LoaderTest test "lookup rules for relative private constants work as expected" do files = [["m/x.rb", <<~EOS1], ["c.rb", <<~EOS2]] module M X = :X private_constant :X end EOS1 class C include M def self.x X end end EOS2 with_setup(files) do assert_equal :X, C.x end end test "lookup rules for qualified private constants work as expected" do files = [["m/x.rb", <<~RUBY]] module M X = :X private_constant :X end RUBY with_setup(files) do assert_raises(NameError) { M::X } assert_equal :X, M.module_eval("X") end end test "reloading works for private constants" do $test_reload_private_constants = 0 files = [["m/x.rb", <<~EOS1], ["c.rb", <<~EOS2]] module M X = $test_reload_private_constants private_constant :X end EOS1 class C include M def self.x X end end EOS2 with_setup(files) do assert_equal 0, C.x loader.reload $test_reload_private_constants = 1 assert_equal 1, C.x end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_push_dir.rb000066400000000000000000000061431442267002100221670ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" require "pathname" class TesPushDir < LoaderTest module Namespace; end def check_dirs roots = loader.__roots non_ignored_roots = roots.reject { |dir, _| loader.send(:ignored_path?, dir) } dirs = loader.dirs assert_equal non_ignored_roots.keys, dirs assert dirs.frozen? dirs = loader.dirs(namespaces: true) assert_equal non_ignored_roots, dirs assert dirs.frozen? assert !dirs.equal?(roots) dirs = loader.dirs(ignored: true) assert_equal roots.keys, dirs assert dirs.frozen? dirs = loader.dirs(namespaces: true, ignored: true) assert_equal roots, dirs assert dirs.frozen? assert !dirs.equal?(roots) end test "accepts dirs as strings and associates them to the Object namespace" do loader.push_dir(".") check_dirs end test "accepts dirs as pathnames and associates them to the Object namespace" do loader.push_dir(Pathname.new(".")) check_dirs end test "there can be several root directories" do files = [["rd1/x.rb", "X = 1"], ["rd2/y.rb", "Y = 1"], ["rd3/z.rb", "Z = 1"]] with_setup(files) do check_dirs end end test "there can be several root directories, some of them may be ignored" do files = [["rd1/x.rb", "X = 1"], ["rd2/y.rb", "Y = 1"], ["rd3/z.rb", "Z = 1"]] with_files(files) do loader.push_dir("rd1") loader.push_dir("rd2") loader.push_dir("rd3") loader.ignore("rd2") check_dirs end end test "accepts dirs as strings and associates them to the given namespace" do loader.push_dir(".", namespace: Namespace) check_dirs end test "accepts dirs as pathnames and associates them to the given namespace" do loader.push_dir(Pathname.new("."), namespace: Namespace) check_dirs end test "there can be several root directories, with custom namespaces" do files = [["rd1/x.rb", "X = 1"], ["rd2/y.rb", "Y = 1"], ["rd3/z.rb", "Z = 1"]] with_setup(files, namespace: Namespace) do check_dirs end end test "there can be several root directories, with custom namespaces, some of them may be ignored" do files = [["rd1/x.rb", "X = 1"], ["rd2/y.rb", "Y = 1"], ["rd3/z.rb", "Z = 1"]] with_files(files) do loader.push_dir("rd1", namespace: Namespace) loader.push_dir("rd2", namespace: Namespace) loader.push_dir("rd3", namespace: Namespace) loader.ignore("rd2") check_dirs end 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 test "raises if the namespace is anonymous" do e = assert_raises(Zeitwerk::Error) { loader.push_dir(".", namespace: Module.new) } assert_equal "root namespaces cannot be anonymous", e.message end end zeitwerk-2.6.8/test/lib/zeitwerk/test_reloading.rb000066400000000000000000000145331442267002100223200ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" require "fileutils" class TestReloading < LoaderTest module Namespace; end def silence_exceptions_in_threads original_report_on_exception = Thread.report_on_exception Thread.report_on_exception = false yield ensure Thread.report_on_exception = original_report_on_exception 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 = [["rd1/x.rb", "X = 1"], ["rd2/y.rb", "Y = 1"]] with_setup(files) do assert X assert Y FileUtils.rm_rf("rd2") 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 test "reload recovers from name errors (w/o on_unload callbacks)" do on_teardown { remove_const :Y } files = [["x.rb", "Y = :typo"]] with_setup(files) do assert_raises(Zeitwerk::NameError) { X } assert !Object.constants.include?(:X) assert !Object.const_defined?(:X, false) assert !Object.autoload?(:X) loader.reload File.write("x.rb", "X = true") assert Object.constants.include?(:X) assert Object.const_defined?(:X, false) assert Object.autoload?(:X) assert X end end test "reload recovers from name errors (w/ on_unload callbacks)" do on_teardown { remove_const :Y } files = [["x.rb", "Y = :typo"]] with_setup(files) do loader.on_unload {} assert_raises(Zeitwerk::NameError) { X } assert !Object.constants.include?(:X) assert !Object.const_defined?(:X, false) assert !Object.autoload?(:X) loader.reload File.write("x.rb", "X = true") assert Object.constants.include?(:X) assert Object.const_defined?(:X, false) assert Object.autoload?(:X) assert X end end test "raises if called before setup" do assert_raises(Zeitwerk::SetupRequired) do loader.reload end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_require_interaction.rb000066400000000000000000000127561442267002100244340ustar00rootroot00000000000000# frozen_string_literal: true require "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 = [ ["rd1/admin/user.rb", "class Admin::User; end"], ["rd2/admin/users_controller.rb", "class Admin::UsersController; end"] ] with_setup(files, load_path: %w(rd1 rd2)) 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 end zeitwerk-2.6.8/test/lib/zeitwerk/test_ruby_compatibility.rb000066400000000000000000000262211442267002100242630ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" require "pathname" class TestRubyCompatibility < LoaderTest # We decorate Kernel#require in lib/zeitwerk/kernel.rb be able to trigger # callbacks, autovivify implicit namespaces, keep track of what has been # autoloaded, and more. 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 # Once a managed file is autoloaded, Zeitwerk verifies the expected constant # has been defined and raises Zeitwerk::NameError if not. This happens within # the context of the require call and is correct because an autoload does not # define the constant by itself, it has to be a side-effect. test "within a file triggered by an autoload, the constant being autoloaded is not defined" do files = [["x.rb", "$const_defined_for_X = Object.const_defined?(:X); X = 1"]] with_setup(files) do $const_defined_for_X = Object.const_defined?(:X) assert $const_defined_for_X assert X assert !$const_defined_for_X end end # Zeitwerk sets autoloads using absolute paths and string concatenation with # the root directories. These paths could contain symlinks, but we can still # identify managed files in our decorated Kernel#require because Ruby stores # the paths as they are in $LOADED_FEATURES with no symlink resolution. test "absolute paths passed to require end up in $LOADED_FEATURES as is" do on_teardown { $LOADED_FEATURES.pop } files = [["real/real_x.rb", ""]] with_files(files) do FileUtils.ln_s("real", "sym") FileUtils.ln_s(File.expand_path("real/real_x.rb"), "sym/sym_x.rb") sym_x = File.expand_path("sym/sym_x.rb") assert require(sym_x) assert $LOADED_FEATURES.last == sym_x 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 of explicit namespaces 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(["admin/users_controller.rb"]) do loader.push_dir(".") loader.setup assert Admin assert !$LOADED_FEATURES.include?(File.expand_path("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` 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 # In some spots like shadowed files detection we need to check if constants # are already defined in the parent class or module. In order to do this and # still be lazy, we rely on this property of const_defined? # # This also matters for autoloads already set by 3rd-party code, for example # in reopened namespaces. Zeitwerk won't override them, but thanks to this # characteristic of const_defined? if won't trigger them either. 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 # Loaders use this property when unloading to be able tell if the autoloads # that are pending according to their state are still pending. While things # are autoloaded that collection is maintained, this should not be needed. But # client code doing unsupported stuff like using require_relative on managed # files could introduce weird state we need to be defensive about. 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 # This edge case justifies the need for the inceptions collection in the # registry. See Zeitwerk::Registry.inceptions. 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.expand_path('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 namespace_dirs.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 TP callback doesn't short-circuit anonymous classes/modules # because Class.new/Module.new do not trigger the :class event. We leverage # this property to save a nil? call. # # However, if that changes in future versions of Ruby, this test would tell us # and we could revisit the callback implementation. 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 still able to intercept it because $LOADED_FEATURES # stores it 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.expand_path("x.rb"), $LOADED_FEATURES.last end end end # This allows Zeitwerk to be thread-safe on regular file autoloads. Module # autovivification is custom, has its own test. test "autoloads and constant references are synchronized" do $ensure_M_is_autoloaded_by_the_thread = Queue.new files = [["m.rb", <<-EOS]] module M $ensure_M_is_autoloaded_by_the_thread << true sleep 0.5 def self.works? true end end EOS with_setup(files) do t = Thread.new { M } $ensure_M_is_autoloaded_by_the_thread.pop() assert M.works? t.join end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_shadowed_files.rb000066400000000000000000000025641442267002100233350ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestShadowedFiles < LoaderTest test "does not autoload from a file shadowed by an existing constant" do on_teardown { remove_const :X } ::X = 1 files = [["x.rb", "X = 2"]] with_setup(files) do assert loader.__shadowed_file?(File.expand_path("x.rb")) assert_equal 1, ::X loader.reload assert_equal 1, ::X end end test "does not autoload from a file shadowed by another one managed by the same loader" do files = [["a/x.rb", "X = 1"], ["b/x.rb", "X = 2"]] with_files(files) do loader.push_dir("a") loader.push_dir("b") loader.setup assert !loader.__shadowed_file?(File.expand_path("a/x.rb")) assert loader.__shadowed_file?(File.expand_path("b/x.rb")) assert_equal 1, ::X loader.reload assert_equal 1, ::X end end test "does not autoload from a file shadowed by another one managed by a different loader" do files = [["a/x.rb", "X = 1"], ["b/x.rb", "X = 2"]] with_files(files) do first_loader = new_loader(dirs: "a") second_loader = new_loader(dirs: "b") assert !first_loader.__shadowed_file?(File.expand_path("a/x.rb")) assert second_loader.__shadowed_file?(File.expand_path("b/x.rb")) assert_equal 1, ::X second_loader.reload assert_equal 1, ::X end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_shared_namespaces.rb000066400000000000000000000032331442267002100240140ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestSharedNamespaces < LoaderTest test "autoloads from a shared 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 shared implicit namespace managed by another loader" do files = [["a/m/x.rb", "M::X = true"], ["b/m/y.rb", "M::Y = true"]] with_files(files) do new_loader(dirs: "a") loader = new_loader(dirs: "b") mod = M assert M::X assert M::Y loader.reload assert_same mod, M assert M::X assert M::Y end end test "autoloads from a shared 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 test "autoloads from a shared explicit namespace (autoload)" do on_teardown do remove_const :M delete_loaded_feature "a/m.rb" end files = [ ["a/m.rb", "class M; end"], ["b/m/x.rb", "M::X = true"] ] with_files(files) do # External code has an autoload defined, could be another loader or not, # does not matter. Object.autoload(:M, File.expand_path("a/m.rb")) loader.push_dir("b") loader.setup mod = M assert M::X loader.reload assert_same mod, M assert M::X end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_sti_old_school_workaround.rb000066400000000000000000000031601442267002100256250ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" # Rails applications are expected to preload 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.6.8/test/lib/zeitwerk/test_top_level.rb000066400000000000000000000041051442267002100223370ustar00rootroot00000000000000# frozen_string_literal: true require "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 = [["user.rb", "class User; end"]] with_setup(files) do assert User end end test "autoloads a simple class in a top-level file (Namespace)" do files = [["user.rb", "class #{Namespace}::User; end"]] with_setup(files, namespace: Namespace) do assert Namespace::User end end test "autoloads several top-level classes" do files = [ ["rd1/user.rb", "class User; end"], ["rd2/users_controller.rb", "class UsersController; User; end"] ] with_setup(files) do assert UsersController end end test "autoloads only the first of multiple occurrences" do files = [ ["rd1/user.rb", "User = :model"], ["rd2/user.rb", "User = :decorator"], ] with_setup(files) 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.6.8/test/lib/zeitwerk/test_unload.rb000066400000000000000000000113141442267002100216300ustar00rootroot00000000000000# frozen_string_literal: true require "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 = [ ["rd1/user.rb", "class User; end"], ["rd1/api/v1/users_controller.rb", "class Api::V1::UsersController; end"], ["rd1/admin/root.rb", "class Admin::Root; end"], ["rd2/user.rb", "class User; end"] ] with_setup(files) do assert User assert Api::V1::UsersController assert !loader.__autoloads.empty? assert !loader.__autoloaded_dirs.empty? assert !loader.__to_unload.empty? assert !loader.__namespace_dirs.empty? loader.unload assert loader.__autoloads.empty? assert loader.__autoloaded_dirs.empty? assert loader.__to_unload.empty? assert loader.__namespace_dirs.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.send(:cpaths)["M"] == la lb = new_loader(dirs: "b") assert Zeitwerk::ExplicitNamespace.send(:cpaths)["X"] == lb la.unload assert_nil Zeitwerk::ExplicitNamespace.send(:cpaths)["M"] assert Zeitwerk::ExplicitNamespace.send(:cpaths)["X"] == lb end end test "unload clears the set of shadowed files" do files = [ ["a/m.rb", "module M; end"], ["b/m.rb", "module M; end"], ] with_files(files) do loader.push_dir("a") loader.push_dir("b") loader.setup assert !loader.__shadowed_files.empty? # precondition loader.unload assert loader.__shadowed_files.empty? end end test "unload clears state even if the autoload failed and the exception was rescued" do on_teardown do remove_const :X_IS_NOT_DEFINED end files = [["x.rb", "X_IS_NOT_DEFINED = true"]] with_setup(files) do begin X rescue Zeitwerk::NameError pass # precondition holds else flunk # precondition failed end loader.unload assert !Object.constants.include?(:X) assert !required?(files) end end test "raises if called before setup" do assert_raises(Zeitwerk::SetupRequired) do loader.unload end end end zeitwerk-2.6.8/test/lib/zeitwerk/test_unloadable_cpath.rb000066400000000000000000000034171442267002100236400ustar00rootroot00000000000000# frozen_string_literal: true require "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.6.8/test/lib/zeitwerk/test_unregister.rb000066400000000000000000000042601442267002100225370ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestUnregister < LoaderTest test "unregister removes the loader from internal state" do loader1 = Zeitwerk::Loader.new registry = Zeitwerk::Registry registry.register_loader(loader1) registry.gem_loaders_by_root_file["dummy1"] = loader1 registry.register_autoload(loader1, "dummy1") registry.register_inception("dummy1", "dummy1", loader1) Zeitwerk::ExplicitNamespace.__register("dummy1", loader1) loader2 = Zeitwerk::Loader.new registry = Zeitwerk::Registry registry.register_loader(loader2) registry.gem_loaders_by_root_file["dummy2"] = loader2 registry.register_autoload(loader2, "dummy2") registry.register_inception("dummy2", "dummy2", loader2) Zeitwerk::ExplicitNamespace.__register("dummy2", loader2) loader1.unregister assert !registry.loaders.include?(loader1) assert !registry.gem_loaders_by_root_file.values.include?(loader1) assert !registry.autoloads.values.include?(loader1) assert !registry.inceptions.values.any? {|_, l| l == loader1} assert !Zeitwerk::ExplicitNamespace.send(:cpaths).values.include?(loader1) assert registry.loaders.include?(loader2) assert registry.gem_loaders_by_root_file.values.include?(loader2) assert registry.autoloads.values.include?(loader2) assert registry.inceptions.values.any? {|_, l| l == loader2} assert Zeitwerk::ExplicitNamespace.send(:cpaths).values.include?(loader2) end test 'with_loader yields and unregisters' do loader = Zeitwerk::Loader.new unregister_was_called = false loader.define_singleton_method(:unregister) { unregister_was_called = true } Zeitwerk::Loader.stub :new, loader do Zeitwerk.with_loader do |l| assert_same loader, l end end assert unregister_was_called end test 'with_loader yields and unregisters, even if an exception happens' do loader = Zeitwerk::Loader.new unregister_was_called = false loader.define_singleton_method(:unregister) { unregister_was_called = true } Zeitwerk::Loader.stub :new, loader do Zeitwerk.with_loader { raise } rescue nil end assert unregister_was_called end end zeitwerk-2.6.8/test/lib/zeitwerk/test_utf8.rb000066400000000000000000000026671442267002100212470ustar00rootroot00000000000000# frozen_string_literal: true require "test_helper" class TestUTF8 < LoaderTest # In CI, the codepage for the Windows file system for Ruby 2.5, 2.6, and 2.7 # is Windows-1252, and UTF-8 for Ruby >= 3.0. In Ubuntu, the file system is # encoded in UTF-8 for all supported Ruby versions. if Encoding::UTF_8 == Encoding.find("filesystem") test "autoloads in a project whose root directories have accented letters" do files = [["líb/x.rb", "X = true"]] with_setup(files, dirs: ["líb"]) do assert X end end test "autoloads constants that have accented letters in the middle" do files = [["màxim.rb", "Màxim = 10_000"]] with_setup(files) do assert Màxim end end if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6') test "autoloads constants that start with a Greek letter" do files = [["ω.rb", "Ω = true"]] with_setup(files) do assert Ω end end test "autoloads implicit namespaces that start with a Greek letter" do files = [["ω/à.rb", "Ω::À = true"]] with_setup(files) do assert Ω::À end end test "autoloads explicit namespaces that start with a Greek letter" do files = [ ["ω.rb", "module Ω; end"], ["ω/à.rb", "Ω::À = true"] ] with_setup(files) do assert Ω::À end end end end end zeitwerk-2.6.8/test/support/000077500000000000000000000000001442267002100160645ustar00rootroot00000000000000zeitwerk-2.6.8/test/support/delete_loaded_feature.rb000066400000000000000000000002701442267002100226750ustar00rootroot00000000000000# frozen_string_literal: true module DeleteLoadedFeature def delete_loaded_feature(path) $LOADED_FEATURES.delete_if do |abspath| abspath.end_with?(path) end end end zeitwerk-2.6.8/test/support/loader_test.rb000066400000000000000000000060561442267002100207250ustar00rootroot00000000000000# frozen_string_literal: true class 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: [], namespace: Object, enable_reloading: true, setup: true) Zeitwerk::Loader.new.tap do |loader| Array(dirs).each do |dir| loader.push_dir(dir, namespace: namespace) end loader.enable_reloading if enable_reloading loader.setup if setup end end def reset_constants Zeitwerk::Registry.loaders.each do |loader| begin loader.unload rescue Zeitwerk::SetupRequired end end end def reset_registry Zeitwerk::Registry.loaders.clear Zeitwerk::Registry.gem_loaders_by_root_file.clear Zeitwerk::Registry.autoloads.clear Zeitwerk::Registry.inceptions.clear end def reset_explicit_namespace Zeitwerk::ExplicitNamespace.send(:cpaths).clear Zeitwerk::ExplicitNamespace.send(: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 yield end ensure mkdir_test if rm end def with_load_path(dirs = loader.dirs) dirs = Array(dirs).map { |dir| File.expand_path(dir) } dirs.each { |dir| $LOAD_PATH.push(dir) } yield ensure dirs.each { |dir| $LOAD_PATH.delete(dir) } end def with_setup(files = [], dirs: nil, namespace: Object, load_path: nil, rm: true) with_files(files, rm: rm) do dirs ||= files.map do |file| file[0] =~ %r{\A(rd\d+)/} ? $1 : "." end.uniq dirs.each { |dir| loader.push_dir(dir, namespace: namespace) } files.each do |file| if File.basename(file[0]) == "ignored.rb" loader.ignore(file[0]) elsif file[0] =~ %r{\A(ignored|.+/ignored)/} loader.ignore($1) end if file[0] =~ %r{\A(collapsed|.+/collapsed)/} loader.collapse($1) end end loader.setup if load_path with_load_path(load_path) { yield } else yield end end end def required?(file_or_files) if file_or_files.is_a?(String) $LOADED_FEATURES.include?(File.expand_path(file_or_files, TMP_DIR)) elsif file_or_files[0].is_a?(String) required?(file_or_files[0]) else file_or_files.all? { |f| required?(f) } end end def assert_abspath(expected, actual) assert_equal(File.expand_path(expected, TMP_DIR), actual) end end zeitwerk-2.6.8/test/support/on_teardown.rb000066400000000000000000000002311442267002100207240ustar00rootroot00000000000000# frozen_string_literal: true module OnTeardown def on_teardown define_singleton_method(:teardown) do yield super() end end end zeitwerk-2.6.8/test/support/remove_const.rb000066400000000000000000000002141442267002100211110ustar00rootroot00000000000000# frozen_string_literal: true module RemoveConst def remove_const(cname, from: Object) from.__send__(:remove_const, cname) end end zeitwerk-2.6.8/test/support/test_macro.rb000066400000000000000000000002711442267002100205510ustar00rootroot00000000000000# frozen_string_literal: true module TestMacro def test(description, &block) method_name = "test_#{description}".gsub(/\W/, "_") define_method(method_name, &block) end end zeitwerk-2.6.8/test/test_helper.rb000066400000000000000000000024501442267002100172140ustar00rootroot00000000000000# frozen_string_literal: true require "minitest/autorun" require "minitest/focus" require "minitest/proveit" require "minitest/reporters" Minitest::Reporters.use!(Minitest::Reporters::DefaultReporter.new) require "warning" Warning.process do |msg| # This warning is issued by Zeitwerk itself, ignore it. if msg.include?("Zeitwerk defines the constant") :default # These ones are issued by the test "autovivification is synchronized" from # test_autovivification.rb, but only on Ruby 2.5. They can be ignored, the # test verifies the constant is set only once. I suspect this is related to # the internal representation of constants in CRuby and # # https://github.com/ruby/ruby/commit/b74131132f8872d23e405c61ecfe18dece17292f # # fixed it, but have not verified it. elsif msg.include?("already initialized constant Admin::V2") || msg.include?("previous definition of V2 was here") :default else :raise end end require_relative "support/test_macro" require_relative "support/delete_loaded_feature" require_relative "support/loader_test" require_relative "support/remove_const" require_relative "support/on_teardown" require "zeitwerk" Minitest::Test.class_eval do extend TestMacro include DeleteLoadedFeature include RemoveConst include OnTeardown prove_it! end zeitwerk-2.6.8/zeitwerk.gemspec000066400000000000000000000020641442267002100166040ustar00rootroot00000000000000# frozen_string_literal: true require_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.5" end