All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.code.scoverage.plugin.SCoverageCheckMojo Maven / Gradle / Ivy

/*
 * Copyright 2014 Grzegorz Slowikowski (gslowikowski at gmail dot com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.google.code.scoverage.plugin;

import java.io.File;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import scala.Predef$;
import scoverage.Coverage;
import scoverage.IOUtils;

/**
 * ...
 * 
 * @author Grzegorz Slowikowski
 * @since 1.0.0
 */
@Mojo( name = "check", threadSafe = false )
@Execute( lifecycle = "scoverage", phase = LifecyclePhase.TEST )
public class SCoverageCheckMojo
    extends AbstractMojo
{

    /**
     * Allows SCoverage to be skipped.
     * 
     * @since 1.0.0
     */
    @Parameter( property = "scoverage.skip", defaultValue = "false" )
    private boolean skip;

//    /**
//     * Scala version used for compiler plugin artifact resolution
// *
// * If specified, and starts with {@code 2.10.} - {@code scalac-scoverage-plugin_2.10} will be used. // * If does not start with {@code 2.10.} or not specified - {@code scalac-scoverage-plugin_2.11} will be used.
// * // * @since 1.0.0 // */ // @Parameter( property = "scala.version" ) // protected String scalaVersion; /** * Directory where the coverage files should be written. *
* * @since 1.0.0 */ @Parameter( property = "scoverage.dataDir", defaultValue = "${project.build.directory}/scoverage-data" ) private File dataDir; /** * ... *
* * @since 1.0.0 */ @Parameter( property = "scoverage.minimumCoverage", defaultValue = "0" ) private Double minimumCoverage; /** * ... * * @since 1.0.0 */ @Parameter( property = "scoverage.failOnMinimumCoverage", defaultValue = "false" ) private boolean failOnMinimumCoverage; /** * ... * * @since 1.0.0 */ @Parameter( property = "scoverage.highlighting", defaultValue = "false" ) private boolean highlighting; /** * ... *
* * @since 1.0.0 */ @Parameter( property = "scoverage.scalacPluginVersion", defaultValue = "" ) private String scalacPluginVersion; /** * Maven project to interact with. */ @Parameter( defaultValue = "${project}", readonly = true, required = true ) protected MavenProject project; /** * ... * * @throws MojoFailureException if coverage is below minimumCoverage and failOnMinimumCoverage option set */ @Override public void execute() throws MojoFailureException { if ( "pom".equals( project.getPackaging() ) ) { getLog().info( "Skipping SCoverage execution for project with packaging type 'pom'" ); //for aggragetor mojo - list of submodules: List modules = project.getCollectedProjects(); return; } if ( skip ) { getLog().info( "Skipping Scoverage execution" ); return; } long ts = System.currentTimeMillis(); if ( !dataDir.exists() || !dataDir.isDirectory() ) { getLog().info( "Cannot perform check, instrumentation not performed - skipping" ); return; } File coverageFile = IOUtils.coverageFile( dataDir ); if ( !coverageFile.exists() ) { getLog().info( "Scoverage data file does not exist. Skipping report generation" ); return; } if ( !coverageFile.isFile() ) { getLog().info( "Scoverage data file is a directory, not a file. Skipping report generation" ); return; } Coverage coverage = IOUtils.deserialize( coverageFile ); File[] measurementFiles = IOUtils.findMeasurementFiles( dataDir ); scala.collection.Set measurements = IOUtils.invoked( Predef$.MODULE$.wrapRefArray( measurementFiles ) ); coverage.apply( measurements ); int branchCount = coverage.branchCount(); int statementCount = coverage.statementCount(); int invokedBranchesCount = coverage.invokedBranchesCount(); int invokedStatementCount = coverage.invokedStatementCount(); getLog().info( String.format( "DEBUG: invokedBranchesCount:%d / branchCount:%d, invokedStatementCount:%d / statementCount:%d", invokedBranchesCount, branchCount, invokedStatementCount, statementCount ) ); if ( minimumCoverage > 0.0 ) { if ( is100( minimumCoverage ) && is100( coverage.statementCoveragePercent() ) ) { getLog().info( "[scoverage] 100% Coverage !" ); } else if ( minimumCoverage > coverage.statementCoveragePercent() ) { getLog().error( String.format( "[scoverage] Coverage is below minimum [%s%% < %d%%]", coverage.statementCoverageFormatted(), minimumCoverage ) ); if ( failOnMinimumCoverage ) { throw new MojoFailureException( "Coverage minimum was not reached" ); } } else { getLog().info( String.format( "[scoverage] Coverage is above minimum [%s%% < %d%%]", coverage.statementCoverageFormatted(), minimumCoverage ) ); } } getLog().info( String.format( "[scoverage] All done. Coverage was [%s%%]", coverage.statementCoverageFormatted() ) ); long te = System.currentTimeMillis(); getLog().debug( String.format( "Mojo execution time: %d ms", te - ts ) ); } // Private utility methods private boolean is100( Double d ) { return Math.abs(100 - d) <= 0.00001d; } }