Skip to content

Commit

Permalink
fix: make CLI return 0 for --version or --help options
Browse files Browse the repository at this point in the history
Fix #1520
  • Loading branch information
rdeltour committed Jul 6, 2023
1 parent 8575a6b commit 49aacb2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
11 changes: 9 additions & 2 deletions src/main/java/com/adobe/epubcheck/tool/EpubChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public class EpubChecker
File listChecksOut;
File customMessageFile;
boolean listChecks = false;
boolean displayHelpOrVersion = false;
boolean useCustomMessageFile = false;
boolean failOnWarnings = false;
private Messages messages = Messages.getInstance();
Expand Down Expand Up @@ -138,6 +139,10 @@ public int run(String[] args)
dumpMessageDictionary(report);
return 0;
}
if (displayHelpOrVersion)
{
return 0;
}
if (useCustomMessageFile)
{
report.setCustomMessageFile(customMessageFile.getAbsolutePath());
Expand Down Expand Up @@ -487,7 +492,7 @@ private boolean processArguments(String[] args)
setCustomMessageFileFromEnvironment();

Pattern argPattern = Pattern.compile("--?(.*)");

for (int i = 0; i < args.length; i++)
{
Matcher argMatch = argPattern.matcher(args[i]);
Expand Down Expand Up @@ -738,9 +743,11 @@ else if (!fileName.startsWith("-"))
case "?":
case "help":
displayHelp(); // display help message
displayHelpOrVersion = true;
break;
case "version":
displayVersion();
displayHelpOrVersion = true;
break;
default:
System.err.println(String.format(messages.get("unrecognized_argument"), args[i]));
Expand Down Expand Up @@ -789,7 +796,7 @@ else if (!fileName.startsWith("-"))

if (path == null)
{
if (listChecks)
if (listChecks || displayHelpOrVersion)
{
return true;
}
Expand Down
37 changes: 16 additions & 21 deletions src/test/java/com/adobe/epubcheck/tools/CommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void notfoundTest()
* Validate that the -out parameter will generate a well formed xml output.
*
* @throws Exception
* Any parsing errors will be thrown as an exception.
* Any parsing errors will be thrown as an exception.
*/
@Test
public void outputXMLReportTest()
Expand All @@ -206,7 +206,7 @@ public void outputXMLReportTest()
* unpacked epubs.
*
* @throws Exception
* Any parsing errors will be thrown as an exception.
* Any parsing errors will be thrown as an exception.
*/
@Test
public void outputXMLModeExpandedReportTest()
Expand Down Expand Up @@ -244,7 +244,7 @@ public void quietTest()
* get a correct xml output report with the output flag.
*
* @throws Exception
* Any parsing errors will be thrown as an exception.
* Any parsing errors will be thrown as an exception.
*/
@Test
public void quietRunWithOutputTest()
Expand Down Expand Up @@ -378,9 +378,8 @@ public void missingLocaleShouldFailTest()
@Test
public void helpMessageTest1()
{
runCommandLineTest(1, "-?");
assertEquals("Command output not as expected", messages.get("no_file_specified"),
errContent.toString().trim());
runCommandLineTest(0, "-?");
assertEquals("", errContent.toString().trim());
String expected = String.format(messages.get("help_text").replaceAll("[\\s]+", " "),
EpubCheck.version());
String actual = outContent.toString();
Expand All @@ -395,9 +394,8 @@ public void helpMessageTest1()
@Test
public void helpMessageTest2()
{
runCommandLineTest(1, "-help");
assertEquals("Command output not as expected", messages.get("no_file_specified"),
errContent.toString().trim());
runCommandLineTest(0, "-help");
assertEquals("", errContent.toString().trim());
String expected = String.format(messages.get("help_text").replaceAll("[\\s]+", " "),
EpubCheck.version());
String actual = outContent.toString();
Expand All @@ -412,9 +410,8 @@ public void helpMessageTest2()
@Test
public void helpMessageTest3()
{
runCommandLineTest(1, "--help");
assertEquals("Command output not as expected", messages.get("no_file_specified"),
errContent.toString().trim());
runCommandLineTest(0, "--help");
assertEquals("", errContent.toString().trim());
String expected = String.format(messages.get("help_text").replaceAll("[\\s]+", " "),
EpubCheck.version());
String actual = outContent.toString();
Expand All @@ -428,9 +425,8 @@ public void helpMessageTest3()
@Test
public void versionDisplayTest1()
{
runCommandLineTest(1, "--version");
assertEquals("Command output not as expected", messages.get("no_file_specified"),
errContent.toString().trim());
runCommandLineTest(0, "--version");
assertEquals("", errContent.toString().trim());
String expected = String.format(
messages.get("epubcheck_version_text").replaceAll("[\\s]+", " "), EpubCheck.version());
String actual = outContent.toString();
Expand All @@ -444,9 +440,8 @@ public void versionDisplayTest1()
@Test
public void versionDisplayTest2()
{
runCommandLineTest(1, "-version");
assertEquals("Command output not as expected", messages.get("no_file_specified"),
errContent.toString().trim());
runCommandLineTest(0, "-version");
assertEquals("", errContent.toString().trim());
String expected = String.format(
messages.get("epubcheck_version_text").replaceAll("[\\s]+", " "), EpubCheck.version());
String actual = outContent.toString();
Expand Down Expand Up @@ -689,7 +684,7 @@ public void failOnWarningsTest()
* document.
*
* @throws Exception
* Throws an exception if the temp file can't be created.
* Throws an exception if the temp file can't be created.
*/
@Test
public void jsonFileTest()
Expand All @@ -714,7 +709,7 @@ public void jsonFileTest()
* document.
*
* @throws Exception
* Any parsing errors will be thrown as an exception.
* Any parsing errors will be thrown as an exception.
*/
@Test
public void xmlFileTest()
Expand All @@ -740,7 +735,7 @@ public void xmlFileTest()
* document.
*
* @throws Exception
* Any parsing errors will be thrown as an exception.
* Any parsing errors will be thrown as an exception.
*/
@Test
public void xmpFileTest()
Expand Down

0 comments on commit 49aacb2

Please sign in to comment.