python: tweak script linking in virtualenv (#613)

* python: tweak script linking in virtualenv

Instead of making the formula author use a slightly awkward block like

  venv.link_scripts(bin) { venv.pip_install buildpath }

avoid exposing this implementation detail and offer the more familiar:

  venv.pip_install buildpath, :link_scripts => bin

* Add non-block form and use instead of recursion

* Update 'pip_install' documentation

* Remove obsolete 'link_scripts'

* Add test for 'pip_install' with linking scripts

Also drop no longer relevant (and broken) `link_scripts` test, that
served as a template for the new test.

* Restore compatibility with Ruby 1.8.7

* Replace option hash with 'pip_install_and_link'

* Avoid confusing 'Object#tap' and fix silly bug

* Avoid side effects in mock object parameter check

* Simplify argument check (no need for a block)
This commit is contained in:
Martin Afanasjew 2016-08-02 22:37:15 +02:00 committed by Tim D. Smith
parent a6033529cf
commit b77a695b7a
2 changed files with 32 additions and 21 deletions

View File

@ -145,7 +145,7 @@ module Language
def virtualenv_install_with_resources def virtualenv_install_with_resources
venv = virtualenv_create(libexec) venv = virtualenv_create(libexec)
venv.pip_install resources venv.pip_install resources
venv.link_scripts(bin) { venv.pip_install buildpath } venv.pip_install_and_link buildpath
venv venv
end end
@ -218,18 +218,18 @@ module Language
end end
end end
# Compares the venv bin directory before and after executing a block, # Installs packages represented by `targets` into the virtualenv, but
# and symlinks any new scripts into `destination`. # unlike {#pip_install} also links new scripts to {Formula#bin}.
# Use like: venv.link_scripts(bin) { venv.pip_install my_package } # @param (see #pip_install)
# @param destination [Pathname, String] Destination into which new # @return (see #pip_install)
# scripts should be linked. def pip_install_and_link(targets)
# @return [void]
def link_scripts(destination)
bin_before = Dir[@venv_root/"bin/*"].to_set bin_before = Dir[@venv_root/"bin/*"].to_set
yield
pip_install(targets)
bin_after = Dir[@venv_root/"bin/*"].to_set bin_after = Dir[@venv_root/"bin/*"].to_set
destination = Pathname.new(destination) bin_to_link = (bin_after - bin_before).to_a
destination.install_symlink((bin_after - bin_before).to_a) @formula.bin.install_symlink(bin_to_link)
end end
private private

View File

@ -6,8 +6,10 @@ class LanguagePythonTests < Homebrew::TestCase
def setup def setup
@dir = Pathname.new(mktmpdir) @dir = Pathname.new(mktmpdir)
resource = stub("resource", :stage => true) resource = stub("resource", :stage => true)
formula_bin = @dir/"formula_bin"
@formula = mock("formula") do @formula = mock("formula") do
stubs(:resource).returns(resource) stubs(:resource).returns(resource)
stubs(:bin).returns(formula_bin)
end end
@venv = Language::Python::Virtualenv::Virtualenv.new(@formula, @dir, "python") @venv = Language::Python::Virtualenv::Virtualenv.new(@formula, @dir, "python")
end end
@ -71,18 +73,27 @@ class LanguagePythonTests < Homebrew::TestCase
@venv.pip_install res @venv.pip_install res
end end
def test_link_scripts_links_scripts def test_pip_install_and_link_links_scripts
bin = (@dir/"bin") bin = @dir/"bin"
dest = (@dir/"dest")
bin.mkpath bin.mkpath
refute((bin/"kilroy").exist?) dest = @formula.bin
refute((dest/"kilroy").exist?)
refute_predicate bin/"kilroy", :exist?
refute_predicate dest/"kilroy", :exist?
FileUtils.touch bin/"irrelevant" FileUtils.touch bin/"irrelevant"
@venv.link_scripts(dest) { FileUtils.touch bin/"kilroy" } bin_before = Dir[bin/"*"]
assert((bin/"kilroy").exist?) FileUtils.touch bin/"kilroy"
assert((dest/"kilroy").exist?) bin_after = Dir[bin/"*"]
assert((dest/"kilroy").symlink?) @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) assert_equal((bin/"kilroy").realpath, (dest/"kilroy").realpath)
refute((dest/"irrelevant").exist?) refute_predicate dest/"irrelevant", :exist?
end end
end end