From a908160088b654e436bdcdd32369c29603ca5fa7 Mon Sep 17 00:00:00 2001 From: Siri Hansen Date: Mon, 19 Nov 2012 15:13:47 +0100 Subject: [test_server] Fix erl2html2.erl to handle badly indented files Line numbering of erlang files that were not correctly indented could be wrong after coverting to html with erl2html2:convert/[2,3]. This has been corrected. This commit also fixes the following: * There are now link targets for each line and not only for each 10th line - meaning that links from test logs are now to the exact line, and not to the last number for which N rem 10 == 0. * there will only be one link target per function, i.e. the faulty link targets for function clauses are removed. * link targets for function now includes the arity (e.g. func/1 has a link target "func-1") And some tests are added. --- lib/test_server/test/Makefile | 3 +- lib/test_server/test/erl2html2_SUITE.erl | 254 +++++++++++++++++++++ .../test/erl2html2_SUITE_data/Makefile.src | 2 + .../test/erl2html2_SUITE_data/header1.hrl | 4 + .../test/erl2html2_SUITE_data/include/header2.hrl | 0 lib/test_server/test/erl2html2_SUITE_data/m1.erl | 46 ++++ 6 files changed, 308 insertions(+), 1 deletion(-) create mode 100644 lib/test_server/test/erl2html2_SUITE.erl create mode 100644 lib/test_server/test/erl2html2_SUITE_data/Makefile.src create mode 100644 lib/test_server/test/erl2html2_SUITE_data/header1.hrl create mode 100644 lib/test_server/test/erl2html2_SUITE_data/include/header2.hrl create mode 100644 lib/test_server/test/erl2html2_SUITE_data/m1.erl (limited to 'lib/test_server/test') diff --git a/lib/test_server/test/Makefile b/lib/test_server/test/Makefile index a3f9820d7f..afccc28662 100644 --- a/lib/test_server/test/Makefile +++ b/lib/test_server/test/Makefile @@ -26,7 +26,8 @@ include $(ERL_TOP)/make/$(TARGET)/otp.mk MODULES= \ test_server_SUITE \ - test_server_test_lib + test_server_test_lib \ + erl2html2_SUITE ERL_FILES= $(MODULES:%=%.erl) diff --git a/lib/test_server/test/erl2html2_SUITE.erl b/lib/test_server/test/erl2html2_SUITE.erl new file mode 100644 index 0000000000..db81c4f059 --- /dev/null +++ b/lib/test_server/test/erl2html2_SUITE.erl @@ -0,0 +1,254 @@ +%%%------------------------------------------------------------------- +%%% @author Siri Hansen +%%% @copyright (C) 2012, Siri Hansen +%%% @doc +%%% +%%% @end +%%% Created : 15 Nov 2012 by Siri Hansen +%%%------------------------------------------------------------------- +-module(erl2html2_SUITE). + +-compile(export_all). + +-include_lib("common_test/include/ct.hrl"). + + +-define(HEADER, + ["\n", + "\n", + "\n", + "Module ", Src, "\n", + "\n", + "\n", + "\n"]). + +%%-------------------------------------------------------------------- +%% @spec suite() -> Info +%% Info = [tuple()] +%% @end +%%-------------------------------------------------------------------- +suite() -> + [{timetrap,{seconds,30}}, + {ct_hooks,[ts_install_cth,test_server_test_lib]}]. + +%%-------------------------------------------------------------------- +%% @spec init_per_suite(Config0) -> +%% Config1 | {skip,Reason} | {skip_and_save,Reason,Config1} +%% Config0 = Config1 = [tuple()] +%% Reason = term() +%% @end +%%-------------------------------------------------------------------- +init_per_suite(Config) -> + Config. + +%%-------------------------------------------------------------------- +%% @spec end_per_suite(Config0) -> void() | {save_config,Config1} +%% Config0 = Config1 = [tuple()] +%% @end +%%-------------------------------------------------------------------- +end_per_suite(_Config) -> + ok. + +%%-------------------------------------------------------------------- +%% @spec init_per_group(GroupName, Config0) -> +%% Config1 | {skip,Reason} | {skip_and_save,Reason,Config1} +%% GroupName = atom() +%% Config0 = Config1 = [tuple()] +%% Reason = term() +%% @end +%%-------------------------------------------------------------------- +init_per_group(_GroupName, Config) -> + Config. + +%%-------------------------------------------------------------------- +%% @spec end_per_group(GroupName, Config0) -> +%% void() | {save_config,Config1} +%% GroupName = atom() +%% Config0 = Config1 = [tuple()] +%% @end +%%-------------------------------------------------------------------- +end_per_group(_GroupName, _Config) -> + ok. + +%%-------------------------------------------------------------------- +%% @spec init_per_testcase(TestCase, Config0) -> +%% Config1 | {skip,Reason} | {skip_and_save,Reason,Config1} +%% TestCase = atom() +%% Config0 = Config1 = [tuple()] +%% Reason = term() +%% @end +%%-------------------------------------------------------------------- +init_per_testcase(_TestCase, Config) -> + Config. + +%%-------------------------------------------------------------------- +%% @spec end_per_testcase(TestCase, Config0) -> +%% void() | {save_config,Config1} | {fail,Reason} +%% TestCase = atom() +%% Config0 = Config1 = [tuple()] +%% Reason = term() +%% @end +%%-------------------------------------------------------------------- +end_per_testcase(_TestCase, _Config) -> + ok. + +%%-------------------------------------------------------------------- +%% @spec groups() -> [Group] +%% Group = {GroupName,Properties,GroupsAndTestCases} +%% GroupName = atom() +%% Properties = [parallel | sequence | Shuffle | {RepeatType,N}] +%% GroupsAndTestCases = [Group | {group,GroupName} | TestCase] +%% TestCase = atom() +%% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}} +%% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail | +%% repeat_until_any_ok | repeat_until_any_fail +%% N = integer() | forever +%% @end +%%-------------------------------------------------------------------- +groups() -> + []. + +%%-------------------------------------------------------------------- +%% @spec all() -> GroupsAndTestCases | {skip,Reason} +%% GroupsAndTestCases = [{group,GroupName} | TestCase] +%% GroupName = atom() +%% TestCase = atom() +%% Reason = term() +%% @end +%%-------------------------------------------------------------------- +all() -> + [m1]. + +%%-------------------------------------------------------------------- +%% @spec TestCase() -> Info +%% Info = [tuple()] +%% @end +%%-------------------------------------------------------------------- +m1() -> + []. + +%%-------------------------------------------------------------------- +%% @spec TestCase(Config0) -> +%% ok | exit() | {skip,Reason} | {comment,Comment} | +%% {save_config,Config1} | {skip_and_save,Reason,Config1} +%% Config0 = Config1 = [tuple()] +%% Reason = term() +%% Comment = term() +%% @end +%%-------------------------------------------------------------------- +m1(Config) -> + {Src,Dst} = convert_module("m1",Config), + {true,L} = check_line_numbers(Src,Dst), + ok = check_link_targets(Src,Dst,L,[{baz,0}]), + ok. + +convert_module(Mod,Config) -> + DataDir = ?config(data_dir,Config), + PrivDir = ?config(priv_dir,Config), + Src = filename:join(DataDir,Mod++".erl"), + Dst = filename:join(PrivDir,Mod++".erl.html"), + io:format("~s\n",[Src,filename:basename(Src)]), + ok = erl2html2:convert(Src, Dst, ""), + io:format("~s\n",[Dst,filename:basename(Dst)]), + {Src,Dst}. + +%% Check that there are the same number of lines in each file, and +%% that all line numbers are displayed in the dst file. +check_line_numbers(Src,Dst) -> + {ok,SFd} = file:open(Src,[read]), + {ok,DFd} = file:open(Dst,[read]), + {ok,SN} = count_src_lines(SFd,1), + ok = file:close(SFd), + {ok,DN} = read_dst_line_numbers(DFd), + ok = file:close(DFd), + {SN == DN,SN}. + +count_src_lines(Fd,N) -> + case io:get_line(Fd,"") of + eof -> + {ok,N}; + {error,Reason} -> + {error,Reason,N}; + _Line -> + count_src_lines(Fd,N+1) + end. + +read_dst_line_numbers(Fd) -> + "
\n" = io:get_line(Fd,""),
+    read_dst_line_numbers(Fd,0).
+read_dst_line_numbers(Fd,Last) when is_integer(Last) ->
+    case io:get_line(Fd,"") of
+	eof ->
+	    {ok,Last};
+	{error,Reason} ->
+	    {error,Reason,Last};
+	"
"++_ -> + {ok,Last}; + ""++_ -> + {ok,Last}; + Line -> + %% erlang:display(Line), + Num = check_line_number(Last,Line,Line), + read_dst_line_numbers(Fd,Num) + end. + +check_line_number(Last,Line,OrigLine) -> + case Line of + " ct:fail({no_line_number_after,Last,OrigLine}) + end, + if Num == Last+1 -> + Num; + true -> + ct:fail({unexpected_integer,Num,Last}) + end + end. + + +%% Check that there is one link target for each line and one for each +%% function. +%% The test module has -compile(export_all), so all functions are +%% found by listing the exported ones. +check_link_targets(Src,Dst,L,RmFncs) -> + Mod = list_to_atom(filename:basename(filename:rootname(Src))), + Exports = Mod:module_info(exports)--[{module_info,0},{module_info,1}|RmFncs], + {ok,{[],L},_} = xmerl_sax_parser:file(Dst, + [{event_fun,fun sax_event/3}, + {event_state,{Exports,0}}]), + ok. + +sax_event(Event,_Loc,State) -> + sax_event(Event,State). + +sax_event({startElement,_Uri,"a",_QN,Attrs},{Exports,PrevLine}) -> + {_,_,"name",Name} = lists:keyfind("name",3,Attrs), + case catch list_to_integer(Name) of + Line when is_integer(Line) -> + case PrevLine + 1 of + Line -> +% erlang:display({found_line,Line}), + {Exports,Line}; + Other -> + ct:fail({unexpected_line_number_target,Other}) + end; + {'EXIT',_} -> + {match,[FStr,AStr]} = + re:run(Name,"^(.*)-([0-9]+)$",[{capture,all_but_first,list}]), + F = list_to_atom(http_uri:decode(FStr)), + A = list_to_integer(AStr), +% erlang:display({found_fnc,F,A}), + A = proplists:get_value(F,Exports), + {lists:delete({F,A},Exports),PrevLine} + end; +sax_event(_,State) -> + State. diff --git a/lib/test_server/test/erl2html2_SUITE_data/Makefile.src b/lib/test_server/test/erl2html2_SUITE_data/Makefile.src new file mode 100644 index 0000000000..942ac0584b --- /dev/null +++ b/lib/test_server/test/erl2html2_SUITE_data/Makefile.src @@ -0,0 +1,2 @@ +all: + erlc -Iinclude m1.erl \ No newline at end of file diff --git a/lib/test_server/test/erl2html2_SUITE_data/header1.hrl b/lib/test_server/test/erl2html2_SUITE_data/header1.hrl new file mode 100644 index 0000000000..53d1b79ac5 --- /dev/null +++ b/lib/test_server/test/erl2html2_SUITE_data/header1.hrl @@ -0,0 +1,4 @@ +baz() -> + ok. + +-define(MACRO_DEFINING_A_FUNCTION,quux() -> ok). diff --git a/lib/test_server/test/erl2html2_SUITE_data/include/header2.hrl b/lib/test_server/test/erl2html2_SUITE_data/include/header2.hrl new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/test_server/test/erl2html2_SUITE_data/m1.erl b/lib/test_server/test/erl2html2_SUITE_data/m1.erl new file mode 100644 index 0000000000..156f1d0a51 --- /dev/null +++ b/lib/test_server/test/erl2html2_SUITE_data/m1.erl @@ -0,0 +1,46 @@ +%% Comment with code & +%% and also some "quotes" and 'single quotes' + +-module(m1). + +-compile(export_all). + +-include("header1.hrl"). +-include("header2.hrl"). + +-define(MACRO1,value). + +%%% Comment +foo(x) -> + %% Comment + ok_x; +foo(y) -> + %% Second clause + ok_y. + +'quoted_foo'() -> + ok. + +'quoted_foo_with_"_and_/'() -> + ok. + +'quoted_foo_with_(_and_)'() -> + ok. + +'quoted_foo_with_<_and_>'() -> + ok. + +bar() -> + do_something(), +ok. % indentation error, OTP-9710 + +%% Function inside macro definition +?MACRO_DEFINING_A_FUNCTION. + +%% Two function one one line +quuux() -> ok. quuuux() -> ok. + +%% do_something/0 does something +do_something() -> + ?MACRO1. +%% comments after last line -- cgit v1.2.3 From ee65e005b0e18d92d786b14e2dd3cf7b8584ca27 Mon Sep 17 00:00:00 2001 From: Siri Hansen Date: Tue, 20 Nov 2012 12:30:45 +0100 Subject: [test_server] Minimize memory usage in erl2html2:convert/[2,3] Reading form by form and line by line, instead of reading the complete file in one go. --- lib/test_server/test/erl2html2_SUITE.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/test_server/test') diff --git a/lib/test_server/test/erl2html2_SUITE.erl b/lib/test_server/test/erl2html2_SUITE.erl index db81c4f059..96175413a1 100644 --- a/lib/test_server/test/erl2html2_SUITE.erl +++ b/lib/test_server/test/erl2html2_SUITE.erl @@ -160,7 +160,7 @@ convert_module(Mod,Config) -> check_line_numbers(Src,Dst) -> {ok,SFd} = file:open(Src,[read]), {ok,DFd} = file:open(Dst,[read]), - {ok,SN} = count_src_lines(SFd,1), + {ok,SN} = count_src_lines(SFd,0), ok = file:close(SFd), {ok,DN} = read_dst_line_numbers(DFd), ok = file:close(DFd), -- cgit v1.2.3