diff options
| author | Jack Nagel | 2013-04-13 17:40:12 -0500 | 
|---|---|---|
| committer | Jack Nagel | 2013-04-13 17:40:12 -0500 | 
| commit | 563d8be6bde6eaddcf4eb8d4fb9a10ed0af7ddb7 (patch) | |
| tree | 26d2ddf590ec47e3a51ebd056863a5d5e5aa9c8f /Library/Homebrew/test/test_formula_validation.rb | |
| parent | 5ba5e215363d0f4737f9004f6e699a2b18eb0761 (diff) | |
| download | homebrew-563d8be6bde6eaddcf4eb8d4fb9a10ed0af7ddb7.tar.bz2 | |
Improved formula attribute validation
The initializer for Formula does a number of validations, but it does
them in a weird order, and some attributes aren't validated under
certain circumstances. This became even more of a mess when most
software package attributes were moved into the SoftwareSpec class.
This commit removes the last vestiges of storing these attributes as
instance variables. In particular, it eliminates #set_instance_variable
and #validate_variable, replacing them with methods that operate on
SoftwareSpec instances, and generate more useful errors.
Doing these validations unconditionally in the initializer means we bail
out much earlier if the formula has invalid attributes or is not fully
specified, and no longer need to validate in #prefix.
Technically we don't need to validate in #brew either, but we continue
to do so anyway as a safety measure, and because we cannot enforce calls
to super in subclasses.
Diffstat (limited to 'Library/Homebrew/test/test_formula_validation.rb')
| -rw-r--r-- | Library/Homebrew/test/test_formula_validation.rb | 69 | 
1 files changed, 69 insertions, 0 deletions
| diff --git a/Library/Homebrew/test/test_formula_validation.rb b/Library/Homebrew/test/test_formula_validation.rb new file mode 100644 index 000000000..be4c38c64 --- /dev/null +++ b/Library/Homebrew/test/test_formula_validation.rb @@ -0,0 +1,69 @@ +require 'testing_env' +require 'formula' + +class FormulaValidationTests < Test::Unit::TestCase +  def formula(*args, &block) +    Class.new(Formula, &block).new(*args) +  end + +  def assert_invalid(attr, &block) +    e = assert_raises(FormulaValidationError, &block) +    assert_equal attr, e.attr +  end + +  def test_cant_override_brew +    assert_raises(RuntimeError) do +      Class.new(Formula) { def brew; end } +    end +  end + +  def test_validates_name +    assert_invalid :name do +      formula "name with spaces" do +        url "foo" +        version "1.0" +      end +    end +  end + +  def test_validates_url +    assert_invalid :url do +      formula do +        url "" +        version "1" +      end +    end +  end + +  def test_validates_version +    assert_invalid :version do +      formula do +        url "foo" +        version "version with spaces" +      end +    end + +    assert_invalid :version do +      formula do +        url "foo" +        version "" +      end +    end +  end + +  def test_validates_when_initialize_overridden +    assert_invalid :name do +      formula do +        def initialize; end +      end.brew {} +    end +  end + +  def test_head_only_valid +    assert_nothing_raised do +      formula do +        head "foo" +      end +    end +  end +end | 
