aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Nagel2013-04-16 01:43:26 -0500
committerJack Nagel2013-04-16 01:58:11 -0500
commit3a0726406bf3482cdfb5d992ee29b1aefde363ea (patch)
treeb294568c541f55ed476c274b63924cdebf6cfa91
parentd600d6c0bebaf56ffb47ff7e9e1ac644d36c7a7d (diff)
downloadbrew-3a0726406bf3482cdfb5d992ee29b1aefde363ea.tar.bz2
Add tests for FormulaPin and simplify implementation
-rw-r--r--Library/Homebrew/cmd/pin.rb14
-rw-r--r--Library/Homebrew/cmd/unpin.rb14
-rw-r--r--Library/Homebrew/formula.rb4
-rw-r--r--Library/Homebrew/formula_pin.rb23
-rw-r--r--Library/Homebrew/test/test_formula.rb21
-rw-r--r--Library/Homebrew/test/test_formula_pin.rb48
6 files changed, 90 insertions, 34 deletions
diff --git a/Library/Homebrew/cmd/pin.rb b/Library/Homebrew/cmd/pin.rb
index 26b487a0d..2c0d5d33d 100644
--- a/Library/Homebrew/cmd/pin.rb
+++ b/Library/Homebrew/cmd/pin.rb
@@ -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
diff --git a/Library/Homebrew/cmd/unpin.rb b/Library/Homebrew/cmd/unpin.rb
index 6855db4bf..265f7a941 100644
--- a/Library/Homebrew/cmd/unpin.rb
+++ b/Library/Homebrew/cmd/unpin.rb
@@ -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
diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb
index 62ec065ac..778a9a688 100644
--- a/Library/Homebrew/formula.rb
+++ b/Library/Homebrew/formula.rb
@@ -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?
diff --git a/Library/Homebrew/formula_pin.rb b/Library/Homebrew/formula_pin.rb
index c6740f104..42a644afa 100644
--- a/Library/Homebrew/formula_pin.rb
+++ b/Library/Homebrew/formula_pin.rb
@@ -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
diff --git a/Library/Homebrew/test/test_formula.rb b/Library/Homebrew/test/test_formula.rb
index 22fb2f238..bd900bf71 100644
--- a/Library/Homebrew/test/test_formula.rb
+++ b/Library/Homebrew/test/test_formula.rb
@@ -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)
-
- assert_match Regexp.new("^#{HOMEBREW_PREFIX}/Library/Formula"),
- path.to_s
+ def test_path
+ name = 'foo-bar'
+ assert_equal Pathname.new("#{HOMEBREW_REPOSITORY}/Library/Formula/#{name}.rb"), Formula.path(name)
+ end
- 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
diff --git a/Library/Homebrew/test/test_formula_pin.rb b/Library/Homebrew/test/test_formula_pin.rb
new file mode 100644
index 000000000..938fd51f8
--- /dev/null
+++ b/Library/Homebrew/test/test_formula_pin.rb
@@ -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