From 452e79cf6866ba2bf60110881680bc811aa8308e Mon Sep 17 00:00:00 2001 From: Jack Nagel Date: Sat, 19 Jan 2013 20:45:58 -0600 Subject: [PATCH] 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 --- Library/Homebrew/dependencies.rb | 57 +++++++++++++++++++---- Library/Homebrew/test/test_requirement.rb | 40 ++++++++++++++++ Library/Homebrew/test/testing_env.rb | 6 ++- 3 files changed, 94 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/dependencies.rb b/Library/Homebrew/dependencies.rb index 54de50f390..adf36c4d94 100644 --- a/Library/Homebrew/dependencies.rb +++ b/Library/Homebrew/dependencies.rb @@ -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 diff --git a/Library/Homebrew/test/test_requirement.rb b/Library/Homebrew/test/test_requirement.rb index 28e2afae71..0f1cc02deb 100644 --- a/Library/Homebrew/test/test_requirement.rb +++ b/Library/Homebrew/test/test_requirement.rb @@ -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 diff --git a/Library/Homebrew/test/testing_env.rb b/Library/Homebrew/test/testing_env.rb index 2380400eaa..7206a71f06 100644 --- a/Library/Homebrew/test/testing_env.rb +++ b/Library/Homebrew/test/testing_env.rb @@ -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 }