Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Extras #11

Merged
merged 2 commits into from

2 participants

@khia

No description provided.

@yrashk
Owner

May be this should be *_test.exs — to conform to ExUnit's behaviour/convention?

@yrashk
Owner

I am trying to understand the purpose of the scanning feature?

@khia

With previous implementation someone need to call CommonTest.run [suite: [SomeModuleToTest]].
So I end up with quite complex test_helper.ex which implements the same feature.
With this patch complex test_helper.ex becomes a one liner.
CommonTest.run ".", [logdir: "log"]

@yrashk yrashk merged commit 1a37f86 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 50 additions and 1 deletion.
  1. +50 −1 lib/ct.ex
View
51 lib/ct.ex
@@ -7,12 +7,61 @@ defmodule CommonTest do
def run(options) do
:ct.run_test(Keyword.put options, :auto_compile, false)
end
+
+ def run(path, options) when is_binary(path), do: run([path], options)
+ def run(paths, options) do
+ files = scan(paths)
+ run(files, options, [])
+ end
+
+ defp run([], _options, acc), do: acc
+ defp run([file_path|rest], options, acc) do
+ res = test_file(file_path, options)
+ run(rest, options, res ++ acc)
+ end
+
+ def test_file(file_path, options) do
+ suites = lc mod inlist extract_modules(file_path), is_test?(mod), do: mod
+ case suites do
+ [] -> []
+ suites ->
+ options = Keyword.merge options, [suite: suites]
+ [run(options)]
+ end
+ end
+
+ def scan(paths) do
+ files =
+ List.concat(
+ lc path inlist paths do
+ if File.regular?(path) do
+ [path]
+ else
+ Path.wildcard("#{path}/**/*.exs")
+ end
+ end)
+ files |> filter |> Enum.uniq
+ end
+
+ def filter(files) do
+ Enum.filter(files, fn(x) -> String.first(Path.basename(x)) != "." end)
+ end
+
+ def extract_modules(file_path) do
+ lc {m, _} inlist :elixir_compiler.file(file_path), do: m
+ end
+
+ def is_test?(module) do
+ Keyword.has_key?(module.__info__(:attributes), :common_test)
+ end
end
defmodule CommonTest.Suite do
defmacro __using__(_) do
quote do
import CommonTest.Suite
+ Module.register_attribute unquote(__CALLER__.module), :common_test
+ @common_test true
def suite, do: []
@@ -107,7 +156,7 @@ defmodule CommonTest.Suite do
defp _test(name, options, caller) do
name = name_to_atom(name)
- cfg = options[:config] || {:_, 0, :quoted}
+ cfg = options[:config] || {:_, [], :quoted}
info = Keyword.delete(Keyword.delete(options, :config), :do)
quote do
if Module.get_attribute(unquote(caller.module), :group) == nil do
Something went wrong with that request. Please try again.