diff --git a/pom.xml b/pom.xml index 43f9f17..0ca6736 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.jenkins-ci.plugins plugin - 1.447 + 1.609.1 @@ -33,6 +33,10 @@ kbrandt Karsten Brandt + + cjbush + Chris Bush + diff --git a/src/main/java/hudson/plugins/emma/AbstractReport.java b/src/main/java/hudson/plugins/emma/AbstractReport.java index 56f67f9..40c1f08 100644 --- a/src/main/java/hudson/plugins/emma/AbstractReport.java +++ b/src/main/java/hudson/plugins/emma/AbstractReport.java @@ -2,6 +2,7 @@ import hudson.model.AbstractBuild; import hudson.model.ModelObject; +import hudson.model.Run; import java.io.IOException; @@ -64,7 +65,7 @@ public SELF getPreviousResult() { } @Override - public AbstractBuild getBuild() { + public Run getBuild() { return parent.getBuild(); } diff --git a/src/main/java/hudson/plugins/emma/CoverageObject.java b/src/main/java/hudson/plugins/emma/CoverageObject.java index a0a67bd..4d8336b 100644 --- a/src/main/java/hudson/plugins/emma/CoverageObject.java +++ b/src/main/java/hudson/plugins/emma/CoverageObject.java @@ -3,6 +3,7 @@ import hudson.Util; import hudson.model.AbstractBuild; import hudson.model.Api; +import hudson.model.Run; import hudson.util.ChartUtil; import hudson.util.ColorPalette; import hudson.util.DataSetBuilder; @@ -96,7 +97,7 @@ public Ratio getConditionCoverage() { /** * Gets the build object that owns the whole coverage report tree. */ - public abstract AbstractBuild getBuild(); + public abstract Run getBuild(); /** * Gets the corresponding coverage report object in the previous @@ -172,7 +173,7 @@ public void doGraph(StaplerRequest req, StaplerResponse rsp) throws IOException return; } - AbstractBuild build = getBuild(); + Run build = getBuild(); Calendar t = build.getTimestamp(); String w = Util.fixEmptyAndTrim(req.getParameter("width")); diff --git a/src/main/java/hudson/plugins/emma/CoverageReport.java b/src/main/java/hudson/plugins/emma/CoverageReport.java index d71ce3d..7136b32 100644 --- a/src/main/java/hudson/plugins/emma/CoverageReport.java +++ b/src/main/java/hudson/plugins/emma/CoverageReport.java @@ -1,6 +1,7 @@ package hudson.plugins.emma; import hudson.model.AbstractBuild; +import hudson.model.Run; import hudson.util.IOException2; import org.apache.commons.digester.Digester; import org.xml.sax.SAXException; @@ -54,7 +55,7 @@ public CoverageReport getPreviousResult() { } @Override - public AbstractBuild getBuild() { + public Run getBuild() { return action.owner; } diff --git a/src/main/java/hudson/plugins/emma/EmmaBuildAction.java b/src/main/java/hudson/plugins/emma/EmmaBuildAction.java index 5b4287c..42d8e96 100644 --- a/src/main/java/hudson/plugins/emma/EmmaBuildAction.java +++ b/src/main/java/hudson/plugins/emma/EmmaBuildAction.java @@ -5,6 +5,7 @@ import hudson.model.HealthReport; import hudson.model.HealthReportingAction; import hudson.model.Result; +import hudson.model.Run; import hudson.util.IOException2; import hudson.util.NullStream; import hudson.util.StreamTaskListener; @@ -32,7 +33,7 @@ */ public final class EmmaBuildAction extends CoverageObject implements HealthReportingAction, StaplerProxy { - public final AbstractBuild owner; + public final Run owner; private transient WeakReference report; @@ -47,7 +48,7 @@ public final class EmmaBuildAction extends CoverageObject imple */ private final EmmaHealthReportThresholds thresholds; - public EmmaBuildAction(AbstractBuild owner, Rule rule, Ratio classCoverage, Ratio methodCoverage, Ratio blockCoverage, Ratio lineCoverage, Ratio conditionCoverage, EmmaHealthReportThresholds thresholds) { + public EmmaBuildAction(Run owner, Rule rule, Ratio classCoverage, Ratio methodCoverage, Ratio blockCoverage, Ratio lineCoverage, Ratio conditionCoverage, EmmaHealthReportThresholds thresholds) { this.owner = owner; this.rule = rule; this.clazz = classCoverage; @@ -148,7 +149,7 @@ public Object getTarget() { } @Override - public AbstractBuild getBuild() { + public Run getBuild() { return owner; } @@ -212,8 +213,8 @@ public EmmaBuildAction getPreviousResult() { /** * Gets the previous {@link EmmaBuildAction} of the given build. */ - /*package*/ static EmmaBuildAction getPreviousResult(AbstractBuild start) { - AbstractBuild b = start; + /*package*/ static EmmaBuildAction getPreviousResult(Run start) { + Run b = start; while(true) { b = b.getPreviousBuild(); if(b==null) @@ -233,14 +234,14 @@ public EmmaBuildAction getPreviousResult() { * @throws IOException * if failed to parse the file. */ - public static EmmaBuildAction load(AbstractBuild owner, Rule rule, EmmaHealthReportThresholds thresholds, FilePath... files) throws IOException { + public static EmmaBuildAction load(Run owner, Rule rule, EmmaHealthReportThresholds thresholds, FilePath... files) throws IOException, InterruptedException { Ratio ratios[] = null; for (FilePath f: files ) { InputStream in = f.read(); try { ratios = loadRatios(in, ratios); } catch (XmlPullParserException e) { - throw new IOException2("Failed to parse " + f, e); + throw new IOException("Failed to parse " + f, e); } finally { in.close(); } diff --git a/src/main/java/hudson/plugins/emma/EmmaPublisher.java b/src/main/java/hudson/plugins/emma/EmmaPublisher.java index 8c86692..edab33d 100644 --- a/src/main/java/hudson/plugins/emma/EmmaPublisher.java +++ b/src/main/java/hudson/plugins/emma/EmmaPublisher.java @@ -12,6 +12,8 @@ import hudson.model.Action; import hudson.model.BuildListener; import hudson.model.Result; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.BuildStepMonitor; import hudson.tasks.Publisher; @@ -26,13 +28,14 @@ import java.io.PrintStream; import java.util.ArrayList; import java.util.Arrays; +import jenkins.tasks.SimpleBuildStep; /** * {@link Publisher} that captures Emma coverage reports. * * @author Kohsuke Kawaguchi */ -public class EmmaPublisher extends Recorder { +public class EmmaPublisher extends Recorder implements SimpleBuildStep { /** * Relative path to the Emma XML file inside the workspace. @@ -104,79 +107,15 @@ protected static void saveCoverageReports(FilePath folder, FilePath[] files) thr @Override public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { - final PrintStream logger = listener.getLogger(); - - // Make sure Emma actually ran - if (build instanceof MavenBuild) { - MavenBuild mavenBuild = (MavenBuild) build; - if (!didEmmaRun(mavenBuild)) { - listener.getLogger().println("Skipping Emma coverage report as mojo did not run."); - return true; - } - } else if (build instanceof MavenModuleSetBuild) { - MavenModuleSetBuild moduleSetBuild = (MavenModuleSetBuild) build; - if (!didEmmaRun(moduleSetBuild.getModuleLastBuilds().values())) { - listener.getLogger().println("Skipping Emma coverage report as mojo did not run."); - return true; - } - } - EnvVars env = build.getEnvironment(listener); - - env.overrideAll(build.getBuildVariables()); - includes = env.expand(includes); - - FilePath[] reports; - if (includes == null || includes.trim().length() == 0) { - logger.println("Emma: looking for coverage reports in the entire workspace: " + build.getWorkspace().getRemote()); - reports = locateCoverageReports(build.getWorkspace(), "**/emma/coverage.xml"); - } else { - logger.println("Emma: looking for coverage reports in the provided path: " + includes); - reports = locateCoverageReports(build.getWorkspace(), includes); - } - - if (reports.length == 0) { - if (build.getResult().isWorseThan(Result.UNSTABLE)) { - return true; - } - - logger.println("Emma: no coverage files found in workspace. Was any report generated?"); - build.setResult(Result.FAILURE); + try{ + perform(build, build.getWorkspace(), launcher, listener); return true; - } else { - String found = ""; - for (FilePath f : reports) { - found += "\n " + f.getRemote(); - } - logger.println("Emma: found " + reports.length + " report files: " + found); + } catch(Exception ex){ + return false; } - - FilePath emmafolder = new FilePath(getEmmaReport(build)); - saveCoverageReports(emmafolder, reports); - logger.println("Emma: stored " + reports.length + " report files in the build folder: " + emmafolder); - - final EmmaBuildAction action = EmmaBuildAction.load(build, rule, healthReports, reports); - - action.applySettings(advancedSettings); - - logger.println("Emma: " + action.getBuildHealth().getDescription()); - - build.getActions().add(action); - - final CoverageReport result = action.getResult(); - if (result == null) { - logger.println("Emma: Could not parse coverage results. Setting Build to failure."); - build.setResult(Result.FAILURE); - } else if (result.isFailed()) { - logger.println("Emma: code coverage enforcement failed. Setting Build to unstable."); - build.setResult(Result.UNSTABLE); - } - - checkThreshold(build, logger, env, action); - - return true; } - private void checkThreshold(AbstractBuild build, + private void checkThreshold(Run build, final PrintStream logger, EnvVars env, final EmmaBuildAction action) { if (useThreshold) { if (isMethodCoverageOk(action) @@ -230,7 +169,7 @@ public BuildStepMonitor getRequiredMonitorService() { /** * Gets the directory to store report files */ - static File getEmmaReport(AbstractBuild build) { + static File getEmmaReport(Run build) { return new File(build.getRootDir(), "emma"); } @@ -260,6 +199,78 @@ private boolean didEmmaRun(MavenBuild mavenBuild) { @Extension public static final BuildStepDescriptor DESCRIPTOR = new DescriptorImpl(); + @Override + public void perform(Run build, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException { + final PrintStream logger = listener.getLogger(); + + // Make sure Emma actually ran + if (build instanceof MavenBuild) { + MavenBuild mavenBuild = (MavenBuild) build; + if (!didEmmaRun(mavenBuild)) { + listener.getLogger().println("Skipping Emma coverage report as mojo did not run."); + return; + } + } else if (build instanceof MavenModuleSetBuild) { + MavenModuleSetBuild moduleSetBuild = (MavenModuleSetBuild) build; + if (!didEmmaRun(moduleSetBuild.getModuleLastBuilds().values())) { + listener.getLogger().println("Skipping Emma coverage report as mojo did not run."); + return; + } + } + EnvVars env = build.getEnvironment(listener); + + //env.overrideAll(build.getBuildVariables()); + includes = env.expand(includes); + + FilePath[] reports; + if (includes == null || includes.trim().length() == 0) { + logger.println("Emma: looking for coverage reports in the entire workspace: " + workspace.getRemote()); + reports = locateCoverageReports(workspace, "**/emma/coverage.xml"); + } else { + logger.println("Emma: looking for coverage reports in the provided path: " + includes); + reports = locateCoverageReports(workspace, includes); + } + + if (reports.length == 0) { + if (build.getResult().isWorseThan(Result.UNSTABLE)) { + return; + } + + logger.println("Emma: no coverage files found in workspace. Was any report generated?"); + build.setResult(Result.FAILURE); + return; + } else { + String found = ""; + for (FilePath f : reports) { + found += "\n " + f.getRemote(); + } + logger.println("Emma: found " + reports.length + " report files: " + found); + } + + FilePath emmafolder = new FilePath(getEmmaReport(build)); + saveCoverageReports(emmafolder, reports); + logger.println("Emma: stored " + reports.length + " report files in the build folder: " + emmafolder); + + final EmmaBuildAction action = EmmaBuildAction.load(build, rule, healthReports, reports); + + action.applySettings(advancedSettings); + + logger.println("Emma: " + action.getBuildHealth().getDescription()); + + build.getActions().add(action); + + final CoverageReport result = action.getResult(); + if (result == null) { + logger.println("Emma: Could not parse coverage results. Setting Build to failure."); + build.setResult(Result.FAILURE); + } else if (result.isFailed()) { + logger.println("Emma: code coverage enforcement failed. Setting Build to unstable."); + build.setResult(Result.UNSTABLE); + } + + checkThreshold(build, logger, env, action); + } + public static class DescriptorImpl extends BuildStepDescriptor { public DescriptorImpl() { diff --git a/src/test/java/hudson/plugins/emma/CoverageObjectTest.java b/src/test/java/hudson/plugins/emma/CoverageObjectTest.java index e808b1f..64324a7 100644 --- a/src/test/java/hudson/plugins/emma/CoverageObjectTest.java +++ b/src/test/java/hudson/plugins/emma/CoverageObjectTest.java @@ -14,15 +14,16 @@ public void testPrintRatioTable() throws Exception { r = new Ratio(0,100); b = new StringBuilder(); CoverageObject.printRatioTable(r, b, no_tests_required); - assertEquals("
0.0%
0/100
", b.toString()); -//[KB-] assertEquals("
0,0%
0/100
", b.toString()); + double value = 0; + String expected = String.format("
%.1f%%
0/100
", value); + assertEquals(expected, b.toString()); r = new Ratio(51,200); b = new StringBuilder(); CoverageObject.printRatioTable(r, b, no_tests_required); - assertEquals("
25.5%
51/200
", b.toString()); -//[KB-] assertEquals("
25,5%
51/200
", b.toString()); - + value = 25.5; + expected = String.format("
%.1f%%
51/200
", value); + assertEquals(expected, b.toString()); } @@ -48,11 +49,10 @@ public void testPrintColumnt() throws Exception { r = new Ratio(51,200); b = new StringBuilder(); CoverageObject.printRatioCell(false, r, b, no_tests_required); - assertEquals("\n" + - "
25.5%
51/200
\n", b.toString()); -//[KB-] assertEquals("\n" + -//[KB-] "
25,5%
51/200
\n", b.toString()); - + double value = 25.5; + String expected = String.format("\n" + + "
%.1f%%
51/200
\n", value, value); + assertEquals(expected, b.toString()); r = new Ratio(0,0); b = new StringBuilder(); diff --git a/src/test/java/hudson/plugins/emma/EmmaConfigSubmitTest.java b/src/test/java/hudson/plugins/emma/EmmaConfigSubmitTest.java index dd7301d..d8dddba 100644 --- a/src/test/java/hudson/plugins/emma/EmmaConfigSubmitTest.java +++ b/src/test/java/hudson/plugins/emma/EmmaConfigSubmitTest.java @@ -16,10 +16,12 @@ public void testIncludeIsEmpty() throws Exception { client.setThrowExceptionOnFailingStatusCode(false); FreeStyleProject fp = createFreeStyleProject(); + EmmaPublisher emma = new EmmaPublisher(); + fp.getPublishersList().add(emma); + HtmlPage p = client.goTo(fp.getUrl() + "/configure"); HtmlForm f = p.getFormByName("config"); - f.getInputByName("hudson-plugins-emma-EmmaPublisher").setChecked(true); // includes is empty submit(f); @@ -33,10 +35,12 @@ public void testIncludeIsSet() throws Exception { client.setThrowExceptionOnFailingStatusCode(false); FreeStyleProject fp = createFreeStyleProject(); + EmmaPublisher emma = new EmmaPublisher(); + fp.getPublishersList().add(emma); + HtmlPage p = client.goTo(fp.getUrl() + "/configure"); HtmlForm f = p.getFormByName("config"); - f.getInputByName("hudson-plugins-emma-EmmaPublisher").setChecked(true); f.getInputByName("emma.includes").setValueAttribute("**/*"); submit(f); @@ -50,10 +54,12 @@ public void testHealthReportDefaultMaxValue() throws Exception { client.setThrowExceptionOnFailingStatusCode(false); FreeStyleProject fp = createFreeStyleProject(); + EmmaPublisher emma = new EmmaPublisher(); + fp.getPublishersList().add(emma); + HtmlPage p = client.goTo(fp.getUrl() + "/configure"); HtmlForm f = p.getFormByName("config"); - f.getInputByName("hudson-plugins-emma-EmmaPublisher").setChecked(true); f.getInputByName("emmaHealthReports.maxClass").setValueAttribute(""); f.getInputByName("emmaHealthReports.maxMethod").setValueAttribute(""); f.getInputByName("emmaHealthReports.maxBlock").setValueAttribute(""); @@ -76,10 +82,12 @@ public void testHealthReportDefaultMinValue() throws Exception { client.setThrowExceptionOnFailingStatusCode(false); FreeStyleProject fp = createFreeStyleProject(); + EmmaPublisher emma = new EmmaPublisher(); + fp.getPublishersList().add(emma); + HtmlPage p = client.goTo(fp.getUrl() + "/configure"); HtmlForm f = p.getFormByName("config"); - f.getInputByName("hudson-plugins-emma-EmmaPublisher").setChecked(true); f.getInputByName("emmaHealthReports.minClass").setValueAttribute(""); f.getInputByName("emmaHealthReports.minMethod").setValueAttribute(""); f.getInputByName("emmaHealthReports.minBlock").setValueAttribute(""); @@ -102,10 +110,12 @@ public void testHealthReportMaxValue() throws Exception { client.setThrowExceptionOnFailingStatusCode(false); FreeStyleProject fp = createFreeStyleProject(); + EmmaPublisher emma = new EmmaPublisher(); + fp.getPublishersList().add(emma); + HtmlPage p = client.goTo(fp.getUrl() + "/configure"); HtmlForm f = p.getFormByName("config"); - f.getInputByName("hudson-plugins-emma-EmmaPublisher").setChecked(true); f.getInputByName("emmaHealthReports.maxClass").setValueAttribute("10"); f.getInputByName("emmaHealthReports.maxMethod").setValueAttribute("10"); f.getInputByName("emmaHealthReports.maxBlock").setValueAttribute("10"); @@ -128,10 +138,12 @@ public void testHealthReportMinValue() throws Exception { client.setThrowExceptionOnFailingStatusCode(false); FreeStyleProject fp = createFreeStyleProject(); + EmmaPublisher emma = new EmmaPublisher(); + fp.getPublishersList().add(emma); + HtmlPage p = client.goTo(fp.getUrl() + "/configure"); HtmlForm f = p.getFormByName("config"); - f.getInputByName("hudson-plugins-emma-EmmaPublisher").setChecked(true); f.getInputByName("emmaHealthReports.minClass").setValueAttribute("10"); f.getInputByName("emmaHealthReports.minMethod").setValueAttribute("10"); f.getInputByName("emmaHealthReports.minBlock").setValueAttribute("10"); @@ -154,11 +166,12 @@ public void test_new_no_tests_required_flag() throws Exception { client.setThrowExceptionOnFailingStatusCode(false); FreeStyleProject fp = createFreeStyleProject(); + EmmaPublisher emma = new EmmaPublisher(); + fp.getPublishersList().add(emma); + HtmlPage p = client.goTo(fp.getUrl() + "/configure"); HtmlForm f = p.getFormByName("config"); - f.getInputByName("hudson-plugins-emma-EmmaPublisher").setChecked(true); - f.getInputByName("emmaAdvancedSettings.testNotMandatory").setChecked(true); submit(f); EmmaPublisher publisher = (EmmaPublisher) fp.getPublisher(EmmaPublisher.DESCRIPTOR); @@ -179,10 +192,12 @@ public void testDataColumnMapping() throws Exception { client.setThrowExceptionOnFailingStatusCode(false); FreeStyleProject fp = createFreeStyleProject(); + EmmaPublisher emma = new EmmaPublisher(); + fp.getPublishersList().add(emma); + HtmlPage p = client.goTo(fp.getUrl() + "/configure"); HtmlForm f = p.getFormByName("config"); - f.getInputByName("hudson-plugins-emma-EmmaPublisher").setChecked(true); f.getInputByName("emmaAdvancedSettings.firstDataColumnDescriptor").setValueAttribute("test_class"); f.getInputByName("emmaAdvancedSettings.secondDataColumnDescriptor").setValueAttribute("test_method"); f.getInputByName("emmaAdvancedSettings.thirdDataColumnDescriptor").setValueAttribute("test_block");