Skip to content

Commit 1b2bbc3

Browse files
committed
Fix dialyzer warnings and test race condition
1 parent fcf1b04 commit 1b2bbc3

4 files changed

Lines changed: 23 additions & 6 deletions

File tree

lib/image_ocr/input.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ defmodule ImageOcr.Input do
5454
end
5555

5656
def to_vimage(charlist) when is_list(charlist) do
57-
case List.to_string(charlist) do
58-
path when is_binary(path) -> to_vimage(path)
59-
_ -> {:error, {:unsupported_input, :invalid_charlist}}
60-
end
57+
to_vimage(List.to_string(charlist))
58+
rescue
59+
ArgumentError -> {:error, {:unsupported_input, :invalid_charlist}}
6160
end
6261

6362
def to_vimage(other), do: {:error, {:unsupported_input, other}}

mix.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule ImageOcr.MixProject do
1818
description: description(),
1919
package: package(),
2020
docs: docs(),
21+
dialyzer: dialyzer(),
2122
source_url: @source_url
2223
]
2324
end
@@ -56,6 +57,16 @@ defmodule ImageOcr.MixProject do
5657
]
5758
end
5859

60+
defp dialyzer do
61+
[
62+
# The mix tasks under lib/mix/tasks/ legitimately call Mix.Task.run/1,
63+
# Mix.shell/0, and Mix.raise/1 — pull :mix into the PLT so dialyzer
64+
# can see those callbacks. :inets / :public_key are used by the
65+
# tessdata HTTP fetcher.
66+
plt_add_apps: [:mix, :inets, :ssl, :public_key, :ex_unit]
67+
]
68+
end
69+
5970
defp docs do
6071
[
6172
main: "readme",

test/image_ocr/concurrency_test.exs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ defmodule ImageOcr.ConcurrencyTest do
5959
end
6060

6161
test "instance is garbage-collected without crashing the VM" do
62+
# Pass :datapath explicitly so this test is not sensitive to other test
63+
# modules transiently mutating the :tessdata_path application env.
64+
datapath = ImageOcr.Tessdata.vendored_path()
65+
6266
Enum.each(1..50, fn _ ->
63-
{:ok, _ocr} = ImageOcr.new()
67+
{:ok, _ocr} = ImageOcr.new(datapath: datapath)
6468
end)
6569

6670
:erlang.garbage_collect()

test/image_ocr/tessdata_test.exs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
defmodule ImageOcr.TessdataTest do
2-
use ExUnit.Case, async: true
2+
# async: false because these tests mutate Application env / OS env
3+
# (`:image_ocr, :tessdata_path` and `TESSDATA_PREFIX`), which is global
4+
# state that would otherwise race ImageOcr.new/1 calls in other modules.
5+
use ExUnit.Case, async: false
36

47
alias ImageOcr.Tessdata
58

0 commit comments

Comments
 (0)