summaryrefslogtreecommitdiff
path: root/test/rebar_dia_tests.erl
blob: 0561bbbe155eed3aa4ef32efe5949b78d9e23706 (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
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et
-module(rebar_dia_tests).

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

-define(REBAR_SCRIPT, "../rebar").

-define(TMP_DIR, "dia_project/").

dia_test_() ->
    case supported_otp_version() of
        true ->
            {"Test the dia compiler",
             setup, fun() -> setup_project(), rebar("compile") end,
             fun teardown/1,
             fun(RebarOut) ->
                 [{"Check ebin is created",
                   ?_assert(filelib:is_dir("ebin") =:= true)},

                  {"Check include is created",
                   ?_assert(filelib:is_dir("include") =:= true)},

                  {"Check dia/a.dia is compiled",
                   ?_assert(string:str(RebarOut, "Compiled dia/a.dia") /= 0)},

                  {"Check dia/b.dia is compiled",
                   ?_assert(string:str(RebarOut, "Compiled dia/b.dia") /= 0)}
                 ]
             end};
        false -> {setup, fun() -> ok end, []}
    end.


%% ====================================================================
%% Setup and Teardown
%% ====================================================================

-define(myapp_dia_a,
        ["@id     0\n",
         "@name   a\n",
         "@vendor 1 ABC\n",
         "@avp_types\n",
         "   IMSI 1 UTF8String V"
        ]).

-define(myapp_dia_b,
        ["@id     2\n",
         "@name   b\n",
         "@vendor 2 ABC\n",
         "@inherits a\n",
         "@avp_types\n",
         "   IMEIV 900 OctetString MV"
        ]).

-define(myapp_rebarconfig,
        ["{dia_first_files, [\"dia/a.dia\"]}.\n",
         "{dia_opts, []}.\n"
        ]).

supported_otp_version() ->
    Min = rebar_require_vsn:version_tuple(keep_going, "15", "configured"),
    Otp = rebar_require_vsn:version_tuple(keep_going,
                                          erlang:system_info(otp_release),
                                          "OTP Release"),
    Otp >= Min.


setup_environment() ->
    ok = file:make_dir(?TMP_DIR),
    prepare_rebar_script(),
    ok = file:set_cwd(?TMP_DIR).

prepare_project() ->
    setup_environment(),
    rebar("create-app appid=myapp").


setup_project() ->
    prepare_project(),
    ok = file:make_dir("dia"),
    ok = file:write_file("dia/a.dia", ?myapp_dia_a),
    ok = file:write_file("dia/b.dia", ?myapp_dia_b),
    ok = file:write_file("rebar.config", ?myapp_rebarconfig).


teardown(_) ->
    ok = file:set_cwd(".."),
    ok = remove_tmp_dir().

remove_tmp_dir() ->
    ok = rebar_file_utils:rm_rf(?TMP_DIR).

%% ====================================================================
%% Helper Functions
%% ====================================================================

prepare_rebar_script() ->
    Rebar = ?TMP_DIR ++ "rebar",
    {ok, _} = file:copy(?REBAR_SCRIPT, Rebar),
    case os:type() of
        {unix, _} ->
            [] = os:cmd("chmod u+x " ++ Rebar);
        {win32, _} ->
            {ok, _} = file:copy(?REBAR_SCRIPT ++ ".cmd",
                                ?TMP_DIR ++ "rebar.cmd")
    end.

rebar(Args) when is_list(Args) ->
    os:cmd(filename:nativename("./rebar") ++ " " ++ Args).