summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAustin Ziegler <austin@zieglers.ca>2018-08-12 11:15:21 -0400
committerAustin Ziegler <austin@zieglers.ca>2018-08-12 11:15:21 -0400
commitedf3cd7ee59590d46ce7ac492cf169a723ca7423 (patch)
treec50540bb938d13d4e72bc59aec218578b0d1b7f1
parentd5d7c57609d90c2113f4d52d29b7218b42236dcb (diff)
downloadmime-types-edf3cd7ee59590d46ce7ac492cf169a723ca7423.tar.gz
Resolve a bug related to the switch to Sets
Fully resolves #117, #127, and #134.
-rw-r--r--History.md10
-rw-r--r--lib/mime/types.rb6
-rw-r--r--lib/mime/types/container.rb6
-rw-r--r--test/test_mime_types.rb6
4 files changed, 21 insertions, 7 deletions
diff --git a/History.md b/History.md
index d910fec..f85ad87 100644
--- a/History.md
+++ b/History.md
@@ -11,10 +11,18 @@
true` to files so that modern Rubies can automatically reduce duplicate
string allocations. [#135][]
-* 1 bug fix
+* 2 bug fixes
* Burke Libbey fixed a problem with cached data loading. [#126][]
+ * Resolved an issue where Enumerable#inject returns +nil+ when provided
+ an empty enumerable and a default value has not been provided. This is
+ because when Enumerable#inject isn't provided a starting value, the
+ first value is used as the default value. In every case where this
+ error was happening, the result was supposed to be an array containing
+ Set objects so they can be reduced to a single Set. [#117][], [#127][],
+ [#134][].
+
* Deprecations:
* Lazy loading (`$RUBY_MIME_TYPES_LAZY_LOAD`) has been deprecated.
diff --git a/lib/mime/types.rb b/lib/mime/types.rb
index e3b9f61..fd56908 100644
--- a/lib/mime/types.rb
+++ b/lib/mime/types.rb
@@ -151,7 +151,7 @@ class MIME::Types
def type_for(filename)
Array(filename).flat_map { |fn|
@extension_index[fn.chomp.downcase[/\.?([^.]*?)$/, 1]]
- }.compact.inject(:+).sort { |a, b|
+ }.compact.inject(Set.new, :+).sort { |a, b|
a.priority_compare(b)
}
end
@@ -171,7 +171,7 @@ class MIME::Types
nil
when MIME::Types
variants = mime_type.instance_variable_get(:@type_variants)
- add(*variants.values.inject(:+).to_a, quiet)
+ add(*variants.values.inject(Set.new, :+).to_a, quiet)
when Array
add(*mime_type, quiet)
else
@@ -218,7 +218,7 @@ Type #{type} is already registered as a variant of #{type.simplified}.
def match(pattern)
@type_variants.select { |k, _|
k =~ pattern
- }.values.inject(:+)
+ }.values.inject(Set.new, :+)
end
end
diff --git a/lib/mime/types/container.rb b/lib/mime/types/container.rb
index 176ae3e..abccc2e 100644
--- a/lib/mime/types/container.rb
+++ b/lib/mime/types/container.rb
@@ -3,9 +3,9 @@
require 'set'
# MIME::Types requires a container Hash with a default values for keys
-# resulting in an empty array (<tt>[]</tt>), but this cannot be dumped through
-# Marshal because of the presence of that default Proc. This class exists
-# solely to satisfy that need.
+# resulting in an empty Set, but this cannot be dumped through Marshal because
+# of the presence of that default Proc. This class exists solely to satisfy
+# that need.
class MIME::Types::Container < Hash # :nodoc:
def initialize
super
diff --git a/test/test_mime_types.rb b/test/test_mime_types.rb
index 4ebb29d..111f771 100644
--- a/test/test_mime_types.rb
+++ b/test/test_mime_types.rb
@@ -85,6 +85,12 @@ describe MIME::Types do
refute_empty mime_types[/gzip/, registered: true]
refute_equal mime_types[/gzip/], mime_types[/gzip/, registered: true]
end
+
+ it 'properly returns an empty result on a regular expression miss' do
+ assert_empty mime_types[/^foo/]
+ assert_empty mime_types[/^foo/, registered: true]
+ assert_empty mime_types[/^foo/, complete: true]
+ end
end
describe '#add' do