File: test_mime_types_cache.rb

package info (click to toggle)
ruby-mime-types 3.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 300 kB
  • sloc: ruby: 1,875; makefile: 6
file content (118 lines) | stat: -rw-r--r-- 3,327 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# frozen_string_literal: true

require "mime/types"
require "minitest_helper"

MUTEX = Mutex.new

describe MIME::Types::Cache do
  include Minitest::Hooks

  def around
    require "fileutils"

    MUTEX.synchronize do
      @cache_file = File.expand_path("../cache.tst", __FILE__)
      ENV["RUBY_MIME_TYPES_CACHE"] = @cache_file
      clear_cache_file

      super

      clear_cache_file
      ENV.delete("RUBY_MIME_TYPES_CACHE")
    end
  end

  def reset_mime_types
    MIME::Types.instance_variable_set(:@__types__, nil)
    MIME::Types.send(:load_default_mime_types)
  end

  def clear_cache_file
    FileUtils.rm @cache_file if File.exist? @cache_file
  end

  describe ".load" do
    it "does not use cache when RUBY_MIME_TYPES_CACHE is unset" do
      ENV.delete("RUBY_MIME_TYPES_CACHE")
      assert_nil MIME::Types::Cache.load
    end

    it "does not use cache when missing" do
      assert_nil MIME::Types::Cache.load
    end

    it "registers the data to be updated by #add_extensions" do
      MIME::Types::Cache.save
      reset_mime_types
      assert_equal([], MIME::Types.type_for("foo.additional"))
      html = MIME::Types["text/html"][0]
      html.add_extensions("additional")
      assert_equal([html], MIME::Types.type_for("foo.additional"))
    end

    it "outputs an error when there is an invalid version" do
      v = MIME::Types::Data::VERSION
      MIME::Types::Data.send(:remove_const, :VERSION)
      MIME::Types::Data.const_set(:VERSION, "0.0")
      MIME::Types::Cache.save
      MIME::Types::Data.send(:remove_const, :VERSION)
      MIME::Types::Data.const_set(:VERSION, v)
      MIME::Types.instance_variable_set(:@__types__, nil)
      assert_output "", /MIME::Types cache: invalid version/ do
        MIME::Types["text/html"]
      end
    end

    it "outputs an error when there is a marshal file incompatibility" do
      MIME::Types::Cache.save
      data = File.binread(@cache_file).reverse
      File.binwrite(@cache_file, data)
      MIME::Types.instance_variable_set(:@__types__, nil)
      assert_output "", /incompatible marshal file format/ do
        MIME::Types["text/html"]
      end
    end
  end

  describe ".save" do
    it "does not create cache when RUBY_MIME_TYPES_CACHE is unset" do
      ENV.delete("RUBY_MIME_TYPES_CACHE")
      assert_nil MIME::Types::Cache.save
    end

    it "creates the cache " do
      assert_equal(false, File.exist?(@cache_file))
      MIME::Types::Cache.save
      assert_equal(true, File.exist?(@cache_file))
    end

    it "uses the cache" do
      MIME::Types["text/html"].first.add_extensions("hex")
      MIME::Types::Cache.save
      MIME::Types.instance_variable_set(:@__types__, nil)

      assert_includes MIME::Types["text/html"].first.extensions, "hex"

      reset_mime_types
    end
  end
end

describe MIME::Types::Container do
  it "marshals and unmarshals correctly" do
    container = MIME::Types::Container.new
    container.add("xyz", "abc")

    # default proc should return Set[]
    assert_equal(Set[], container["abc"])
    assert_equal(Set["abc"], container["xyz"])

    marshalled = Marshal.dump(container)
    loaded_container = Marshal.load(marshalled)

    # default proc should still return Set[]
    assert_equal(Set[], loaded_container["abc"])
    assert_equal(Set["abc"], container["xyz"])
  end
end