From e2cc67c59133d1af866abfa4a4401739e13002e0 Mon Sep 17 00:00:00 2001 From: Jan van Nunen Date: Thu, 5 Mar 2015 12:22:56 +0100 Subject: [PATCH] Fixed exit status of PMD when error occurs When an error occured the exit status of PMD was 0. The error occured because the logic of 'NO_EXIT_AFTER_RUN' was incorrect/inverted. A 'System.exit()' was performed when 'NO_EXIT_AFTER_RUN' was set while it should be skipped. I copied the fix from the CPDCommandLineInterface class. Furthermore I made sure all error messages are printed to System.err instead of System.out, so they can easily extracted/found when PMD is invoked by external tools. --- pmd-core/pom.xml | 6 ++++ .../main/java/net/sourceforge/pmd/PMD.java | 2 +- .../pmd/cli/PMDCommandLineInterface.java | 12 +++++--- .../pmd/cpd/CPDCommandLineInterface.java | 2 +- .../sourceforge/pmd/cpd/CPDConfiguration.java | 2 +- .../pmd/cli/PMDCommandLineInterfaceTest.java | 30 +++++++++++++++++++ 6 files changed, 47 insertions(+), 7 deletions(-) diff --git a/pmd-core/pom.xml b/pmd-core/pom.xml index d8b6643221..611f09d4ae 100644 --- a/pmd-core/pom.xml +++ b/pmd-core/pom.xml @@ -121,5 +121,11 @@ mockito-all test + + com.github.stefanbirkner + system-rules + 1.8.0 + test + diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java b/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java index 042e442d12..28bb5d2985 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java @@ -439,7 +439,7 @@ public class PMD { } catch (Exception e) { System.out.println(PMDCommandLineInterface.buildUsageText()); System.out.println(); - System.out.println(e.getMessage()); + System.err.println(e.getMessage()); status = PMDCommandLineInterface.ERROR_STATUS; } finally { logHandlerManager.close(); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java b/pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java index 8f0f66538f..8ecec514cd 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java @@ -41,7 +41,7 @@ public class PMDCommandLineInterface { } catch (ParameterException e) { jcommander.usage(); System.out.println(buildUsageText(jcommander)); - System.out.println(e.getMessage()); + System.err.println(e.getMessage()); setStatusCodeOrExit(ERROR_STATUS); } return arguments; @@ -174,9 +174,13 @@ public class PMDCommandLineInterface { } } - private static boolean isExitAfterRunSet() { - return (System.getenv(NO_EXIT_AFTER_RUN) == null ? false : true); - } + private static boolean isExitAfterRunSet() { + String noExit = System.getenv(NO_EXIT_AFTER_RUN); + if (noExit == null) { + noExit = System.getProperty(NO_EXIT_AFTER_RUN); + } + return (noExit == null ? true : false); + } private static void setStatusCode(int statusCode) { System.setProperty(STATUS_CODE_PROPERTY, Integer.toString(statusCode)); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java index 0294f5c3a9..e0a0d11da7 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java @@ -63,7 +63,7 @@ public class CPDCommandLineInterface { } catch (ParameterException e) { jcommander.usage(); System.out.println(buildUsageText()); - System.out.println(e.getMessage()); + System.err.println(e.getMessage()); setStatusCodeOrExit(1); return; } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java index 8fe283cf4f..07d74660dd 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java @@ -163,7 +163,7 @@ public class CPDConfiguration extends AbstractConfiguration { try { return (Renderer) Class.forName(name).newInstance(); } catch (Exception e) { - System.out.println("Can't find class '" + name + System.err.println("Can't find class '" + name + "', defaulting to SimpleRenderer."); } return new SimpleRenderer(); diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/cli/PMDCommandLineInterfaceTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/cli/PMDCommandLineInterfaceTest.java index 7d51439525..8979b9faad 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/cli/PMDCommandLineInterfaceTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/cli/PMDCommandLineInterfaceTest.java @@ -4,12 +4,27 @@ package net.sourceforge.pmd.cli; import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.contrib.java.lang.system.ExpectedSystemExit; +import org.junit.contrib.java.lang.system.RestoreSystemProperties; /** * Unit test for {@link PMDCommandLineInterface} */ public class PMDCommandLineInterfaceTest { + @Rule + public final ExpectedSystemExit exit = ExpectedSystemExit.none(); + + @Rule //Restores system properties after test + public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties(); + + @Before + public void clearSystemProperties () { + System.clearProperty(PMDCommandLineInterface.NO_EXIT_AFTER_RUN); + System.clearProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY); + } @Test public void testProperties() { @@ -20,4 +35,19 @@ public class PMDCommandLineInterfaceTest { Assert.assertEquals("output_folder", params.getProperties().getProperty("outputDir")); } + + @Test + public void testSetStatusCodeOrExit_DoExit() { + exit.expectSystemExitWithStatus(0); + + PMDCommandLineInterface.setStatusCodeOrExit(0); + } + + @Test + public void testSetStatusCodeOrExit_SetStatus() { + System.setProperty(PMDCommandLineInterface.NO_EXIT_AFTER_RUN, "1"); + + PMDCommandLineInterface.setStatusCodeOrExit(0); + Assert.assertEquals(System.getProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY), "0"); + } }