From a0e8b871c1cfb1a3eebeaa06b90f2695274a6986 Mon Sep 17 00:00:00 2001 From: Colin Seymour Date: Tue, 4 Jun 2019 10:18:29 +0100 Subject: [PATCH] Force name to be string when looking up mime type (#4545) * Always pass name as string, even if nil * Update comment * Add nil filename test --- lib/linguist/blob_helper.rb | 4 ++-- test/test_file_blob.rb | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb index 82d1d09cfb..09e59c1b5a 100644 --- a/lib/linguist/blob_helper.rb +++ b/lib/linguist/blob_helper.rb @@ -26,14 +26,14 @@ def extname File.extname(name.to_s) end - # Internal: Lookup mime type for extension. + # Internal: Lookup mime type for filename. # # Returns a MIME::Type def _mime_type if defined? @_mime_type @_mime_type else - @_mime_type = MiniMime.lookup_by_filename(name) + @_mime_type = MiniMime.lookup_by_filename(name.to_s) end end diff --git a/test/test_file_blob.rb b/test/test_file_blob.rb index 6d189005c9..20e6c1e246 100644 --- a/test/test_file_blob.rb +++ b/test/test_file_blob.rb @@ -49,6 +49,9 @@ def test_mime_type assert_equal "application/xml", sample_blob("XML/bar.xml").mime_type assert_equal "audio/ogg", fixture_blob("Binary/foo.ogg").mime_type assert_equal "text/plain", fixture_blob("Data/README").mime_type + # GitHub doesn't use a filename when returning raw blobs + blob = Struct.new(:name) { include Linguist::BlobHelper } + assert_equal "text/plain", blob.new(nil).mime_type end def test_content_type