Skip to content

Commit d2b4003

Browse files
authored
Merge custom extensions without overriding the default ones (#73)
1 parent 9f3a908 commit d2b4003

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
test:
11-
runs-on: ubuntu-latest
11+
runs-on: ubuntu-20.04
1212
env:
1313
MIX_ENV: test
1414
strategy:

lib/mime.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ defmodule MIME do
134134
end
135135

136136
all_exts = Map.merge(exts, to_exts.(custom_types))
137-
all_types = Map.merge(types, custom_types)
137+
138+
all_types =
139+
Map.merge(types, custom_types, fn _, default_exts, custom_exts ->
140+
Enum.uniq(custom_exts ++ default_exts)
141+
end)
138142

139143
@doc """
140144
Returns the custom types compiled into the MIME module.

test/mime_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,30 @@ defmodule MIMETest do
5454
{expr, val}
5555
end)
5656
end
57+
58+
test "add custom extensions" do
59+
Application.put_env(:mime, :types, %{"text/plain" => ["env"]})
60+
61+
[File.cwd!(), "lib", "mime.ex"]
62+
|> Path.join()
63+
|> Code.compile_file()
64+
65+
assert MIME.extensions("text/plain") == ["env", "txt", "text"]
66+
assert MIME.type("env") == "text/plain"
67+
assert MIME.type("txt") == "text/plain"
68+
assert MIME.type("text") == "text/plain"
69+
end
70+
71+
test "add custom extensions avoiding duplicates" do
72+
Application.put_env(:mime, :types, %{"text/plain" => ["env", "txt", "text"]})
73+
74+
[File.cwd!(), "lib", "mime.ex"]
75+
|> Path.join()
76+
|> Code.compile_file()
77+
78+
assert MIME.extensions("text/plain") == ["env", "txt", "text"]
79+
assert MIME.type("env") == "text/plain"
80+
assert MIME.type("txt") == "text/plain"
81+
assert MIME.type("text") == "text/plain"
82+
end
5783
end

test/test_helper.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
ExUnit.start()
2+
3+
Code.compiler_options(ignore_module_conflict: true)

0 commit comments

Comments
 (0)