diff options
| author | Jack Nagel | 2013-01-19 20:45:58 -0600 |
|---|---|---|
| committer | Jack Nagel | 2013-01-21 17:24:12 -0600 |
| commit | b48e89cbb2a14a935626c64f5e19044bc2b08d85 (patch) | |
| tree | f04776f4f9052aa47145eb2f9bcf1c3e2fb4e450 /Library | |
| parent | 3f1eb3d18a417589f91c6aee4effe750440e2adc (diff) | |
| download | homebrew-b48e89cbb2a14a935626c64f5e19044bc2b08d85.tar.bz2 | |
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
Diffstat (limited to 'Library')
| -rw-r--r-- | Library/Homebrew/dependencies.rb | 57 | ||||
| -rw-r--r-- | Library/Homebrew/test/test_requirement.rb | 40 | ||||
| -rw-r--r-- | 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 54de50f39..adf36c4d9 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 28e2afae7..0f1cc02de 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 2380400ea..7206a71f0 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 } |
