Skip to content

Commit c7b3345

Browse files
authored
Merge pull request #8584 from rubygems/segiddins/ensure-that-gem-platform-parses-strings-to-a-fix-point
Ensure that Gem::Platform parses strings to a fix point
2 parents c100118 + 407c1cb commit c7b3345

2 files changed

Lines changed: 44 additions & 40 deletions

File tree

lib/rubygems/platform.rb

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -88,56 +88,45 @@ def initialize(arch)
8888
when Array then
8989
@cpu, @os, @version = arch
9090
when String then
91-
arch = arch.split "-"
91+
cpu, os = arch.sub(/-+$/, "").split("-", 2)
9292

93-
if arch.length > 2 && !arch.last.match?(/\d+(\.\d+)?$/) # reassemble x86-linux-{libc}
94-
extra = arch.pop
95-
arch.last << "-#{extra}"
96-
end
97-
98-
cpu = arch.shift
99-
100-
@cpu = case cpu
101-
when /i\d86/ then "x86"
102-
else cpu
103-
end
104-
105-
if arch.length == 2 && arch.last.match?(/^\d+(\.\d+)?$/) # for command-line
106-
@os, @version = arch
107-
return
93+
@cpu = if cpu.match?(/i\d86/)
94+
"x86"
95+
else
96+
cpu
10897
end
10998

110-
os, = arch
11199
if os.nil?
112100
@cpu = nil
113101
os = cpu
114102
end # legacy jruby
115103

116104
@os, @version = case os
117-
when /aix(\d+)?/ then ["aix", $1]
118-
when /cygwin/ then ["cygwin", nil]
119-
when /darwin(\d+)?/ then ["darwin", $1]
120-
when /^macruby$/ then ["macruby", nil]
121-
when /freebsd(\d+)?/ then ["freebsd", $1]
122-
when /^java$/, /^jruby$/ then ["java", nil]
123-
when /^java([\d.]*)/ then ["java", $1]
124-
when /^dalvik(\d+)?$/ then ["dalvik", $1]
125-
when /^dotnet$/ then ["dotnet", nil]
126-
when /^dotnet([\d.]*)/ then ["dotnet", $1]
127-
when /linux-?(\w+)?/ then ["linux", $1]
128-
when /mingw32/ then ["mingw32", nil]
129-
when /mingw-?(\w+)?/ then ["mingw", $1]
130-
when /(mswin\d+)(\_(\d+))?/ then
105+
when /aix-?(\d+)?/ then ["aix", $1]
106+
when /cygwin/ then ["cygwin", nil]
107+
when /darwin-?(\d+)?/ then ["darwin", $1]
108+
when "macruby" then ["macruby", nil]
109+
when /^macruby-?(\d+(?:\.\d+)*)?/ then ["macruby", $1]
110+
when /freebsd-?(\d+)?/ then ["freebsd", $1]
111+
when "java", "jruby" then ["java", nil]
112+
when /^java-?(\d+(?:\.\d+)*)?/ then ["java", $1]
113+
when /^dalvik-?(\d+)?$/ then ["dalvik", $1]
114+
when /^dotnet$/ then ["dotnet", nil]
115+
when /^dotnet-?(\d+(?:\.\d+)*)?/ then ["dotnet", $1]
116+
when /linux-?(\w+)?/ then ["linux", $1]
117+
when /mingw32/ then ["mingw32", nil]
118+
when /mingw-?(\w+)?/ then ["mingw", $1]
119+
when /(mswin\d+)(?:[_-](\d+))?/ then
131120
os = $1
132-
version = $3
133-
@cpu = "x86" if @cpu.nil? && os =~ /32$/
121+
version = $2
122+
@cpu = "x86" if @cpu.nil? && os.end_with?("32")
134123
[os, version]
135-
when /netbsdelf/ then ["netbsdelf", nil]
136-
when /openbsd(\d+\.\d+)?/ then ["openbsd", $1]
137-
when /solaris(\d+\.\d+)?/ then ["solaris", $1]
138-
when /wasi/ then ["wasi", nil]
124+
when /netbsdelf/ then ["netbsdelf", nil]
125+
when /openbsd-?(\d+\.\d+)?/ then ["openbsd", $1]
126+
when /solaris-?(\d+\.\d+)?/ then ["solaris", $1]
127+
when /wasi/ then ["wasi", nil]
139128
# test
140-
when /^(\w+_platform)(\d+)?/ then [$1, $2]
129+
when /^(\w+_platform)-?(\d+)?/ then [$1, $2]
141130
else ["unknown", nil]
142131
end
143132
when Gem::Platform then
@@ -154,7 +143,7 @@ def to_a
154143
end
155144

156145
def to_s
157-
to_a.compact.join "-"
146+
to_a.compact.join(@cpu.nil? ? "" : "-")
158147
end
159148

160149
##

test/rubygems/test_gem_platform.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,27 @@ def test_initialize
148148
"wasm32-wasi" => ["wasm32", "wasi", nil],
149149
"wasm32-wasip1" => ["wasm32", "wasi", nil],
150150
"wasm32-wasip2" => ["wasm32", "wasi", nil],
151+
152+
"darwin-java-java" => ["darwin", "java", nil],
153+
"linux-linux-linux" => ["linux", "linux", "linux"],
154+
"linux-linux-linux1.0" => ["linux", "linux", "linux1"],
155+
"x86x86-1x86x86x86x861linuxx86x86" => ["x86x86", "linux", "x86x86"],
156+
"freebsd0" => [nil, "freebsd", "0"],
157+
"darwin0" => [nil, "darwin", "0"],
158+
"darwin0---" => [nil, "darwin", "0"],
159+
"x86-linux-x8611.0l" => ["x86", "linux", "x8611"],
160+
"0-x86linuxx86---" => ["0", "linux", "x86"],
161+
"x86_64-macruby-x86" => ["x86_64", "macruby", nil],
162+
"x86_64-dotnetx86" => ["x86_64", "dotnet", nil],
163+
"x86_64-dalvik0" => ["x86_64", "dalvik", "0"],
164+
"x86_64-dotnet1." => ["x86_64", "dotnet", "1"],
151165
}
152166

153167
test_cases.each do |arch, expected|
154168
platform = Gem::Platform.new arch
155169
assert_equal expected, platform.to_a, arch.inspect
156-
assert_equal expected, Gem::Platform.new(platform.to_s).to_a, arch.inspect
170+
platform2 = Gem::Platform.new platform.to_s
171+
assert_equal expected, platform2.to_a, "#{arch.inspect} => #{platform2.inspect}"
157172
end
158173
end
159174

0 commit comments

Comments
 (0)