<commit_before>%%% File    : coverize.erl
%%% Author  : <NAME> <<EMAIL>>
%%% Description : A wrapper for generating cover output with a colorized index and stats
%%% Created : 28 Jan 2009 by <NAME> <<EMAIL>>
%%% Copyright (c) 2009 <NAME>

-module(coverize).
-author("<EMAIL>").
-export([run/2,run/4,run/5]).
-export([module_summary_file/1]).

%% Wrapper for cover to make command line calling easy
-spec(run/2 :: (list(string()), atom()) -> ok).
run(SourceDirs,TestSuiteModule) ->
  CompileOptions = [ debug_info,
                     { i, "./include" },
                     { outdir, "./ebin" }],
    run(SourceDirs,CompileOptions,TestSuiteModule,"./coverage").

%% TestSuiteModule should have a function test/0.
-spec(run/4 :: (list(string()), list(atom()|tuple()), atom(), string()) -> ok).
run(SourceDirs,CompileOptions,TestSuiteModule,OutputDir) ->
  ok = run(SourceDirs,CompileOptions,TestSuiteModule, 'test', OutputDir).

%% Call TestSuiteModule:TestSuiteFunction to run the test
-spec(run/5 :: (list(string()), list(atom()|tuple()), atom(), atom(), string()) -> ok).
run(SourceDirs,CompileOptions,TestSuiteModule,TestSuiteFunction=test, OutputDir) ->
  case cover:start() of
      {error,{already_started,_Pid}} -> ok;
      {error,Error} -> throw(io_lib:format("Could not start cover module: ~p~n",[Error]));
      {ok, _Pid}    -> ok
  end,

  _CompileResults = [cover:compile_directory(X, CompileOptions) || X <- SourceDirs ],

  TestSuiteModule:TestSuiteFunction(),
  SummaryFileName = filename:join([
                                   OutputDir,
                                   "index.html"
                                  ]),

  {ok, SummaryFile} = file:open(SummaryFileName, [write]),

  io:fwrite(SummaryFile, "<html><body><table><th>Module</th><th>Covered</th><th>Not Covered</th><th>% Complete</th>~n", []),

  {_, _, {TotCovered,TotUncovered}} = dump_coverage(OutputDir, SummaryFile, cover:modules()),

  io:fwrite(SummaryFile,
            "<div style=\"margin-right: 0; margin-top:0\">Overall Coverage: ~p% </div>~n",
            [calc_percentage(TotCovered,TotUncovered)]),

  io:fwrite(SummaryFile, "</table></body></html>~n", []),
  ok.

-spec dump_coverage(OutputDir::string(),SummaryFile::pid(),[Module::atom(),...]) -> {'ok',[any()],{number(),number()}}.
dump_coverage(OutputDir, SummaryFile, [Module|RemainingModules]) ->
  dump_coverage(OutputDir, SummaryFile, [Module|RemainingModules], [], {0,0}).

-spec dump_coverage(OutputDir::string(),SummaryFile::pid(),[Module::atom()],RemainingModules::[any()],{number(),number()}) -> {'ok',[any()],{number(),number()}}.
dump_coverage(OutputDir, SummaryFile, [Module|RemainingModules], Acc, {TotCovered,TotUncovered}) ->
  OutputFile = filename:join([
			      OutputDir,
			      atom_to_list(Module)++".COVER.html"
			     ]),
  Acc2 = case cover:analyse_to_file(Module, OutputFile, [html]) of
           {ok, OutFile} ->
             [OutFile|Acc] ;
           {error, _} ->
             Acc
         end,

  {ok,{_name,{CoveredLines,UncoveredLines}}} = cover:analyse(Module,coverage,module),
  TotalsAcc2 = {TotCovered+CoveredLines, TotUncovered+UncoveredLines},

  BackgroundStyle = case UncoveredLines > 0 of
                      true ->
                        "background: #E05070;";  % something red-ish
                      false ->
                        "background: #30d42a;"  % something green-ish
                    end,

  io:fwrite(SummaryFile,
            "<tr style=~p><td><a href=\"~s\">~p</a></td><td style=\"text-align: right\">~p</td><td style=\"text-align: right\">~p</td><td style=\"text-align: center\">~p</td></tr>~n",
            [BackgroundStyle,module_summary_file(Module),Module,CoveredLines,UncoveredLines, calc_percentage(CoveredLines,UncoveredLines)]),

  dump_coverage(OutputDir, SummaryFile, RemainingModules, Acc2, TotalsAcc2);

dump_coverage(_, _, [], Acc, TotalsAcc) ->
  {ok, Acc, TotalsAcc}.

-spec calc_percentage(number(),number()) -> integer().
calc_percentage(0,_UncoveredLines) -> 0;
calc_percentage(_,0) ->  100;
calc_percentage(CoveredLines,UncoveredLines) ->
  round((CoveredLines / (CoveredLines+UncoveredLines)) * 100).

module_summary_file(ModuleName) ->
  atom_to_list(ModuleName) ++ ".COVER.html". 
<commit_msg>use ensure_dir to make sure OutputFile has a place to go<commit_after>%%% File    : coverize.erl
%%% Author  : <NAME> <<EMAIL>>
%%% Description : A wrapper for generating cover output with a colorized index and stats
%%% Created : 28 Jan 2009 by <NAME> <<EMAIL>>
%%% Copyright (c) 2009 <NAME>

-module(coverize).
-author("<EMAIL>").
-export([run/2,run/4,run/5]).
-export([module_summary_file/1]).

%% Wrapper for cover to make command line calling easy
-spec(run/2 :: (list(string()), atom()) -> ok).
run(SourceDirs,TestSuiteModule) ->
  CompileOptions = [ debug_info,
                     { i, "./include" },
                     { outdir, "./ebin" }],
    run(SourceDirs,CompileOptions,TestSuiteModule,"./coverage").

%% TestSuiteModule should have a function test/0.
-spec(run/4 :: (list(string()), list(atom()|tuple()), atom(), string()) -> ok).
run(SourceDirs,CompileOptions,TestSuiteModule,OutputDir) ->
  ok = run(SourceDirs,CompileOptions,TestSuiteModule, 'test', OutputDir).

%% Call TestSuiteModule:TestSuiteFunction to run the test
-spec(run/5 :: (list(string()), list(atom()|tuple()), atom(), atom(), string()) -> ok).
run(SourceDirs,CompileOptions,TestSuiteModule,TestSuiteFunction=test, OutputDir) ->
  case cover:start() of
      {error,{already_started,_Pid}} -> ok;
      {error,Error} -> throw(io_lib:format("Could not start cover module: ~p~n",[Error]));
      {ok, _Pid}    -> ok
  end,

  _CompileResults = [cover:compile_directory(X, CompileOptions) || X <- SourceDirs ],

  TestSuiteModule:TestSuiteFunction(),
  SummaryFileName = filename:join([
                                   OutputDir,
                                   "index.html"
                                  ]),

  {ok, SummaryFile} = file:open(SummaryFileName, [write]),

  io:fwrite(SummaryFile, "<html><body><table><th>Module</th><th>Covered</th><th>Not Covered</th><th>% Complete</th>~n", []),

  {_, _, {TotCovered,TotUncovered}} = dump_coverage(OutputDir, SummaryFile, cover:modules()),

  io:fwrite(SummaryFile,
            "<div style=\"margin-right: 0; margin-top:0\">Overall Coverage: ~p% </div>~n",
            [calc_percentage(TotCovered,TotUncovered)]),

  io:fwrite(SummaryFile, "</table></body></html>~n", []),
  ok.

-spec dump_coverage(OutputDir::string(),SummaryFile::pid(),[Module::atom(),...]) -> {'ok',[any()],{number(),number()}}.
dump_coverage(OutputDir, SummaryFile, [Module|RemainingModules]) ->
  dump_coverage(OutputDir, SummaryFile, [Module|RemainingModules], [], {0,0}).

-spec dump_coverage(OutputDir::string(),SummaryFile::pid(),[Module::atom()],RemainingModules::[any()],{number(),number()}) -> {'ok',[any()],{number(),number()}}.
dump_coverage(OutputDir, SummaryFile, [Module|RemainingModules], Acc, {TotCovered,TotUncovered}) ->
  OutputFile = filename:join([
			      OutputDir,
			      atom_to_list(Module)++".COVER.html"
			     ]),
  filelib:ensure_dir(OutputFile),  %% make sure OutputFile has a place to go
  Acc2 = case cover:analyse_to_file(Module, OutputFile, [html]) of
           {ok, OutFile} ->
             [OutFile|Acc] ;
           {error, _} ->
             Acc
         end,

  {ok,{_name,{CoveredLines,UncoveredLines}}} = cover:analyse(Module,coverage,module),
  TotalsAcc2 = {TotCovered+CoveredLines, TotUncovered+UncoveredLines},

  BackgroundStyle = case UncoveredLines > 0 of
                      true ->
                        "background: #E05070;";  % something red-ish
                      false ->
                        "background: #30d42a;"  % something green-ish
                    end,

  io:fwrite(SummaryFile,
            "<tr style=~p><td><a href=\"~s\">~p</a></td><td style=\"text-align: right\">~p</td><td style=\"text-align: right\">~p</td><td style=\"text-align: center\">~p</td></tr>~n",
            [BackgroundStyle,module_summary_file(Module),Module,CoveredLines,UncoveredLines, calc_percentage(CoveredLines,UncoveredLines)]),

  dump_coverage(OutputDir, SummaryFile, RemainingModules, Acc2, TotalsAcc2);

dump_coverage(_, _, [], Acc, TotalsAcc) ->
  {ok, Acc, TotalsAcc}.

-spec calc_percentage(number(),number()) -> integer().
calc_percentage(0,_UncoveredLines) -> 0;
calc_percentage(_,0) ->  100;
calc_percentage(CoveredLines,UncoveredLines) ->
  round((CoveredLines / (CoveredLines+UncoveredLines)) * 100).

module_summary_file(ModuleName) ->
  atom_to_list(ModuleName) ++ ".COVER.html". 

{'fraction_non_alphanumeric': 0.181374825824431, 'fraction_numerical': 0.01045053413841152, 'mean_word_length': 3.7272228320526892, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3693689', 'n_tokens_mistral': 2880, 'n_tokens_neox': 2723, 'n_words': 477}