Skip to content

Commit ca4f88a

Browse files
committed
test(check.rb): improve total_physical_memory
- On Linux, read /proc/meminfo directly instead of using free. - On Windows, replace deprecated wmic with PowerShell.
1 parent 9b0d11c commit ca4f88a

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

check/check.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,20 @@ def read_env_positive_int(key, default_value)
7777
# Get the total size of the physical memory available on the host machine.
7878
def total_physical_memory
7979
platform = RbConfig::CONFIG["host_os"].downcase
80-
if platform.include?("linux")
81-
mem_info = `free -b | grep Mem`
82-
mem_info.split[1].to_i
83-
elsif platform.include?("darwin")
84-
mem_info = `sysctl -n hw.memsize`
85-
mem_info.to_i
86-
elsif platform.include?("mingw") || platform.include?("mswin")
87-
mem_info = `wmic ComputerSystem get TotalPhysicalMemory`
88-
mem_info.split[1].to_i
80+
81+
case
82+
when platform.include?("linux")
83+
File.foreach("/proc/meminfo") do |line|
84+
m = line.match(/\bMemTotal\b\D+(\d+)/)
85+
return m[1].to_i * 1024 if m
86+
end
87+
when platform.include?("darwin")
88+
return `sysctl -n hw.memsize`.to_i
89+
when platform.include?("mingw") || platform.include?("mswin")
90+
return `powershell -NoProfile -Command "(Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory"`.to_i
8991
end
92+
93+
nil
9094
end
9195

9296
# The default prefix for the root temporary directory. See TempDir.root.
@@ -359,6 +363,9 @@ def total_memory
359363
else
360364
@@cached_total_memory = result
361365
end
366+
if result.nil? || result <= 0
367+
warn("failed to determine total physical memory")
368+
end
362369
end
363370
@@cached_total_memory
364371
end

0 commit comments

Comments
 (0)