diff options
| author | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2026-05-01 11:30:50 +0900 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2026-05-07 09:24:50 +0000 |
| commit | 199af7f7cb1130b19d46ddf4208b18eee8cc9181 (patch) | |
| tree | 09841d37b8a13d562de0bb0c7382c81440f33fae | |
| parent | bb1b229685cc5acb18f6bba71830bb9df3110fe1 (diff) | |
[ruby/rubygems] Add `override` DSL method to Bundler::Dsl
Introduce Gemfile-level `override target, field: operation, ...` that
collects Bundler::Override instances and forwards them to Definition
via to_definition. Validation and resolver hookup come in later
commits; this commit only wires the entry point.
https://github.com/ruby/rubygems/commit/e2fc49141c
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
| -rw-r--r-- | lib/bundler/dsl.rb | 13 | ||||
| -rw-r--r-- | spec/bundler/bundler/dsl_spec.rb | 32 |
2 files changed, 43 insertions, 2 deletions
diff --git a/lib/bundler/dsl.rb b/lib/bundler/dsl.rb index 6f06c4e918..a5df3235b1 100644 --- a/lib/bundler/dsl.rb +++ b/lib/bundler/dsl.rb @@ -22,7 +22,7 @@ module Bundler GITHUB_PULL_REQUEST_URL = %r{\Ahttps://github\.com/([A-Za-z0-9_\-\.]+/[A-Za-z0-9_\-\.]+)/pull/(\d+)\z} GITLAB_MERGE_REQUEST_URL = %r{\Ahttps://gitlab\.com/([A-Za-z0-9_\-\./]+)/-/merge_requests/(\d+)\z} - attr_reader :gemspecs, :gemfile + attr_reader :gemspecs, :gemfile, :overrides attr_accessor :dependencies def initialize @@ -40,6 +40,7 @@ module Bundler @gemfile = nil @gemfiles = [] @lockfile = nil + @overrides = [] add_git_sources end @@ -184,10 +185,18 @@ module Bundler with_source(git_source) { yield } end + def override(target, **operations) + operations.each do |field, operation| + @overrides << Override.new(target, field, operation) + end + end + def to_definition(lockfile, unlock) check_primary_source_safety lockfile = @lockfile unless @lockfile.nil? - Definition.new(lockfile, @dependencies, @sources, unlock, @ruby_version, @optional_groups, @gemfiles) + definition = Definition.new(lockfile, @dependencies, @sources, unlock, @ruby_version, @optional_groups, @gemfiles) + definition.overrides = @overrides + definition end def group(*args, &blk) diff --git a/spec/bundler/bundler/dsl_spec.rb b/spec/bundler/bundler/dsl_spec.rb index 6ba2e728b5..b0aa0fa05d 100644 --- a/spec/bundler/bundler/dsl_spec.rb +++ b/spec/bundler/bundler/dsl_spec.rb @@ -366,4 +366,36 @@ RSpec.describe Bundler::Dsl do end end end + + describe "#override" do + it "stores an Override for a gem with a version: operation" do + subject.override("rails", version: ">= 8.0") + + expect(subject.overrides.size).to eq(1) + override = subject.overrides.first + expect(override.target).to eq("rails") + expect(override.field).to eq(:version) + expect(override.operation).to eq(">= 8.0") + end + + it "accepts :ignore_upper as the operation" do + subject.override("nokogiri", version: :ignore_upper) + expect(subject.overrides.first.operation).to eq(:ignore_upper) + end + + it "accepts nil as the operation" do + subject.override("legacy", version: nil) + expect(subject.overrides.first.operation).to be_nil + end + + it "appends to overrides across multiple statements" do + subject.override("rails", version: ">= 8.0") + subject.override("nokogiri", version: :ignore_upper) + expect(subject.overrides.map(&:target)).to eq(["rails", "nokogiri"]) + end + + it "is empty by default" do + expect(subject.overrides).to eq([]) + end + end end |
