From e8ec6810b88ede7f252715e9bfc19d87238818ae Mon Sep 17 00:00:00 2001 From: mopp Date: Wed, 14 Nov 2018 16:22:09 +0900 Subject: Add --generator option for eunit --- src/rebar_prv_eunit.erl | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index f120926..d8136a2 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -40,14 +40,18 @@ init(State) -> -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. do(State) -> - Tests = prepare_tests(State), - %% inject `eunit_first_files`, `eunit_compile_opts` and any - %% directories required by tests into the applications - NewState = inject_eunit_state(State, Tests), - case compile(NewState) of - %% successfully compiled apps - {ok, S} -> do(S, Tests); - Error -> Error + try + Tests = prepare_tests(State), + %% inject `eunit_first_files`, `eunit_compile_opts` and any + %% directories required by tests into the applications + NewState = inject_eunit_state(State, Tests), + case compile(NewState) of + %% successfully compiled apps + {ok, S} -> do(S, Tests); + Error -> Error + end + catch + throw:Reason -> {error, Reason} end. do(State, Tests) -> @@ -134,17 +138,28 @@ resolve_tests(State) -> Files = resolve(file, RawOpts), Modules = resolve(module, RawOpts), Suites = resolve(suite, module, RawOpts), - Apps ++ Applications ++ Dirs ++ Files ++ Modules ++ Suites. + Generator = resolve(generator, RawOpts), + Apps ++ Applications ++ Dirs ++ Files ++ Modules ++ Suites ++ Generator. 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, - rebar_string:lexemes(Args, [$,])) + Args -> + lists:flatten(lists:map(fun(Arg) -> normalize(EUnitKey, Arg) end, + rebar_string:lexemes(Args, [$,]))) end. +normalize(generator, Value) -> + case string:tokens(Value, [$:]) of + [Module0, Functions] -> + Module = list_to_atom(Module0), + lists:map(fun(F) -> {generator, Module, list_to_atom(F)} end, + string:tokens(Functions, [$;])); + _ -> + throw(lists:concat(["Generator `", Value, "' is invalid format."])) + end; normalize(Key, Value) when Key == dir; Key == file -> {Key, Value}; normalize(Key, Value) -> {Key, list_to_atom(Value)}. @@ -353,6 +368,8 @@ validate(State, {module, Module}) -> validate_module(State, Module); validate(State, {suite, Module}) -> validate_module(State, Module); +validate(State, {generator, Module, Function}) -> + validate_generator(State, Module, Function); validate(State, Module) when is_atom(Module) -> validate_module(State, Module); validate(State, Path) when is_list(Path) -> @@ -395,6 +412,9 @@ validate_module(_State, Module) -> _ -> ok end. +validate_generator(State, Module, _Function) -> + validate_module(State, Module). + resolve_eunit_opts(State) -> {Opts, _} = rebar_state:command_parsed_args(State), EUnitOpts = rebar_state:get(State, eunit_opts, []), @@ -490,6 +510,7 @@ eunit_opts(_State) -> {file, $f, "file", string, help(file)}, {module, $m, "module", string, help(module)}, {suite, $s, "suite", string, help(module)}, + {generator, $g, "generator", string, help(generator)}, {verbose, $v, "verbose", boolean, help(verbose)}, {name, undefined, "name", atom, help(name)}, {sname, undefined, "sname", atom, help(sname)}, @@ -501,6 +522,7 @@ help(cover_export_name) -> "Base name of the coverdata file to write"; 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(generator) -> "Comma separated list of generators (the format is `module:function`) to load tests from. Equivalent to `[{generator, Module, Function}]`."; help(verbose) -> "Verbose output. Defaults to false."; help(name) -> "Gives a long name to the node"; help(sname) -> "Gives a short name to the node"; -- cgit v1.1 From 3071319c43c6a8e06f1536bf573584cc402c388e Mon Sep 17 00:00:00 2001 From: mopp Date: Sun, 30 Dec 2018 00:48:58 +0900 Subject: Use ?PRV_ERROR instead of throwing error --- src/rebar_prv_eunit.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index d8136a2..fe0caa2 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -158,7 +158,7 @@ normalize(generator, Value) -> lists:map(fun(F) -> {generator, Module, list_to_atom(F)} end, string:tokens(Functions, [$;])); _ -> - throw(lists:concat(["Generator `", Value, "' is invalid format."])) + ?PRV_ERROR({generator, {"Generator `~p` is invalid format", {Value}}}) end; normalize(Key, Value) when Key == dir; Key == file -> {Key, Value}; normalize(Key, Value) -> {Key, list_to_atom(Value)}. -- cgit v1.1 From ab68f3df269a9254ac08806b34a6f826f0177e02 Mon Sep 17 00:00:00 2001 From: mopp Date: Sun, 30 Dec 2018 00:59:41 +0900 Subject: Revert try-catch --- src/rebar_prv_eunit.erl | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index fe0caa2..4cb982e 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -40,18 +40,14 @@ init(State) -> -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. do(State) -> - try - Tests = prepare_tests(State), - %% inject `eunit_first_files`, `eunit_compile_opts` and any - %% directories required by tests into the applications - NewState = inject_eunit_state(State, Tests), - case compile(NewState) of - %% successfully compiled apps - {ok, S} -> do(S, Tests); - Error -> Error - end - catch - throw:Reason -> {error, Reason} + Tests = prepare_tests(State), + %% inject `eunit_first_files`, `eunit_compile_opts` and any + %% directories required by tests into the applications + NewState = inject_eunit_state(State, Tests), + case compile(NewState) of + %% successfully compiled apps + {ok, S} -> do(S, Tests); + Error -> Error end. do(State, Tests) -> -- cgit v1.1 From 44b250c174a2e5c0e0c671c958951a2c280a0524 Mon Sep 17 00:00:00 2001 From: mopp Date: Sun, 30 Dec 2018 02:59:29 +0900 Subject: Use format_error/1 --- src/rebar_prv_eunit.erl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index 4cb982e..b2f307e 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -105,6 +105,8 @@ format_error({eunit_test_errors, Errors}) -> lists:map(fun(Error) -> "~n " ++ Error end, Errors)), []); format_error({badconfig, {Msg, {Value, Key}}}) -> io_lib:format(Msg, [Value, Key]); +format_error({generator, Value}) -> + io_lib:format("Generator ~p has an invalid format", [Value]); format_error({error, Error}) -> format_error({error_running_tests, Error}). @@ -154,7 +156,7 @@ normalize(generator, Value) -> lists:map(fun(F) -> {generator, Module, list_to_atom(F)} end, string:tokens(Functions, [$;])); _ -> - ?PRV_ERROR({generator, {"Generator `~p` is invalid format", {Value}}}) + ?PRV_ERROR({generator, Value}) end; normalize(Key, Value) when Key == dir; Key == file -> {Key, Value}; normalize(Key, Value) -> {Key, list_to_atom(Value)}. -- cgit v1.1 From ecd5e1342a99241e71d809b2172fc1a6f4dd41ba Mon Sep 17 00:00:00 2001 From: mopp Date: Sun, 30 Dec 2018 04:04:03 +0900 Subject: Refactor normalize --- src/rebar_prv_eunit.erl | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index b2f307e..0b00e89 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -144,12 +144,16 @@ resolve(Flag, RawOpts) -> resolve(Flag, Flag, RawOpts). resolve(Flag, EUnitKey, RawOpts) -> case proplists:get_value(Flag, RawOpts) of undefined -> []; - Args -> - lists:flatten(lists:map(fun(Arg) -> normalize(EUnitKey, Arg) end, - rebar_string:lexemes(Args, [$,]))) + Args -> normalize(EUnitKey, + rebar_string:lexemes(Args, [$,])) end. -normalize(generator, Value) -> +normalize(generator, Args) -> + lists:flatmap(fun(Value) -> normalize_(generator, Value) end, Args); +normalize(EUnitKey, Args) -> + lists:map(fun(Arg) -> normalize_(EUnitKey, Arg) end, Args). + +normalize_(generator, Value) -> case string:tokens(Value, [$:]) of [Module0, Functions] -> Module = list_to_atom(Module0), @@ -158,8 +162,8 @@ normalize(generator, Value) -> _ -> ?PRV_ERROR({generator, Value}) end; -normalize(Key, Value) when Key == dir; Key == file -> {Key, Value}; -normalize(Key, Value) -> {Key, list_to_atom(Value)}. +normalize_(Key, Value) when Key == dir; Key == file -> {Key, Value}; +normalize_(Key, Value) -> {Key, list_to_atom(Value)}. cfg_tests(State) -> case rebar_state:get(State, eunit_tests, []) of -- cgit v1.1