Skip to content
This repository has been archived by the owner on Aug 7, 2020. It is now read-only.

lcov and cobertura reports sources paths #113

Open
guidograzioli opened this issue Dec 13, 2013 · 5 comments
Open

lcov and cobertura reports sources paths #113

guidograzioli opened this issue Dec 13, 2013 · 5 comments

Comments

@guidograzioli
Copy link

I am using jasmine-maven-plugin 1.3.1.3 and saga-maven-plugin 1.5.2 to build a jquery plugin. I have some htmlunit failures in jasmine:test so i need to use phantomjs, but this might be unrelated to the problem.

I want to use the cobertura report generated by saga-plugin for jenkins cobertura plugin, and the lcov report for sonar (3.7 with javascript module 1.4).

My sources are in src/main/javascript (as default layout using the maven-jasmine-plugin archetype), jasmine deploys the sources for running the tests in http://host:port/src with the runner in the root.

To be able to load the reported coverage in the jenkins plugin and make the plugin find the sources, i need to add an ant task like (this strips out the filename prefix path):

<replaceregexp match='name=\"src\/' replace='name=\"' file="target/reports/total-coverage.xml" flags="gm"/>

To let sonar load correctly the lcov coverage, another ant task (this converts relative to absolute):

<property name="absolute.src.path" location="${project.build.sourceDirectory}"/>
<replaceregexp match="SF:src" replace="SF:${absolute.src.path}" file="target/reports/total-coverage.dat" flags="gm"/>

I tried with all possible combinations of relative paths, but it seems sonar is only able to locate the files using absolute paths.

This is my configuration:

<plugins>
  <plugin>
    <groupId>com.github.searls</groupId>
    <artifactId>jasmine-maven-plugin</artifactId>
    <version>1.3.1.3</version>
    <executions>
      <execution>
        <goals>
          <goal>test</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <webDriverClassName>org.openqa.selenium.phantomjs.PhantomJSDriver</webDriverClassName>
      <keepServerAlive>true</keepServerAlive>
      <haltOnFailure>false</haltOnFailure>
      <jasmineTargetDir>${project.build.directory}${file.separator}reports</jasmineTargetDir>
      <preloadSources>
         <source>${basedir}/src/main/javascript/lib/jquery.js</source>
         <source>${basedir}/src/main/javascript/lib/bootstrap.min.js</source>
         <source>${basedir}/src/main/javascript/lib/bootstrap-paginator.min.js</source>
         <source>${basedir}/src/main/javascript/lib/jasmine-jquery.js</source>
      </preloadSources>
    </configuration>
  </plugin>
  <plugin>
    <groupId>com.github.timurstrekalov</groupId>
    <artifactId>saga-maven-plugin</artifactId>
    <version>1.5.2</version>
    <executions>
      <execution>
        <phase>integration-test</phase>
        <goals>
          <goal>coverage</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <baseDir>http://localhost:${jasmine.serverPort}</baseDir>
      <outputDir>${project.build.directory}/reports</outputDir>
      <sourceDirs>
         <sourceDir>src/main/javascript</sourceDir>
      </sourceDirs>
      <noInstrumentPatterns>
        <pattern>.*/spec/.*</pattern>
        <pattern>.*/lib/.*\.js</pattern>
      </noInstrumentPatterns>
    </configuration>
  </plugin>
 [....]
<properties>
  <sonar.language>js</sonar.language>
  <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
  <sonar.javascript.reportsPath>target/reports</sonar.javascript.reportsPath>
  <sonar.javascript.jstestdriver.reportsPath>target/reports</sonar.javascript.jstestdriver.reportsPath>
  <sonar.javascript.coveragePlugin>lcov</sonar.javascript.coveragePlugin>
  <sonar.javascript.lcov.reportPath>target/reports/total-coverage.dat</sonar.javascript.lcov.reportPath>
</properties>

Am i missing some property that lets me avoid using ant for fixing paths?

@sieverssj
Copy link
Contributor

I can't speak to the Sonar pieces, but I know that this will work out-of-the-box with Jenkins depending on your project structure.

By default, the Cobertura report will generate a "sources" element with a pointer to the maven ${basedir} environment variable, which is an absolute path. You are overriding this by providing "sourceDirs" in your configuration. The problem is that you are providing a relative path, so it will not resolve. You can try to use the default sourceDir (by omitting your configuration for sourceDirs) or you can define an absolute path via an environment variable you provide in the configuration. Otherwise, Jenkins won't be able to find the source files.

UPDATE: Updating to remove angle brackets from some of the elements I was quoting, as GH markdown interprets them

@guidograzioli
Copy link
Author

I don't completely get your answer; the problem is that the cobertura is generated like this:

<sources>
    <source>src/main/javascript</source>
</sources>
<packages>
    <package name="default" line-rate="0.0" branch-rate="0.0" complexity="0">
        <classes>
            <class name="lovd-api-client.js" filename="src/lovd-api-client.js" line-rate="0.2" branch-rate="0.0" complexity="0">

while the last line should be:

            <class name="lovd-api-client.js" filename="lovd-api-client.js" line-rate="0.2" branch-rate="0.0" complexity="0">

The src in the filename attribute is the directory in which jasmine-maven-plugin decides to deploy the files during the tests; jenkins cannot find sources under src/, because there is not src/. Unfortunaly it seems it is not possible to set the deployment directory to the root.

@sieverssj
Copy link
Contributor

Based on my experience, the src in the filename is not the issue. The issue is that the Jenkins cobertura plugin is trying to build an absolute path to the resource by appending the filename to the source element you have listed.

Here's an example of working output from a local Jenkins build I did of the OpenSocial Explorer Project:

<coverage branch-rate="0.0" branches-covered="0" branches-valid="0" complexity="0" line-rate="0.8014629049111808" lines-valid="957" lines-covered="767" timestamp="1376875707" version="1.9">
    <sources>
        <source>C:\.jenkins\jobs\OpenSocial Explorer\workspace\opensocial-explorer-webcontent</source>
    </sources>
    <packages>
        <package name="default" line-rate="0.0" branch-rate="0.0" complexity="0">
            <classes>
                <class name="GadgetToolbar.js" filename="src/main/javascript/modules/widgets/gadgetarea/GadgetToolbar.js" line-rate="0.3333333333333333" branch-rate="0.0" complexity="0">
                    <methods />
                    <lines>

Notice the source is an absolute path to the base directory for the maven module.

The opensocial-explorer-webcontent pom is using the default sourceDir config for Saga, so the source element in the output is populated with the maven ${project.basedir} property. Here's the project's config for Jasmine and Saga if you want to compare:

 <plugin>
    <!-- Documentation: http://searls.github.com/jasmine-maven-plugin/ -->
    <groupId>com.github.searls</groupId>
    <artifactId>jasmine-maven-plugin</artifactId>
    <version>${jasmin-maven-plugin.version}</version>
    <extensions>true</extensions>
    <executions>
      <execution>
        <goals>
          <goal>test</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <!-- configuration properties will go here -->
      <specIncludes>
        <include>javascript/**/*.js</include>
      </specIncludes>
      <sourceExcludes>
        <exclude>**/*.js</exclude>
      </sourceExcludes>
      <jsTestSrcDir>${basedir}/src/test</jsTestSrcDir>
      <preloadSources>
        <source>${project.basedir}/src/test/resources/jasmine-test-bootstrap.js</source>
        <source>${project.basedir}/src/test/resources/dojo-release-1.8.3/dojo/dojo.js</source>
      </preloadSources>
      <customRunnerTemplate>src/test/resources/DojoSpecRunner.htmltemplate</customRunnerTemplate>
      <keepServerAlive>true</keepServerAlive>
    </configuration>
  </plugin>
    <plugin>
      <groupId>com.github.timurstrekalov</groupId>
      <artifactId>saga-maven-plugin</artifactId>
      <version>${saga-maven-plugin.version}</version>
      <executions>
        <execution>
          <goals>
            <goal>coverage</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <baseDir>http://localhost:${jasmine.serverPort}</baseDir>
        <outputDir>${project.build.directory}/coverage</outputDir>
        <noInstrumentPatterns>
          <pattern>.*/spec/.*</pattern>
        </noInstrumentPatterns>
      </configuration>
    </plugin> 

I hope that helps!

@guidograzioli
Copy link
Author

cobertura-plugin in my case works just fine with relative paths; it is just that, in the filename attribute of the class element, the path outputted by saga contains the deploy path generated by jasmine.

@sieverssj
Copy link
Contributor

Sorry, I need to have my coffee before I try to answer questions. :)

I understand your issue now. Looking at your Jasmine config I don't understand why your files are being hosted at localhost:8234/src instead of localhost:8234/src/main/javascript since the latter reflects that location of your source per the jasmine maven archetype.

When I run mvn jasmine:bdd for the OpenSocial explorer project I mentioned above, Jasmine loads the source file via src/main/javascript.

I don't understand why your setup is different. Offhand, I don't see anything different in our Jasmine configs that would cause this, unless it's a product of the DojoSpecRunner.htmltemplate that the OSE project is using.

If you run mvn jasmine:bdd and go to the test page, where do you see your source being loaded from?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants