-
Notifications
You must be signed in to change notification settings - Fork 17
/
gleeunit.gleam
74 lines (60 loc) · 1.75 KB
/
gleeunit.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import gleam/dynamic.{type Dynamic}
import gleam/list
import gleam/result
import gleam/string
/// Find and run all test functions for the current project using Erlang's EUnit
/// test framework.
///
/// Any Erlang or Gleam function in the `test` directory with a name ending in
/// `_test` is considered a test function and will be run.
///
/// If running on JavaScript tests will be run with a custom test runner.
///
pub fn main() -> Nil {
do_main()
}
@external(javascript, "./gleeunit_ffi.mjs", "main")
fn do_main() -> Nil {
let options = [Verbose, NoTty, Report(#(GleeunitProgress, [Colored(True)]))]
let result =
find_files(matching: "**/*.{erl,gleam}", in: "test")
|> list.map(gleam_to_erlang_module_name)
|> list.map(dangerously_convert_string_to_atom(_, Utf8))
|> run_eunit(options)
|> dynamic.result(dynamic.dynamic, dynamic.dynamic)
|> result.unwrap(Error(dynamic.from(Nil)))
let code = case result {
Ok(_) -> 0
Error(_) -> 1
}
halt(code)
}
@external(erlang, "erlang", "halt")
fn halt(a: Int) -> Nil
fn gleam_to_erlang_module_name(path: String) -> String {
path
|> string.replace(".gleam", "")
|> string.replace(".erl", "")
|> string.replace("/", "@")
}
@external(erlang, "gleeunit_ffi", "find_files")
fn find_files(matching matching: String, in in: String) -> List(String)
type Atom
type Encoding {
Utf8
}
@external(erlang, "erlang", "binary_to_atom")
fn dangerously_convert_string_to_atom(a: String, b: Encoding) -> Atom
type ReportModuleName {
GleeunitProgress
}
type GleeunitProgressOption {
Colored(Bool)
}
type EunitOption {
Verbose
NoTty
Report(#(ReportModuleName, List(GleeunitProgressOption)))
}
@external(erlang, "eunit", "test")
fn run_eunit(a: List(Atom), b: List(EunitOption)) -> Dynamic