1%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
2%% ex: ts=4 sw=4 et
3%% -------------------------------------------------------------------
4%%
5%% rebar: Erlang Build Tools
6%%
7%% Copyright (c) 2014 Vlad Dumitrescu
8%%
9%% Permission is hereby granted, free of charge, to any person obtaining a copy
10%% of this software and associated documentation files (the "Software"), to deal
11%% in the Software without restriction, including without limitation the rights
12%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13%% copies of the Software, and to permit persons to whom the Software is
14%% furnished to do so, subject to the following conditions:
15%%
16%% The above copyright notice and this permission notice shall be included in
17%% all copies or substantial portions of the Software.
18%%
19%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25%% THE SOFTWARE.
26%% -------------------------------------------------------------------
27-module(app_src_script_rt).
28
29-export([setup/1, files/0, run/1]).
30
31-include_lib("eunit/include/eunit.hrl").
32
33setup([Target]) ->
34  retest_utils:load_module(filename:join(Target, "inttest_utils.erl")),
35  ok.
36
37files() ->
38    [
39     {create, "src/app_src_script.app.src.script", app_script(app_src_script)}
40    ] ++ inttest_utils:rebar_setup().
41
42run(Dir) ->
43    retest_log:log(debug, "Running in Dir: ~s~n", [Dir]),
44    {ok, [_Pid|Output]} = retest:sh("./rebar compile -vv",
45                                    [{async, false}]),
46
47    Regexp = "DEBUG: Evaluating config script .*/app_src_script\.app\.src\.script.*",
48    ?assertEqual(true, has_line(Output, Regexp)),
49    retest_log:log(debug, "Evaluated .app.src.script~n", []),
50
51    %% check that ebin/app_src.app exists
52    ?assertMatch(true, filelib:is_regular("ebin/app_src_script.app")),
53    retest_log:log(debug, "Generated ebin/.app~n", []),
54
55    %% check that ebin/.app has vsn="2"
56    {ok, Bin} = file:read_file("ebin/app_src_script.app"),
57    Str = binary_to_list(Bin),
58    ?assertMatch({match, _}, re:run(Str, "{vsn, *\"2\"}")),
59    retest_log:log(debug, "Variable replacement in .app is ok.~n", []),
60
61    %% check that ebin/.app doesn't have foo=ok
62    ?assertMatch(nomatch, re:run(Str, "{foo, *ok}")),
63    retest_log:log(debug, "app.src hasn't 'foo' config.~n", []),
64
65    ok.
66
67has_line([], _RE) ->
68    false;
69has_line([L|T], RE) ->
70    case re:run(L, RE, []) of
71        {match, _Captured} ->
72            true;
73        match ->
74            true;
75        nomatch ->
76            has_line(T, RE)
77    end.
78
79%%
80%% Generate the contents of a simple .app.src.script file
81%%
82app_script(Name) ->
83    "Vsn=\"2\".\n" ++
84        "{application, " ++ atom_to_list(Name) ++ ",
85           [{vsn, Vsn},
86            {modules, []},
87            {registered, []},
88            {applications, [kernel, stdlib]}]}.\n".
89