Add tests for FormulaPin and simplify implementation

This commit is contained in:
Jack Nagel 2013-04-16 01:43:26 -05:00
parent d600d6c0be
commit 3a0726406b
6 changed files with 90 additions and 34 deletions

View File

@ -6,11 +6,15 @@ module Homebrew extend self
abort "Cowardly refusing to `sudo pin'"
end
raise FormulaUnspecifiedError if ARGV.named.empty?
ARGV.formulae.each do |fmla|
f = Formula.factory(fmla.to_s)
onoe "Cannot pin uninstalled formula #{f.name}!" unless f.pinable?
opoo "Formula #{f.name} already pinned!" if f.pinable? and f.pinned?
f.pin if f.pinable? and not f.pinned?
ARGV.formulae.each do |f|
if f.pinned?
opoo "#{f.name} already pinned"
elsif !f.pinnable?
onoe "#{f.name} not installed"
else
f.pin
end
end
end
end

View File

@ -6,11 +6,15 @@ module Homebrew extend self
abort "Cowardly refusing to `sudo unpin'"
end
raise FormulaUnspecifiedError if ARGV.named.empty?
ARGV.formulae.each do |fmla|
f = Formula.factory(fmla.to_s)
onoe "Cannot unpin uninstalled formula #{f.name}!" unless f.pinable?
opoo "Formula #{f.name} already unpinned!" if f.pinable? and not f.pinned?
f.unpin if f.pinable? and f.pinned?
ARGV.formulae.each do |f|
if f.pinned?
f.unpin
elsif !f.pinnable?
onoe "#{f.name} not installed"
else
opoo "#{f.name} not pinned"
end
end
end
end

View File

@ -99,8 +99,8 @@ class Formula
(dir = installed_prefix).directory? && dir.children.length > 0
end
def pinable?
@pin.pinable?
def pinnable?
@pin.pinnable?
end
def pinned?

View File

@ -1,38 +1,37 @@
require 'fileutils'
class FormulaPin
HOMEBREW_PINNED = HOMEBREW_LIBRARY+'PinnedKegs'
PINDIR = Pathname.new("#{HOMEBREW_LIBRARY}/PinnedKegs")
def initialize(formula)
@formula = formula
@name = formula.name
def initialize(f)
@f = f
end
def path
HOMEBREW_PINNED+@name
Pathname.new("#{PINDIR}/#{@f.name}")
end
def pin_at(version)
HOMEBREW_PINNED.mkpath unless HOMEBREW_PINNED.exist?
version_path = @formula.installed_prefix.parent.join(version)
FileUtils.ln_s version_path, path unless pinned? or not version_path.exist?
PINDIR.mkpath unless PINDIR.exist?
version_path = @f.rack.join(version)
FileUtils.ln_s(version_path, path) unless pinned? or not version_path.exist?
end
def pin
versions = @formula.installed_prefix.parent.children.map { |item| item.basename.to_s }
versions = @f.rack.children.map { |item| item.basename.to_s }
version = versions.map { |item| Version.new(item) }.sort[0].to_s
pin_at(version)
end
def unpin
FileUtils.rm path if pinned?
FileUtils.rm(path) if pinned?
end
def pinned?
path.symlink?
end
def pinable?
@formula.installed_prefix.parent.children.length > 0
def pinnable?
@f.rack.exist? && @f.rack.children.length > 0
end
end

View File

@ -106,19 +106,19 @@ class FormulaTests < Test::Unit::TestCase
assert_instance_of HeadSoftwareSpec, f.head
end
def test_formula_funcs
foobar = 'foo-bar'
path = Formula.path(foobar)
def test_path
name = 'foo-bar'
assert_equal Pathname.new("#{HOMEBREW_REPOSITORY}/Library/Formula/#{name}.rb"), Formula.path(name)
end
assert_match Regexp.new("^#{HOMEBREW_PREFIX}/Library/Formula"),
path.to_s
path = HOMEBREW_PREFIX+"Library/Formula/#{foobar}.rb"
def test_factory
name = 'foo-bar'
path = HOMEBREW_PREFIX+"Library/Formula/#{name}.rb"
path.dirname.mkpath
File.open(path, 'w') do |f|
f << %{
require 'formula'
class #{Formula.class_s(foobar)} < Formula
class #{Formula.class_s(name)} < Formula
url 'foo-1.0'
def initialize(*args)
@homepage = 'http://example.com/'
@ -127,7 +127,8 @@ class FormulaTests < Test::Unit::TestCase
end
}
end
assert_not_nil Formula.factory(foobar)
assert_kind_of Formula, Formula.factory(name)
ensure
path.unlink
end
end

View File

@ -0,0 +1,48 @@
require 'testing_env'
require 'formula_pin'
class FormulaPinTests < Test::Unit::TestCase
class FormulaDouble
def name
"double"
end
def rack
Pathname.new("#{HOMEBREW_CELLAR}/#{name}")
end
end
def setup
@f = FormulaDouble.new
@pin = FormulaPin.new(@f)
@f.rack.mkpath
end
def test_not_pinnable
assert !@pin.pinnable?
end
def test_pinnable_if_kegs_exist
(@f.rack+'0.1').mkpath
assert @pin.pinnable?
end
def test_pin
(@f.rack+'0.1').mkpath
@pin.pin
assert @pin.pinned?
assert_equal 1, FormulaPin::PINDIR.children.length
end
def test_unpin
(@f.rack+'0.1').mkpath
@pin.pin
@pin.unpin
assert !@pin.pinned?
assert_equal 0, FormulaPin::PINDIR.children.length
end
def teardown
@f.rack.rmtree
end
end