summaryrefslogtreecommitdiff
path: root/inttest/proto_gpb/proto_gpb_rt.erl
blob: 307aca0cca50800b087735913f17682862630b9d (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
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et
%% -------------------------------------------------------------------
%%
%% rebar: Erlang Build Tools
%%
%% Copyright (c) 2014 Luis Rascão (luis.rascao@gmail.com)
%%
%% Permission is hereby granted, free of charge, to any person obtaining a copy
%% of this software and associated documentation files (the "Software"), to deal
%% in the Software without restriction, including without limitation the rights
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the Software is
%% furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%% THE SOFTWARE.
%% -------------------------------------------------------------------
-module(proto_gpb_rt).
-export([files/0,
         run/1]).

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

-define(MODULES,
        [foo,
         foo_app,
         foo_sup]).

-define(GENERATED_MODULES,
        [test_gpb,
         test2_gpb,
         test3_gpb,
         test4_gpb,
         test5_gpb]).

-define(SOURCE_PROTO_FILES,
        ["test.proto",
         "a/test2.proto",
         "a/b/test3.proto",
         "c/test4.proto",
         "c/d/test5.proto"]).

setup([Target]) ->
  retest_utils:load_module(filename:join(Target, "inttest_utils.erl")),
  ok.

files() ->
    [
     {copy, "rebar.config", "rebar.config"},
     {copy, "rebar2.config", "rebar2.config"},
     {copy, "rebar.bad.config", "rebar.bad.config"},
     {copy, "include", "include"},
     {copy, "src", "src"},
     {copy, "proto", "proto"},
     {copy, "proto.bad", "proto.bad"},
     {copy, "mock", "deps"},
     {create, "ebin/foo.app", app(foo, ?MODULES ++ ?GENERATED_MODULES)}
    ] ++ inttest_utils:rebar_setup().

run(_Dir) ->
    % perform test obtaining the .proto files from src dir
    ok = run_from_dir(success_expected, "src", "rebar.config"),
    % perform test obtaining the .proto files from proto dir
    ok = run_from_dir(success_expected, "proto", "rebar2.config"),
    % perform a test where a failure is expected
    ok = run_from_dir(fail_expected, "proto.bad", "rebar.bad.config").

run_from_dir(fail_expected, _ProtoDir, ConfigFile) ->
    %% we expect a failure to happen, however rebar should not crash;
    %% We make sure of that by scanning the error.
    {error, {stopped, {1, Error}}} = retest_sh:run("./rebar --config "
                                                   ++ ConfigFile
                                                   ++ " compile",
                                                   []),
    %% No matches of the string 'EXIT' should occur, these
    %% indicate a rebar crash and not a exit with error.
    0 = string:str(lists:flatten(Error), "'EXIT'"),
    ok;
run_from_dir(success_expected, ProtoDir, ConfigFile) ->
    ?assertMatch({ok, _}, retest_sh:run("./rebar --config "
                                        ++ ConfigFile
                                        ++ " clean",
                                        [])),
    ?assertMatch({ok, _}, retest_sh:run("./rebar --config "
                                        ++ ConfigFile
                                        ++ " compile",
                                        [])),
    %% Foo includes test_gpb.hrl,
    %% So if it compiled, that also means gpb succeeded in
    %% generating the test_gpb.hrl file, and also that it generated
    %% the .hrl file was generated before foo was compiled.
    ok = check_beams_generated(),

    retest_log:log(debug, "Verifying recompilation~n", []),
    TestErl = hd(generated_erl_files()),
    TestProto = hd(source_proto_files(ProtoDir)),
    make_proto_newer_than_erl(TestProto, TestErl),
    TestMTime1 = read_mtime(TestErl),
    ?assertMatch({ok, _}, retest_sh:run("./rebar --config "
                                        ++ ConfigFile
                                        ++ " compile",
                                        [])),
    TestMTime2 = read_mtime(TestErl),
    ?assert(TestMTime2 > TestMTime1),

    retest_log:log(debug, "Verifying recompilation with no changes~n", []),
    TestMTime3 = read_mtime(TestErl),
    ?assertMatch({ok, _}, retest_sh:run("./rebar --config "
                                        ++ ConfigFile
                                        ++ " compile",
                                        [])),
    TestMTime4 = read_mtime(TestErl),
    ?assert(TestMTime3 =:= TestMTime4),

    retest_log:log(debug, "Verify cleanup~n", []),
    ?assertMatch({ok, _}, retest_sh:run("./rebar --config "
                                        ++ ConfigFile
                                        ++ " clean",
                                        [])),
    ok = check_files_deleted(),
    ok.

check_beams_generated() ->
    check(fun filelib:is_regular/1,
          beam_files()).

check_files_deleted() ->
    check(fun file_does_not_exist/1,
          beam_files() ++ generated_erl_files() ++ generated_hrl_files()).

beam_files() ->
    add_dir("ebin", add_ext(?MODULES, ".beam")).

generated_erl_files() ->
    add_dir("src", add_ext(?GENERATED_MODULES, ".erl")).

generated_hrl_files() ->
    add_dir("include", add_ext(?GENERATED_MODULES, ".hrl")).

generated_beam_files() ->
    add_dir("ebin", add_ext(?GENERATED_MODULES, ".beam")).

source_proto_files(ProtoDir) ->
    add_dir(ProtoDir, ?SOURCE_PROTO_FILES).

file_does_not_exist(F) ->
    not filelib:is_regular(F).

add_ext(Modules, Ext) ->
    [lists:concat([Module, Ext]) || Module <- Modules].

add_dir(Dir, Files) ->
    [filename:join(Dir, File) || File <- Files].

read_mtime(File) ->
    {ok, #file_info{mtime=MTime}} = file:read_file_info(File),
    MTime.


make_proto_newer_than_erl(Proto, Erl) ->
    %% Do this by back-dating the erl file instead of touching the
    %% proto file.  Do this instead of sleeping for a second to get a
    %% reliable test.  Sleeping would have been needed sin ce the
    %% #file_info{} (used by eg. filelib:last_modified) does not have
    %% sub-second resolution (even though most file systems have).
    {ok, #file_info{mtime=ProtoMTime}} = file:read_file_info(Proto),
    {ok, ErlInfo} = file:read_file_info(Erl),
    OlderMTime = update_seconds_to_datetime(ProtoMTime, -2),
    OlderErlInfo = ErlInfo#file_info{mtime = OlderMTime},
    ok = file:write_file_info(Erl, OlderErlInfo).

update_seconds_to_datetime(DT, ToAdd) ->
    calendar:gregorian_seconds_to_datetime(
      calendar:datetime_to_gregorian_seconds(DT) + ToAdd).

touch_file(File) ->
    ?assertMatch({ok, _}, retest_sh:run("touch " ++ File, [])).

check(Check, Files) ->
    lists:foreach(
      fun(F) ->
              ?assertMatch({true, _}, {Check(F), F})
      end,
      Files).

%%
%% Generate the contents of a simple .app file
%%
app(Name, Modules) ->
    App = {application, Name,
           [{description, atom_to_list(Name)},
            {vsn, "1"},
            {modules, Modules},
            {registered, []},
            {applications, [kernel, stdlib, gpb]}]},
    io_lib:format("~p.\n", [App]).