-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathtest_helper.exs
134 lines (109 loc) · 3.08 KB
/
test_helper.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
otp_eep48? = Code.ensure_loaded?(:edoc_doclet_chunks)
otp_eep59? = Code.ensure_loaded?(:beam_doc)
ci? = System.get_env("CI") == "true"
exclude = [
otp_eep48: not otp_eep48?,
otp_eep59: not otp_eep59?,
otp_has_docs: not match?({:docs_v1, _, _, _, _, _, _}, Code.fetch_docs(:array)),
ci: not ci?
]
ExUnit.start(exclude: Enum.filter(exclude, &elem(&1, 1)))
# Prepare module fixtures
File.rm_rf!("test/tmp")
File.mkdir_p!("test/tmp/beam")
Code.prepend_path("test/tmp/beam")
# Compile module fixtures
"test/fixtures/*.ex"
|> Path.wildcard()
|> Kernel.ParallelCompiler.compile_to_path("test/tmp/beam")
defmodule TestHelper do
def elixirc(context, filename \\ "nofile", code) do
dir = context.tmp_dir
src_path = Path.join([dir, filename])
src_path |> Path.dirname() |> File.mkdir_p!()
File.write!(src_path, code)
ebin_dir = Path.join(dir, "ebin")
File.mkdir_p!(ebin_dir)
{:ok, modules, []} = Kernel.ParallelCompiler.compile_to_path([src_path], ebin_dir)
true = Code.prepend_path(ebin_dir)
ExUnit.Callbacks.on_exit(fn ->
for module <- modules do
:code.purge(module)
:code.delete(module)
end
File.rm_rf!(dir)
end)
modules
end
def erlc(context, module, code, opts \\ []) do
dir = context.tmp_dir
docs = Keyword.get(opts, :docs, true)
src_path = Path.join([dir, "#{module}.erl"])
src_path |> Path.dirname() |> File.mkdir_p!()
File.write!(src_path, code)
ebin_dir = Path.join(dir, "ebin")
File.mkdir_p!(ebin_dir)
beam_docs = docstrings(docs, context)
# not to be confused with the regular :debug_info opt
debug_info_opts =
Enum.filter(opts, fn
{:debug_info, _debug_info} -> true
{:debug_info_key, _debug_info_key} -> true
:encrypt_debug_info -> true
_ -> false
end)
{:ok, module} =
:compile.file(
String.to_charlist(src_path),
[:return_errors, :debug_info, outdir: String.to_charlist(ebin_dir)] ++
beam_docs ++ debug_info_opts
)
true = Code.prepend_path(ebin_dir)
{:module, ^module} = :code.load_file(module)
ExUnit.Callbacks.on_exit(fn ->
:code.purge(module)
:code.delete(module)
File.rm_rf!(dir)
ExDoc.Refs.clear()
end)
if docs && !context[:otp_eep59] do
edoc_to_chunk(module)
end
[module]
end
if otp_eep59? do
def docstrings(docs, context) do
if docs && context[:otp_eep59] do
[]
else
[:no_docs]
end
end
else
def docstrings(docs, context) do
if docs && context[:otp_eep59] do
raise "not supported"
else
[]
end
end
end
if otp_eep48? do
def edoc_to_chunk(module) do
source_path = module.module_info(:compile)[:source]
dir = :filename.dirname(source_path)
:ok =
:edoc.files([source_path],
preprocess: true,
doclet: :edoc_doclet_chunks,
layout: :edoc_layout_chunks,
dir: dir ++ ~c"/doc"
)
module
end
else
def edoc_to_chunk(_) do
raise "not supported"
end
end
end