Refactor Hardware::CPU spec.
This commit is contained in:
parent
d0d7116dc8
commit
b91628a614
@ -167,15 +167,15 @@ module Hardware
|
||||
|
||||
def intel_can_run?(arch)
|
||||
case arch
|
||||
when :ppc
|
||||
when *PPC_32BIT_ARCHS
|
||||
# Rosetta is still available
|
||||
MacOS.version < :lion
|
||||
when :ppc64
|
||||
when *PPC_64BIT_ARCHS
|
||||
# Rosetta never supported PPC64
|
||||
false
|
||||
when :x86_64
|
||||
when *INTEL_64BIT_ARCHS
|
||||
Hardware::CPU.is_64_bit?
|
||||
when :i386
|
||||
when *INTEL_32BIT_ARCHS
|
||||
true
|
||||
else # dunno
|
||||
false
|
||||
@ -184,9 +184,9 @@ module Hardware
|
||||
|
||||
def ppc_can_run?(arch)
|
||||
case arch
|
||||
when :ppc
|
||||
when *PPC_32BIT_ARCHS
|
||||
true
|
||||
when :ppc64
|
||||
when *PPC_64BIT_ARCHS
|
||||
Hardware::CPU.is_64_bit?
|
||||
else
|
||||
# Intel is never supported
|
||||
|
||||
@ -2,7 +2,7 @@ module Hardware
|
||||
class CPU
|
||||
INTEL_32BIT_ARCHS = [:i386].freeze
|
||||
INTEL_64BIT_ARCHS = [:x86_64].freeze
|
||||
PPC_32BIT_ARCHS = [:ppc, :ppc7400, :ppc7450, :ppc970].freeze
|
||||
PPC_32BIT_ARCHS = [:ppc, :ppc32, :ppc7400, :ppc7450, :ppc970].freeze
|
||||
PPC_64BIT_ARCHS = [:ppc64].freeze
|
||||
|
||||
class << self
|
||||
@ -118,9 +118,9 @@ module Hardware
|
||||
if is_32_bit?
|
||||
arch_32_bit == arch
|
||||
elsif intel?
|
||||
[:i386, :x86_64].include? arch
|
||||
(INTEL_32BIT_ARCHS + INTEL_64BIT_ARCHS).include?(arch)
|
||||
elsif ppc?
|
||||
[:ppc, :ppc64].include? arch
|
||||
(PPC_32BIT_ARCHS + PPC_64BIT_ARCHS).include?(arch)
|
||||
else
|
||||
false
|
||||
end
|
||||
|
||||
114
Library/Homebrew/test/hardware/cpu_spec.rb
Normal file
114
Library/Homebrew/test/hardware/cpu_spec.rb
Normal file
@ -0,0 +1,114 @@
|
||||
require "hardware"
|
||||
|
||||
describe Hardware::CPU do
|
||||
describe "::type" do
|
||||
let(:cpu_types) {
|
||||
[
|
||||
:intel,
|
||||
:ppc,
|
||||
:dunno,
|
||||
]
|
||||
}
|
||||
|
||||
it "returns the current CPU's type as a symbol, or :dunno if it cannot be detected" do
|
||||
expect(cpu_types).to include(described_class.type)
|
||||
end
|
||||
end
|
||||
|
||||
describe "::family" do
|
||||
let(:cpu_families) {
|
||||
[
|
||||
:core,
|
||||
:core2,
|
||||
:penryn,
|
||||
:nehalem,
|
||||
:arrandale,
|
||||
:sandybridge,
|
||||
:ivybridge,
|
||||
:haswell,
|
||||
:broadwell,
|
||||
:skylake,
|
||||
:kabylake,
|
||||
:dunno,
|
||||
]
|
||||
}
|
||||
|
||||
it "returns the current CPU's family name as a symbol, or :dunno if it cannot be detected" do
|
||||
expect(cpu_families).to include described_class.family
|
||||
end
|
||||
end
|
||||
|
||||
describe "::can_run?" do
|
||||
subject { described_class }
|
||||
|
||||
matcher :be_able_to_run do |arch|
|
||||
match do |expected|
|
||||
allow(expected).to receive(:type).and_return type
|
||||
allow(expected).to receive(:bits).and_return bits
|
||||
|
||||
expect(expected.can_run?(arch)).to be true
|
||||
end
|
||||
end
|
||||
|
||||
let(:type) { described_class.type }
|
||||
let(:bits) { described_class.bits }
|
||||
|
||||
before do
|
||||
allow(described_class).to receive(:type).and_return type
|
||||
allow(described_class).to receive(:bits).and_return bits
|
||||
end
|
||||
|
||||
context "when on an 32-bit Intel machine" do
|
||||
let(:type) { :intel }
|
||||
let(:bits) { 32 }
|
||||
|
||||
it { is_expected.to be_able_to_run :i386 }
|
||||
it { is_expected.not_to be_able_to_run :x86_64 }
|
||||
it { is_expected.not_to be_able_to_run :ppc32 }
|
||||
it { is_expected.not_to be_able_to_run :ppc64 }
|
||||
end
|
||||
|
||||
context "when on an 64-bit Intel machine" do
|
||||
let(:type) { :intel }
|
||||
let(:bits) { 64 }
|
||||
|
||||
it { is_expected.to be_able_to_run :i386 }
|
||||
it { is_expected.to be_able_to_run :x86_64 }
|
||||
it { is_expected.not_to be_able_to_run :ppc32 }
|
||||
it { is_expected.not_to be_able_to_run :ppc64 }
|
||||
end
|
||||
|
||||
context "when on a 32-bit PowerPC machine" do
|
||||
let(:type) { :ppc }
|
||||
let(:bits) { 32 }
|
||||
|
||||
it { is_expected.not_to be_able_to_run :i386 }
|
||||
it { is_expected.not_to be_able_to_run :x86_64 }
|
||||
it { is_expected.to be_able_to_run :ppc32 }
|
||||
it { is_expected.not_to be_able_to_run :ppc64 }
|
||||
end
|
||||
|
||||
context "when on a 64-bit PowerPC machine" do
|
||||
let(:type) { :ppc }
|
||||
let(:bits) { 64 }
|
||||
|
||||
it { is_expected.not_to be_able_to_run :i386 }
|
||||
it { is_expected.not_to be_able_to_run :x86_64 }
|
||||
it { is_expected.to be_able_to_run :ppc32 }
|
||||
it { is_expected.to be_able_to_run :ppc64 }
|
||||
end
|
||||
|
||||
context "when the CPU type is unknown" do
|
||||
let(:type) { :dunno }
|
||||
|
||||
it { is_expected.not_to be_able_to_run :i386 }
|
||||
it { is_expected.not_to be_able_to_run :x86_64 }
|
||||
it { is_expected.not_to be_able_to_run :ppc32 }
|
||||
it { is_expected.not_to be_able_to_run :ppc64 }
|
||||
end
|
||||
|
||||
context "when the architecture is unknown" do
|
||||
it { is_expected.not_to be_able_to_run :blah }
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,87 +0,0 @@
|
||||
require "hardware"
|
||||
|
||||
module Hardware
|
||||
describe CPU do
|
||||
describe "::type" do
|
||||
it "returns the current CPU's type as a symbol, or :dunno if it cannot be detected" do
|
||||
expect(
|
||||
[
|
||||
:intel,
|
||||
:ppc,
|
||||
:dunno,
|
||||
],
|
||||
).to include(described_class.type)
|
||||
end
|
||||
end
|
||||
|
||||
describe "::family" do
|
||||
it "returns the current CPU's family name as a symbol, or :dunno if it cannot be detected" do
|
||||
skip "Needs an Intel CPU." unless described_class.intel?
|
||||
|
||||
expect(
|
||||
[
|
||||
:core,
|
||||
:core2,
|
||||
:penryn,
|
||||
:nehalem,
|
||||
:arrandale,
|
||||
:sandybridge,
|
||||
:ivybridge,
|
||||
:haswell,
|
||||
:broadwell,
|
||||
:skylake,
|
||||
:kabylake,
|
||||
:dunno,
|
||||
],
|
||||
).to include(described_class.family)
|
||||
end
|
||||
end
|
||||
|
||||
describe "::can_run?" do
|
||||
it "reports that Intel machines can run Intel executables" do
|
||||
allow(Hardware::CPU).to receive(:type).and_return :intel
|
||||
allow(Hardware::CPU).to receive(:bits).and_return 64
|
||||
expect(Hardware::CPU.can_run?(:i386)).to be true
|
||||
expect(Hardware::CPU.can_run?(:x86_64)).to be true
|
||||
end
|
||||
|
||||
it "reports that PowerPC machines can run PowerPC executables" do
|
||||
allow(Hardware::CPU).to receive(:type).and_return :ppc
|
||||
allow(Hardware::CPU).to receive(:bits).and_return 64
|
||||
expect(Hardware::CPU.can_run?(:ppc)).to be true
|
||||
expect(Hardware::CPU.can_run?(:ppc64)).to be true
|
||||
end
|
||||
|
||||
it "reports that 32-bit Intel machines can't run x86_64 executables" do
|
||||
allow(Hardware::CPU).to receive(:type).and_return :intel
|
||||
allow(Hardware::CPU).to receive(:bits).and_return 32
|
||||
expect(Hardware::CPU.can_run?(:x86_64)).to be false
|
||||
end
|
||||
|
||||
it "reports that 32-bit PowerPC machines can't run ppc64 executables" do
|
||||
allow(Hardware::CPU).to receive(:type).and_return :ppc
|
||||
allow(Hardware::CPU).to receive(:bits).and_return 32
|
||||
expect(Hardware::CPU.can_run?(:ppc64)).to be false
|
||||
end
|
||||
|
||||
it "identifies that Intel and PowerPC machines can't run each others' executables" do
|
||||
allow(Hardware::CPU).to receive(:type).and_return :ppc
|
||||
expect(Hardware::CPU.can_run?(:i386)).to be false
|
||||
expect(Hardware::CPU.can_run?(:x86_64)).to be false
|
||||
|
||||
allow(Hardware::CPU).to receive(:type).and_return :intel
|
||||
expect(Hardware::CPU.can_run?(:ppc)).to be false
|
||||
expect(Hardware::CPU.can_run?(:ppc64)).to be false
|
||||
end
|
||||
|
||||
it "returns false for unknown CPU types" do
|
||||
allow(Hardware::CPU).to receive(:type).and_return :dunno
|
||||
expect(Hardware::CPU.can_run?(:i386)).to be false
|
||||
end
|
||||
|
||||
it "returns false for unknown arches" do
|
||||
expect(Hardware::CPU.can_run?(:blah)).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user