Allow satisfied? to be specified in a block

Instead of overriding #satisfied?, Requirement subclasses can specify
the condition in a block:

  satisfy do
    some_condition?
  end

The contents of the block are evaluated in the context of the instance,
and so have access to instance variables and instance methods as before.
Additionally, it is wrapped in an ENV.with_build_environment block. This
can be disabled by passing :build_env => false to satisfy:

  satisfy :build_env => false do
    some_condition?
  end
This commit is contained in:
Jack Nagel 2013-01-19 20:45:58 -06:00
parent 09d4a7fb36
commit 452e79cf68
3 changed files with 94 additions and 9 deletions

View File

@ -178,19 +178,31 @@ class Requirement
@tags = tags.flatten.compact
end
# Should return true if this requirement is met.
def satisfied?; false; end
# Should return true if not meeting this requirement should fail the build.
# The message to show when the requirement is not met.
def message; "" end
# Overriding #satisfied? is deprepcated.
# Pass a block or boolean to the satisfied DSL method instead.
def satisfied?
self.class.satisfy.yielder do |proc|
instance_eval(&proc)
end
rescue NoMethodError
self.class.satisfy || false
rescue ArgumentError
false
end
# Overriding #fatal? is deprecated.
# Pass a boolean to the fatal DSL method instead.
def fatal?
self.class.fatal || false
end
# The message to show when the requirement is not met.
def message; ""; end
# Overriding modify_build_environment is deprecated, pass a block to
# the env DSL method instead.
# Overriding #modify_build_environment is deprecated.
# Pass a block to the the env DSL method instead.
def modify_build_environment
env.modify_build_environment(self)
satisfied? and env.modify_build_environment(self)
end
def env
@ -209,6 +221,35 @@ class Requirement
def fatal(val=nil)
val.nil? ? @fatal : @fatal = val
end
def satisfy(options={}, &block)
if block_given?
options[:userpaths] = true if env.userpaths?
@satisfied ||= Requirement::Satisfier.new(options, &block)
else
@satisfied ||= options
end
end
end
class Satisfier
def initialize(options={}, &block)
@options = { :build_env => true }
@options.merge!(options)
@proc = block
end
def yielder
if @options[:build_env]
require 'superenv'
ENV.with_build_environment do
ENV.userpaths! if @options[:userpaths]
yield @proc
end
else
yield @proc
end
end
end
end

View File

@ -30,4 +30,44 @@ class RequirementTests < Test::Unit::TestCase
req = Class.new(Requirement) { fatal true }.new
assert req.fatal?
end
def test_satisfy_true
req = Class.new(Requirement) do
satisfy(:build_env => false) { true }
end.new
assert req.satisfied?
end
def test_satisfy_false
req = Class.new(Requirement) do
satisfy(:build_env => false) { false }
end.new
assert !req.satisfied?
end
def test_satisfy_with_userpaths_from_env
ENV.expects(:with_build_environment).yields.returns(true)
ENV.expects(:userpaths!)
req = Class.new(Requirement) do
env :userpaths
satisfy(:build_env => true) { true }
end.new
assert req.satisfied?
end
def test_satisfy_with_userpaths_from_options
ENV.expects(:with_build_environment).yields.returns(true)
ENV.expects(:userpaths!)
req = Class.new(Requirement) do
satisfy(:build_env => true, :userpaths => true) { true }
end.new
assert req.satisfied?
end
def test_satisfy_with_boolean
req = Class.new(Requirement) do
satisfy true
end.new
assert req.satisfied?
end
end

View File

@ -31,7 +31,11 @@ MACOS = true
MACOS_VERSION = 10.6
MACOS_FULL_VERSION = '10.6.8'
(HOMEBREW_PREFIX+'Library/Formula').mkpath
%w{Library/Formula Library/ENV}.each do |d|
HOMEBREW_REPOSITORY.join(d).mkpath
end
ORIGINAL_PATHS = ENV['PATH'].split(':').map{ |p| Pathname.new(p).expand_path rescue nil }.compact.freeze
at_exit { HOMEBREW_PREFIX.parent.rmtree }