Convert Language::Python test to spec.

This commit is contained in:
Markus Reiter 2017-02-18 05:30:13 +01:00
parent 61a41d0da6
commit 9e0c0a8b3a
2 changed files with 106 additions and 96 deletions

View File

@ -0,0 +1,106 @@
require "language/python"
require "resource"
describe Language::Python::Virtualenv::Virtualenv do
subject { described_class.new(formula, dir, "python") }
let(:dir) { @dir = Pathname.new(Dir.mktmpdir) }
let(:resource) { double("resource", stage: true) }
let(:formula_bin) { dir/"formula_bin" }
let(:formula) { double("formula", resource: resource, bin: formula_bin) }
after(:each) { dir.rmtree unless @dir.nil? }
describe "#create" do
it "creates a virtual environment" do
expect(formula).to receive(:resource).with("homebrew-virtualenv").and_return(resource)
subject.create
end
specify "virtual environment creation is idempotent" do
expect(formula).to receive(:resource).with("homebrew-virtualenv").and_return(resource)
subject.create
FileUtils.mkdir_p dir/"bin"
FileUtils.touch dir/"bin/python"
subject.create
FileUtils.rm dir/"bin/python"
end
end
describe "#pip_install" do
it "accepts a string" do
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "-v", "--no-deps",
"--no-binary", ":all:", "--ignore-installed", "foo")
.and_return(true)
subject.pip_install "foo"
end
it "accepts a multi-line strings" do
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "-v", "--no-deps",
"--no-binary", ":all:", "--ignore-installed", "foo", "bar")
.and_return(true)
subject.pip_install <<-EOS.undent
foo
bar
EOS
end
it "accepts an array" do
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "-v", "--no-deps",
"--no-binary", ":all:", "--ignore-installed", "foo")
.and_return(true)
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "-v", "--no-deps",
"--no-binary", ":all:", "--ignore-installed", "bar")
.and_return(true)
subject.pip_install ["foo", "bar"]
end
it "accepts a Resource" do
res = Resource.new("test")
expect(res).to receive(:stage).and_yield
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "-v", "--no-deps",
"--no-binary", ":all:", "--ignore-installed", Pathname.pwd)
.and_return(true)
subject.pip_install res
end
end
describe "#pip_install_and_link" do
let(:src_bin) { dir/"bin" }
let(:dest_bin) { formula.bin }
it "can link scripts" do
src_bin.mkpath
expect(src_bin/"kilroy").not_to exist
expect(dest_bin/"kilroy").not_to exist
FileUtils.touch src_bin/"irrelevant"
bin_before = Dir.glob(src_bin/"*")
FileUtils.touch src_bin/"kilroy"
bin_after = Dir.glob(src_bin/"*")
expect(subject).to receive(:pip_install).with("foo")
expect(Dir).to receive(:[]).with(src_bin/"*").twice.and_return(bin_before, bin_after)
subject.pip_install_and_link "foo"
expect(src_bin/"kilroy").to exist
expect(dest_bin/"kilroy").to exist
expect(dest_bin/"kilroy").to be_a_symlink
expect((src_bin/"kilroy").realpath).to eq((dest_bin/"kilroy").realpath)
expect(dest_bin/"irrelevant").not_to exist
end
end
end

View File

@ -1,96 +0,0 @@
require "testing_env"
require "language/python"
require "resource"
class LanguagePythonTests < Homebrew::TestCase
def setup
super
@dir = Pathname.new(mktmpdir)
resource = stub("resource", stage: true)
formula_bin = @dir/"formula_bin"
@formula = mock("formula") do
stubs(:resource).returns(resource)
stubs(:bin).returns(formula_bin)
end
@venv = Language::Python::Virtualenv::Virtualenv.new(@formula, @dir, "python")
end
def test_virtualenv_creation
@formula.expects(:resource).with("homebrew-virtualenv").returns(
mock("resource", stage: true),
)
@venv.create
end
# or at least doesn't crash the second time
def test_virtualenv_creation_is_idempotent
@formula.expects(:resource).with("homebrew-virtualenv").returns(
mock("resource", stage: true),
)
@venv.create
FileUtils.mkdir_p @dir/"bin"
FileUtils.touch @dir/"bin/python"
@venv.create
FileUtils.rm @dir/"bin/python"
end
def test_pip_install_accepts_string
@formula.expects(:system).returns(true).with do |*params|
params.first == @dir/"bin/pip" && params.last == "foo"
end
@venv.pip_install "foo"
end
def test_pip_install_accepts_multiline_string
@formula.expects(:system).returns(true).with do |*params|
params.first == @dir/"bin/pip" && params[-2..-1] == ["foo", "bar"]
end
@venv.pip_install <<-EOS.undent
foo
bar
EOS
end
def test_pip_install_accepts_array
@formula.expects(:system).returns(true).with do |*params|
params.first == @dir/"bin/pip" && params.last == "foo"
end
@formula.expects(:system).returns(true).with do |*params|
params.first == @dir/"bin/pip" && params.last == "bar"
end
@venv.pip_install ["foo", "bar"]
end
def test_pip_install_accepts_resource
res = Resource.new "test"
res.expects(:stage).yields(nil)
@formula.expects(:system).returns(true).with do |*params|
params.first == @dir/"bin/pip" && params.last == Pathname.pwd
end
@venv.pip_install res
end
def test_pip_install_and_link_links_scripts
bin = @dir/"bin"
bin.mkpath
dest = @formula.bin
refute_predicate bin/"kilroy", :exist?
refute_predicate dest/"kilroy", :exist?
FileUtils.touch bin/"irrelevant"
bin_before = Dir[bin/"*"]
FileUtils.touch bin/"kilroy"
bin_after = Dir[bin/"*"]
@venv.expects(:pip_install).with("foo")
Dir.expects(:[]).twice.returns(bin_before, bin_after)
@venv.pip_install_and_link "foo"
assert_predicate bin/"kilroy", :exist?
assert_predicate dest/"kilroy", :exist?
assert_predicate dest/"kilroy", :symlink?
assert_equal((bin/"kilroy").realpath, (dest/"kilroy").realpath)
refute_predicate dest/"irrelevant", :exist?
end
end