Merge pull request #2079 from reitermarkus/spec-compiler_selector

Convert CompilerSelector test to spec.
This commit is contained in:
Markus Reiter 2017-02-23 09:31:57 +01:00 committed by GitHub
commit e7e2beb67a
2 changed files with 122 additions and 117 deletions

View File

@ -0,0 +1,122 @@
require "compilers"
require "software_spec"
describe CompilerSelector do
subject { described_class.new(software_spec, versions, compilers) }
let(:compilers) { [:clang, :gcc, :llvm, :gnu] }
let(:software_spec) { SoftwareSpec.new }
let(:cc) { :clang }
let(:versions) do
double(
gcc_4_0_build_version: Version::NULL,
gcc_build_version: Version.create("5666"),
llvm_build_version: Version::NULL,
clang_build_version: Version.create("425"),
)
end
before(:each) do
allow(versions).to receive(:non_apple_gcc_version) do |name|
case name
when "gcc-4.8" then Version.create("4.8.1")
when "gcc-4.7" then Version.create("4.7.1")
else Version::NULL
end
end
end
describe "#compiler" do
it "raises an error if no matching compiler can be found" do
software_spec.fails_with(:clang)
software_spec.fails_with(:llvm)
software_spec.fails_with(:gcc)
software_spec.fails_with(gcc: "4.8")
software_spec.fails_with(gcc: "4.7")
expect { subject.compiler }.to raise_error(CompilerSelectionError)
end
it "defaults to cc" do
expect(subject.compiler).to eq(cc)
end
it "returns gcc if it fails with clang" do
software_spec.fails_with(:clang)
expect(subject.compiler).to eq(:gcc)
end
it "returns clang if it fails with llvm" do
software_spec.fails_with(:llvm)
expect(subject.compiler).to eq(:clang)
end
it "returns clang if it fails with gcc" do
software_spec.fails_with(:gcc)
expect(subject.compiler).to eq(:clang)
end
it "returns clang if it fails with non-Apple gcc" do
software_spec.fails_with(gcc: "4.8")
expect(subject.compiler).to eq(:clang)
end
it "still returns gcc-4.8 if it fails with gcc without a specific version" do
software_spec.fails_with(:clang)
software_spec.fails_with(:gcc)
expect(subject.compiler).to eq("gcc-4.8")
end
it "returns gcc if it fails with clang and llvm" do
software_spec.fails_with(:clang)
software_spec.fails_with(:llvm)
expect(subject.compiler).to eq(:gcc)
end
it "returns clang if it fails with gcc and llvm" do
software_spec.fails_with(:gcc)
software_spec.fails_with(:llvm)
expect(subject.compiler).to eq(:clang)
end
example "returns gcc if it fails with a specific gcc version" do
software_spec.fails_with(:clang)
software_spec.fails_with(gcc: "4.8")
expect(subject.compiler).to eq(:gcc)
end
example "returns a lower version of gcc if it fails with the highest version" do
software_spec.fails_with(:clang)
software_spec.fails_with(:gcc)
software_spec.fails_with(:llvm)
software_spec.fails_with(gcc: "4.8")
expect(subject.compiler).to eq("gcc-4.7")
end
it "prefers gcc" do
software_spec.fails_with(:clang)
software_spec.fails_with(:gcc)
expect(subject.compiler).to eq("gcc-4.8")
end
it "raises an error when gcc is missing" do
allow(versions).to receive(:gcc_build_version).and_return(Version::NULL)
software_spec.fails_with(:clang)
software_spec.fails_with(:llvm)
software_spec.fails_with(gcc: "4.8")
software_spec.fails_with(gcc: "4.7")
expect { subject.compiler }.to raise_error(CompilerSelectionError)
end
it "raises an error when llvm and gcc are missing" do
allow(versions).to receive(:gcc_build_version).and_return(Version::NULL)
software_spec.fails_with(:clang)
software_spec.fails_with(gcc: "4.8")
software_spec.fails_with(gcc: "4.7")
expect { subject.compiler }.to raise_error(CompilerSelectionError)
end
end
end

View File

@ -1,117 +0,0 @@
require "testing_env"
require "compilers"
require "software_spec"
class CompilerSelectorTests < Homebrew::TestCase
class Double < SoftwareSpec
def <<(cc)
fails_with(cc)
self
end
end
class CompilerVersions
attr_accessor :gcc_4_0_build_version, :gcc_build_version,
:clang_build_version
def initialize
@gcc_4_0_build_version = Version::NULL
@gcc_build_version = Version.create("5666")
@llvm_build_version = Version::NULL
@clang_build_version = Version.create("425")
end
def non_apple_gcc_version(name)
case name
when "gcc-4.8" then Version.create("4.8.1")
when "gcc-4.7" then Version.create("4.7.1")
else Version::NULL
end
end
end
def setup
super
@f = Double.new
@cc = :clang
@versions = CompilerVersions.new
@selector = CompilerSelector.new(
@f, @versions, [:clang, :gcc, :llvm, :gnu]
)
end
def actual_cc
@selector.compiler
end
def test_all_compiler_failures
@f << :clang << :llvm << :gcc << { gcc: "4.8" } << { gcc: "4.7" }
assert_raises(CompilerSelectionError) { actual_cc }
end
def test_no_compiler_failures
assert_equal @cc, actual_cc
end
def test_fails_with_clang
@f << :clang
assert_equal :gcc, actual_cc
end
def test_fails_with_llvm
@f << :llvm
assert_equal :clang, actual_cc
end
def test_fails_with_gcc
@f << :gcc
assert_equal :clang, actual_cc
end
def test_fails_with_non_apple_gcc
@f << { gcc: "4.8" }
assert_equal :clang, actual_cc
end
def test_mixed_failures_1
@f << :clang << :gcc
assert_equal "gcc-4.8", actual_cc
end
def test_mixed_failures_2
@f << :clang << :llvm
assert_equal :gcc, actual_cc
end
def test_mixed_failures_3
@f << :gcc << :llvm
assert_equal :clang, actual_cc
end
def test_mixed_failures_4
@f << :clang << { gcc: "4.8" }
assert_equal :gcc, actual_cc
end
def test_mixed_failures_5
@f << :clang << :gcc << :llvm << { gcc: "4.8" }
assert_equal "gcc-4.7", actual_cc
end
def test_gcc_precedence
@f << :clang << :gcc
assert_equal "gcc-4.8", actual_cc
end
def test_missing_gcc
@versions.gcc_build_version = Version::NULL
@f << :clang << :llvm << { gcc: "4.8" } << { gcc: "4.7" }
assert_raises(CompilerSelectionError) { actual_cc }
end
def test_missing_llvm_and_gcc
@versions.gcc_build_version = Version::NULL
@f << :clang << { gcc: "4.8" } << { gcc: "4.7" }
assert_raises(CompilerSelectionError) { actual_cc }
end
end