aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew
diff options
context:
space:
mode:
authorMisty De Meo2017-04-15 22:05:26 +0800
committerMisty De Meo2017-04-15 22:59:55 +0800
commitd7ff53fa1ddf649ce8ca3910fc5e901ea4aa5b5a (patch)
treedad1a230e9785c20367905804771f9d5210a69d5 /Library/Homebrew
parent30582a5fbd35843e183f1591cc8413309fa5e705 (diff)
downloadbrew-d7ff53fa1ddf649ce8ca3910fc5e901ea4aa5b5a.tar.bz2
Hardware: add helper to determine if an executable can be run
Diffstat (limited to 'Library/Homebrew')
-rw-r--r--Library/Homebrew/hardware.rb12
-rw-r--r--Library/Homebrew/test/hardware_spec.rb47
2 files changed, 59 insertions, 0 deletions
diff --git a/Library/Homebrew/hardware.rb b/Library/Homebrew/hardware.rb
index fe07aee9d..997598def 100644
--- a/Library/Homebrew/hardware.rb
+++ b/Library/Homebrew/hardware.rb
@@ -80,6 +80,18 @@ module Hardware
def feature?(name)
features.include?(name)
end
+
+ def can_run?(arch)
+ if is_32_bit?
+ arch_32_bit == arch
+ elsif intel?
+ [:i386, :x86_64].include? arch
+ elsif ppc?
+ [:ppc, :ppc64].include? arch
+ else
+ false
+ end
+ end
end
end
diff --git a/Library/Homebrew/test/hardware_spec.rb b/Library/Homebrew/test/hardware_spec.rb
index c5f8daf4e..de8d77e68 100644
--- a/Library/Homebrew/test/hardware_spec.rb
+++ b/Library/Homebrew/test/hardware_spec.rb
@@ -36,5 +36,52 @@ module Hardware
).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