Merge branch 'tiobe/fix-error-handling' of https://github.com/tiobe/pmd into tiobe-tiobe/fix-error-handling
This commit is contained in:
@ -121,5 +121,11 @@
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.stefanbirkner</groupId>
|
||||
<artifactId>system-rules</artifactId>
|
||||
<version>1.8.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -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();
|
||||
|
@ -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));
|
||||
|
@ -64,7 +64,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;
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user