FIX: Broken spec

FIX: Broken spec
diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb
index a80103c..01f870b 100644
--- a/app/models/optimized_image.rb
+++ b/app/models/optimized_image.rb
@@ -174,7 +174,20 @@ class OptimizedImage < ActiveRecord::Base
   IM_DECODERS ||= /\A(jpe?g|png|tiff?|bmp|ico|gif)\z/i
 
   def self.prepend_decoder!(path, ext_path = nil, opts = nil)
-    extension = File.extname((opts && opts[:filename]) || path || ext_path)[1..-1]
+    opts ||= {}
+
+    # This logic is a little messy but the result of using mocks for most
+    # of the image tests. The idea here is you shouldn't trust the "original"
+    # path of a file to figure out its extension. However, in certain cases
+    # such as generating the loading upload thumbnail, we force the format,
+    # and this allows us to use the forced format in that case.
+    extension = nil
+    if (opts[:format] && path != ext_path)
+      extension = File.extname(path)[1..-1]
+    else
+      extension = File.extname(opts[:filename] || ext_path || path)[1..-1]
+    end
+
     raise Discourse::InvalidAccess if !extension || !extension.match?(IM_DECODERS)
     "#{extension}:#{path}"
   end
diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb
index 6e64ee8..aba1b21 100644
--- a/spec/models/upload_spec.rb
+++ b/spec/models/upload_spec.rb
@@ -43,7 +43,6 @@ describe Upload do
       upload.reload
       expect(upload.optimized_images.count).to eq(1)
     end
-
   end
 
   it "can reconstruct dimensions on demand" do

GitHub
sha: 6080e3a2

I think we don’t need this assignment here because the following conditional is making an assignment either way.

1 Like

I was trying to fix the mocks in our test but replace the entire addition here with extension = File.extname((opts && opts[:filename]) || ext_path || path)[1..-1] doesn’t result in any broken test case. Was wondering if I’m missed something here.

I think we can get away with even less code :wink:

File.extname(opts&.dig(:filename) || ext_path || path)[1..-1]
2 Likes