diff options
-rw-r--r-- | test/rebar_eunit_SUITE.erl | 37 | ||||
-rw-r--r-- | test/rebar_test_utils.erl | 31 |
2 files changed, 64 insertions, 4 deletions
diff --git a/test/rebar_eunit_SUITE.erl b/test/rebar_eunit_SUITE.erl new file mode 100644 index 0000000..190fbfa --- /dev/null +++ b/test/rebar_eunit_SUITE.erl @@ -0,0 +1,37 @@ +-module(rebar_eunit_SUITE). + +-export([suite/0, + init_per_suite/1, + end_per_suite/1, + init_per_testcase/2, + all/0, + test_basic_app/1]). + +-include_lib("common_test/include/ct.hrl"). +-include_lib("eunit/include/eunit.hrl"). +-include_lib("kernel/include/file.hrl"). + +suite() -> + []. + +init_per_suite(Config) -> + Config. + +end_per_suite(_Config) -> + ok. + +init_per_testcase(_, Config) -> + rebar_test_utils:init_rebar_state(Config). + +all() -> + [test_basic_app]. + +test_basic_app(Config) -> + AppDir = ?config(apps, Config), + + Name = rebar_test_utils:create_random_name("eunit_"), + Vsn = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + + RebarConfig = [{erl_opts, [{d, some_define}]}], + rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}). diff --git a/test/rebar_test_utils.erl b/test/rebar_test_utils.erl index 4982929..0a74a5f 100644 --- a/test/rebar_test_utils.erl +++ b/test/rebar_test_utils.erl @@ -62,6 +62,7 @@ run_and_check(Config, RebarConfig, Command, Expect) -> %% And returns a `rebar_app_info' object. create_app(AppDir, Name, Vsn, Deps) -> write_src_file(AppDir, Name), + write_test_file(AppDir, Name), write_app_src_file(AppDir, Name, Vsn, Deps), rebar_app_info:new(Name, Vsn, AppDir, Deps). @@ -133,9 +134,14 @@ check_results(AppDir, Expected) -> end, Expected). write_src_file(Dir, Name) -> - Erl = filename:join([Dir, "src", "not_a_real_src" ++ Name ++ ".erl"]), + Erl = filename:join([Dir, "src", "not_a_real_src_" ++ Name ++ ".erl"]), ok = filelib:ensure_dir(Erl), - ok = ec_file:write(Erl, erl_src_file("not_a_real_src" ++ Name ++ ".erl")). + ok = ec_file:write(Erl, erl_src_file("not_a_real_src_" ++ Name ++ ".erl")). + +write_test_file(Dir, Name) -> + Erl = filename:join([Dir, "test", "not_a_real_src_" ++ Name ++ "_tests.erl"]), + ok = filelib:ensure_dir(Erl), + ok = ec_file:write(Erl, erl_test_file("not_a_real_src_" ++ Name ++ ".erl")). write_app_file(Dir, Name, Version, Deps) -> Filename = filename:join([Dir, "ebin", Name ++ ".app"]), @@ -149,8 +155,25 @@ write_app_src_file(Dir, Name, Version, Deps) -> erl_src_file(Name) -> io_lib:format("-module(~s).\n" - "-export([main/0]).\n" - "main() -> ok.\n", [filename:basename(Name, ".erl")]). + "-export([main/0]).\n" + "main() -> ok.\n" + "-ifdef(TEST).\n" + "-include_lib(\"eunit/include/eunit.hrl\").\n" + "some_test_() -> ?_assertEqual(ok, main()).\n" + "-endif.\n", [filename:basename(Name, ".erl")]). + +erl_test_file(Name) -> + BaseName = filename:basename(Name, ".erl"), + io_lib:format("-module(~s_tests).\n" + "-compile(export_all).\n" + "-ifndef(some_define).\n" + "-define(some_define, false).\n" + "-endif.\n" + "-ifdef(TEST).\n" + "-include_lib(\"eunit/include/eunit.hrl\").\n" + "some_test_() -> ?_assertEqual(ok, ~s:main()).\n" + "define_test_() -> ?_assertEqual(true, ?some_define).\n" + "-endif.\n", [BaseName, BaseName]). get_app_metadata(Name, Vsn, Deps) -> {application, erlang:list_to_atom(Name), |