Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding elixir files scanning feature to CommonTest

  • Loading branch information...
commit c0c3ba3f18ef182e97ad2827586ac4a779122b8b 1 parent b4dfdd8
khia authored

Showing 1 changed file with 49 additions and 0 deletions. Show diff stats Hide diff stats

  1. +49 0 lib/ct.ex
49 lib/ct.ex
@@ -7,12 +7,61 @@ defmodule CommonTest do
7 7 def run(options) do
8 8 :ct.run_test(Keyword.put options, :auto_compile, false)
9 9 end
  10 +
  11 + def run(path, options) when is_binary(path), do: run([path], options)
  12 + def run(paths, options) do
  13 + files = scan(paths)
  14 + run(files, options, [])
  15 + end
  16 +
  17 + defp run([], _options, acc), do: acc
  18 + defp run([file_path|rest], options, acc) do
  19 + res = test_file(file_path, options)
  20 + run(rest, options, res ++ acc)
  21 + end
  22 +
  23 + def test_file(file_path, options) do
  24 + suites = lc mod inlist extract_modules(file_path), is_test?(mod), do: mod
  25 + case suites do
  26 + [] -> []
  27 + suites ->
  28 + options = Keyword.merge options, [suite: suites]
  29 + [run(options)]
  30 + end
  31 + end
  32 +
  33 + def scan(paths) do
  34 + files =
  35 + List.concat(
  36 + lc path inlist paths do
  37 + if File.regular?(path) do
  38 + [path]
  39 + else
  40 + Path.wildcard("#{path}/**/*.exs")
  41 + end
  42 + end)
  43 + files |> filter |> Enum.uniq
  44 + end
  45 +
  46 + def filter(files) do
  47 + Enum.filter(files, fn(x) -> String.first(Path.basename(x)) != "." end)
  48 + end
  49 +
  50 + def extract_modules(file_path) do
  51 + lc {m, _} inlist :elixir_compiler.file(file_path), do: m
  52 + end
  53 +
  54 + def is_test?(module) do
  55 + Keyword.has_key?(module.__info__(:attributes), :common_test)
  56 + end
10 57 end
11 58
12 59 defmodule CommonTest.Suite do
13 60 defmacro __using__(_) do
14 61 quote do
15 62 import CommonTest.Suite
  63 + Module.register_attribute unquote(__CALLER__.module), :common_test
  64 + @common_test true
16 65
17 66 def suite, do: []
18 67

0 comments on commit c0c3ba3

Yurii Rashkovskii

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

Please sign in to comment.
Something went wrong with that request. Please try again.