summaryrefslogtreecommitdiff
path: root/test/rebar_profiles_SUITE.erl
blob: 11aca6ad39acc6b663da7b6c0017841f14c4c95b (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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
-module(rebar_profiles_SUITE).

-export([init_per_suite/1,
         end_per_suite/1,
         init_per_testcase/2,
         end_per_testcase/2,
         all/0,
         profile_new_key/1,
         profile_merge_keys/1,
         profile_merge_umbrella_keys/1,
         explicit_profile_deduplicate_deps/1,
         implicit_profile_deduplicate_deps/1,
         all_deps_code_paths/1,
         profile_merges/1,
         same_profile_deduplication/1,
         stack_deduplication/1,
         add_to_profile/1,
         add_to_existing_profile/1,
         profiles_remain_applied_with_config_present/1,
         deduplicated_paths/1,
         test_profile_applied_at_completion/1,
         test_profile_applied_before_compile/1,
         test_profile_applied_before_eunit/1,
         test_profile_applied_to_apps/1,
         test_profile_erl_opts_order_1/1,
         test_profile_erl_opts_order_2/1,
         test_profile_erl_opts_order_3/1,
         test_profile_erl_opts_order_4/1,
         test_profile_erl_opts_order_5/1,
         first_files_exception/1]).

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

all() ->
    [profile_new_key, profile_merge_keys, profile_merge_umbrella_keys,
     all_deps_code_paths, profile_merges,
     explicit_profile_deduplicate_deps, implicit_profile_deduplicate_deps,
     same_profile_deduplication, stack_deduplication,
     add_to_profile, add_to_existing_profile,
     profiles_remain_applied_with_config_present,
     deduplicated_paths,
     test_profile_applied_at_completion,
     test_profile_applied_before_compile,
     test_profile_applied_before_eunit,
     test_profile_applied_to_apps,
     test_profile_erl_opts_order_1,
     test_profile_erl_opts_order_2,
     test_profile_erl_opts_order_3,
     test_profile_erl_opts_order_4,
     test_profile_erl_opts_order_5,
     first_files_exception].

init_per_suite(Config) ->
    application:start(meck),
    Config.

end_per_suite(_Config) ->
    application:stop(meck).

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

end_per_testcase(_, Config) ->
    meck:unload(),
    Config.

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

    AllDeps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
                                                ,{"b", "1.0.0", []}]),
    {SrcDeps, []} = rebar_test_utils:flat_deps(AllDeps),
    mock_git_resource:mock([{deps, SrcDeps}]),

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

    Deps = rebar_test_utils:top_level_deps(
             rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
                                               ,{"b", "1.0.0", []}])),
    ct:pal("Deps ~p", [Deps]),
    RebarConfig = [{profiles,
                   [{ct,
                    [{deps, Deps}]}]}],

    rebar_test_utils:run_and_check(Config, RebarConfig,
                                   ["as", "ct", "compile"], {ok, [{app, Name}
                                                                 ,{dep, "a", "1.0.0"}
                                                                 ,{dep, "b", "1.0.0"}]}).

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

    AllDeps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
                                                ,{"b", "1.0.0", []}
                                                ,{"b", "2.0.0", []}]),
    {SrcDeps, []} = rebar_test_utils:flat_deps(AllDeps),
    mock_git_resource:mock([{deps, SrcDeps}]),

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

    Deps = rebar_test_utils:top_level_deps(
             rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
                                               ,{"b", "1.0.0", []}])),
    ProfileDeps = rebar_test_utils:top_level_deps(
                    rebar_test_utils:expand_deps(git, [{"b", "2.0.0", []}])),

    RebarConfig = [{deps, Deps},
                   {profiles,
                    [{ct,
                      [{deps, ProfileDeps}]}]}],

    rebar_test_utils:run_and_check(Config, RebarConfig,
                                   ["as", "ct", "compile"], {ok, [{app, Name}
                                                                 ,{dep, "a", "1.0.0"}
                                                                 ,{dep, "b", "2.0.0"}]}).

profile_merge_umbrella_keys(Config) ->
    AppDir = ?config(apps, Config),
    ct:pal("Path: ~s", [AppDir]),
    Name = rebar_test_utils:create_random_name("profile_merge_umbrella_keys"),
    Vsn = rebar_test_utils:create_random_vsn(),
    SubAppDir = filename:join([AppDir, "apps", Name]),

    RebarConfig = [{vals, [{a,1},{b,1}]},
                   {profiles,
                    [{ct,
                      [{vals, [{a,1},{b,2}]}]}]}],
    
    SubRebarConfig = [{vals, []},
                       {profiles, [{ct, [{vals, [{c,1}]}]}]}],

    rebar_test_utils:create_app(SubAppDir, Name, Vsn, [kernel, stdlib]),
    rebar_test_utils:create_config(SubAppDir, SubRebarConfig),
    {ok, RebarConfigRead} = file:consult(rebar_test_utils:create_config(AppDir, RebarConfig)),

    {ok, State} = rebar_test_utils:run_and_check(
        Config, RebarConfigRead, ["as", "ct", "compile"], return
    ),

    [ProjectApp] = rebar_state:project_apps(State),
    ?assertEqual(Name, binary_to_list(rebar_app_info:name(ProjectApp))),
    Opts = rebar_app_info:opts(ProjectApp),
    ?assertEqual([{a,1},{b,2},{b,1},{c,1}], dict:fetch(vals, Opts)),
    ok.

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

    AllDeps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
                                                ,{"a", "2.0.0", []}
                                                ,{"b", "1.0.0", []}
                                                ,{"b", "2.0.0", []}]),
    {SrcDeps, []} = rebar_test_utils:flat_deps(AllDeps),
    mock_git_resource:mock([{deps, SrcDeps}]),

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

    FooDeps = rebar_test_utils:top_level_deps(
                    rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []},
                                                       {"b", "2.0.0", []}])),
    BarDeps = rebar_test_utils:top_level_deps(
                    rebar_test_utils:expand_deps(git, [{"b", "1.0.0", []}])),

    RebarConfig = [{profiles,
                    [{foo,
                      [{deps, FooDeps}]},
                     {bar,
                      [{deps, BarDeps}]}]}],

    rebar_test_utils:run_and_check(Config, RebarConfig,
                                   ["as", "bar,foo,bar", "compile"], {ok, [{app, Name}
                                                                 ,{dep, "a", "1.0.0"}
                                                                 ,{dep, "b", "1.0.0"}]}).

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

    AllDeps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
                                                ,{"a", "2.0.0", []}
                                                ,{"b", "1.0.0", []}
                                                ,{"b", "2.0.0", []}]),
    {SrcDeps, []} = rebar_test_utils:flat_deps(AllDeps),
    mock_git_resource:mock([{deps, SrcDeps}]),

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

    TestDeps = rebar_test_utils:top_level_deps(
                    rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []},
                                                       {"b", "2.0.0", []}])),
    ProfileDeps = rebar_test_utils:top_level_deps(
                    rebar_test_utils:expand_deps(git, [{"b", "1.0.0", []}])),

    RebarConfig = [{profiles,
                    [{test,
                      [{deps, TestDeps}]},
                     {bar,
                      [{deps, ProfileDeps}]}]}],

    rebar_test_utils:run_and_check(Config, RebarConfig,
                                   ["as", "test,bar", "eunit"], {ok, [{app, Name}
                                                                 ,{dep, "a", "1.0.0"}
                                                                 ,{dep, "b", "2.0.0"}]}).

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

    AllDeps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
                                                ,{"b", "2.0.0", []}]),
    {SrcDeps, []} = rebar_test_utils:flat_deps(AllDeps),
    mock_git_resource:mock([{deps, SrcDeps}]),

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

    Deps = rebar_test_utils:top_level_deps(
             rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}])),
    ProfileDeps = rebar_test_utils:top_level_deps(
                    rebar_test_utils:expand_deps(git, [{"b", "2.0.0", []}])),

    RebarConfig = [{deps, Deps},
                   {profiles,
                    [{all_deps_test,
                      [{deps, ProfileDeps}]}]}],
    os:putenv("REBAR_PROFILE", "all_deps_test"),
    {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig,
                                   ["compile"], {ok, [{app, Name}
                                                     ,{dep, "a", "1.0.0"}
                                                     ,{dep, "b", "2.0.0"}]}),
    os:putenv("REBAR_PROFILE", ""),

    Paths = rebar_state:code_paths(State, all_deps),
    Path = lists:reverse(["_build", "all_deps_test", "lib", "b", "ebin"]),
    ?assert(lists:any(fun(X) ->
                              Path =:= lists:sublist(lists:reverse(filename:split(X)), 5)
                      end, Paths)).


profile_merges(_Config) ->
    RebarConfig = [{test1, [{key1, 1, 2}, key2]},
                   {test2, "hello"},
                   {test3, [key3]},
                   {test4, "oldvalue"},
                   {test5, [{key5, true}]},
                   {test6, [{key6, false}]},
                   {profiles,
                    [{profile1,
                      [{test1, [{key3, 5}, key1]}]},
                     {profile2, [{test2, "goodbye"},
                                 {test3, []},
                                 {test4, []},
                                 {test5, [{key5, false}]},
                                 {test6, [{key6, true}]}
                                ]}]}],
    State = rebar_state:new(RebarConfig),
    State1 = rebar_state:apply_profiles(State, [profile1, profile2]),

    %% Combine lists
    ?assertEqual(lists:sort([key1, key2, {key1, 1, 2}, {key3, 5}]),
                 lists:sort(rebar_state:get(State1, test1))),

    %% Use new value for strings
    "goodbye" = rebar_state:get(State1, test2),

    %% Check that a newvalue of []/"" doesn't override non-string oldvalues
    [key3] = rebar_state:get(State1, test3),
    [] = rebar_state:get(State1, test4),
    [{key5, false}, {key5, true}] = rebar_state:get(State1, test5),
    [{key6, true}, {key6, false}] = rebar_state:get(State1, test6).

same_profile_deduplication(_Config) ->
    RebarConfig = [{test1, [{key1, 1, 2}, key2]},
                   {test2, [foo]},
                   {test3, [key3]},
                   {profiles,
                    [{profile1,
                      [{test1, [{key3, 5}, {key2, "hello"}]},
                       {test2, [bar]},
                       {test3, []}
                    ]}]
                   }],
    State = rebar_state:new(RebarConfig),
    State1 = rebar_state:apply_profiles(State, [profile1, profile1, profile1]),

    ?assertEqual([default, profile1], rebar_state:current_profiles(State1)),
    Test1 = rebar_state:get(State1, test1),

    %% Combine lists
    ?assertEqual(lists:sort([key2, {key1, 1, 2}, {key3, 5}, {key2, "hello"}]),
                 lists:sort(Test1)),

    %% Key2 from profile1 overrides key2 from default profile
    ?assertEqual("hello", proplists:get_value(key2, Test1)),

    %% Check that a newvalue of []/"" doesn't override non-string oldvalues
    ?assertEqual([key3], rebar_state:get(State1, test3)),
    ?assertEqual([bar, foo], rebar_state:get(State1, test2)).

stack_deduplication(_Config) ->
    RebarConfig = [
        {test_key, default},
        {test_list, [ {foo, default}  ]},
        {profiles, [
            {a, [
                {test_key, a},
                {test_list, [ {foo, a} ]}
            ]},
            {b, [
                {test_key, b},
                {test_list, [ {foo, b} ]}
            ]},
            {c, [
                {test_key, c},
                {test_list, [ {foo, c} ]}
            ]},
            {d, [
                {test_key, d},
                {test_list, [ {foo, d} ]}
            ]},
            {e, [
                {test_key, e},
                {test_list, [ {foo, e} ]}
            ]}
        ]}
    ],
    State = rebar_state:new(RebarConfig),
    State1 = rebar_state:apply_profiles(State, [a, b, c, d, e, a, e, b]),
    ?assertEqual(b, rebar_state:get(State1, test_key)),

    TestList = rebar_state:get(State1, test_list),
    ?assertEqual(
        [{foo, b}, {foo, e}, {foo, a}, {foo, d}, {foo, c}, {foo, default} ],
        TestList
    ),
    ?assertEqual(b, proplists:get_value(foo, TestList)).

add_to_profile(_Config) ->
    RebarConfig = [{foo, true}, {bar, false}],
    State = rebar_state:new(RebarConfig),
    State1 = rebar_state:add_to_profile(State, test, [{foo, false}]),
    State2 = rebar_state:apply_profiles(State1, test),

    Opts = rebar_state:opts(State2),
    lists:map(fun(K) -> false = dict:fetch(K, Opts) end, [foo, bar]).

add_to_existing_profile(_Config) ->
    RebarConfig = [{foo, true}, {bar, false}, {profiles, [
        {test, [{foo, false}]}
    ]}],
    State = rebar_state:new(RebarConfig),
    State1 = rebar_state:add_to_profile(State, test, [{baz, false}]),
    State2 = rebar_state:apply_profiles(State1, test),

    Opts = rebar_state:opts(State2),
    lists:map(fun(K) -> false = dict:fetch(K, Opts) end, [foo, bar, baz]).

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

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

    RebarConfig = [{erl_opts, []}, {profiles, [
        {not_ok, [{erl_opts, [{d, not_ok}]}]}
    ]}],

    rebar_test_utils:create_config(AppDir, RebarConfig),

    rebar_test_utils:run_and_check(Config, RebarConfig,
                                   ["as", "not_ok", "compile"], {ok, [{app, Name}]}),

    Path = filename:join([AppDir, "_build", "not_ok", "lib", Name, "ebin"]),
    code:add_patha(Path),

    Mod = list_to_atom("not_a_real_src_" ++ Name),

    true = lists:member({d, not_ok}, proplists:get_value(options, Mod:module_info(compile), [])).

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

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

    RebarConfig = [],
    rebar_test_utils:create_config(AppDir, RebarConfig),
    rebar_test_utils:run_and_check(Config, RebarConfig,
                                   ["as", "a,b,c,d,e,a,e,b", "compile"],
                                   {ok, [{app, Name}]}),

    Path = filename:join([AppDir, "_build", "c+d+a+e+b", "lib", Name, "ebin"]),
    ?assert(filelib:is_dir(Path)).

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

    Name = rebar_test_utils:create_random_name("test_profile_at_completion_"),
    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:create_config(AppDir, RebarConfig),

    {ok, State} = rebar_test_utils:run_and_check(Config,
                                                 RebarConfig,
                                                 ["eunit"],
                                                 return),

    [App] = rebar_state:project_apps(State),
    ErlOpts = rebar_app_info:get(App, erl_opts),
    true = lists:member({d, 'TEST'}, ErlOpts).

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

    Name = rebar_test_utils:create_random_name("test_profile_before_compile_"),
    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:create_config(AppDir, RebarConfig),

    {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}),
    code:add_paths(rebar_state:code_paths(State, all_deps)),

    S = list_to_atom("not_a_real_src_" ++ Name),
    true = lists:member({d, 'TEST'}, proplists:get_value(options, S:module_info(compile), [])).

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

    Name = rebar_test_utils:create_random_name("test_profile_before_eunit_"),
    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:create_config(AppDir, RebarConfig),

    {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}),
    code:add_paths(rebar_state:code_paths(State, all_deps)),

    T = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"),
    true = lists:member({d, 'TEST'}, proplists:get_value(options, T:module_info(compile), [])).

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

    Name = rebar_test_utils:create_random_name("test_profile_applied_to_apps_"),
    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:create_config(AppDir, RebarConfig),

    {ok, State} = rebar_test_utils:run_and_check(Config,
                                                 RebarConfig,
                                                 ["eunit"],
                                                 return),

    Apps = rebar_state:project_apps(State),
    lists:foreach(fun(App) ->
        Opts = rebar_app_info:opts(App),
        ErlOpts = dict:fetch(erl_opts, Opts),
        true = lists:member({d, 'TEST'}, ErlOpts)
    end, Apps).

test_profile_erl_opts_order_1(Config) ->
    Opts = get_compiled_profile_erl_opts([default], Config),
    Opt = last_erl_opt(Opts, [warn_export_all, nowarn_export_all], undefined),
    undefined = Opt.

test_profile_erl_opts_order_2(Config) ->
    Opts = get_compiled_profile_erl_opts([strict], Config),
    Opt = last_erl_opt(Opts, [warn_export_all, nowarn_export_all], undefined),
    warn_export_all = Opt.

test_profile_erl_opts_order_3(Config) ->
    Opts = get_compiled_profile_erl_opts([loose], Config),
    Opt = last_erl_opt(Opts, [warn_export_all, nowarn_export_all], undefined),
    nowarn_export_all = Opt.

test_profile_erl_opts_order_4(Config) ->
    Opts = get_compiled_profile_erl_opts([strict, loose], Config),
    Opt = last_erl_opt(Opts, [warn_export_all, nowarn_export_all], undefined),
    nowarn_export_all = Opt.

test_profile_erl_opts_order_5(Config) ->
    Opts = get_compiled_profile_erl_opts([loose, strict], Config),
    Opt = last_erl_opt(Opts, [warn_export_all, nowarn_export_all], undefined),
    warn_export_all = Opt.

first_files_exception(_Config) ->
    RebarConfig = [{erl_first_files, ["c","a","b"]},
                   {mib_first_files, ["c","a","b"]},
                   {other, ["c","a","b"]},
                   {profiles,
                    [{profile2, [{erl_first_files, ["a","e"]},
                                 {mib_first_files, ["a","e"]},
                                 {other, ["a","e"]}
                                ]}]}],
    State = rebar_state:new(RebarConfig),
    State1 = rebar_state:apply_profiles(State, [profile2]),

    %% Combine lists
    ?assertEqual(["a","b","c","e"], rebar_state:get(State1, other)),
    %% there is no specific reason not to dedupe "a" here aside from "this is how it is"
    ?assertEqual(["c","a","b","a","e"], rebar_state:get(State1, erl_first_files)),
    ?assertEqual(["c","a","b","a","e"], rebar_state:get(State1, mib_first_files)),
    ok.

get_compiled_profile_erl_opts(Profiles, Config) ->
    AppDir = ?config(apps, Config),
    PStrs = [atom_to_list(P) || P <- Profiles],

    Name = rebar_test_utils:create_random_name(
        lists:flatten(["erl_opts_order_" | [[S, $_] || S <- PStrs]])),
    Vsn = rebar_test_utils:create_random_vsn(),
    rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),

    RebarConfig = [
        {erl_opts, [warnings_as_errors, {d, profile_default}]},
        {profiles, [
            {strict, [{erl_opts, [warn_export_all, {d, profile_strict}]}]},
            {loose, [{erl_opts, [nowarn_export_all, {d, profile_loose}]}]} ]}],
    rebar_test_utils:create_config(AppDir, RebarConfig),

    Command = case Profiles of
        [] ->
            ["compile"];
        [default] ->
            ["compile"];
        _ ->
            ["as", string:join(PStrs, ","), "compile"]
    end,
    {ok, State} = rebar_test_utils:run_and_check(
        Config, RebarConfig, Command, {ok, [{app, Name}]}),
    code:add_paths(rebar_state:code_paths(State, all_deps)),
    Mod = list_to_atom(Name),
    proplists:get_value(options, Mod:module_info(compile), []).

% macro definitions get special handling
last_erl_opt([{d, Macro} = Opt | Opts], Targets, Last) ->
    case lists:any(erl_opt_macro_match_fun(Macro), Targets) of
        true ->
            last_erl_opt(Opts, Targets, Opt);
        _ ->
            last_erl_opt(Opts, Targets, Last)
    end;
last_erl_opt([{d, Macro, _} = Opt | Opts], Targets, Last) ->
    case lists:any(erl_opt_macro_match_fun(Macro), Targets) of
        true ->
            last_erl_opt(Opts, Targets, Opt);
        _ ->
            last_erl_opt(Opts, Targets, Last)
    end;
last_erl_opt([Opt | Opts], Targets, Last) ->
    case lists:member(Opt, Targets) of
        true ->
            last_erl_opt(Opts, Targets, Opt);
        _ ->
            last_erl_opt(Opts, Targets, Last)
    end;
last_erl_opt([], _, Last) ->
    Last.

erl_opt_macro_match_fun(Macro) ->
    fun({d, M}) ->
            M == Macro;
        ({d, M, _}) ->
            M == Macro;
        (_) ->
            false
    end.