This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.exs
143 lines (111 loc) · 4.68 KB
/
test.exs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
path = hd(System.argv())
passes = :file.list_dir(path) |>
elem(1) |>
Enum.filter(fn(x) -> Path.extname(x) == '.pass' end) |>
Enum.sort() |>
Enum.map(fn(x) -> Path.join(path, x) end) |>
Enum.map(fn(x) -> [pass: x |> Path.basename() |> Path.rootname()] ++ hd(elem(:file.consult(x), 1)) end)
files = :file.list_dir(path) |>
elem(1) |>
Enum.filter(fn(x) -> Path.extname(x) == '.fl' end) |>
Enum.map(fn(x) -> :unicode.characters_to_binary(x) end) |>
Enum.sort()
vars = lc {a, s} inlist [arch: "ARCH",
os: "OS",
abi: "ABI",
fpabi: "FPABI",
endian: "ENDIAN",
cross: "CROSS",
cc_type: "CC_TYPE",
ld_type: "LD_TYPE"] do
{a, System.get_env("FLECT_" <> s) |> String.replace("-", "_") |> binary_to_atom()}
end
if System.get_env("FLECT_COVER") == "1" do
Mix.loadpaths()
:cover.compile_beam_directory(Mix.project()[:compile_path] |> to_char_list())
dir = Path.join(Mix.project()[:compile_path], "cover")
:cover.import(Path.join(dir, "flect.coverdata") |> to_char_list())
end
File.cd!(path)
results = Enum.map(passes, fn(pass) ->
IO.puts("")
IO.puts(" Testing #{path} (#{pass[:description]})...")
IO.puts("")
Enum.map(files, fn(file) ->
check = fn(file, pass, text, code) ->
exp = case File.read(file <> "." <> pass[:pass] <> ".exp") do
{:ok, data} -> String.strip(text) == String.strip(data)
{:error, :enoent} -> true
end
cond do
code != pass[:code] ->
IO.puts(IO.ANSI.escape("%{red, bright}fail (#{code})"))
false
!exp ->
IO.puts(IO.ANSI.escape("%{red, bright}fail (exp)"))
false
true ->
IO.puts(IO.ANSI.escape("%{green, bright}ok (#{code})"))
true
end
end
{extra_args, expr, condition} = case :file.consult(file <> "." <> pass[:pass] <> ".arg") do
{:ok, [args, expr]} ->
args = Enum.map(args, fn(arg) -> :unicode.characters_to_binary(arg) end)
expr = :unicode.characters_to_binary(expr)
condition = elem(Code.eval_string(expr, vars), 0)
{args, expr, condition}
{:error, :enoent} -> {[], "", true}
end
args = Enum.map(pass[:command], fn(arg) ->
arg |>
:unicode.characters_to_binary() |>
String.replace("<file>", file) |>
String.replace("<name>", Path.rootname(file))
end) ++ extra_args
IO.write(" flect #{args |> Enum.join(" ")} ... ")
if condition do
{opts, rest} = Flect.Application.parse(args)
cfg = Flect.Config[tool: binary_to_atom(hd(rest)),
options: opts,
arguments: Enum.drop(rest, 1)]
:application.set_env(:flect, :flect_event_pid, self())
Flect.Application.start()
proc = Process.whereis(:flect_worker)
Flect.Worker.work(proc, cfg)
Flect.Application.stop()
recv = fn(recv, acc) ->
receive do
{:flect_stdout, str} -> recv.(recv, acc <> str)
{:flect_shutdown, code} -> {acc, code}
end
end
{text, code} = recv.(recv, "")
check.(file, pass, text, code)
else
IO.puts(IO.ANSI.escape("%{blue, bright}skip (#{expr})"))
nil
end
end)
end)
File.cd!(Path.join("..", ".."))
results = List.flatten(results)
test_passes = Enum.count(results, fn(x) -> x end)
test_skips = Enum.count(results, fn(x) -> x == nil end)
test_failures = Enum.count(results, fn(x) -> x == false end)
tests = test_passes + test_failures
IO.puts("")
IO.puts(IO.ANSI.escape_fragment(" %{yellow, bright}#{tests}%{reset} total tests, " <>
"%{blue, bright}#{test_skips}%{reset} skipped, " <>
"%{green, bright}#{test_passes}%{reset} successful, " <>
"%{red, bright}#{test_failures}%{reset} failed"))
IO.puts("")
code = if test_failures > 0, do: 1, else: 0
if System.get_env("FLECT_COVER") == "1" && code == 0 do
File.mkdir_p!(dir)
:cover.export(Path.join(dir, "flect.coverdata") |> to_char_list())
Enum.each(:cover.modules(), fn(x) ->
:cover.analyse_to_file(x, Path.join(dir, "#{x}.html") |> to_char_list(), [:html])
end)
end
System.halt(code)