[rubygems/rubygems] Ensure that Gem::Platform parses strings to a fix point

The issue was that the property that

```ruby
platform = Gem::Platform.new $string
platform == Gem::Platform.new(platform.to_s)
```

was not always true.

This property (of acchieving a fix point) is important,
since `Gem::Platform` gets serialized to a string and
then deserialized back to a `Gem::Platform` object.
If it doesn't deserialize to the same object, then
different platforms are used for the initial serialization
than subsequent runs.

I used https://github.com/segiddins/Scratch/blob/main/2025/03/rubygems-platform.rb
to find the failing cases and then fixed them.
With this patch, the prop check test now passes.

https://github.com/rubygems/rubygems/commit/313fb4bcec
This commit is contained in:
Samuel Giddins 2025-03-24 10:48:42 -07:00 committed by Hiroshi SHIBATA
parent 72387ebd0e
commit eb48418b40
2 changed files with 32 additions and 11 deletions

View File

@ -88,25 +88,32 @@ class Gem::Platform
when Array then
@cpu, @os, @version = arch
when String then
arch_str = arch
arch = arch.split "-"
if arch.length > 2 && !arch.last.match?(/\d+(\.\d+)?$/) # reassemble x86-linux-{libc}
if arch.length > 2 && !arch.last.match?(/^\d+(\.\d+)?$/) # reassemble x86-linux-{libc}
extra = arch.pop
arch.last << "-#{extra}"
end
cpu = arch.shift
@cpu = case cpu
when /i\d86/ then "x86"
else cpu
if cpu.nil? || "" == cpu
raise ArgumentError, "empty cpu in platform #{arch_str.inspect}"
end
@cpu = if cpu.match?(/i\d86/)
"x86"
else
cpu
end
if arch.length == 2 && arch.last.match?(/^\d+(\.\d+)?$/) # for command-line
@os, @version = arch
return
end
# discard the version element, it didn't match the version pattern (\d+(\.\d+)?)
os, = arch
if os.nil?
@cpu = nil
@ -120,17 +127,17 @@ class Gem::Platform
when /^macruby$/ then ["macruby", nil]
when /freebsd(\d+)?/ then ["freebsd", $1]
when /^java$/, /^jruby$/ then ["java", nil]
when /^java([\d.]*)/ then ["java", $1]
when /^java(\d+(?:\.\d+)*)?/ then ["java", $1]
when /^dalvik(\d+)?$/ then ["dalvik", $1]
when /^dotnet$/ then ["dotnet", nil]
when /^dotnet([\d.]*)/ then ["dotnet", $1]
when /^dotnet(\d+(?:\.\d+)*)?/ then ["dotnet", $1]
when /linux-?(\w+)?/ then ["linux", $1]
when /mingw32/ then ["mingw32", nil]
when /mingw-?(\w+)?/ then ["mingw", $1]
when /(mswin\d+)(\_(\d+))?/ then
when /(mswin\d+)(?:\_(\d+))?/ then
os = $1
version = $3
@cpu = "x86" if @cpu.nil? && os =~ /32$/
version = $2
@cpu = "x86" if @cpu.nil? && os.end_with?("32")
[os, version]
when /netbsdelf/ then ["netbsdelf", nil]
when /openbsd(\d+\.\d+)?/ then ["openbsd", $1]
@ -154,6 +161,9 @@ class Gem::Platform
end
def to_s
if @cpu.nil? && @os && @version
return "#{@os}#{@version}"
end
to_a.compact.join "-"
end

View File

@ -148,12 +148,23 @@ class TestGemPlatform < Gem::TestCase
"wasm32-wasi" => ["wasm32", "wasi", nil],
"wasm32-wasip1" => ["wasm32", "wasi", nil],
"wasm32-wasip2" => ["wasm32", "wasi", nil],
"darwin-java-java" => ["darwin", "java", nil],
"linux-linux-linux" => ["linux", "linux", "linux"],
"linux-linux-linux1.0" => ["linux", "linux", "linux1"],
"x86x86-1x86x86x86x861linuxx86x86" => ["x86x86", "linux", "x86x86"],
"freebsd0" => [nil, "freebsd", "0"],
"darwin0" => [nil, "darwin", "0"],
"darwin0---" => [nil, "darwin", "0"],
"x86-linux-x8611.0l" => ["x86", "linux", "x8611"],
"0-x86linuxx86---" => ["0", "linux", "x86"],
}
test_cases.each do |arch, expected|
platform = Gem::Platform.new arch
assert_equal expected, platform.to_a, arch.inspect
assert_equal expected, Gem::Platform.new(platform.to_s).to_a, arch.inspect
platform2 = Gem::Platform.new platform.to_s
assert_equal expected, platform2.to_a, "#{arch.inspect} => #{platform2.inspect}"
end
end