forked from lycus/flect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.exs
151 lines (114 loc) · 4.86 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
144
145
146
147
148
149
150
151
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()] ++ Enum.at!(elem(:file.consult(x), 1), 0) end)
files = :file.list_dir(path) |>
elem(1) |>
Enum.filter(fn(x) -> Path.extname(x) == '.fl' end) |>
Enum.map(fn(x) -> list_to_binary(x) end) |>
Enum.sort()
otp = :erlang.system_info(:otp_release)
if otp >= 'R16B' && 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 = case :file.consult(file <> "." <> pass[:pass] <> ".arg") do
{:ok, [list]} -> Enum.map(list, fn(arg) -> list_to_binary(arg) end)
{:error, :enoent} -> []
end
if is_list(pass[:command]) do
args = Enum.map(pass[:command], fn(arg) ->
arg |>
list_to_binary() |>
String.replace("<file>", file) |>
String.replace("<name>", Path.rootname(file))
end) ++ extra_args
IO.write(" flect #{args |> Enum.join(" ")} ... ")
{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
cmd = pass[:command] |>
list_to_binary() |>
String.replace("<file>", file) |>
String.replace("<name>", Path.rootname(file))
if extra_args != [] do
cmd = cmd <> " " <> Enum.join(extra_args, " ")
end
IO.write(" #{cmd} ... ")
port = Port.open({:spawn, binary_to_list(cmd)}, [:stream,
:binary,
:exit_status,
:hide])
recv = fn(recv, port, acc) ->
receive do
{^port, {:data, data}} -> recv.(recv, port, acc <> data)
{^port, {:exit_status, code}} -> {acc, code}
end
end
{text, code} = recv.(recv, port, "")
check.(file, pass, text, code)
end
end)
end)
File.cd!(Path.join("..", ".."))
results = List.flatten(results)
test_passes = Enum.count(results, fn(x) -> x end)
test_failures = Enum.count(results, fn(x) -> !x end)
tests = test_passes + test_failures
IO.puts("")
IO.puts(IO.ANSI.escape_fragment(" %{yellow, bright}#{tests}%{reset} test passes executed, " <>
"%{green, bright}#{test_passes}%{reset} successful, " <>
"%{red, bright}#{test_failures}%{reset} failed"))
IO.puts("")
code = if test_failures > 0, do: 1, else: 0
if otp >= 'R16B' && 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)