DbTestPreviewer.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @ingroup Testing
  20. */
  21. class DbTestPreviewer extends TestRecorder {
  22. protected $filter; // /< Test name filter callback
  23. protected $lb; // /< Database load balancer
  24. protected $db; // /< Database connection to the main DB
  25. protected $curRun; // /< run ID number for the current run
  26. protected $prevRun; // /< run ID number for the previous run, if any
  27. protected $results; // /< Result array
  28. /**
  29. * This should be called before the table prefix is changed
  30. * @param IDatabase $db
  31. * @param bool|string $filter
  32. */
  33. function __construct( $db, $filter = false ) {
  34. $this->db = $db;
  35. $this->filter = $filter;
  36. }
  37. /**
  38. * Set up result recording; insert a record for the run with the date
  39. * and all that fun stuff
  40. */
  41. function start() {
  42. if ( !$this->db->tableExists( 'testrun', __METHOD__ )
  43. || !$this->db->tableExists( 'testitem', __METHOD__ )
  44. ) {
  45. print "WARNING> `testrun` table not found in database.\n";
  46. $this->prevRun = false;
  47. } else {
  48. // We'll make comparisons against the previous run later...
  49. $this->prevRun = $this->db->selectField( 'testrun', 'MAX(tr_id)' );
  50. }
  51. $this->results = [];
  52. }
  53. function record( $test, ParserTestResult $result ) {
  54. $this->results[$test['desc']] = $result->isSuccess() ? 1 : 0;
  55. }
  56. function report() {
  57. if ( $this->prevRun ) {
  58. // f = fail, p = pass, n = nonexistent
  59. // codes show before then after
  60. $table = [
  61. 'fp' => 'previously failing test(s) now PASSING! :)',
  62. 'pn' => 'previously PASSING test(s) removed o_O',
  63. 'np' => 'new PASSING test(s) :)',
  64. 'pf' => 'previously passing test(s) now FAILING! :(',
  65. 'fn' => 'previously FAILING test(s) removed O_o',
  66. 'nf' => 'new FAILING test(s) :(',
  67. 'ff' => 'still FAILING test(s) :(',
  68. ];
  69. $prevResults = [];
  70. $res = $this->db->select( 'testitem', [ 'ti_name', 'ti_success' ],
  71. [ 'ti_run' => $this->prevRun ], __METHOD__ );
  72. $filter = $this->filter;
  73. foreach ( $res as $row ) {
  74. if ( !$filter || $filter( $row->ti_name ) ) {
  75. $prevResults[$row->ti_name] = $row->ti_success;
  76. }
  77. }
  78. $combined = array_keys( $this->results + $prevResults );
  79. # Determine breakdown by change type
  80. $breakdown = [];
  81. foreach ( $combined as $test ) {
  82. if ( !isset( $prevResults[$test] ) ) {
  83. $before = 'n';
  84. } elseif ( $prevResults[$test] == 1 ) {
  85. $before = 'p';
  86. } else /* if ( $prevResults[$test] == 0 ) */ {
  87. $before = 'f';
  88. }
  89. if ( !isset( $this->results[$test] ) ) {
  90. $after = 'n';
  91. } elseif ( $this->results[$test] == 1 ) {
  92. $after = 'p';
  93. } else /* if ( $this->results[$test] == 0 ) */ {
  94. $after = 'f';
  95. }
  96. $code = $before . $after;
  97. if ( isset( $table[$code] ) ) {
  98. $breakdown[$code][$test] = $this->getTestStatusInfo( $test, $after );
  99. }
  100. }
  101. # Write out results
  102. foreach ( $table as $code => $label ) {
  103. if ( !empty( $breakdown[$code] ) ) {
  104. $count = count( $breakdown[$code] );
  105. printf( "\n%4d %s\n", $count, $label );
  106. foreach ( $breakdown[$code] as $differing_test_name => $statusInfo ) {
  107. print " * $differing_test_name [$statusInfo]\n";
  108. }
  109. }
  110. }
  111. } else {
  112. print "No previous test runs to compare against.\n";
  113. }
  114. print "\n";
  115. }
  116. /**
  117. * Returns a string giving information about when a test last had a status change.
  118. * Could help to track down when regressions were introduced, as distinct from tests
  119. * which have never passed (which are more change requests than regressions).
  120. * @param string $testname
  121. * @param string $after
  122. * @return string
  123. */
  124. private function getTestStatusInfo( $testname, $after ) {
  125. // If we're looking at a test that has just been removed, then say when it first appeared.
  126. if ( $after == 'n' ) {
  127. $changedRun = $this->db->selectField( 'testitem',
  128. 'MIN(ti_run)',
  129. [ 'ti_name' => $testname ],
  130. __METHOD__ );
  131. $appear = $this->db->selectRow( 'testrun',
  132. [ 'tr_date', 'tr_mw_version' ],
  133. [ 'tr_id' => $changedRun ],
  134. __METHOD__ );
  135. return "First recorded appearance: "
  136. . date( "d-M-Y H:i:s", strtotime( $appear->tr_date ) )
  137. . ", " . $appear->tr_mw_version;
  138. }
  139. // Otherwise, this test has previous recorded results.
  140. // See when this test last had a different result to what we're seeing now.
  141. $conds = [
  142. 'ti_name' => $testname,
  143. 'ti_success' => ( $after == 'f' ? "1" : "0" ) ];
  144. if ( $this->curRun ) {
  145. $conds[] = "ti_run != " . $this->db->addQuotes( $this->curRun );
  146. }
  147. $changedRun = $this->db->selectField( 'testitem', 'MAX(ti_run)', $conds, __METHOD__ );
  148. // If no record of ever having had a different result.
  149. if ( is_null( $changedRun ) ) {
  150. if ( $after == "f" ) {
  151. return "Has never passed";
  152. } else {
  153. return "Has never failed";
  154. }
  155. }
  156. // Otherwise, we're looking at a test whose status has changed.
  157. // (i.e. it used to work, but now doesn't; or used to fail, but is now fixed.)
  158. // In this situation, give as much info as we can as to when it changed status.
  159. $pre = $this->db->selectRow( 'testrun',
  160. [ 'tr_date', 'tr_mw_version' ],
  161. [ 'tr_id' => $changedRun ],
  162. __METHOD__ );
  163. $post = $this->db->selectRow( 'testrun',
  164. [ 'tr_date', 'tr_mw_version' ],
  165. [ "tr_id > " . $this->db->addQuotes( $changedRun ) ],
  166. __METHOD__,
  167. [ "LIMIT" => 1, "ORDER BY" => 'tr_id' ]
  168. );
  169. if ( $post ) {
  170. $postDate = date( "d-M-Y H:i:s", strtotime( $post->tr_date ) ) . ", {$post->tr_mw_version}";
  171. } else {
  172. $postDate = 'now';
  173. }
  174. return ( $after == "f" ? "Introduced" : "Fixed" ) . " between "
  175. . date( "d-M-Y H:i:s", strtotime( $pre->tr_date ) ) . ", " . $pre->tr_mw_version
  176. . " and $postDate";
  177. }
  178. }