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'" abort "Cowardly refusing to `sudo pin'"
end end
raise FormulaUnspecifiedError if ARGV.named.empty? raise FormulaUnspecifiedError if ARGV.named.empty?
ARGV.formulae.each do |fmla|
f = Formula.factory(fmla.to_s) ARGV.formulae.each do |f|
onoe "Cannot pin uninstalled formula #{f.name}!" unless f.pinable? if f.pinned?
opoo "Formula #{f.name} already pinned!" if f.pinable? and f.pinned? opoo "#{f.name} already pinned"
f.pin if f.pinable? and not f.pinned? elsif !f.pinnable?
onoe "#{f.name} not installed"
else
f.pin
end
end end
end end
end end

View File

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

View File

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

View File

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

View File

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