summaryrefslogtreecommitdiff
path: root/test/rebar_eunit_SUITE.erl
blob: 0600b45796588268f5f7b9782aca22c0e9cb6cc3 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
-module(rebar_eunit_SUITE).

-export([suite/0,
         init_per_suite/1,
         end_per_suite/1,
         init_per_testcase/2,
         all/0]).
-export([test_basic_app/1,
         test_multi_app/1,
         test_profile/1,
         test_basic_exports/1,
         test_multi_exports/1,
         test_basic_defines/1,
         test_multi_defines/1,
         test_single_app_flag/1,
         test_multiple_app_flag/1,
         test_single_suite_flag/1,
         test_suite_in_app_flag/1,
         test_single_file_flag/1,
         test_multiple_file_flag/1,
         test_config_tests/1]).

-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("kernel/include/file.hrl").

suite() ->
    [].

init_per_suite(Config) ->
    Config.

end_per_suite(_Config) ->
    ok.

init_per_testcase(_, Config) ->
    rebar_test_utils:init_rebar_state(Config, "eunit_").

all() ->
    [test_basic_app, test_multi_app, test_profile,
     test_basic_exports, test_multi_exports,
     test_basic_defines, test_multi_defines,
     test_single_app_flag, test_multiple_app_flag,
     test_single_suite_flag, test_suite_in_app_flag,
     test_single_file_flag, test_multiple_file_flag,
     test_config_tests].

test_basic_app(Config) ->
    AppDir = ?config(apps, Config),

    Name = rebar_test_utils:create_random_name("basic_"),
    Vsn = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),

    RebarConfig = [{erl_opts, [{d, some_define}]}],
    rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}).

test_multi_app(Config) ->
    AppDir = ?config(apps, Config),

    Name1 = rebar_test_utils:create_random_name("multi_app1_"),
    Vsn1 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
                                      Name1,
                                      Vsn1,
                                      [kernel, stdlib]),
    Name2 = rebar_test_utils:create_random_name("multi_app2_"),
    Vsn2 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
                                      Name2,
                                      Vsn2,
                                      [kernel, stdlib]),

    RebarConfig = [{erl_opts, [{d, some_define}]}],
    rebar_test_utils:run_and_check(Config,
                                   RebarConfig,
                                   ["eunit"],
                                   {ok, [{app, Name1}, {app, Name2}]}).

test_profile(Config) ->
    AppDir = ?config(apps, Config),

    Name = rebar_test_utils:create_random_name("profile_"),
    Vsn = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),

    RebarConfig = [{erl_opts, [{d, some_define}]},
                  {profiles, [{test, [{erl_opts, [debug_info]}]}]}],
    rebar_test_utils:run_and_check(Config,
                                   RebarConfig,
                                   ["as", "test", "eunit"],
                                   {ok, [{app, Name}]}).

test_basic_exports(Config) ->
    AppDir = ?config(apps, Config),

    Name = rebar_test_utils:create_random_name("basic_exports_"),
    Vsn = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),

    RebarConfig = [{erl_opts, [{d, some_define}]}],
    rebar_test_utils:run_and_check(Config,
                                   RebarConfig,
                                   ["eunit"],
                                   {ok, [{app, Name}]}),

    App = list_to_atom("not_a_real_src_" ++ Name),
    Suite = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"),
    AppExports = App:module_info(exports),
    SuiteExports = Suite:module_info(exports),
    AppExpect = [{some_test_, 0}],
    SuiteExpect = [{some_test_, 0}, {define_test_, 0}],
    lists:foreach(fun(Expect) -> true = lists:member(Expect, AppExports) end, AppExpect),
    lists:foreach(fun(Expect) -> true = lists:member(Expect, SuiteExports) end, SuiteExpect).

test_multi_exports(Config) ->
    AppDir = ?config(apps, Config),

    Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"),
    Vsn1 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
                                      Name1,
                                      Vsn1,
                                      [kernel, stdlib]),
    Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"),
    Vsn2 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
                                      Name2,
                                      Vsn2,
                                      [kernel, stdlib]),

    RebarConfig = [{erl_opts, [{d, some_define}]}],
    rebar_test_utils:run_and_check(Config,
                                   RebarConfig,
                                   ["eunit"],
                                   {ok, [{app, Name1}, {app, Name2}]}),

    App1 = list_to_atom("not_a_real_src_" ++ Name1),
    Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"),
    AppExports1 = App1:module_info(exports),
    SuiteExports1 = Suite1:module_info(exports),
    App2 = list_to_atom("not_a_real_src_" ++ Name2),
    Suite2 = list_to_atom("not_a_real_src_" ++ Name2 ++ "_tests"),
    AppExports2 = App2:module_info(exports),
    SuiteExports2 = Suite2:module_info(exports),
    AppExpect = [{some_test_, 0}],
    SuiteExpect = [{some_test_, 0}, {define_test_, 0}],
    lists:foreach(fun(Expect) -> true = lists:member(Expect, AppExports1) end, AppExpect),
    lists:foreach(fun(Expect) -> true = lists:member(Expect, SuiteExports1) end, SuiteExpect),
    lists:foreach(fun(Expect) -> true = lists:member(Expect, AppExports2) end, AppExpect),
    lists:foreach(fun(Expect) -> true = lists:member(Expect, SuiteExports2) end, SuiteExpect).

test_basic_defines(Config) ->
    AppDir = ?config(apps, Config),

    Name = rebar_test_utils:create_random_name("basic_"),
    Vsn = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),

    RebarConfig = [{erl_opts, [{d, some_define}]}],
    rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}),

    App = list_to_atom("not_a_real_src_" ++ Name),
    Suite = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"),
    AppOpts = proplists:get_value(options, App:module_info(compile), []),
    SuiteOpts = proplists:get_value(options, Suite:module_info(compile), []),
    Expect = [{d, some_define}],
    lists:foreach(fun(E) -> true = lists:member(E, AppOpts) end, Expect),
    lists:foreach(fun(E) -> true = lists:member(E, SuiteOpts) end, Expect).

test_multi_defines(Config) ->
    AppDir = ?config(apps, Config),

    Name1 = rebar_test_utils:create_random_name("multi_app1_"),
    Vsn1 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
                                      Name1,
                                      Vsn1,
                                      [kernel, stdlib]),
    Name2 = rebar_test_utils:create_random_name("multi_app2_"),
    Vsn2 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
                                      Name2,
                                      Vsn2,
                                      [kernel, stdlib]),

    RebarConfig = [{erl_opts, [{d, some_define}]}],
    rebar_test_utils:run_and_check(Config,
                                   RebarConfig,
                                   ["eunit"],
                                   {ok, [{app, Name1}, {app, Name2}]}),

    App1 = list_to_atom("not_a_real_src_" ++ Name1),
    Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"),
    AppOpts1 = proplists:get_value(options, App1:module_info(compile), []),
    SuiteOpts1 = proplists:get_value(options, Suite1:module_info(compile), []),
    App2 = list_to_atom("not_a_real_src_" ++ Name2),
    Suite2 = list_to_atom("not_a_real_src_" ++ Name2 ++ "_tests"),
    AppOpts2 = proplists:get_value(options, App2:module_info(compile), []),
    SuiteOpts2 = proplists:get_value(options, Suite2:module_info(compile), []),
    Expect = [{d, some_define}],
    lists:foreach(fun(E) -> true = lists:member(E, AppOpts1) end, Expect),
    lists:foreach(fun(E) -> true = lists:member(E, SuiteOpts1) end, Expect),
    lists:foreach(fun(E) -> true = lists:member(E, AppOpts2) end, Expect),
    lists:foreach(fun(E) -> true = lists:member(E, SuiteOpts2) end, Expect).

test_single_app_flag(Config) ->
    AppDir = ?config(apps, Config),

    Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"),
    Vsn1 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
                                      Name1,
                                      Vsn1,
                                      [kernel, stdlib]),
    Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"),
    Vsn2 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
                                      Name2,
                                      Vsn2,
                                      [kernel, stdlib]),

    BareSuite = io_lib:format("-module(all_tests).\n"
                              "-compile(export_all).\n"
                              "-include_lib(\"eunit/include/eunit.hrl\").\n"
                              "some_test_() -> ?_assert(true).\n"
                              "define_test_() -> ?_assertEqual(true, ?some_define).\n", []),
    FileName = filename:join([AppDir, "test", "all_tests.erl"]),
    ok = filelib:ensure_dir(FileName),
    ok = ec_file:write(FileName, BareSuite),

    RebarConfig = [{erl_opts, [{d, some_define}]}],
    rebar_test_utils:run_and_check(Config,
                                   RebarConfig,
                                   ["eunit", "--app=" ++ Name1],
                                   {ok, [{app, Name1}, {app, Name2}]}),

    Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"),
    {module, Suite1} = code:ensure_loaded(Suite1),
    Suite2 = list_to_atom("not_a_real_src_" ++ Name2 ++ "_tests"),
    {error, nofile} = code:ensure_loaded(Suite2),
    {error, nofile} = code:ensure_loaded(all_tests).

test_multiple_app_flag(Config) ->
    AppDir = ?config(apps, Config),

    Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"),
    Vsn1 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
                                      Name1,
                                      Vsn1,
                                      [kernel, stdlib]),
    Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"),
    Vsn2 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
                                      Name2,
                                      Vsn2,
                                      [kernel, stdlib]),

    BareSuite = io_lib:format("-module(all_tests).\n"
                              "-compile(export_all).\n"
                              "-include_lib(\"eunit/include/eunit.hrl\").\n"
                              "some_test_() -> ?_assert(true).\n"
                              "define_test_() -> ?_assertEqual(true, ?some_define).\n", []),
    FileName = filename:join([AppDir, "test", "all_tests.erl"]),
    ok = filelib:ensure_dir(FileName),
    ok = ec_file:write(FileName, BareSuite),

    RebarConfig = [{erl_opts, [{d, some_define}]}],
    rebar_test_utils:run_and_check(Config,
                                   RebarConfig,
                                   ["eunit", "--app=" ++ Name1 ++ "," ++ Name2],
                                   {ok, [{app, Name1}, {app, Name2}]}),

    Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"),
    {module, Suite1} = code:ensure_loaded(Suite1),
    Suite2 = list_to_atom("not_a_real_src_" ++ Name2 ++ "_tests"),
    {module, Suite2} = code:ensure_loaded(Suite2),
    {error, nofile} = code:ensure_loaded(all_tests).

test_single_suite_flag(Config) ->
    AppDir = ?config(apps, Config),

    Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"),
    Vsn1 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
                                      Name1,
                                      Vsn1,
                                      [kernel, stdlib]),
    Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"),
    Vsn2 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
                                      Name2,
                                      Vsn2,
                                      [kernel, stdlib]),

    RebarConfig = [{erl_opts, [{d, some_define}]}],
    rebar_test_utils:run_and_check(Config,
                                   RebarConfig,
                                   ["eunit", "--suite=not_a_real_src_" ++ Name1],
                                   {ok, [{app, Name1}, {app, Name2}]}),

    Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"),
    {module, Suite1} = code:ensure_loaded(Suite1).

test_suite_in_app_flag(Config) ->
    AppDir = ?config(apps, Config),

    Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"),
    Vsn1 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
                                      Name1,
                                      Vsn1,
                                      [kernel, stdlib]),
    Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"),
    Vsn2 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
                                      Name2,
                                      Vsn2,
                                      [kernel, stdlib]),

    RebarConfig = [{erl_opts, [{d, some_define}]}],
    rebar_test_utils:run_and_check(Config,
                                   RebarConfig,
                                   ["eunit",
                                    "--app=" ++ Name1,
                                    "--suite=not_a_real_src_" ++ Name1],
                                   {ok, [{app, Name1}, {app, Name2}]}),

    Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"),
    {module, Suite1} = code:ensure_loaded(Suite1),
    Suite2 = list_to_atom("not_a_real_src_" ++ Name2 ++ "_tests"),
    {error, nofile} = code:ensure_loaded(Suite2).

test_single_file_flag(Config) ->
    AppDir = ?config(apps, Config),

    Name = rebar_test_utils:create_random_name("single_file_flag_app_"),
    Vsn = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),

    RebarConfig = [{erl_opts, [{d, some_define}]}],
    rebar_test_utils:run_and_check(Config,
                                   RebarConfig,
                                   ["eunit", "--file=" ++ AppDir ++ "/_build/test/lib/" ++ Name ++ "/ebin/not_a_real_src_" ++ Name ++ "_tests.beam"],
                                   {ok, [{app, Name}]}),

    File = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"),
    {module, File} = code:ensure_loaded(File).

test_multiple_file_flag(Config) ->
    AppDir = ?config(apps, Config),

    Name = rebar_test_utils:create_random_name("multiple_file_flag_app_"),
    Vsn = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),

    RebarConfig = [{erl_opts, [{d, some_define}]}],
    rebar_test_utils:run_and_check(Config,
                                   RebarConfig,
                                   ["eunit", "--file=" ++ AppDir ++ "/_build/test/lib/" ++ Name ++ "/ebin/not_a_real_src_" ++ Name ++ "_tests.beam," ++ AppDir ++ "/_build/test/lib/" ++ Name ++ "/ebin/not_a_real_src_" ++ Name ++ ".beam"],
                                   {ok, [{app, Name}]}),

    File1 = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"),
    {module, File1} = code:ensure_loaded(File1),

    File2 = list_to_atom("not_a_real_src_" ++ Name),
    {module, File2} = code:ensure_loaded(File2).

test_config_tests(Config) ->
    AppDir = ?config(apps, Config),

    Name1 = rebar_test_utils:create_random_name("config_tests_app1_"),
    Vsn1 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
                                      Name1,
                                      Vsn1,
                                      [kernel, stdlib]),
    Name2 = rebar_test_utils:create_random_name("config_tests_app2_"),
    Vsn2 = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
                                      Name2,
                                      Vsn2,
                                      [kernel, stdlib]),

    BareSuite = io_lib:format("-module(all_tests).\n"
                              "-compile(export_all).\n"
                              "-include_lib(\"eunit/include/eunit.hrl\").\n"
                              "some_test_() -> ?_assert(true).\n"
                              "define_test_() -> ?_assertEqual(true, ?some_define).\n", []),
    FileName = filename:join([AppDir, "test", "all_tests.erl"]),
    ok = filelib:ensure_dir(FileName),
    ok = ec_file:write(FileName, BareSuite),

    RebarConfig = [{erl_opts, [{d, some_define}]}, {eunit_tests, [{application, list_to_atom(Name1)}]}],
    rebar_test_utils:run_and_check(Config,
                                   RebarConfig,
                                   ["eunit"],
                                   {ok, [{app, Name1}, {app, Name2}]}),

    Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"),
    {module, Suite1} = code:ensure_loaded(Suite1),
    Suite2 = list_to_atom("not_a_real_src_" ++ Name2 ++ "_tests"),
    {error, nofile} = code:ensure_loaded(Suite2),
    {error, nofile} = code:ensure_loaded(all_tests).