Skip to content

Instantly share code, notes, and snippets.

@msassak
Created April 8, 2014 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msassak/10157553 to your computer and use it in GitHub Desktop.
Save msassak/10157553 to your computer and use it in GitHub Desktop.
eunit test runner script
#!/usr/bin/env escript
-define(dbg(X), io:format("DEBUG: ~p~n", [X])).
-record(args, {primitives = [],
patha = [],
pathz = [],
unrecognized = [],
verbose = false,
help = false}).
main([]) ->
usage();
main(RawArgs) ->
case parse(RawArgs, #args{}) of
#args{help = true} ->
usage();
Args ->
warn_unrecognized_args(Args),
load_code_paths(Args),
case run_tests(Args) of
ok ->
halt(0);
error ->
halt(1)
end
end.
parse([], #args{primitives = Prims, unrecognized = Unrec} = Args) ->
Args#args{primitives = lists:flatten(Prims),
unrecognized = lists:reverse(Unrec)};
parse(["-v"|Rest], Args) ->
parse(Rest, Args#args{verbose = true});
parse(["-h"|Rest], Args) ->
parse(Rest, Args#args{help = true});
parse(["-pa"|Rest], #args{patha = PA} = Args) ->
{Dirs, Rest2} = slurp_args(Rest),
parse(Rest2, Args#args{patha = lists:merge([PA, Dirs])});
parse(["-pz"|Rest], #args{pathz = PZ} = Args) ->
{Dirs, Rest2} = slurp_args(Rest),
parse(Rest2, Args#args{pathz = lists:merge([PZ, Dirs])});
parse(["-mod"|Rest], #args{primitives = Prims} = Args) ->
{Modules, Rest2} = slurp_args(Rest),
NewPrims = [{module, list_to_atom(Module)} || Module <- Modules],
parse(Rest2, Args#args{primitives = [NewPrims|Prims]});
parse(["-app"|Rest], #args{primitives = Prims} = Args) ->
{Apps, Rest2} = slurp_args(Rest),
NewPrims = [{application, list_to_atom(App)} || App <- Apps],
parse(Rest2, Args#args{primitives = [NewPrims|Prims]});
parse(["-file"|Rest], #args{primitives = Prims} = Args) ->
{Files, Rest2} = slurp_args(Rest),
NewPrims = [{file, File} || File <- Files],
parse(Rest2, Args#args{primitives = [NewPrims|Prims]});
parse(["-dir"|Rest], #args{primitives = Prims} = Args) ->
{Dirs, Rest2} = slurp_args(Rest),
NewPrims = [{dir, Dir} || Dir <- Dirs],
parse(Rest2, Args#args{primitives = [NewPrims|Prims]});
parse(["-gen"|Rest], #args{primitives = Prims} = Args) ->
{GenStrings, Rest2} = slurp_args(Rest),
NewPrims = lists:map(fun(GenString) ->
Tokens = string:tokens(GenString, ":"),
Atoms = [list_to_atom(Tok) || Tok <- Tokens],
list_to_tuple([generator|Atoms])
end, GenStrings),
parse(Rest2, Args#args{primitives = [NewPrims|Prims]});
parse([H|Rest], #args{unrecognized = Unrec} = Args) ->
parse(Rest, Args#args{unrecognized = [H|Unrec]}).
run_tests(#args{primitives = Prims, verbose = true}) ->
eunit:test(Prims, [verbose]);
run_tests(#args{primitives = Prims, verbose = false}) ->
eunit:test(Prims).
load_code_paths(#args{patha = PA, pathz = PZ}) ->
[true = code:add_patha(Path) || Path <- PA],
[true = code:add_pathz(Path) || Path <- PZ].
warn_unrecognized_args(#args{unrecognized = Unrec}) ->
case Unrec of
[] ->
no_op;
Args ->
io:format("WARNING: Unrecognized arguments: ~p~n", [Args])
end.
slurp_args(Args) ->
TheArgs = lists:takewhile(fun("-" ++ _)-> false; (_) -> true end, Args),
TheRest = Args -- TheArgs,
{TheArgs, TheRest}.
usage() ->
Usage = "usage: eunit_run [-h] [-v] [-pa Path1 Path2 ...] [-pz Path1 Path2 ...]\n"
" [-mod Mod1 Mod2 ...]\n"
" [-app App1 App2 ...]\n"
" [-file File1 File2 ...]\n"
" [-dir Dir1 Dir2 ...]\n"
" [-gen TestGen1 TestGen2 ...]\n"
"\n"
"Run EUnit tests from a combination of test directories, suites and test\n"
"cases.\n"
"\n"
" -h Show help\n"
" -v Set EUnit's verbose option\n"
"\n"
"The test sources given on the command line are used to create a test\n"
"representation accepted by the eunit:test function.\n"
"\n"
"Examples:\n"
" eunit_run -gen my_mod:foo_test\n"
" eunit_run -gen my_mod:bar_test_",
io:format("~s~n", [Usage]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment