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 (vladdu55@gmail.com) 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%% @author Vlad Dumitrescu <vladdu55@gmail.com> 28%% @author Chris Bernard <cebernard@gmail.com> 29%% @doc This tests functionality provided by the rebar command 'compile'. 30%% ------------------------------------------------------------------- 31-module(rebar_compiler_tests). 32 33-include_lib("eunit/include/eunit.hrl"). 34 35%% Assuming this test is run inside the rebar 'eunit' 36%% command, the current working directory will be '.eunit' 37-define(REBAR_SCRIPT, "../rebar"). 38 39-define(TMP_DIR, "tmp_eunit/"). 40 41%% ==================================================================== 42%% Rebar compiler tests 43%% ==================================================================== 44 45keep_going_test_() -> 46 {"Ensure compiler keeps going after finding errors, when --keep-going is requested", 47 setup, 48 fun() -> 49 setup_basic_project(), 50 rebar("-k compile") 51 end, 52 fun teardown/1, 53 fun(RebarOut)-> 54 [ 55 {"Continue after error", 56 ?_assert(string:str(RebarOut, "Continuing after build error") =/= 0)}, 57 ?_assert(string:str(RebarOut, "ERROR: compile failed") == 0) 58 ] ++ assert_files_in("ebin", ["ebin/myapp_mymod2.beam"]) 59 end 60 }. 61 62keep_going_firstfiles_test_() -> 63 {"Ensure compiler keeps going after finding errors in erl_first_files, when --keep-going is requested", 64 setup, 65 fun() -> 66 setup_basic_project(), 67 setup_rebar_config(), 68 rebar("-k compile") 69 end, 70 fun teardown/1, 71 fun(RebarOut)-> 72 [ 73 {"Continue after error", 74 ?_assert(string:str(RebarOut, "Continuing after build error") =/= 0)}, 75 ?_assert(string:str(RebarOut, "ERROR: compile failed") == 0) 76 ] ++ assert_files_in("ebin", ["ebin/myapp_mymod2.beam"]) 77 end 78 }. 79 80not_keep_going_test_() -> 81 {"Ensure compiler stops after finding errors", 82 setup, 83 fun() -> 84 setup_basic_project(), 85 setup_rebar_config() 86 end, 87 fun teardown/1, 88 fun()-> 89 RebarOut = rebar("compile"), 90 [ 91 {"Exit after error", 92 ?_assert(string:str(RebarOut, "ERROR: compile failed") =/= 0)} 93 ] ++ assert_files_not_in("ebin", ["ebin/myapp_mymod2.beam"]) 94 end 95 }. 96 97%% ==================================================================== 98%% Setup and Teardown 99%% ==================================================================== 100 101-define(rebar_config, 102 ["{erl_first_files, [\"src/myapp_mymod1.erl\"]}." 103 ]). 104 105%% this file has a syntax error 106-define(myapp_mymod1, 107 ["-module(myapp_mymod1).\n", 108 "-export([myfunc/0]).\n", 109 "my func() -> [4], ok.\n"]). 110 111-define(myapp_mymod2, 112 ["-module(myapp_mymod2).\n", 113 "-export([myfunc/0]).\n", 114 "myfunc() -> [4], ok.\n"]). 115 116 117make_tmp_dir() -> 118 case file:make_dir(?TMP_DIR) of 119 ok -> 120 ok; 121 {error, eexist} -> 122 remove_tmp_dir(), 123 make_tmp_dir(); 124 Error -> 125 throw(Error) 126 end. 127 128setup_environment() -> 129 ok = make_tmp_dir(), 130 prepare_rebar_script(), 131 ok = file:set_cwd(?TMP_DIR). 132 133setup_rebar_config() -> 134 ok = file:write_file("rebar.config", ?rebar_config). 135 136setup_basic_project() -> 137 setup_environment(), 138 rebar("create-app appid=myapp"), 139 ok = file:make_dir("ebin"), 140 ok = file:write_file("src/myapp_mymod1.erl", ?myapp_mymod1), 141 ok = file:write_file("src/myapp_mymod2.erl", ?myapp_mymod2). 142 143teardown(_) -> 144 ok = file:set_cwd(".."), 145 ok = remove_tmp_dir(). 146 147remove_tmp_dir() -> 148 remove_tmp_dir(arg_for_eunit). 149 150remove_tmp_dir(_) -> 151 ok = rebar_file_utils:rm_rf(?TMP_DIR). 152 153%% ==================================================================== 154%% Helper Functions 155%% ==================================================================== 156 157prepare_rebar_script() -> 158 Rebar = ?TMP_DIR ++ "rebar", 159 {ok, _} = file:copy(?REBAR_SCRIPT, Rebar), 160 case os:type() of 161 {unix, _} -> 162 [] = os:cmd("chmod u+x " ++ Rebar); 163 {win32, _} -> 164 {ok, _} = file:copy(?REBAR_SCRIPT ++ ".cmd", 165 ?TMP_DIR ++ "rebar.cmd") 166 end. 167 168rebar(Args) when is_list(Args) -> 169 Out = os:cmd(filename:nativename("./rebar") ++ " " ++ Args), 170 %% ?debugMsg("**** Begin"), ?debugMsg(Out), ?debugMsg("**** End"), 171 Out. 172 173assert_files_in(Name, [File|T]) -> 174 [{Name ++ " has file: " ++ File, ?_assert(filelib:is_regular(File))} | 175 assert_files_in(Name, T)]; 176assert_files_in(_, []) -> []. 177 178assert_files_not_in(Name, [File|T]) -> 179 [{Name ++ " does not have file: " ++ File, 180 ?_assertNot(filelib:is_regular(File))} | assert_files_not_in(Name, T)]; 181assert_files_not_in(_, []) -> []. 182 183