summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJames Every <devstopfix@gmail.com>2020-03-25 16:19:15 +0000
committerGitHub <noreply@github.com>2020-03-25 16:19:15 +0000
commit20c4068e5d8bd725c42513301b288094c8857041 (patch)
treebd226f972dfeaed7b11e76158ac1bd2243b89420 /test
parentMerge commit '82cea2a0db4af442a3ea89a340e54fcd11cf8180' (diff)
Convert to ports (#5)
* Remove erlexec dependency * feat: async verify worker has started * test: move infinite to script * fix: timeout response * fix: unix compile [Closes #169398412] * feat: allow database patterns as worker param This allows us to expand paths in an application, which is not possible from the configuration file. * OTP 22 compatible with Elixir 1.7-1.10 * Install make on CI server libmagic-dev contains magic.h Fix this error: ** (Mix) Could not compile with "make" (exit status: 2). * Fix make install * Fix plurality * Fix credo warning * Allow multiple error messages OS X and Linux return different errors messages. * Allow named GenServer processes * Types * Disable test broken in ci Works locally, not in CI. test/gen_magic_test.exs:50 ** (EXIT from #PID<0.1672.0>) shutdown
Diffstat (limited to 'test')
-rw-r--r--test/elixir6
-rw-r--r--test/elixir.mgcbin0 -> 688 bytes
-rw-r--r--test/gen_magic_test.exs100
-rw-r--r--test/soak.exs35
-rw-r--r--test/test_helper.exs6
5 files changed, 131 insertions, 16 deletions
diff --git a/test/elixir b/test/elixir
new file mode 100644
index 0000000..8e62729
--- /dev/null
+++ b/test/elixir
@@ -0,0 +1,6 @@
+#------------------------------------------------------------------------------
+# file: file(1) magic for Elixir
+# URL: https://elixir-lang.org/
+
+0 string/w defmodule Elixir module source text
+!:mime text/x-elixir
diff --git a/test/elixir.mgc b/test/elixir.mgc
new file mode 100644
index 0000000..cafeaee
--- /dev/null
+++ b/test/elixir.mgc
Binary files differ
diff --git a/test/gen_magic_test.exs b/test/gen_magic_test.exs
index b0e187a..f9aae17 100644
--- a/test/gen_magic_test.exs
+++ b/test/gen_magic_test.exs
@@ -4,32 +4,102 @@ defmodule GenMagicTest do
alias GenMagic.ApprenticeServer, as: Magic
- setup_all do
- {:ok, pid} = Magic.start_link()
- {:ok, %{pid: pid}}
- end
+ @iterations 10
- test "Makefile is text file", %{pid: pid} do
- path = File.cwd!() |> Path.join("Makefile")
+ test "Makefile is text file" do
+ {:ok, pid} = Magic.start_link([])
+ path = makefile_path()
assert {:ok, [mime_type: "text/x-makefile", encoding: _, content: _]} =
- GenServer.call(pid, {:perform, path})
+ GenServer.call(pid, {:file, path})
+ end
+
+ test "Top level helper function" do
+ path = makefile_path()
+ assert {:ok, [mime_type: "text/x-makefile", encoding: _, content: _]} = GenMagic.perform(path)
end
@tag load: true, timeout: 180_000
- test "Load test local files", %{pid: pid} do
- "/usr/share/**/*"
- |> Path.wildcard()
- |> Stream.reject(&File.dir?/1)
- |> Stream.chunk_every(500)
- |> Stream.flat_map(&Enum.shuffle/1)
+ test "Load test local files" do
+ {:ok, pid} = Magic.start_link([])
+
+ files_stream()
|> Stream.cycle()
- |> Stream.take(10000)
+ |> Stream.take(@iterations)
|> Stream.map(
- &assert {:ok, [mime_type: _, encoding: _, content: _]} = GenServer.call(pid, {:perform, &1})
+ &assert {:ok, [mime_type: _, encoding: _, content: _]} = GenServer.call(pid, {:file, &1})
)
|> Enum.all?()
|> assert
end
+ test "Non-existent file" do
+ {:ok, pid} = Magic.start_link([])
+ path = missing_filename()
+ assert_no_file(GenServer.call(pid, {:file, path}))
+ end
+
+ test "Named process" do
+ {:ok, _pid} = Magic.start_link(name: :gen_magic)
+ path = makefile_path()
+
+ assert {:ok, [mime_type: "text/x-makefile", encoding: _, content: _]} =
+ GenServer.call(:gen_magic, {:file, path})
+ end
+
+ @tag :ci
+ test "Custom database file recognises Elixir files" do
+ database = Path.join(File.cwd!(), "test/elixir.mgc")
+ {:ok, pid} = Magic.start_link(database_patterns: [database])
+ path = Path.join(File.cwd!(), "mix.exs")
+
+ assert GenServer.call(pid, {:file, path}) ==
+ {:ok,
+ [
+ mime_type: "text/x-elixir",
+ encoding: "us-ascii",
+ content: "Elixir module source text"
+ ]}
+ end
+
+ @tag :breaking
+ test "Load test local files and missing files" do
+ {:ok, pid} = Magic.start_link([])
+
+ files_stream()
+ |> Stream.intersperse(missing_filename())
+ |> Stream.cycle()
+ |> Stream.take(@iterations)
+ |> Stream.map(fn path ->
+ case GenServer.call(pid, {:file, path}) do
+ {:ok, [mime_type: _, encoding: _, content: _]} -> true
+ {:error, "no_file"} -> true
+ end
+ end)
+ |> Enum.all?()
+ |> assert
+ end
+
+ defp missing_filename do
+ f =
+ make_ref()
+ |> inspect
+ |> String.replace(~r/\D/, "")
+
+ Path.join("/tmp", f)
+ end
+
+ defp files_stream,
+ do:
+ "/usr/share/**/*"
+ |> Path.wildcard()
+ |> Stream.reject(&File.dir?/1)
+ |> Stream.chunk_every(500)
+ |> Stream.flat_map(&Enum.shuffle/1)
+
+ defp assert_no_file({:error, msg}) do
+ assert msg == "no_file" || msg == "", msg
+ end
+
+ defp makefile_path, do: Path.join(File.cwd!(), "Makefile")
end
diff --git a/test/soak.exs b/test/soak.exs
new file mode 100644
index 0000000..ab366f0
--- /dev/null
+++ b/test/soak.exs
@@ -0,0 +1,35 @@
+defmodule Soak do
+ @moduledoc """
+ Run with a list of files to inspect:
+
+ find /usr/share/ -name *png | xargs mix run test/soak.exs
+ """
+
+ def perform_infinite([]), do: false
+
+ def perform_infinite(paths) do
+ {:ok, pid} =
+ GenMagic.ApprenticeServer.start_link(database_patterns: ["/usr/local/share/misc/*.mgc"])
+
+ perform_infinite(paths, [], pid, 0)
+ end
+
+ defp perform_infinite([], done, pid, count) do
+ perform_infinite(done, [], pid, count)
+ end
+
+ defp perform_infinite([path | paths], done, pid, count) do
+ if rem(count, 1000) == 0, do: IO.puts(Integer.to_string(count))
+
+ {:ok, [mime_type: _, encoding: _, content: _]} = GenServer.call(pid, {:file, path})
+ perform_infinite(paths, [path | done], pid, count + 1)
+ end
+end
+
+# Run with a list of files to inspect
+#
+# find /usr/share/ -name *png | xargs mix run test/soak.exs
+
+System.argv()
+|> Enum.filter(&File.exists?/1)
+|> Soak.perform_infinite()
diff --git a/test/test_helper.exs b/test/test_helper.exs
index 997c393..a7e2f12 100644
--- a/test/test_helper.exs
+++ b/test/test_helper.exs
@@ -1 +1,5 @@
-ExUnit.start(exclude: [:load])
+excluded =
+ [:breaking] ++
+ if(System.get_env("TRAVIS") != nil, do: [:ci], else: [])
+
+ExUnit.start(exclude: excluded)