d4e080d265efeeefc250f3ea39ffeb5af0812f7f
[erlang-estap.git] / bin / estap
1 #!/usr/bin/escript
2 %%
3 %%----------------------------------------------------------------------------
4
5 main([]) -> main(["--help"]);
6 main(["-h"]) -> main(["--help"]);
7 main(["--help"]) ->
8   io:fwrite("Usage: ~s filename~n", [escript:script_name()]);
9
10 main([File]) ->
11   {ok, {_Module, Forms}} = estap_file:read_file(File, []),
12   {ok, {Plan, Tests}} = estap_file:load_code(Forms),
13   case find_ebin(File) of
14     {ok, Path} -> code:add_patha(Path);
15     nothing -> ok
16   end,
17   estap_test:run(Plan, Tests),
18   ok.
19
20 %%----------------------------------------------------------------------------
21
22 -include_lib("kernel/include/file.hrl").
23
24 find_ebin(Path) ->
25   EBin = filename:join(Path, ebin),
26   case file:read_file_info(EBin) of
27     {ok, #file_info{type = directory}} ->
28       {ok, EBin};
29     _ ->
30       case Path of
31         "." -> nothing; % don't check parent
32         "/" -> nothing; % no parent
33         _ -> find_ebin(filename:dirname(Path))
34       end
35   end.
36
37 %%----------------------------------------------------------------------------
38 %% vim:ft=erlang