From d080c96156609e089641a1a2ee268f4f06c74ebc Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Sun, 13 Sep 2015 15:15:56 -0700 Subject: reworked eunit provider to allow access to full range of eunit tests --- src/rebar_prv_eunit.erl | 189 +++++++++++++++++++++++++----------------------- 1 file changed, 97 insertions(+), 92 deletions(-) (limited to 'src') diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index 5d0f65f..309f417 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -63,6 +63,8 @@ do(State) -> do_tests(State, Tests) -> EUnitOpts = resolve_eunit_opts(State), + ?DEBUG("eunit_tests ~p", [Tests]), + ?DEBUG("eunit_opts ~p", [EUnitOpts]), Result = eunit:test(Tests, EUnitOpts), ok = rebar_prv_cover:maybe_write_coverdata(State, ?PROVIDER), case handle_results(Result) of @@ -76,7 +78,9 @@ do_tests(State, Tests) -> format_error(unknown_error) -> io_lib:format("Error running tests", []); format_error({error_running_tests, Reason}) -> - io_lib:format("Error running tests: ~p", [Reason]). + io_lib:format("Error running tests: ~p", [Reason]); +format_error({error, Error}) -> + format_error({error_running_tests, Error}). %% =================================================================== %% Internal functions @@ -108,55 +112,87 @@ first_files(State) -> prepare_tests(State) -> {RawOpts, _} = rebar_state:command_parsed_args(State), ok = maybe_cover_compile(State, RawOpts), + CmdTests = resolve_tests(RawOpts), + CfgTests = rebar_state:get(State, eunit_tests, []), ProjectApps = project_apps(State), - resolve_tests(ProjectApps, RawOpts). - -maybe_cover_compile(State, Opts) -> - State1 = case proplists:get_value(cover, Opts, false) of - true -> rebar_state:set(State, cover_enabled, true); - false -> State - end, - rebar_prv_cover:maybe_cover_compile(State1). + Tests = select_tests(ProjectApps, CmdTests, CfgTests), + validate_tests(State, ProjectApps, Tests). + +resolve_tests(RawOpts) -> + Apps = resolve(app, application, RawOpts), + Dirs = resolve(dir, RawOpts), + Files = resolve(file, RawOpts), + Modules = resolve(module, RawOpts), + Suites = resolve(suite, module, RawOpts), + Apps ++ Dirs ++ Files ++ Modules ++ Suites. + +resolve(Flag, RawOpts) -> resolve(Flag, Flag, RawOpts). + +resolve(Flag, EUnitKey, RawOpts) -> + case proplists:get_value(Flag, RawOpts) of + undefined -> []; + Args -> lists:map(fun(Arg) -> normalize(EUnitKey, Arg) end, string:tokens(Args, [$,])) + end. -resolve_tests(ProjectApps, RawOpts) -> - case proplists:get_value(file, RawOpts) of - undefined -> resolve_apps(ProjectApps, RawOpts); - Files -> resolve_files(ProjectApps, Files, RawOpts) +normalize(Key, Value) when Key == dir; Key == file -> {Key, Value}; +normalize(Key, Value) -> {Key, list_to_atom(Value)}. + +select_tests(ProjectApps, [], []) -> default_tests(ProjectApps); +select_tests(_ProjectApps, A, B) -> A ++ B. + +validate_tests(State, ProjectApps, Tests) -> + {ok, lists:filter(fun(Elem) -> validate(State, ProjectApps, Elem) end, Tests)}. + +validate(_State, ProjectApps, {application, App}) -> + validate_app(App, ProjectApps); +validate(State, _ProjectApps, {dir, Dir}) -> + ok = maybe_compile_dir(State, Dir), + validate_dir(Dir); +validate(State, _ProjectApps, {file, File}) -> + ok = maybe_compile_file(State, File), + validate_file(File); +validate(_State, ProjectApps, {module, Module}) -> + validate_module(Module, ProjectApps); +validate(_State, ProjectApps, {suite, Module}) -> + validate_module(Module, ProjectApps); +validate(_State, ProjectApps, Module) when is_atom(Module) -> + validate_module(Module, ProjectApps); +validate(State, ProjectApps, Path) when is_list(Path) -> + case ec_file:is_dir(Path) of + true -> validate(State, ProjectApps, {dir, Path}); + false -> validate(State, ProjectApps, {file, Path}) + end; +%% unrecognized tests should be included. if they're invalid eunit will error +%% and rebar.config may contain arbitrarily complex tests that are effectively +%% unvalidatable +validate(_State, _ProjectApps, _Test) -> true. + +validate_app(AppName, []) -> + ?WARN(lists:concat(["Application `", AppName, "' not found in project."]), []), + false; +validate_app(AppName, [App|Rest]) -> + case AppName == binary_to_atom(rebar_app_info:name(App), unicode) of + true -> true; + false -> validate_app(AppName, Rest) 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 +validate_dir(Dir) -> + case ec_file:is_dir(Dir) of + true -> true; + false -> ?WARN(lists:concat(["Directory `", Dir, "' not found."]), []), false 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); - %% convert app name strings to `rebar_app_info` objects - Apps -> AppNames = string:tokens(Apps, [$,]), - case filter_apps_by_name(AppNames, ProjectApps) of - {ok, TestApps} -> resolve_suites(TestApps, RawOpts); - Error -> Error - end +validate_file(File) -> + case ec_file:exists(File) of + true -> true; + false -> ?WARN(lists:concat(["File `", File, "' not found."]), []), false end. -resolve_suites(Apps, RawOpts) -> - case proplists:get_value(suite, RawOpts) of - undefined -> test_set(Apps, all); - Suites -> SuiteNames = string:tokens(Suites, [$,]), - case filter_suites_by_apps(SuiteNames, Apps) of - {ok, S} -> test_set(Apps, S); - Error -> Error - end +validate_module(Module, Apps) -> + AppModules = app_modules([binary_to_atom(rebar_app_info:name(A), unicode) || A <- Apps], []), + case lists:member(Module, AppModules) of + true -> true; + false -> ?WARN(lists:concat(["Module `", Module, "' not found in applications."]), []), false end. project_apps(State) -> @@ -171,42 +207,6 @@ filter_checkouts([App|Rest], Acc) -> false -> filter_checkouts(Rest, [App|Acc]) end. -%% make sure applications specified actually exist -filter_apps_by_name(AppNames, ProjectApps) -> - filter_apps_by_name(AppNames, ProjectApps, []). - -filter_apps_by_name([], _ProjectApps, Acc) -> {ok, lists:reverse(Acc)}; -filter_apps_by_name([Name|Rest], ProjectApps, Acc) -> - case find_app_by_name(Name, ProjectApps) of - {error, app_not_found} -> - ?PRV_ERROR({error_running_tests, - "Application `" ++ Name ++ "' not found in project."}); - App -> - filter_apps_by_name(Rest, ProjectApps, [App|Acc]) - end. - -find_app_by_name(_, []) -> {error, app_not_found}; -find_app_by_name(Name, [App|Rest]) -> - case Name == binary_to_list(rebar_app_info:name(App)) of - true -> App; - false -> find_app_by_name(Name, Rest) - end. - -%% ensure specified suites are in the applications included -filter_suites_by_apps(Suites, ProjectApps) -> - filter_suites_by_apps(Suites, ProjectApps, []). - -filter_suites_by_apps([], _ProjectApps, Acc) -> {ok, lists:reverse(Acc)}; -filter_suites_by_apps([Suite|Rest], Apps, Acc) -> - Modules = app_modules([binary_to_atom(rebar_app_info:name(A), unicode) || A <- Apps], []), - case lists:member(list_to_atom(Suite), Modules) of - false -> - ?PRV_ERROR({error_running_tests, - "Module `" ++ Suite ++ "' not found in applications."}); - true -> - filter_suites_by_apps(Rest, Apps, [Suite|Acc]) - end. - app_modules([], Acc) -> Acc; app_modules([App|Rest], Acc) -> Unload = case application:load(App) of @@ -225,22 +225,13 @@ app_modules([App|Rest], Acc) -> app_modules(Rest, NewAcc) end. -test_set(Apps, all) -> {ok, set_apps(Apps, [])}; -test_set(_Apps, Suites) -> {ok, set_suites(Suites, [])}. +default_tests(Apps) -> set_apps(Apps, []). set_apps([], Acc) -> lists:reverse(Acc); set_apps([App|Rest], Acc) -> AppName = list_to_atom(binary_to_list(rebar_app_info:name(App))), set_apps(Rest, [{application, AppName}|Acc]). -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, []), @@ -256,6 +247,16 @@ set_verbose(Opts) -> false -> [verbose] ++ Opts end. +maybe_compile_dir(_, _) -> ok. +maybe_compile_file(_, _) -> ok. + +maybe_cover_compile(State, Opts) -> + State1 = case proplists:get_value(cover, Opts, false) of + true -> rebar_state:set(State, cover_enabled, true); + false -> State + end, + rebar_prv_cover:maybe_cover_compile(State1). + handle_results(ok) -> ok; handle_results(error) -> {error, unknown_error}; @@ -265,12 +266,16 @@ 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)}, + {dir, undefined, "dir", string, help(dir)}, + {file, undefined, "file", string, help(file)}, + {module, undefined, "module", string, help(module)}, {suite, undefined, "suite", string, help(suite)}, {verbose, $v, "verbose", boolean, help(verbose)}]. -help(app) -> "Comma seperated list of application test suites to run. Equivalent to `[{application, App}]`."; +help(app) -> "Comma separated 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(dir) -> "Comma separated list of dirs to load tests from. Equivalent to `[{dir, Dir}]`."; +help(file) -> "Comma separated list of files to load tests from. Equivalent to `[{file, File}]`."; +help(module) -> "Comma separated list of modules to load tests from. Equivalent to `[{module, Module}]`."; +help(suite) -> "Comma separated list of test suites to run. Equivalent to `[{module, Suite}]`."; help(verbose) -> "Verbose output. Defaults to false.". -- cgit v1.1 From 8825e648e5a07f0f4f06cfdf714baa70557485bd Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Sun, 13 Sep 2015 18:51:03 -0700 Subject: add `error_on_warning' option to eunit provider --- src/rebar_prv_eunit.erl | 73 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index 309f417..eb19838 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -79,6 +79,9 @@ format_error(unknown_error) -> io_lib:format("Error running tests", []); format_error({error_running_tests, Reason}) -> io_lib:format("Error running tests: ~p", [Reason]); +format_error({eunit_test_errors, Errors}) -> + io_lib:format(lists:concat(["Error Running EUnit Tests:"] ++ + lists:map(fun(Error) -> "~n " ++ Error end, Errors)), []); format_error({error, Error}) -> format_error({error_running_tests, Error}). @@ -141,22 +144,39 @@ select_tests(ProjectApps, [], []) -> default_tests(ProjectApps); select_tests(_ProjectApps, A, B) -> A ++ B. validate_tests(State, ProjectApps, Tests) -> - {ok, lists:filter(fun(Elem) -> validate(State, ProjectApps, Elem) end, Tests)}. + gather_tests(fun(Elem) -> validate(State, ProjectApps, Elem) end, Tests, []). + +gather_tests(_F, [], Acc) -> {ok, lists:reverse(Acc)}; +gather_tests(F, [Test|Rest], Acc) -> + case F(Test) of + true -> gather_tests(F, Rest, [Test|Acc]); + false -> gather_tests(F, Rest, Acc); + %% failure mode, switch to gathering errors + Error -> gather_errors(F, Rest, [Error]) + end. + +gather_errors(_F, [], Acc) -> ?PRV_ERROR({eunit_test_errors, lists:reverse(Acc)}); +gather_errors(F, [Test|Rest], Acc) -> + case F(Test) of + true -> gather_errors(F, Rest, Acc); + false -> gather_errors(F, Rest, Acc); + Error -> gather_errors(F, Rest, [Error|Acc]) + end. -validate(_State, ProjectApps, {application, App}) -> - validate_app(App, ProjectApps); +validate(State, ProjectApps, {application, App}) -> + validate_app(State, ProjectApps, App); validate(State, _ProjectApps, {dir, Dir}) -> ok = maybe_compile_dir(State, Dir), - validate_dir(Dir); + validate_dir(State, Dir); validate(State, _ProjectApps, {file, File}) -> ok = maybe_compile_file(State, File), - validate_file(File); -validate(_State, ProjectApps, {module, Module}) -> - validate_module(Module, ProjectApps); -validate(_State, ProjectApps, {suite, Module}) -> - validate_module(Module, ProjectApps); -validate(_State, ProjectApps, Module) when is_atom(Module) -> - validate_module(Module, ProjectApps); + validate_file(State, File); +validate(State, ProjectApps, {module, Module}) -> + validate_module(State, ProjectApps, Module); +validate(State, ProjectApps, {suite, Module}) -> + validate_module(State, ProjectApps, Module); +validate(State, ProjectApps, Module) when is_atom(Module) -> + validate_module(State, ProjectApps, Module); validate(State, ProjectApps, Path) when is_list(Path) -> case ec_file:is_dir(Path) of true -> validate(State, ProjectApps, {dir, Path}); @@ -167,32 +187,39 @@ validate(State, ProjectApps, Path) when is_list(Path) -> %% unvalidatable validate(_State, _ProjectApps, _Test) -> true. -validate_app(AppName, []) -> - ?WARN(lists:concat(["Application `", AppName, "' not found in project."]), []), - false; -validate_app(AppName, [App|Rest]) -> +validate_app(State, [], AppName) -> + warn_or_error(State, lists:concat(["Application `", AppName, "' not found in project."])); +validate_app(State, [App|Rest], AppName) -> case AppName == binary_to_atom(rebar_app_info:name(App), unicode) of true -> true; - false -> validate_app(AppName, Rest) + false -> validate_app(State, Rest, AppName) end. -validate_dir(Dir) -> +validate_dir(State, Dir) -> case ec_file:is_dir(Dir) of true -> true; - false -> ?WARN(lists:concat(["Directory `", Dir, "' not found."]), []), false + false -> warn_or_error(State, lists:concat(["Directory `", Dir, "' not found."])) end. -validate_file(File) -> +validate_file(State, File) -> case ec_file:exists(File) of true -> true; - false -> ?WARN(lists:concat(["File `", File, "' not found."]), []), false + false -> warn_or_error(State, lists:concat(["File `", File, "' not found."])) end. -validate_module(Module, Apps) -> +validate_module(State, Apps, Module) -> AppModules = app_modules([binary_to_atom(rebar_app_info:name(A), unicode) || A <- Apps], []), case lists:member(Module, AppModules) of true -> true; - false -> ?WARN(lists:concat(["Module `", Module, "' not found in applications."]), []), false + false -> warn_or_error(State, lists:concat(["Module `", Module, "' not found in applications."])) + end. + +warn_or_error(State, Msg) -> + {RawOpts, _} = rebar_state:command_parsed_args(State), + Error = proplists:get_value(error_on_warning, RawOpts, false), + case Error of + true -> Msg; + false -> ?WARN(Msg, []), false end. project_apps(State) -> @@ -267,6 +294,7 @@ eunit_opts(_State) -> [{app, undefined, "app", string, help(app)}, {cover, $c, "cover", boolean, help(cover)}, {dir, undefined, "dir", string, help(dir)}, + {error_on_warning, $e, "error_on_warning", boolean, help(error)}, {file, undefined, "file", string, help(file)}, {module, undefined, "module", string, help(module)}, {suite, undefined, "suite", string, help(suite)}, @@ -275,6 +303,7 @@ eunit_opts(_State) -> help(app) -> "Comma separated list of application test suites to run. Equivalent to `[{application, App}]`."; help(cover) -> "Generate cover data. Defaults to false."; help(dir) -> "Comma separated list of dirs to load tests from. Equivalent to `[{dir, Dir}]`."; +help(error) -> "Error on invalid test specifications instead of warning."; help(file) -> "Comma separated list of files to load tests from. Equivalent to `[{file, File}]`."; help(module) -> "Comma separated list of modules to load tests from. Equivalent to `[{module, Module}]`."; help(suite) -> "Comma separated list of test suites to run. Equivalent to `[{module, Suite}]`."; -- cgit v1.1 From 391d95d9532fb1c3a2792542653aa065da775228 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Sun, 13 Sep 2015 20:38:27 -0700 Subject: add `application' flag and additional tests --- src/rebar_prv_eunit.erl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index eb19838..b1c78b3 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -122,12 +122,13 @@ prepare_tests(State) -> validate_tests(State, ProjectApps, Tests). resolve_tests(RawOpts) -> - Apps = resolve(app, application, RawOpts), - Dirs = resolve(dir, RawOpts), - Files = resolve(file, RawOpts), - Modules = resolve(module, RawOpts), - Suites = resolve(suite, module, RawOpts), - Apps ++ Dirs ++ Files ++ Modules ++ Suites. + Apps = resolve(app, application, RawOpts), + Applications = resolve(application, RawOpts), + Dirs = resolve(dir, RawOpts), + Files = resolve(file, RawOpts), + Modules = resolve(module, RawOpts), + Suites = resolve(suite, module, RawOpts), + Apps ++ Applications ++ Dirs ++ Files ++ Modules ++ Suites. resolve(Flag, RawOpts) -> resolve(Flag, Flag, RawOpts). @@ -292,12 +293,13 @@ handle_results({error, Reason}) -> eunit_opts(_State) -> [{app, undefined, "app", string, help(app)}, + {application, undefined, "application", string, help(app)}, {cover, $c, "cover", boolean, help(cover)}, {dir, undefined, "dir", string, help(dir)}, {error_on_warning, $e, "error_on_warning", boolean, help(error)}, {file, undefined, "file", string, help(file)}, {module, undefined, "module", string, help(module)}, - {suite, undefined, "suite", string, help(suite)}, + {suite, undefined, "suite", string, help(module)}, {verbose, $v, "verbose", boolean, help(verbose)}]. help(app) -> "Comma separated list of application test suites to run. Equivalent to `[{application, App}]`."; @@ -306,5 +308,4 @@ help(dir) -> "Comma separated list of dirs to load tests from. Equivalent to help(error) -> "Error on invalid test specifications instead of warning."; help(file) -> "Comma separated list of files to load tests from. Equivalent to `[{file, File}]`."; help(module) -> "Comma separated list of modules to load tests from. Equivalent to `[{module, Module}]`."; -help(suite) -> "Comma separated list of test suites to run. Equivalent to `[{module, Suite}]`."; help(verbose) -> "Verbose output. Defaults to false.". -- cgit v1.1 From 9e9c4212ce2c6b109bc1c027745bc2282df7977b Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Mon, 14 Sep 2015 10:19:33 -0700 Subject: drop `eunit_first_files' and `eunit_compile_opts'. that's what profiles are for --- src/rebar_prv_eunit.erl | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index b1c78b3..4005098 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -31,7 +31,7 @@ init(State) -> {opts, eunit_opts(State)}, {profiles, [test]}]), State1 = rebar_state:add_provider(State, Provider), - State2 = rebar_state:add_to_profile(State1, test, test_state(State1)), + State2 = rebar_state:add_to_profile(State1, test, test_state()), {ok, State2}. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. @@ -89,28 +89,7 @@ format_error({error, Error}) -> %% Internal functions %% =================================================================== -test_state(State) -> - ErlOpts = rebar_state:get(State, eunit_compile_opts, []), - TestOpts = safe_define_test_macro(ErlOpts), - TestDir = [{extra_src_dirs, ["test"]}], - first_files(State) ++ [{erl_opts, TestOpts ++ TestDir}]. - -safe_define_test_macro(Opts) -> - %% defining a compile macro twice results in an exception so - %% make sure 'TEST' is only defined once - case test_defined(Opts) of - true -> Opts; - false -> [{d, 'TEST'}] ++ Opts - end. - -test_defined([{d, 'TEST'}|_]) -> true; -test_defined([{d, 'TEST', true}|_]) -> true; -test_defined([_|Rest]) -> test_defined(Rest); -test_defined([]) -> false. - -first_files(State) -> - EUnitFirst = rebar_state:get(State, eunit_first_files, []), - [{erl_first_files, EUnitFirst}]. +test_state() -> [{extra_src_dirs, ["test"]}, {erl_opts, [{d, 'TEST'}]}]. prepare_tests(State) -> {RawOpts, _} = rebar_state:command_parsed_args(State), -- cgit v1.1 From 95716058650508f15fc1873e5ec34c08097217ca Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Sun, 20 Sep 2015 17:41:21 -0500 Subject: Revert "drop `eunit_first_files' and `eunit_compile_opts'. that's what" This reverts commit dde60d491f64e8545c586d07015a466eb8e6e126. --- src/rebar_prv_eunit.erl | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index 4005098..b1c78b3 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -31,7 +31,7 @@ init(State) -> {opts, eunit_opts(State)}, {profiles, [test]}]), State1 = rebar_state:add_provider(State, Provider), - State2 = rebar_state:add_to_profile(State1, test, test_state()), + State2 = rebar_state:add_to_profile(State1, test, test_state(State1)), {ok, State2}. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. @@ -89,7 +89,28 @@ format_error({error, Error}) -> %% Internal functions %% =================================================================== -test_state() -> [{extra_src_dirs, ["test"]}, {erl_opts, [{d, 'TEST'}]}]. +test_state(State) -> + ErlOpts = rebar_state:get(State, eunit_compile_opts, []), + TestOpts = safe_define_test_macro(ErlOpts), + TestDir = [{extra_src_dirs, ["test"]}], + first_files(State) ++ [{erl_opts, TestOpts ++ TestDir}]. + +safe_define_test_macro(Opts) -> + %% defining a compile macro twice results in an exception so + %% make sure 'TEST' is only defined once + case test_defined(Opts) of + true -> Opts; + false -> [{d, 'TEST'}] ++ Opts + end. + +test_defined([{d, 'TEST'}|_]) -> true; +test_defined([{d, 'TEST', true}|_]) -> true; +test_defined([_|Rest]) -> test_defined(Rest); +test_defined([]) -> false. + +first_files(State) -> + EUnitFirst = rebar_state:get(State, eunit_first_files, []), + [{erl_first_files, EUnitFirst}]. prepare_tests(State) -> {RawOpts, _} = rebar_state:command_parsed_args(State), -- cgit v1.1 From 2a1e0dd07eedad4ad92a3d5c3840dfa74573b7c8 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Sun, 20 Sep 2015 19:09:26 -0500 Subject: inject `eunit_compile_opts`, `eunit_first_files` and `TEST` macro prior to running compile and compile prehooks --- src/rebar_prv_eunit.erl | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index b1c78b3..d744204 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -13,7 +13,8 @@ -include_lib("providers/include/providers.hrl"). -define(PROVIDER, eunit). --define(DEPS, [compile]). +%% we need to modify app_info state before compile +-define(DEPS, [lock]). %% =================================================================== %% Public API @@ -36,10 +37,21 @@ init(State) -> -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. do(State) -> + %% inject the `TEST` macro, `eunit_first_files` and `eunit_compile_opts` + %% into the applications to be compiled + NewState = inject_eunit_state(State), + + case rebar_prv_compile:do(NewState) of + {ok, S} -> do_tests(S); + %% this should look like a compiler error, not an eunit error + Error -> Error + end. + +do_tests(State) -> ?INFO("Performing EUnit tests...", []), rebar_utils:update_code(rebar_state:code_paths(State, all_deps)), - %% Run eunit provider prehooks + %% Run compile provider prehooks Providers = rebar_state:providers(State), Cwd = rebar_dir:get_cwd(), rebar_hooks:run_all_hooks(Cwd, pre, ?PROVIDER, Providers, State), @@ -89,11 +101,28 @@ format_error({error, Error}) -> %% Internal functions %% =================================================================== -test_state(State) -> - ErlOpts = rebar_state:get(State, eunit_compile_opts, []), - TestOpts = safe_define_test_macro(ErlOpts), - TestDir = [{extra_src_dirs, ["test"]}], - first_files(State) ++ [{erl_opts, TestOpts ++ TestDir}]. +%% currently only add the `extra_drc_dirs` on provider init +test_state(_State) -> [{extra_src_dirs, ["test"]}]. + +inject_eunit_state(State) -> + Apps = project_apps(State), + ModdedApps = lists:map(fun(App) -> inject(State, App) end, Apps), + rebar_state:project_apps(State, ModdedApps). + +inject(State, App) -> + %% append `eunit_compile_opts` to app defined `erl_opts` and define + %% the `TEST` macro if not already compiled + ErlOpts = rebar_app_info:get(App, erl_opts, []), + EUnitOpts = rebar_state:get(State, eunit_compile_opts, []), + NewOpts = safe_define_test_macro(EUnitOpts ++ ErlOpts), + %% append `eunit_first_files` to app defined `erl_first_files` + FirstFiles = rebar_app_info:get(App, erl_first_files, []), + EUnitFirstFiles = rebar_state:get(State, eunit_first_files, []), + NewFirstFiles = EUnitFirstFiles ++ FirstFiles, + %% insert the new keys into the app + lists:foldl(fun({K, V}, NewApp) -> rebar_app_info:set(NewApp, K, V) end, + App, + [{erl_opts, NewOpts}, {erl_first_files, NewFirstFiles}]). safe_define_test_macro(Opts) -> %% defining a compile macro twice results in an exception so @@ -108,10 +137,6 @@ test_defined([{d, 'TEST', true}|_]) -> true; test_defined([_|Rest]) -> test_defined(Rest); test_defined([]) -> false. -first_files(State) -> - EUnitFirst = rebar_state:get(State, eunit_first_files, []), - [{erl_first_files, EUnitFirst}]. - prepare_tests(State) -> {RawOpts, _} = rebar_state:command_parsed_args(State), ok = maybe_cover_compile(State, RawOpts), -- cgit v1.1 From 420d804ea3915a12de0c12a9fd3952911f19be5b Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Tue, 29 Sep 2015 14:08:31 -0700 Subject: shiny and new test suite for eunit provider --- src/rebar_prv_eunit.erl | 231 ++++++++++++++++++++++++++---------------------- 1 file changed, 124 insertions(+), 107 deletions(-) (limited to 'src') diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index d744204..18a8993 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -8,6 +8,8 @@ -export([init/1, do/1, format_error/1]). +%% exported solely for tests +-export([compile/1, prepare_tests/1, eunit_opts/1]). -include("rebar.hrl"). -include_lib("providers/include/providers.hrl"). @@ -37,11 +39,8 @@ init(State) -> -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. do(State) -> - %% inject the `TEST` macro, `eunit_first_files` and `eunit_compile_opts` - %% into the applications to be compiled - NewState = inject_eunit_state(State), - - case rebar_prv_compile:do(NewState) of + case compile(State) of + %% successfully compiled apps {ok, S} -> do_tests(S); %% this should look like a compiler error, not an eunit error Error -> Error @@ -49,9 +48,10 @@ do(State) -> do_tests(State) -> ?INFO("Performing EUnit tests...", []), + rebar_utils:update_code(rebar_state:code_paths(State, all_deps)), - %% Run compile provider prehooks + %% Run eunit provider prehooks Providers = rebar_state:providers(State), Cwd = rebar_dir:get_cwd(), rebar_hooks:run_all_hooks(Cwd, pre, ?PROVIDER, Providers, State), @@ -101,8 +101,32 @@ format_error({error, Error}) -> %% Internal functions %% =================================================================== -%% currently only add the `extra_drc_dirs` on provider init -test_state(_State) -> [{extra_src_dirs, ["test"]}]. +test_state(State) -> + ErlOpts = rebar_state:get(State, erl_opts, []), + TestOpts = safe_define_test_macro(ErlOpts), + [{extra_src_dirs, ["test"]}, {erl_opts, TestOpts}]. + +safe_define_test_macro(Opts) -> + %% defining a compile macro twice results in an exception so + %% make sure 'TEST' is only defined once + case test_defined(Opts) of + true -> Opts; + false -> [{d, 'TEST'}] ++ Opts + end. + +compile(State) -> + %% inject `eunit_first_files` and `eunit_compile_opts` into the applications to be compiled + NewState = inject_eunit_state(State), + + case rebar_prv_compile:do(NewState) of + %% successfully compiled apps + {ok, S} -> + _ = maybe_cover_compile(S), + _ = maybe_compile_bare_testdir(S), + {ok, S}; + %% this should look like a compiler error, not an eunit error + Error -> Error + end. inject_eunit_state(State) -> Apps = project_apps(State), @@ -110,11 +134,10 @@ inject_eunit_state(State) -> rebar_state:project_apps(State, ModdedApps). inject(State, App) -> - %% append `eunit_compile_opts` to app defined `erl_opts` and define - %% the `TEST` macro if not already compiled + %% append `eunit_compile_opts` to app defined `erl_opts` ErlOpts = rebar_app_info:get(App, erl_opts, []), EUnitOpts = rebar_state:get(State, eunit_compile_opts, []), - NewOpts = safe_define_test_macro(EUnitOpts ++ ErlOpts), + NewOpts = EUnitOpts ++ ErlOpts, %% append `eunit_first_files` to app defined `erl_first_files` FirstFiles = rebar_app_info:get(App, erl_first_files, []), EUnitFirstFiles = rebar_state:get(State, eunit_first_files, []), @@ -124,29 +147,27 @@ inject(State, App) -> App, [{erl_opts, NewOpts}, {erl_first_files, NewFirstFiles}]). -safe_define_test_macro(Opts) -> - %% defining a compile macro twice results in an exception so - %% make sure 'TEST' is only defined once - case test_defined(Opts) of - true -> Opts; - false -> [{d, 'TEST'}] ++ Opts - end. - test_defined([{d, 'TEST'}|_]) -> true; test_defined([{d, 'TEST', true}|_]) -> true; test_defined([_|Rest]) -> test_defined(Rest); test_defined([]) -> false. prepare_tests(State) -> - {RawOpts, _} = rebar_state:command_parsed_args(State), - ok = maybe_cover_compile(State, RawOpts), - CmdTests = resolve_tests(RawOpts), + %% parse and translate command line tests + CmdTests = resolve_tests(State), CfgTests = rebar_state:get(State, eunit_tests, []), ProjectApps = project_apps(State), - Tests = select_tests(ProjectApps, CmdTests, CfgTests), + %% prioritize tests to run first trying any command line specified + %% tests falling back to tests specified in the config file finally + %% running a default set if no other tests are present + Tests = select_tests(State, ProjectApps, CmdTests, CfgTests), + %% check applications for existence in project, modules for existence + %% in applications, files and dirs for existence on disk and allow + %% any unrecognized tests through for eunit to handle validate_tests(State, ProjectApps, Tests). -resolve_tests(RawOpts) -> +resolve_tests(State) -> + {RawOpts, _} = rebar_state:command_parsed_args(State), Apps = resolve(app, application, RawOpts), Applications = resolve(application, RawOpts), Dirs = resolve(dir, RawOpts), @@ -166,8 +187,38 @@ resolve(Flag, EUnitKey, RawOpts) -> normalize(Key, Value) when Key == dir; Key == file -> {Key, Value}; normalize(Key, Value) -> {Key, list_to_atom(Value)}. -select_tests(ProjectApps, [], []) -> default_tests(ProjectApps); -select_tests(_ProjectApps, A, B) -> A ++ B. +project_apps(State) -> + filter_checkouts(rebar_state:project_apps(State)). + +filter_checkouts(Apps) -> filter_checkouts(Apps, []). + +filter_checkouts([], Acc) -> lists:reverse(Acc); +filter_checkouts([App|Rest], Acc) -> + case rebar_app_info:is_checkout(App) of + true -> filter_checkouts(Rest, Acc); + false -> filter_checkouts(Rest, [App|Acc]) + end. + +select_tests(State, ProjectApps, [], []) -> default_tests(State, ProjectApps); +select_tests(_State, _ProjectApps, [], Tests) -> Tests; +select_tests(_State, _ProjectApps, Tests, _) -> Tests. + +default_tests(State, Apps) -> + Tests = set_apps(Apps, []), + BareTest = filename:join([rebar_state:dir(State), "test"]), + F = fun(App) -> rebar_app_info:dir(App) == rebar_state:dir(State) end, + case filelib:is_dir(BareTest) andalso not lists:any(F, Apps) of + %% `test` dir at root of project is already scheduled to be + %% included or `test` does not exist + false -> lists:reverse(Tests); + %% need to add `test` dir at root to dirs to be included + true -> lists:reverse([{dir, filename:join([rebar_dir:base_dir(State), "test"])}|Tests]) + end. + +set_apps([], Acc) -> Acc; +set_apps([App|Rest], Acc) -> + AppName = list_to_atom(binary_to_list(rebar_app_info:name(App))), + set_apps(Rest, [{application, AppName}|Acc]). validate_tests(State, ProjectApps, Tests) -> gather_tests(fun(Elem) -> validate(State, ProjectApps, Elem) end, Tests, []). @@ -175,34 +226,30 @@ validate_tests(State, ProjectApps, Tests) -> gather_tests(_F, [], Acc) -> {ok, lists:reverse(Acc)}; gather_tests(F, [Test|Rest], Acc) -> case F(Test) of - true -> gather_tests(F, Rest, [Test|Acc]); - false -> gather_tests(F, Rest, Acc); + ok -> gather_tests(F, Rest, [Test|Acc]); %% failure mode, switch to gathering errors - Error -> gather_errors(F, Rest, [Error]) + {error, Error} -> gather_errors(F, Rest, [Error]) end. gather_errors(_F, [], Acc) -> ?PRV_ERROR({eunit_test_errors, lists:reverse(Acc)}); gather_errors(F, [Test|Rest], Acc) -> case F(Test) of - true -> gather_errors(F, Rest, Acc); - false -> gather_errors(F, Rest, Acc); - Error -> gather_errors(F, Rest, [Error|Acc]) + ok -> gather_errors(F, Rest, Acc); + {error, Error} -> gather_errors(F, Rest, [Error|Acc]) end. validate(State, ProjectApps, {application, App}) -> validate_app(State, ProjectApps, App); validate(State, _ProjectApps, {dir, Dir}) -> - ok = maybe_compile_dir(State, Dir), validate_dir(State, Dir); validate(State, _ProjectApps, {file, File}) -> - ok = maybe_compile_file(State, File), validate_file(State, File); -validate(State, ProjectApps, {module, Module}) -> - validate_module(State, ProjectApps, Module); -validate(State, ProjectApps, {suite, Module}) -> - validate_module(State, ProjectApps, Module); -validate(State, ProjectApps, Module) when is_atom(Module) -> - validate_module(State, ProjectApps, Module); +validate(State, _ProjectApps, {module, Module}) -> + validate_module(State, Module); +validate(State, _ProjectApps, {suite, Module}) -> + validate_module(State, Module); +validate(State, _ProjectApps, Module) when is_atom(Module) -> + validate_module(State, Module); validate(State, ProjectApps, Path) when is_list(Path) -> case ec_file:is_dir(Path) of true -> validate(State, ProjectApps, {dir, Path}); @@ -211,80 +258,35 @@ validate(State, ProjectApps, Path) when is_list(Path) -> %% unrecognized tests should be included. if they're invalid eunit will error %% and rebar.config may contain arbitrarily complex tests that are effectively %% unvalidatable -validate(_State, _ProjectApps, _Test) -> true. +validate(_State, _ProjectApps, _Test) -> ok. -validate_app(State, [], AppName) -> - warn_or_error(State, lists:concat(["Application `", AppName, "' not found in project."])); +validate_app(_State, [], AppName) -> + {error, lists:concat(["Application `", AppName, "' not found in project."])}; validate_app(State, [App|Rest], AppName) -> case AppName == binary_to_atom(rebar_app_info:name(App), unicode) of - true -> true; + true -> ok; false -> validate_app(State, Rest, AppName) end. -validate_dir(State, Dir) -> +validate_dir(_State, Dir) -> case ec_file:is_dir(Dir) of - true -> true; - false -> warn_or_error(State, lists:concat(["Directory `", Dir, "' not found."])) + true -> ok; + false -> {error, lists:concat(["Directory `", Dir, "' not found."])} end. -validate_file(State, File) -> +validate_file(_State, File) -> case ec_file:exists(File) of - true -> true; - false -> warn_or_error(State, lists:concat(["File `", File, "' not found."])) - end. - -validate_module(State, Apps, Module) -> - AppModules = app_modules([binary_to_atom(rebar_app_info:name(A), unicode) || A <- Apps], []), - case lists:member(Module, AppModules) of - true -> true; - false -> warn_or_error(State, lists:concat(["Module `", Module, "' not found in applications."])) - end. - -warn_or_error(State, Msg) -> - {RawOpts, _} = rebar_state:command_parsed_args(State), - Error = proplists:get_value(error_on_warning, RawOpts, false), - case Error of - true -> Msg; - false -> ?WARN(Msg, []), false - end. - -project_apps(State) -> - filter_checkouts(rebar_state:project_apps(State)). - -filter_checkouts(Apps) -> filter_checkouts(Apps, []). - -filter_checkouts([], Acc) -> lists:reverse(Acc); -filter_checkouts([App|Rest], Acc) -> - case rebar_app_info:is_checkout(App) of - true -> filter_checkouts(Rest, Acc); - false -> filter_checkouts(Rest, [App|Acc]) + true -> ok; + false -> {error, lists:concat(["File `", File, "' not found."])} end. -app_modules([], Acc) -> Acc; -app_modules([App|Rest], Acc) -> - Unload = case application:load(App) of - ok -> true; - {error, {already_loaded, _}} -> false - end, - NewAcc = case application:get_key(App, modules) of - {ok, Modules} -> Modules ++ Acc; - undefined -> Acc - end, - case Unload of - true -> - application:unload(App), - app_modules(Rest, NewAcc); - false -> - app_modules(Rest, NewAcc) +validate_module(_State, Module) -> + Path = code:which(Module), + case beam_lib:chunks(Path, [exports]) of + {ok, _} -> ok; + {error, beam_lib, _} -> {error, lists:concat(["Module `", Module, "' not found in project."])} end. -default_tests(Apps) -> set_apps(Apps, []). - -set_apps([], Acc) -> lists:reverse(Acc); -set_apps([App|Rest], Acc) -> - AppName = list_to_atom(binary_to_list(rebar_app_info:name(App))), - set_apps(Rest, [{application, AppName}|Acc]). - resolve_eunit_opts(State) -> {Opts, _} = rebar_state:command_parsed_args(State), EUnitOpts = rebar_state:get(State, eunit_opts, []), @@ -300,16 +302,33 @@ set_verbose(Opts) -> false -> [verbose] ++ Opts end. -maybe_compile_dir(_, _) -> ok. -maybe_compile_file(_, _) -> ok. - -maybe_cover_compile(State, Opts) -> - State1 = case proplists:get_value(cover, Opts, false) of +maybe_cover_compile(State) -> + State1 = case rebar_state:get(State, cover, false) of true -> rebar_state:set(State, cover_enabled, true); false -> State end, rebar_prv_cover:maybe_cover_compile(State1). +maybe_compile_bare_testdir(State) -> + case ec_file:is_dir(filename:join([rebar_state:dir(State), "test"])) of + true -> + ErlOpts = rebar_state:get(State, erl_opts, []), + EUnitOpts = rebar_state:get(State, eunit_compile_opts, []), + NewErlOpts = safe_define_test_macro(EUnitOpts ++ ErlOpts), + %% append `eunit_first_files` to app defined `erl_first_files` + FirstFiles = rebar_state:get(State, erl_first_files, []), + EUnitFirstFiles = rebar_state:get(State, eunit_first_files, []), + NewFirstFiles = EUnitFirstFiles ++ FirstFiles, + Opts = rebar_state:opts(State), + NewOpts = lists:foldl(fun({K, V}, Dict) -> rebar_opts:set(Dict, K, V) end, + Opts, + [{erl_opts, NewErlOpts}, {erl_first_files, NewFirstFiles}, {src_dirs, ["test"]}]), + OutDir = filename:join([rebar_dir:base_dir(State), "test"]), + filelib:ensure_dir(filename:join([OutDir, "dummy.beam"])), + rebar_erlc_compiler:compile(NewOpts, rebar_state:dir(State), OutDir); + false -> ok + end. + handle_results(ok) -> ok; handle_results(error) -> {error, unknown_error}; @@ -321,7 +340,6 @@ eunit_opts(_State) -> {application, undefined, "application", string, help(app)}, {cover, $c, "cover", boolean, help(cover)}, {dir, undefined, "dir", string, help(dir)}, - {error_on_warning, $e, "error_on_warning", boolean, help(error)}, {file, undefined, "file", string, help(file)}, {module, undefined, "module", string, help(module)}, {suite, undefined, "suite", string, help(module)}, @@ -330,7 +348,6 @@ eunit_opts(_State) -> help(app) -> "Comma separated list of application test suites to run. Equivalent to `[{application, App}]`."; help(cover) -> "Generate cover data. Defaults to false."; help(dir) -> "Comma separated list of dirs to load tests from. Equivalent to `[{dir, Dir}]`."; -help(error) -> "Error on invalid test specifications instead of warning."; help(file) -> "Comma separated list of files to load tests from. Equivalent to `[{file, File}]`."; help(module) -> "Comma separated list of modules to load tests from. Equivalent to `[{module, Module}]`."; help(verbose) -> "Verbose output. Defaults to false.". -- cgit v1.1 From 2b799ba1c7fedc5e4edc2266e4b7f77311ea0979 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Tue, 29 Sep 2015 15:00:46 -0700 Subject: fix failing cover test --- src/rebar_prv_eunit.erl | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index 18a8993..959be3f 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -78,7 +78,7 @@ do_tests(State, Tests) -> ?DEBUG("eunit_tests ~p", [Tests]), ?DEBUG("eunit_opts ~p", [EUnitOpts]), Result = eunit:test(Tests, EUnitOpts), - ok = rebar_prv_cover:maybe_write_coverdata(State, ?PROVIDER), + ok = maybe_write_coverdata(State), case handle_results(Result) of {error, Reason} -> ?PRV_ERROR(Reason); @@ -121,8 +121,8 @@ compile(State) -> case rebar_prv_compile:do(NewState) of %% successfully compiled apps {ok, S} -> - _ = maybe_cover_compile(S), - _ = maybe_compile_bare_testdir(S), + ok = maybe_cover_compile(S), + ok = maybe_compile_bare_testdir(S), {ok, S}; %% this should look like a compiler error, not an eunit error Error -> Error @@ -303,14 +303,34 @@ set_verbose(Opts) -> end. maybe_cover_compile(State) -> - State1 = case rebar_state:get(State, cover, false) of + {RawOpts, _} = rebar_state:command_parsed_args(State), + State1 = case proplists:get_value(cover, RawOpts, false) of true -> rebar_state:set(State, cover_enabled, true); false -> State end, rebar_prv_cover:maybe_cover_compile(State1). +maybe_cover_compile(State, Dir) -> + {RawOpts, _} = rebar_state:command_parsed_args(State), + State1 = case proplists:get_value(cover, RawOpts, false) of + true -> rebar_state:set(State, cover_enabled, true); + false -> State + end, + rebar_prv_cover:maybe_cover_compile(State1, [Dir]). + +maybe_write_coverdata(State) -> + {RawOpts, _} = rebar_state:command_parsed_args(State), + State1 = case proplists:get_value(cover, RawOpts, false) of + true -> rebar_state:set(State, cover_enabled, true); + false -> State + end, + rebar_prv_cover:maybe_write_coverdata(State1, ?PROVIDER). + maybe_compile_bare_testdir(State) -> - case ec_file:is_dir(filename:join([rebar_state:dir(State), "test"])) of + Apps = project_apps(State), + BareTest = filename:join([rebar_state:dir(State), "test"]), + F = fun(App) -> rebar_app_info:dir(App) == rebar_state:dir(State) end, + case ec_file:is_dir(BareTest) andalso not lists:any(F, Apps) of true -> ErlOpts = rebar_state:get(State, erl_opts, []), EUnitOpts = rebar_state:get(State, eunit_compile_opts, []), @@ -325,7 +345,8 @@ maybe_compile_bare_testdir(State) -> [{erl_opts, NewErlOpts}, {erl_first_files, NewFirstFiles}, {src_dirs, ["test"]}]), OutDir = filename:join([rebar_dir:base_dir(State), "test"]), filelib:ensure_dir(filename:join([OutDir, "dummy.beam"])), - rebar_erlc_compiler:compile(NewOpts, rebar_state:dir(State), OutDir); + rebar_erlc_compiler:compile(NewOpts, rebar_state:dir(State), OutDir), + maybe_cover_compile(State, [OutDir]); false -> ok end. -- cgit v1.1