diff options
| author | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2026-05-01 12:12:43 +0900 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2026-05-07 09:24:51 +0000 |
| commit | a093612797e5436dd4a8b57231c3848cff86e35b (patch) | |
| tree | 917e200e58062845736261e1e9c8951dc6794d77 | |
| parent | 77c7e15a67ae4eff724158b7faf841af0f4a56c2 (diff) | |
[ruby/rubygems] Validate override target, field, and operation types
Reject anything other than :all or a String for the target,
fields outside `:version`, and operations that are not a String,
nil, or one of the supported Symbol values (currently only
:ignore_upper). Validation runs before any Override is recorded so
that a multi-field call with an invalid entry leaves the DSL state
untouched.
https://github.com/ruby/rubygems/commit/c3b7aeb6b9
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
| -rw-r--r-- | lib/bundler/dsl.rb | 33 | ||||
| -rw-r--r-- | spec/bundler/bundler/dsl_spec.rb | 31 |
2 files changed, 64 insertions, 0 deletions
diff --git a/lib/bundler/dsl.rb b/lib/bundler/dsl.rb index 7a4fc3909e..2f9d1f9054 100644 --- a/lib/bundler/dsl.rb +++ b/lib/bundler/dsl.rb @@ -185,12 +185,22 @@ module Bundler with_source(git_source) { yield } end + SUPPORTED_OVERRIDE_FIELDS = [:version].freeze + SUPPORTED_OVERRIDE_SYMBOL_OPERATIONS = [:ignore_upper].freeze + def override(target, **operations) + validate_override_target!(target) + if target == :all && operations.key?(:version) raise ArgumentError, "`override :all, version:` is not allowed; version requirements are per-gem" end operations.each do |field, operation| + validate_override_field!(field) + validate_override_operation!(operation) + end + + operations.each do |field, operation| @overrides << Override.new(target, field, operation) end end @@ -257,6 +267,29 @@ module Bundler private + def validate_override_target!(target) + return if target == :all + return if target.is_a?(String) + raise ArgumentError, "override target must be :all or a gem name string, got #{target.inspect}" + end + + def validate_override_field!(field) + return if SUPPORTED_OVERRIDE_FIELDS.include?(field) + raise ArgumentError, "unsupported override field `#{field}:`; only `version:` is currently supported" + end + + def validate_override_operation!(operation) + case operation + when String, nil + # ok + when Symbol + return if SUPPORTED_OVERRIDE_SYMBOL_OPERATIONS.include?(operation) + raise ArgumentError, "unsupported override operation: #{operation.inspect}" + else + raise ArgumentError, "override operation must be a String, Symbol, or nil, got #{operation.inspect}" + end + end + def add_dependency(name, version = nil, options = {}) options["gemfile"] = @gemfile options["source"] ||= @source diff --git a/spec/bundler/bundler/dsl_spec.rb b/spec/bundler/bundler/dsl_spec.rb index d5e5f830ce..86bcb3be8d 100644 --- a/spec/bundler/bundler/dsl_spec.rb +++ b/spec/bundler/bundler/dsl_spec.rb @@ -416,5 +416,36 @@ RSpec.describe Bundler::Dsl do end.to raise_error(ArgumentError) expect(subject.overrides).to eq([]) end + + it "raises ArgumentError when target is neither :all nor a string" do + expect do + subject.override(:rails, version: ">= 8.0") + end.to raise_error(ArgumentError, /target must be :all or a gem name string/) + end + + it "raises ArgumentError for an unsupported field" do + expect do + subject.override("rails", required_ruby_version: :ignore_upper) + end.to raise_error(ArgumentError, /unsupported override field `required_ruby_version:`/) + end + + it "raises ArgumentError for a non-string, non-symbol, non-nil operation" do + expect do + subject.override("rails", version: 42) + end.to raise_error(ArgumentError, /override operation must be a String, Symbol, or nil/) + end + + it "raises ArgumentError for an unsupported symbol operation" do + expect do + subject.override("rails", version: :explode) + end.to raise_error(ArgumentError, /unsupported override operation/) + end + + it "rejects atomically when one field in a multi-field call is invalid" do + expect do + subject.override("rails", version: ">= 8.0", required_ruby_version: :ignore_upper) + end.to raise_error(ArgumentError, /unsupported override field/) + expect(subject.overrides).to eq([]) + end end end |
