[erlang-questions] Test coverage -- propose patch of cover.erl

Thomas Arts <>
Sat Sep 8 18:46:31 CEST 2007


I'm probably alone in this, but I sometimes use another directory than "src" to put my source files in, mainly because the sources are automatically generated and I don't want to mix them with sources manually written.

Anyway, if you test your code and you use cover to find out which lines were visited, then cover will nicely format your output in a file found in the "src" directory. Even if the beam file contains the source! I would eventually like to see that that source is used, but I am already content if the correct source code directory (also in .beam) is used.

Therefore, I propose the following patch to cover.erl

%% Given a .beam file, find the .erl file.
%% Try to extract the path from .beam file, otherwise,
%% look first in same directory as the .beam file, then in <beamdir>/../src
%%
%% Actually would be better to use the code in the .beam file, if included.
find_source(File0) ->
    case path_in_beam(File0) of
         {ok,Path} ->
           Dir = filename:dirname(File0),
           filename:join([Dir,Path]);
         _ ->
           case filename:rootname(File0,".beam") of
             File0 ->
                 File0;
             File ->
                 InSameDir = File++".erl",
                 case filelib:is_file(InSameDir) of
                 true ->
                     InSameDir;
                 false ->
                     Dir = filename:dirname(File),
                     Mod = filename:basename(File),
                     InDotDotSrc = filename:join([Dir,"..","src",Mod++".erl"]),
                     case filelib:is_file(InDotDotSrc) of
                     true ->
                         InDotDotSrc;
                     false ->
                         {beam,File0}
                     end
                 end
          end
    end.

path_in_beam(File) ->
    case beam_lib:chunks(File,[abstract_code]) of
         {ok,{_,[{abstract_code,{_,AC}}]}} ->
           case [ F || {attribute,1,file,{F,_}}<-AC] of
                [ Main | _ ] ->
                  {ok,Main};
                _ ->
                  no_file_info_in_abstract_code
           end;
         _ ->
           no_abstract_code
    end. 


/Thomas




More information about the erlang-questions mailing list