diff options
-rw-r--r-- | src/rebar_prv_eunit.erl | 36 | ||||
-rw-r--r-- | test/rebar_eunit_SUITE.erl | 60 |
2 files changed, 89 insertions, 7 deletions
diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index 41dd434..5d0f65f 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -109,7 +109,7 @@ prepare_tests(State) -> {RawOpts, _} = rebar_state:command_parsed_args(State), ok = maybe_cover_compile(State, RawOpts), ProjectApps = project_apps(State), - resolve_apps(ProjectApps, RawOpts). + resolve_tests(ProjectApps, RawOpts). maybe_cover_compile(State, Opts) -> State1 = case proplists:get_value(cover, Opts, false) of @@ -118,6 +118,26 @@ maybe_cover_compile(State, Opts) -> end, rebar_prv_cover:maybe_cover_compile(State1). +resolve_tests(ProjectApps, RawOpts) -> + case proplists:get_value(file, RawOpts) of + undefined -> resolve_apps(ProjectApps, RawOpts); + Files -> resolve_files(ProjectApps, Files, RawOpts) + end. + +resolve_files(ProjectApps, Files, RawOpts) -> + case {proplists:get_value(app, RawOpts), proplists:get_value(suite, RawOpts)} of + {undefined, undefined} -> resolve_files(Files, []); + _ -> + case resolve_apps(ProjectApps, RawOpts) of + {ok, TestSet} -> resolve_files(Files, TestSet); + Error -> Error + end + end. + +resolve_files(Files, TestSet) -> + FileNames = string:tokens(Files, [$,]), + {ok, TestSet ++ set_files(FileNames, [])}. + resolve_apps(ProjectApps, RawOpts) -> case proplists:get_value(app, RawOpts) of undefined -> resolve_suites(ProjectApps, RawOpts); @@ -217,6 +237,10 @@ set_suites([], Acc) -> lists:reverse(Acc); set_suites([Suite|Rest], Acc) -> set_suites(Rest, [{module, list_to_atom(Suite)}|Acc]). +set_files([], Acc) -> lists:reverse(Acc); +set_files([File|Rest], Acc) -> + set_files(Rest, [{file, File}|Acc]). + resolve_eunit_opts(State) -> {Opts, _} = rebar_state:command_parsed_args(State), EUnitOpts = rebar_state:get(State, eunit_opts, []), @@ -241,10 +265,12 @@ handle_results({error, Reason}) -> eunit_opts(_State) -> [{app, undefined, "app", string, help(app)}, {cover, $c, "cover", boolean, help(cover)}, + {file, $f, "file", string, help(file)}, {suite, undefined, "suite", string, help(suite)}, {verbose, $v, "verbose", boolean, help(verbose)}]. -help(app) -> "List of application test suites to run"; -help(cover) -> "Generate cover data"; -help(suite) -> "List of test suites to run"; -help(verbose) -> "Verbose output". +help(app) -> "Comma seperated list of application test suites to run. Equivalent to `[{application, App}]`."; +help(cover) -> "Generate cover data. Defaults to false."; +help(file) -> "Comma seperated list of files to run. Equivalent to `[{file, File}]`."; +help(suite) -> "Comma seperated list of test suites to run. Equivalent to `[{module, Suite}]`."; +help(verbose) -> "Verbose output. Defaults to false.". diff --git a/test/rebar_eunit_SUITE.erl b/test/rebar_eunit_SUITE.erl index 79decac..d2dac1d 100644 --- a/test/rebar_eunit_SUITE.erl +++ b/test/rebar_eunit_SUITE.erl @@ -18,7 +18,10 @@ test_single_suite_flag/1, test_suite_in_app_flag/1, test_suite_in_wrong_app_flag/1, - test_nonexistent_suite_flag/1]). + test_nonexistent_suite_flag/1, + test_single_file_flag/1, + test_multiple_file_flag/1, + test_nonexistent_file_flag/1]). -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). @@ -42,7 +45,8 @@ all() -> test_basic_defines, test_multi_defines, test_single_app_flag, test_multiple_app_flag, test_nonexistent_app_flag, test_single_suite_flag, test_suite_in_app_flag, - test_suite_in_wrong_app_flag, test_nonexistent_suite_flag]. + test_suite_in_wrong_app_flag, test_nonexistent_suite_flag, + test_single_file_flag, test_multiple_file_flag, test_nonexistent_file_flag]. test_basic_app(Config) -> AppDir = ?config(apps, Config), @@ -406,3 +410,55 @@ test_nonexistent_suite_flag(Config) -> return), Error = {error_running_tests, "Module `not_a_real_module' not found in applications."}. + +test_single_file_flag(Config) -> + AppDir = ?config(apps, Config), + + Name = rebar_test_utils:create_random_name("single_file_flag_app_"), + 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", "--file=not_a_real_src_" ++ Name ++ "_tests.beam"], + {ok, [{app, Name}]}), + + File = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"), + {module, File} = code:ensure_loaded(File). + +test_multiple_file_flag(Config) -> + AppDir = ?config(apps, Config), + + Name = rebar_test_utils:create_random_name("multiple_file_flag_app_"), + 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", "--file=not_a_real_src_" ++ Name ++ "_tests.beam,not_a_real_src_" ++ Name ++ ".beam"], + {ok, [{app, Name}]}), + + File1 = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"), + {module, File1} = code:ensure_loaded(File1), + + File2 = list_to_atom("not_a_real_src_" ++ Name), + {module, File2} = code:ensure_loaded(File2). + +test_nonexistent_file_flag(Config) -> + AppDir = ?config(apps, Config), + + Name = rebar_test_utils:create_random_name("nonexistent_file_flag_app_"), + Vsn = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_eunit_app(AppDir, + Name, + Vsn, + [kernel, stdlib]), + + RebarConfig = [{erl_opts, [{d, some_define}]}], + {error, {rebar_prv_eunit, _Error}} = rebar_test_utils:run_and_check(Config, + RebarConfig, + ["eunit", "--file=" ++ filename:join(["some_path", "not_a_real_file.erl"])], + return). + |