Skip to content

Commit

Permalink
Merge pull request #80 from jenkinsci/logimprove
Browse files Browse the repository at this point in the history
JENKINS-25781 Add common prefix to all logs
  • Loading branch information
jxpearce-godaddy committed Nov 9, 2017
2 parents 70db38f + 12fa58f commit f244b1f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 36 deletions.
@@ -0,0 +1,14 @@
package hudson.plugins.cobertura;

import hudson.AbortException;

/**
*
* @author jeffpearce
*/
public class CoberturaAbortException extends AbortException {
public CoberturaAbortException(String message) {
super("[Cobertura] " + message);
}

}
60 changes: 34 additions & 26 deletions src/main/java/hudson/plugins/cobertura/CoberturaPublisher.java
Expand Up @@ -532,11 +532,11 @@ public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, fina
Result threshold = onlyStable ? Result.SUCCESS : Result.FAILURE;
Result buildResult = build.getResult();
if (buildResult != null && buildResult.isWorseThan(threshold)) {
listener.getLogger().println("Skipping Cobertura coverage report as build was not " + threshold.toString() + " or better ...");
logMessage(listener, "Skipping Cobertura coverage report as build was not " + threshold.toString() + " or better ...");
return;
}

listener.getLogger().println("[Cobertura] Publishing Cobertura coverage report...");
logMessage(listener, "Publishing Cobertura coverage report...");
final File buildCoberturaDir = build.getRootDir();
FilePath buildTarget = new FilePath(buildCoberturaDir);

Expand All @@ -553,20 +553,20 @@ public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, fina
} catch (IOException e) {
Util.displayIOException(e, listener);
e.printStackTrace(listener.fatalError("Unable to find coverage results"));
throw new AbortException("Unable to find coverage results");
throw new CoberturaAbortException("Unable to find coverage results");
}

if (reports.length == 0) {
String msg = "[Cobertura] No coverage results were found using the pattern '"
String msg = "No coverage results were found using the pattern '"
+ coberturaReportFile + "' relative to '"
+ workspace.getRemote() + "'."
+ " Did you enter a pattern relative to the correct directory?"
+ " Did you generate the XML report(s) for Cobertura?";
listener.getLogger().println(msg);
logMessage(listener, msg);
if (failNoReports) {
throw new AbortException(msg);
throw new CoberturaAbortException(msg);
} else {
listener.getLogger().println("[Cobertura] Skipped cobertura reports.");
logMessage(listener, "Skipped cobertura reports.");
}
return;
}
Expand All @@ -579,11 +579,11 @@ public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, fina
Util.displayIOException(e, listener);
String msg = "Unable to copy coverage from " + reports[i] + " to " + buildTarget;
e.printStackTrace(listener.fatalError(msg));
throw new AbortException(msg);
throw new CoberturaAbortException(msg);
}
}

listener.getLogger().println("Publishing Cobertura coverage results...");
logMessage(listener, "Publishing Cobertura coverage results...");
Set<String> sourcePaths = new HashSet<String>();
CoverageResult result = null;
for (File coberturaXmlReport : getCoberturaReports(build)) {
Expand All @@ -592,11 +592,11 @@ public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, fina
} catch (IOException e) {
Util.displayIOException(e, listener);
e.printStackTrace(listener.fatalError("Unable to parse " + coberturaXmlReport));
throw new AbortException("Unable to parse " + coberturaXmlReport);
throw new CoberturaAbortException("Unable to parse " + coberturaXmlReport);
}
}
if (result != null) {
listener.getLogger().println("Cobertura coverage report found.");
logMessage(listener, "Cobertura coverage report found.");
result.setOwner(build);
final FilePath paintedSourcesPath = new FilePath(new File(build.getParent().getRootDir(), "cobertura"));
paintedSourcesPath.mkdirs();
Expand All @@ -623,33 +623,33 @@ unhealthyTarget, getOnlyStable(), getFailUnhealthy(), getFailUnstable(), getAuto
build.addAction(action);
Set<CoverageMetric> failingMetrics = failingTarget.getFailingMetrics(result);
if (!failingMetrics.isEmpty()) {
listener.getLogger().println("Code coverage enforcement failed for the following metrics:");
logMessage(listener, "Code coverage enforcement failed for the following metrics:");
float oldStabilityPercent;
float setStabilityPercent;
for (CoverageMetric metric : failingMetrics) {
oldStabilityPercent = failingTarget.getObservedPercent(result, metric);
setStabilityPercent = failingTarget.getSetPercent(result, metric);
listener.getLogger().println(" " + metric.getName() + "'s stability is " + roundDecimalFloat(oldStabilityPercent * 100f) + " and set mininum stability is " + roundDecimalFloat(setStabilityPercent * 100f) + ".");
logMessage(listener, " " + metric.getName() + "'s stability is " + roundDecimalFloat(oldStabilityPercent * 100f) + " and set mininum stability is " + roundDecimalFloat(setStabilityPercent * 100f) + ".");
}
if (!getFailUnstable()) {
listener.getLogger().println("Setting Build to unstable.");
logMessage(listener, "Setting Build to unstable.");
build.setResult(Result.UNSTABLE);
} else {
throw new AbortException("Failing build due to unstability.");
throw new CoberturaAbortException("Failing build due to unstability.");
}
}
if (getFailUnhealthy()) {
Set<CoverageMetric> unhealthyMetrics = unhealthyTarget.getFailingMetrics(result);
if (!unhealthyMetrics.isEmpty()) {
listener.getLogger().println("Unhealthy for the following metrics:");
logMessage(listener, "Unhealthy for the following metrics:");
float oldHealthyPercent;
float setHealthyPercent;
for (CoverageMetric metric : unhealthyMetrics) {
oldHealthyPercent = unhealthyTarget.getObservedPercent(result, metric);
setHealthyPercent = unhealthyTarget.getSetPercent(result, metric);
listener.getLogger().println(" " + metric.getName() + "'s health is " + roundDecimalFloat(oldHealthyPercent * 100f) + " and set minimum health is " + roundDecimalFloat(setHealthyPercent * 100f) + ".");
}
throw new AbortException("Failing build because it is unhealthy.");
throw new CoberturaAbortException("Failing build because it is unhealthy.");
}
}
if (build.getResult() == null || build.getResult() == Result.SUCCESS) {
Expand All @@ -662,7 +662,7 @@ unhealthyTarget, getOnlyStable(), getFailUnhealthy(), getFailUnstable(), getAuto
}
}
} else {
throw new AbortException("No coverage results were successfully parsed. Did you generate "
throw new CoberturaAbortException("No coverage results were successfully parsed. Did you generate "
+ "the XML report(s) for Cobertura?");
}
}
Expand All @@ -671,54 +671,54 @@ unhealthyTarget, getOnlyStable(), getFailUnhealthy(), getFailUnstable(), getAuto
* Parses any coverage strings provided to the plugin and sets the
* coverage targets.
*
* @throws AbortException
* @throws CoberturaAbortException
*/
private void setAllCoverageTargets() throws AbortException {
private void setAllCoverageTargets() throws CoberturaAbortException {
if (lineCoverageTargets != null) {
try {
setCoverageTargets(CoverageMetric.LINE, lineCoverageTargets);
} catch (NumberFormatException e) {
throw new AbortException("Invalid value for lineCoverageTargets");
throw new CoberturaAbortException("Invalid value for lineCoverageTargets");
}
}

if (packageCoverageTargets != null) {
try {
setCoverageTargets(CoverageMetric.PACKAGES, packageCoverageTargets);
} catch (NumberFormatException e) {
throw new AbortException("Invalid value for packageCoverageTargets");
throw new CoberturaAbortException("Invalid value for packageCoverageTargets");
}
}

if (fileCoverageTargets != null) {
try {
setCoverageTargets(CoverageMetric.FILES, fileCoverageTargets);
} catch (NumberFormatException e) {
throw new AbortException("Invalid value for fileCoverageTargets");
throw new CoberturaAbortException("Invalid value for fileCoverageTargets");
}
}

if (classCoverageTargets != null) {
try {
setCoverageTargets(CoverageMetric.CLASSES, classCoverageTargets);
} catch (NumberFormatException e) {
throw new AbortException("Invalid value for classCoverageTargets");
throw new CoberturaAbortException("Invalid value for classCoverageTargets");
}
}

if (methodCoverageTargets != null) {
try {
setCoverageTargets(CoverageMetric.METHOD, methodCoverageTargets);
} catch (NumberFormatException e) {
throw new AbortException("Invalid value for methodCoverageTargets");
throw new CoberturaAbortException("Invalid value for methodCoverageTargets");
}
}

if (conditionalCoverageTargets != null) {
try {
setCoverageTargets(CoverageMetric.CONDITIONAL, conditionalCoverageTargets);
} catch (NumberFormatException e) {
throw new AbortException("Invalid value for conditionalCoverageTargets");
throw new CoberturaAbortException("Invalid value for conditionalCoverageTargets");
}
}
}
Expand Down Expand Up @@ -788,6 +788,14 @@ private void setNewPercentages(CoverageResult result, boolean select, TaskListen
}
}
}

static private void logMessage(TaskListener listener, String message) {
listener.getLogger().printf("%s%n", wrappedMessage(message));
}

static private String wrappedMessage(String message) {
return String.format("[Cobertura] %s%n", message);
}

/**
* {@inheritDoc}
Expand Down
Expand Up @@ -313,7 +313,7 @@ public void testLineCoverageFail() throws Exception {
jenkinsRule.assertLogContains("Publishing Cobertura coverage report...", run);
jenkinsRule.assertLogContains("Unhealthy for the following metrics:", run);
jenkinsRule.assertLogContains("Lines's health is 90.0 and set minimum health is 91.0.", run);
jenkinsRule.assertLogContains("ERROR: Failing build because it is unhealthy.", run);
jenkinsRule.assertLogContains("ERROR: [Cobertura] Failing build because it is unhealthy.", run);
}

/**
Expand Down Expand Up @@ -356,7 +356,7 @@ public void testLineCoverageFailUnstable() throws Exception {
jenkinsRule.assertLogContains("Publishing Cobertura coverage report...", run);
jenkinsRule.assertLogContains("Code coverage enforcement failed for the following metrics:", run);
jenkinsRule.assertLogContains("Lines's stability is 90.0 and set mininum stability is 91.0.", run);
jenkinsRule.assertLogContains("ERROR: Failing build due to unstability.", run);
jenkinsRule.assertLogContains("ERROR: [Cobertura] Failing build due to unstability.", run);
}

/**
Expand Down Expand Up @@ -397,7 +397,7 @@ public void testLineCoverageSuccess() throws Exception {

jenkinsRule.assertLogContains("Publishing Cobertura coverage report...", run);
jenkinsRule.assertLogNotContains("Unhealthy for the following metrics:", run);
jenkinsRule.assertLogNotContains("ERROR: Failing build because it is unhealthy.", run);
jenkinsRule.assertLogNotContains("ERROR:", run);
}

/**
Expand All @@ -418,7 +418,7 @@ public void testBranchCoverageFail() throws Exception {
jenkinsRule.assertLogContains("Publishing Cobertura coverage report...", run);
jenkinsRule.assertLogContains("Unhealthy for the following metrics:", run);
jenkinsRule.assertLogContains("Conditionals's health is 75.0 and set minimum health is 76.0.", run);
jenkinsRule.assertLogContains("ERROR: Failing build because it is unhealthy.", run);
jenkinsRule.assertLogContains("ERROR: [Cobertura] Failing build because it is unhealthy.", run);
}

/**
Expand All @@ -439,7 +439,7 @@ public void testBranchCoverageFailUnstable() throws Exception {
jenkinsRule.assertLogContains("Publishing Cobertura coverage report...", run);
jenkinsRule.assertLogContains("Code coverage enforcement failed for the following metrics:", run);
jenkinsRule.assertLogContains("Conditionals's stability is 75.0 and set mininum stability is 76.0.", run);
jenkinsRule.assertLogContains("ERROR: Failing build due to unstability.", run);
jenkinsRule.assertLogContains("ERROR: [Cobertura] Failing build due to unstability.", run);
}

/**
Expand All @@ -459,7 +459,7 @@ public void testBranchCoverageSuccess() throws Exception {

jenkinsRule.assertLogContains("Publishing Cobertura coverage report...", run);
jenkinsRule.assertLogNotContains("Unhealthy for the following metrics:", run);
jenkinsRule.assertLogNotContains("ERROR: Failing build because it is unhealthy.", run);
jenkinsRule.assertLogNotContains("ERROR:", run);
}

/**
Expand All @@ -481,7 +481,7 @@ public void testFileCoverageFail() throws Exception {
jenkinsRule.assertLogContains("Publishing Cobertura coverage report...", run);
jenkinsRule.assertLogContains("Unhealthy for the following metrics:", run);
jenkinsRule.assertLogContains("Files's health is 100.0 and set minimum health is 101.0.", run);
jenkinsRule.assertLogContains("ERROR: Failing build because it is unhealthy.", run);
jenkinsRule.assertLogContains("ERROR: [Cobertura] Failing build because it is unhealthy.", run);
jenkinsRule.assertLogContains("Finished: FAILURE", run);
}

Expand All @@ -504,7 +504,7 @@ public void testPackageCoverageFail() throws Exception {
jenkinsRule.assertLogContains("Publishing Cobertura coverage report...", run);
jenkinsRule.assertLogContains("Unhealthy for the following metrics:", run);
jenkinsRule.assertLogContains("Packages's health is 100.0 and set minimum health is 101.0.", run);
jenkinsRule.assertLogContains("ERROR: Failing build because it is unhealthy.", run);
jenkinsRule.assertLogContains("ERROR: [Cobertura] Failing build because it is unhealthy.", run);
jenkinsRule.assertLogContains("Finished: FAILURE", run);
}

Expand All @@ -527,7 +527,7 @@ public void testClassCoverageFail() throws Exception {
jenkinsRule.assertLogContains("Publishing Cobertura coverage report...", run);
jenkinsRule.assertLogContains("Unhealthy for the following metrics:", run);
jenkinsRule.assertLogContains("Classes's health is 100.0 and set minimum health is 101.0.", run);
jenkinsRule.assertLogContains("ERROR: Failing build because it is unhealthy.", run);
jenkinsRule.assertLogContains("ERROR: [Cobertura] Failing build because it is unhealthy.", run);
jenkinsRule.assertLogContains("Finished: FAILURE", run);
}

Expand All @@ -550,7 +550,7 @@ public void testMethodCoverageFail() throws Exception {
jenkinsRule.assertLogContains("Publishing Cobertura coverage report...", run);
jenkinsRule.assertLogContains("Unhealthy for the following metrics:", run);
jenkinsRule.assertLogContains("Methods's health is 100.0 and set minimum health is 101.0.", run);
jenkinsRule.assertLogContains("ERROR: Failing build because it is unhealthy.", run);
jenkinsRule.assertLogContains("ERROR: [Cobertura] Failing build because it is unhealthy.", run);
jenkinsRule.assertLogContains("Finished: FAILURE", run);
}

Expand Down

0 comments on commit f244b1f

Please sign in to comment.