aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorJack Nagel2013-10-25 17:29:36 -0500
committerJack Nagel2013-10-25 17:29:45 -0500
commit08d52978c2ca2e4ecf1dce2ac2c9a6547c6bc7f8 (patch)
tree77f87525f2dc2ad44d69c85a84c778590977a026 /Library
parent4e6f20272a514d594044790fda7d759d7bac07a2 (diff)
downloadhomebrew-08d52978c2ca2e4ecf1dce2ac2c9a6547c6bc7f8.tar.bz2
Disallow initializing Versions with non-strings
Closes #23553.
Diffstat (limited to 'Library')
-rw-r--r--Library/Homebrew/test/test_versions.rb13
-rw-r--r--Library/Homebrew/version.rb7
2 files changed, 19 insertions, 1 deletions
diff --git a/Library/Homebrew/test/test_versions.rb b/Library/Homebrew/test/test_versions.rb
index 927cd2bac..c4fbb86f3 100644
--- a/Library/Homebrew/test/test_versions.rb
+++ b/Library/Homebrew/test/test_versions.rb
@@ -1,6 +1,19 @@
require 'testing_env'
require 'version'
+class VersionTests < Test::Unit::TestCase
+ def test_accepts_objects_responding_to_to_str
+ value = stub(:to_str => '0.1')
+ assert_equal '0.1', Version.new(value).to_s
+ end
+
+ def test_raises_for_non_string_objects
+ assert_raises(TypeError) { Version.new(1.1) }
+ assert_raises(TypeError) { Version.new(1) }
+ assert_raises(TypeError) { Version.new(:symbol) }
+ end
+end
+
class VersionComparisonTests < Test::Unit::TestCase
include VersionAssertions
diff --git a/Library/Homebrew/version.rb b/Library/Homebrew/version.rb
index 6c91bec4a..4c268639d 100644
--- a/Library/Homebrew/version.rb
+++ b/Library/Homebrew/version.rb
@@ -163,7 +163,12 @@ class Version
end
def initialize(val, detected=false)
- @version = val.to_s
+ if val.respond_to?(:to_str)
+ @version = val.to_str
+ else
+ raise TypeError, "Version value must be a string"
+ end
+
@detected_from_url = detected
end