From 069ed62690a3208c7a80ca440b5ae3efa6c7ad5e Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Fri, 20 Mar 2015 06:33:17 -0700 Subject: add a more direct way to generate an `eunitized` test app to test utils and add better eunit tests --- test/rebar_eunit_SUITE.erl | 159 ++++++++++++++++++++++++++++++++++++++++-- test/rebar_profiles_SUITE.erl | 8 +-- test/rebar_test_utils.erl | 30 ++++++-- 3 files changed, 181 insertions(+), 16 deletions(-) (limited to 'test') diff --git a/test/rebar_eunit_SUITE.erl b/test/rebar_eunit_SUITE.erl index bf6b8ec..39dc30c 100644 --- a/test/rebar_eunit_SUITE.erl +++ b/test/rebar_eunit_SUITE.erl @@ -4,9 +4,14 @@ init_per_suite/1, end_per_suite/1, init_per_testcase/2, - all/0, - test_basic_app/1, - test_profile/1]). + all/0]). +-export([test_basic_app/1, + test_multi_app/1, + test_profile/1, + test_basic_exports/1, + test_multi_exports/1, + test_basic_defines/1, + test_multi_defines/1]). -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). @@ -25,25 +30,165 @@ init_per_testcase(_, Config) -> rebar_test_utils:init_rebar_state(Config, "eunit_"). all() -> - [test_basic_app, test_profile]. + [test_basic_app, test_multi_app, test_profile, + test_basic_exports, test_multi_exports, + test_basic_defines, test_multi_defines]. test_basic_app(Config) -> AppDir = ?config(apps, Config), Name = rebar_test_utils:create_random_name("basic_"), Vsn = rebar_test_utils:create_random_vsn(), - rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), RebarConfig = [{erl_opts, [{d, some_define}]}], rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}). +test_multi_app(Config) -> + AppDir = ?config(apps, Config), + + Name1 = rebar_test_utils:create_random_name("multi_app1_"), + Vsn1 = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_eunit_app(filename:join([AppDir,Name1]), + Name1, + Vsn1, + [kernel, stdlib]), + Name2 = rebar_test_utils:create_random_name("multi_app2_"), + Vsn2 = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_eunit_app(filename:join([AppDir,Name2]), + Name2, + Vsn2, + [kernel, stdlib]), + + RebarConfig = [{erl_opts, [{d, some_define}]}], + rebar_test_utils:run_and_check(Config, + RebarConfig, + ["eunit"], + {ok, [{app, Name1}, {app, Name2}]}). + test_profile(Config) -> AppDir = ?config(apps, Config), - Name = rebar_test_utils:create_random_name("basic_"), + Name = rebar_test_utils:create_random_name("profile_"), Vsn = rebar_test_utils:create_random_vsn(), rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), RebarConfig = [{erl_opts, [{d, some_define}]}, {profiles, [{test, [{erl_opts, [debug_info]}]}]}], - rebar_test_utils:run_and_check(Config, RebarConfig, ["as", "test", "eunit"], {ok, [{app, Name}]}). + rebar_test_utils:run_and_check(Config, + RebarConfig, + ["as", "test", "eunit"], + {ok, [{app, Name}]}). + +test_basic_exports(Config) -> + AppDir = ?config(apps, Config), + + Name = rebar_test_utils:create_random_name("basic_exports_"), + Vsn = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), + + RebarConfig = [{erl_opts, [{d, some_define}]}], + rebar_test_utils:run_and_check(Config, + RebarConfig, + ["eunit"], + {ok, [{app, Name}]}), + + App = list_to_atom("not_a_real_src_" ++ Name), + Suite = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"), + AppExports = App:module_info(exports), + SuiteExports = Suite:module_info(exports), + AppExpect = [{some_test_, 0}], + SuiteExpect = [{some_test_, 0}, {define_test_, 0}], + lists:foreach(fun(Expect) -> true = lists:member(Expect, AppExports) end, AppExpect), + lists:foreach(fun(Expect) -> true = lists:member(Expect, SuiteExports) end, SuiteExpect). + +test_multi_exports(Config) -> + AppDir = ?config(apps, Config), + + Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"), + Vsn1 = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_eunit_app(filename:join([AppDir,Name1]), + Name1, + Vsn1, + [kernel, stdlib]), + Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"), + Vsn2 = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_eunit_app(filename:join([AppDir,Name2]), + Name2, + Vsn2, + [kernel, stdlib]), + + RebarConfig = [{erl_opts, [{d, some_define}]}], + rebar_test_utils:run_and_check(Config, + RebarConfig, + ["eunit"], + {ok, [{app, Name1}, {app, Name2}]}), + + App1 = list_to_atom("not_a_real_src_" ++ Name1), + Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"), + AppExports1 = App1:module_info(exports), + SuiteExports1 = Suite1:module_info(exports), + App2 = list_to_atom("not_a_real_src_" ++ Name2), + Suite2 = list_to_atom("not_a_real_src_" ++ Name2 ++ "_tests"), + AppExports2 = App2:module_info(exports), + SuiteExports2 = Suite2:module_info(exports), + AppExpect = [{some_test_, 0}], + SuiteExpect = [{some_test_, 0}, {define_test_, 0}], + lists:foreach(fun(Expect) -> true = lists:member(Expect, AppExports1) end, AppExpect), + lists:foreach(fun(Expect) -> true = lists:member(Expect, SuiteExports1) end, SuiteExpect), + lists:foreach(fun(Expect) -> true = lists:member(Expect, AppExports2) end, AppExpect), + lists:foreach(fun(Expect) -> true = lists:member(Expect, SuiteExports2) end, SuiteExpect). + +test_basic_defines(Config) -> + AppDir = ?config(apps, Config), + + Name = rebar_test_utils:create_random_name("basic_"), + Vsn = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), + + RebarConfig = [{erl_opts, [{d, some_define}]}], + rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}), + + App = list_to_atom("not_a_real_src_" ++ Name), + Suite = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"), + AppOpts = proplists:get_value(options, App:module_info(compile), []), + SuiteOpts = proplists:get_value(options, Suite:module_info(compile), []), + Expect = [{d, some_define}], + lists:foreach(fun(Expect) -> true = lists:member(Expect, AppOpts) end, Expect), + lists:foreach(fun(Expect) -> true = lists:member(Expect, SuiteOpts) end, Expect). + +test_multi_defines(Config) -> + AppDir = ?config(apps, Config), + + Name1 = rebar_test_utils:create_random_name("multi_app1_"), + Vsn1 = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_eunit_app(filename:join([AppDir,Name1]), + Name1, + Vsn1, + [kernel, stdlib]), + Name2 = rebar_test_utils:create_random_name("multi_app2_"), + Vsn2 = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_eunit_app(filename:join([AppDir,Name2]), + Name2, + Vsn2, + [kernel, stdlib]), + + RebarConfig = [{erl_opts, [{d, some_define}]}], + rebar_test_utils:run_and_check(Config, + RebarConfig, + ["eunit"], + {ok, [{app, Name1}, {app, Name2}]}), + + App1 = list_to_atom("not_a_real_src_" ++ Name1), + Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"), + AppOpts1 = proplists:get_value(options, App1:module_info(compile), []), + SuiteOpts1 = proplists:get_value(options, Suite1:module_info(compile), []), + App2 = list_to_atom("not_a_real_src_" ++ Name2), + Suite2 = list_to_atom("not_a_real_src_" ++ Name2 ++ "_tests"), + AppOpts2 = proplists:get_value(options, App2:module_info(compile), []), + SuiteOpts2 = proplists:get_value(options, Suite2:module_info(compile), []), + Expect = [{d, some_define}], + lists:foreach(fun(Expect) -> true = lists:member(Expect, AppOpts1) end, Expect), + lists:foreach(fun(Expect) -> true = lists:member(Expect, SuiteOpts1) end, Expect), + lists:foreach(fun(Expect) -> true = lists:member(Expect, AppOpts2) end, Expect), + lists:foreach(fun(Expect) -> true = lists:member(Expect, SuiteOpts2) end, Expect). diff --git a/test/rebar_profiles_SUITE.erl b/test/rebar_profiles_SUITE.erl index ab37255..6288053 100644 --- a/test/rebar_profiles_SUITE.erl +++ b/test/rebar_profiles_SUITE.erl @@ -172,7 +172,7 @@ test_profile_applied_at_completion(Config) -> Name = rebar_test_utils:create_random_name("test_profile_at_completion_"), Vsn = rebar_test_utils:create_random_vsn(), - rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), RebarConfig = [{erl_opts, [{d, some_define}]}], rebar_test_utils:create_config(AppDir, RebarConfig), @@ -191,7 +191,7 @@ test_profile_applied_before_compile(Config) -> Name = rebar_test_utils:create_random_name("test_profile_before_compile_"), Vsn = rebar_test_utils:create_random_vsn(), - rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), RebarConfig = [{erl_opts, [{d, some_define}]}], rebar_test_utils:create_config(AppDir, RebarConfig), @@ -206,7 +206,7 @@ test_profile_applied_before_eunit(Config) -> Name = rebar_test_utils:create_random_name("test_profile_before_eunit_"), Vsn = rebar_test_utils:create_random_vsn(), - rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), RebarConfig = [{erl_opts, [{d, some_define}]}], rebar_test_utils:create_config(AppDir, RebarConfig), @@ -221,7 +221,7 @@ test_profile_applied_to_apps(Config) -> Name = rebar_test_utils:create_random_name("test_profile_applied_to_apps_"), Vsn = rebar_test_utils:create_random_vsn(), - rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), RebarConfig = [{erl_opts, [{d, some_define}]}], rebar_test_utils:create_config(AppDir, RebarConfig), diff --git a/test/rebar_test_utils.erl b/test/rebar_test_utils.erl index 7d57e0d..2eb14ac 100644 --- a/test/rebar_test_utils.erl +++ b/test/rebar_test_utils.erl @@ -3,7 +3,7 @@ -include_lib("eunit/include/eunit.hrl"). -export([init_rebar_state/1, init_rebar_state/2, run_and_check/4]). -export([expand_deps/2, flat_deps/1, flat_pkgdeps/1, top_level_deps/1]). --export([create_app/4, create_empty_app/4, create_config/2]). +-export([create_app/4, create_eunit_app/4, create_empty_app/4, create_config/2]). -export([create_random_name/1, create_random_vsn/0]). %%%%%%%%%%%%%% @@ -70,7 +70,17 @@ 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). + +%% @doc Creates a dummy application including: +%% - src/.erl +%% - src/.app.src +%% - test/_tests.erl +%% And returns a `rebar_app_info' object. +create_eunit_app(AppDir, Name, Vsn, Deps) -> + write_eunitized_src_file(AppDir, Name), + write_eunit_suite_file(AppDir, Name), write_app_src_file(AppDir, Name, Vsn, Deps), rebar_app_info:new(Name, Vsn, AppDir, Deps). @@ -260,10 +270,15 @@ write_src_file(Dir, Name) -> ok = filelib:ensure_dir(Erl), ok = ec_file:write(Erl, erl_src_file("not_a_real_src_" ++ Name ++ ".erl")). -write_test_file(Dir, Name) -> +write_eunitized_src_file(Dir, Name) -> + Erl = filename:join([Dir, "src", "not_a_real_src_" ++ Name ++ ".erl"]), + ok = filelib:ensure_dir(Erl), + ok = ec_file:write(Erl, erl_eunitized_src_file("not_a_real_src_" ++ Name ++ ".erl")). + +write_eunit_suite_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")). + ok = ec_file:write(Erl, erl_eunit_suite_file("not_a_real_src_" ++ Name ++ ".erl")). write_app_file(Dir, Name, Version, Deps) -> Filename = filename:join([Dir, "ebin", Name ++ ".app"]), @@ -278,13 +293,18 @@ 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")]). + +erl_eunitized_src_file(Name) -> + io_lib:format("-module(~s).\n" + "-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) -> +erl_eunit_suite_file(Name) -> BaseName = filename:basename(Name, ".erl"), io_lib:format("-module(~s_tests).\n" "-compile(export_all).\n" -- cgit v1.1