Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85630083
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/majic.ex b/lib/majic.ex
index 6f8d530..b4cecbc 100644
--- a/lib/majic.ex
+++ b/lib/majic.ex
@@ -1,51 +1,52 @@
defmodule Majic do
alias Majic.{Once, Pool, Result, Server}
@moduledoc """
Robust libmagic integration for Elixir.
"""
@doc """
Perform on `path`.
An option of `server: ServerName`, `pool: PoolName` or `once: true` must be passed.
"""
@type target :: Path.t() | {:bytes, binary()}
@type result :: {:ok, Result.t()} | {:error, term() | String.t()}
@type name :: {:pool, atom()} | {:server, Server.t()} | {:once, true}
@type option :: name | Server.start_option() | Pool.option()
@spec perform(target(), [option()]) :: result()
def perform(path, opts) do
mod =
cond do
Keyword.has_key?(opts, :pool) -> {Pool, Keyword.get(opts, :pool)}
Keyword.has_key?(opts, :server) -> {Server, Keyword.get(opts, :server)}
Keyword.has_key?(opts, :once) -> {Once, nil}
true -> nil
end
opts =
opts
|> Keyword.drop([:pool, :server, :once])
if mod do
do_perform(mod, path, opts)
else
{:error, :no_method}
end
end
defp do_perform({Server = mod, name}, path, opts) do
timeout = Keyword.get(opts, :timeout, Majic.Config.default_process_timeout())
mod.perform(name, path, timeout)
end
defp do_perform({Once = mod, _}, path, opts) do
- mod.perform(path, opts)
+ timeout = Keyword.get(opts, :timeout, Majic.Config.default_process_timeout())
+ mod.perform(path, opts, timeout)
end
defp do_perform({Pool = mod, name}, path, opts) do
mod.perform(name, path, opts)
end
end
diff --git a/test/majic/helpers_test.exs b/test/majic/helpers_test.exs
index e645baf..a76436e 100644
--- a/test/majic/helpers_test.exs
+++ b/test/majic/helpers_test.exs
@@ -1,22 +1,27 @@
defmodule Majic.OnceTest do
use Majic.MagicCase
doctest Majic.Once
test "perform" do
path = absolute_path("Makefile")
assert {:ok, %{mime_type: "text/x-makefile"}} = Majic.Once.perform(path)
end
test "Majic.perform" do
path = absolute_path("Makefile")
assert {:ok, %{mime_type: "text/x-makefile"}} = Majic.perform(path, once: true)
end
+ test "Majic.perform once path passes timeout option" do
+ path = absolute_path("Makefile")
+ assert {:ok, %{mime_type: "text/x-makefile"}} = Majic.perform(path, once: true, timeout: 5000)
+ end
+
test "perform does not leak server on error" do
before = Process.list()
assert {:error, :enoent} = Majic.Once.perform(missing_filename())
after_call = Process.list()
leaked = after_call -- before
assert leaked == [], "Leaked processes: #{inspect(leaked)}"
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Aug 9, 9:55 AM (1 d, 17 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1722882
Default Alt Text
(2 KB)
Attached To
Mode
R20 majic
Attached
Detach File
Event Timeline
Log In to Comment