User:Dnessett/Parser Tests/MacGyver Tests

Each section (except the first) contains one of the quick and dirty analysis programs developed to get better insight into how well parserTests exercises the parser. These programs are called MacGyver tests in reference to a mid-80s to early-90s television program named after the protagonist (Angus MacGyver). In each episode MacGyver would get out of some difficulty by improvising tools from objects lying around. By naming these MacGyver tools, I want to emphasize their practical rather than aesthetic value. They are not examples of beautifully designed and well thought out programs. They were thrown together in about 2 days. They met the needs I had when I wrote them, but I had no intention of creating something with a long lifetime. So, anyone using them should do so at their own risk and with the understanding that parserTests may have changed between the time they were written and the time of their use - in which case, they may need some maintenance.

Test Information
Each test has some assumptions built into them that affects their operation. Both RunParserTests and AnalyzeCodeCoverage assume they are installed the /phase3/maintenance/ directory of a MediaWiki software distribution. Both write files into this directory. RunParserTests outputs two files: 1) parserTestsOut.txt, which contains the information generated by parserTests and normally written to StandardOut, and 2) parserTestsCodeCoverage.txt, which contains the code coverage information generated by Xdebug. AnalyzeCodeCoverage uses the information in parserTestsCodeCoverage.txt to generate code coverage analysis information. It writes the file parserTestsCoverageAnalysis.txt, which contains the analysis information. More details on these tests are found in the sections below.

RunParserTests.php
RunParserTests comprises a single class (RunParserTests) with one method (run). It assumes the Xdebug extension is installed. It calls xdebug_start_code_coverage with the XDEBUG_CC_UNUSED option. This generates a two-dimensional array indexed by file path name as the primary key and line number as the secondary key (see Xdebug documentation). Use of the XDEBUG_CC_UNUSED option generates information about code lines visited and code lines not visited. In the former case the value assigned to the line number is 1 and in the latter case it is -1. The sum of line numbers with either of these values assigned represents the total number of executable lines in a file. The percentage code coverage is the number of lines visited in a file divided by this total (times 100). RunParserTests is run from the terminal and takes one optional parameter, --run-disabled. This parameter is passed to parserTests. It controls whether tests marked "disabled" are run.

It must be noted that Xdebug has an unusual definition for an executable line of code. Understandably, comments are not counted as executable, but somewhat strangely Xdebug does not judge lines that have variable initialization to be executable. The documentation on Xdebug code coverage is very sparse and figuring out which class of lines are counted and which are not requires looking at some analyzed files and using inductive logic.

AnalyzeCodeCoverage.php
AnalyzeCodeCoverage uses the file parserTestsCodeCoverage.txt located in the /maintenance/ directory (by executing an include with it as parameter). This file is generated by RunParserTests.php. AnalyzeCodeCoverage counts the total number of executable lines in the files referenced by parserTests (as defined by the Xdebug code coverage logic) and the total number of executable lines visited. It divides the latter by the former to get a grand total percentage code coverage and then generates other grand total statistics. It then uses the per file code coverage statistics to generate a histogram of percentage code coverage and two file lists. The first is ordered by the percentage of code covered in each file and contains the total number of executable lines in the file, the number of lines visited and left unvisited and the percentage visited. The second file list contains the same per file information with the list ordered alphabetically by file pathname. All of this information is output to the file parserTestsCoverageAnalysis.txt

AnalyzeCodeCoverage takes one optional parameter, --file. If absent, the statistical information is formatted for display on a wiki page. If present, the statistical information is formatted for display in a file. In order to strip the prefix from the file pathnames and root their names in the distribution directory, the variable $path_prefix is set to the full pathname of the distribution. Users of this program must modify this string to the valid value on their machine.