Today i publish the solution for a huge problem.

lcov and CI.

 

This php script converts a lcov data file to a corona xml file.
It thus enables CI systems like hudson to take advantage from the information in the file. Instead of just displaying the data.

 

  1. $arg1 = 'jsTestDriver.conf-coverage.dat' ;
  2. $arg2 = '' ;
  3. $arg3 = false ;
  4. if ( $argc > 1 ) {
  5.         $arg1 = $argv [ 1 ] ;
  6.  
  7.         if ( $argc > 2 ) {
  8.                 $arg2 = $argv [ 2 ] ;
  9.         }
  10.  
  11.         if ( $argc > 3 ) {
  12.                 $arg3 = true ;
  13.         }
  14. }
  15.  
  16. $c = new Lcover ( $arg1 , $arg2 , $arg3 ) ;
  17.  
  18. /**
  19. *
  20. * Lcover class is a class used to generate a clover xml file from a lcov dat file.
  21. * Usage:
  22. *           cli:
  23. *                PHP ./Lcover.php testFile.dat testOutput
  24. *            Normal:
  25. *                  c = new Lcover( './testFile.dat', 'testOutput' );
  26. * @author            erik
  27. *
  28. * @param $inputFile
  29. * @param $outputFolder
  30. */
  31. class Lcover
  32. {
  33.         public $lcoverVersion = '0.2' ;
  34.         protected $_inputFile = "" ;
  35.         protected $_outputFolder = "" ;
  36.         function __construct ( $inputFile , $outputFolder , $console )
  37.         {
  38.                 $this->_inputFile = dirname ( __FILE__ ) . '/' . $inputFile ;
  39.                 $this->_outputFolder = dirname ( __FILE__ ) . '/' . $outputFolder ;
  40.                 if ( mb_strlen ( $outputFolder ) > 0 && '/' != $outputFolder [ mb_strlen ( $outputFolder ) -1 ] ) {
  41.                         $this->_outputFolder .= '/' ;
  42.                 }
  43.  
  44.                 $this-> startConverting ( $console ) ;
  45.         }
  46.  
  47.         /**
  48.          * The start and main function to control the converting process
  49.          */
  50.         protected function startConverting ( $console )
  51.         {
  52.                 if ( file_exists ( $this->_inputFile ) ) {
  53.                         $input = $this-> getFileContent ( $this->_inputFile ) ;
  54.                         $clover = $this-> createClover ( $input ) ;
  55.                         $this-> write ( $clover , $console ) ;
  56.                 } else {
  57.                         echo 'file: ' . $this->_inputFile . ' does not exist' .PHP_EOL;
  58.                 }
  59.         }
  60.  
  61.         /**
  62.          * The function to change the input to a readable array
  63.          */
  64.         protected function getFileContent ( & $file )
  65.         {
  66.                 $in = file_get_contents ( $file ) ;
  67.                 $out = array ( ) ;
  68.                 $step1 = explode ( 'end_of_record' , $in ) ;
  69.                 $size = sizeof ( $step1 ) ;
  70.                 for ( $i = 0 ; $i < $size ; ++ $i ) {
  71.                         $out [ $i ] = explode ( PHP_EOL, trim ( $step1 [ $i ] ) ) ;
  72.                 }
  73.                 return $out ;
  74.         }
  75.  
  76.         /**
  77.          * This methode should create the clover report
  78.          * @param array $input
  79.          * @return String clover
  80.          */
  81.         protected function createClover ( array $input )
  82.         {
  83.                 $now = microtime ( ) ;
  84.                 $totalLines = 0 ;
  85.                 $totalLinesCovered = 0 ;
  86.  
  87.                 // Basic start
  88.                 $out = '<?xml version="1.0" encoding="UTF-8"?>' ;
  89.  
  90.                 // Who Covered this?
  91.                 $out .= '<coverage generated="' . $now . '" Lcover="' . $this-> lcoverVersion . '">' ;
  92.  
  93.                 // Project
  94.                 // TODO:Get a good project name
  95.                 $out .= '<project name="' . $this->_inputFile . '" timestamp="' . $now . '">' ;
  96.  
  97.                 foreach ( $input as $in ) {
  98.                         $lineCount = count ( $in ) -1 ;
  99.                         $out .= '<file name="' . substr ( $in [ 0 ] , 3 ) . '" >' ;
  100.                         $lines = "" ;
  101.                         $coveredLines = 0 ;
  102.                         for ( $i = 1 ; $i <= $lineCount ; ++ $i ) {
  103.                                 $thisLineCount = substr ( $in [ $i ] , strripos ( $in [ $i ] , ',' ) + 1 ) ;
  104.                                 if ( ( int ) $thisLineCount > 0 ) {
  105.                                         ++ $coveredLines ;
  106.                                 }
  107.                                 $lines .= '<line num="' . substr ( $in [ $i ] , 3 , ( mb_strlen ( $in [ $i ] ) - 3 - ( mb_strlen ( $in [ $i ] ) - strripos ( $in [ $i ] , ',' ) ) ) ) . '" type="" count="' . $thisLineCount . '" />' ;
  108.                         }
  109.  
  110.                         $out .= '<class name="' . substr ( $in [ 0 ] , strripos ( $in [ 0 ] , '/' ) ) . '">' ;
  111.                         $out .= '<metrics elements="' . $lineCount . '" coveredelements="' . $coveredLines . '" />' ;
  112.                         $out .= '</class>' ;
  113.  
  114.                         $out .= $lines ;
  115.  
  116.                         $out .= '<metrics elements="' . $lineCount . '" coveredelements="' . $coveredLines . '" />' ;
  117.  
  118.                         $out .= '</file>' ;
  119.  
  120.                         $totalLines += $lineCount ;
  121.                         $totalLinesCovered += $coveredLines ;
  122.                 }
  123.  
  124.                 $out .= '<metrics files="' . count ( $input ) . '" statements="' . $totalLines . '" coveredstatements="' . $totalLinesCovered . '" elements="' . $totalLines . '" coveredelements="' . $totalLinesCovered . '" />' ;
  125.  
  126.                 // End of file
  127.                 $out .= '</project>' ;
  128.                 $out .= '</coverage>' ;
  129.  
  130.                 return $out ;
  131.         }
  132.  
  133.         /**
  134.          * This function writes the file to the disk.
  135.          * @param $toWrite
  136.          */
  137.         public function write ( $toWrite , $console )
  138.         {
  139.                 if ( $console ) {
  140.                         echo $toWrite ;
  141.                 } else {
  142.                         $file = fopen ( $this->_outputFolder . 'clover.xml' , 'w+' ) ;
  143.                         fwrite ( $file , $toWrite ) ;
  144.                         fclose ( $file ) ;
  145.                         echo "Done!!!" ;
  146.                 }
  147.         }
  148. }

Note however:
This script has been written for use with JsTestDriver, and has not been tested with other programs.
The information in the Lcov file is not as detailed as that in a normal carona xml file and thus less information is displayed.

Note2:
The $console flag is added later on. It should write the output to the console instead of to a file.

Note3:
This script has been written to use from the command line, but should be able to be used as a normal webpage too.

Latest Twitter


    Warning: DOMDocument::load(https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=reefclaw) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.0 410 Gone in /home/deb15621/domains/erikmaas.com/public_html/plugins/twitter/rss.php on line 78

    Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=reefclaw" in /home/deb15621/domains/erikmaas.com/public_html/plugins/twitter/rss.php on line 78
My Twitter My Twitter Rss

latest blog