diff --git a/Library/Homebrew/extend/os/mac/hardware/cpu.rb b/Library/Homebrew/extend/os/mac/hardware/cpu.rb index 14ee2fd9aececf0c07a15bb24740e631294d359e..340e4212217a2fa88142bcbbcbd5c85e45e9784e 100644 --- a/Library/Homebrew/extend/os/mac/hardware/cpu.rb +++ b/Library/Homebrew/extend/os/mac/hardware/cpu.rb @@ -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 diff --git a/Library/Homebrew/hardware.rb b/Library/Homebrew/hardware.rb index b2a559d59cf0b76293b7d73ef3791c5a9f73d450..c7756dd3b52d295f3350b77802f45bcc263d993a 100644 --- a/Library/Homebrew/hardware.rb +++ b/Library/Homebrew/hardware.rb @@ -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 diff --git a/Library/Homebrew/test/hardware/cpu_spec.rb b/Library/Homebrew/test/hardware/cpu_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..5bcb9fca5c7513e0ed9defa2bad036c59fb3c97f --- /dev/null +++ b/Library/Homebrew/test/hardware/cpu_spec.rb @@ -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 diff --git a/Library/Homebrew/test/hardware_spec.rb b/Library/Homebrew/test/hardware_spec.rb deleted file mode 100644 index de8d77e680483194a123ff9e967db4796bbcda4b..0000000000000000000000000000000000000000 --- a/Library/Homebrew/test/hardware_spec.rb +++ /dev/null @@ -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