This is an automated email from the ASF dual-hosted git repository. khmarbaise pushed a commit to branch MSHARED-749 in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git
commit 43210ae850b9c2d9426fc850aa9b9ad71d543294 Author: Kathryn <[email protected]> AuthorDate: Fri Jul 27 09:38:54 2018 +0100 [MSHARED-749] - Commandline does not thrown CommandLineException when uneven number of quotation marks used --- .../org/apache/maven/shared/utils/cli/Arg.java | 2 +- .../apache/maven/shared/utils/cli/Commandline.java | 19 +++++----------- .../shared/utils/cli/CommandLineUtilsTest.java | 26 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/utils/cli/Arg.java b/src/main/java/org/apache/maven/shared/utils/cli/Arg.java index bd1fa08..e0eccf3 100644 --- a/src/main/java/org/apache/maven/shared/utils/cli/Arg.java +++ b/src/main/java/org/apache/maven/shared/utils/cli/Arg.java @@ -34,7 +34,7 @@ public interface Arg /** * @param line The line of arguments. */ - void setLine( String line ); + void setLine( String line ) throws CommandLineException; /** * @param value The file to be set. diff --git a/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java b/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java index 94fbdcc..db13770 100644 --- a/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java +++ b/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java @@ -92,19 +92,11 @@ public class Commandline * * @param toProcess The command to process */ - public Commandline( String toProcess ) + public Commandline( String toProcess ) throws CommandLineException { setDefaultShell(); - String[] tmp = new String[0]; - try - { - tmp = CommandLineUtils.translateCommandline( toProcess ); - } - catch ( Exception e ) - { - System.err.println( "Error translating Commandline." ); - } - if ( ( tmp != null ) && ( tmp.length > 0 ) ) + String[] tmp = CommandLineUtils.translateCommandline( toProcess ); + if ( ( tmp.length > 0 ) ) { setExecutable( tmp[0] ); for ( int i = 1; i < tmp.length; i++ ) @@ -484,7 +476,7 @@ public class Commandline /** * {@inheritDoc} */ - public void setLine( String line ) + public void setLine( String line ) throws CommandLineException { if ( line == null ) { @@ -494,9 +486,10 @@ public class Commandline { parts = CommandLineUtils.translateCommandline( line ); } - catch ( Exception e ) + catch ( CommandLineException e ) { System.err.println( "Error translating Commandline." ); + throw( e ); } } diff --git a/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java b/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java index d64fdd8..5b5ec2a 100644 --- a/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java +++ b/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java @@ -19,6 +19,7 @@ package org.apache.maven.shared.utils.cli; * under the License. */ +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -102,6 +103,31 @@ public class CommandLineUtilsTest @Test + public void givenASingleQuoteMarkInArgument_whenExecutingCode_thenNoExceptionIsThrown() throws Exception { + new Commandline("echo \"let's go\"").execute(); + } + + @Test + public void givenADoubleQuoteMarkInArgument_whenExecutingCode_thenCommandLineExceptionIsThrown() throws Exception { + try { + new Commandline("echo \"let\"s go\"").execute(); + } catch (CommandLineException e) { + assertTrue(true); + return; + } + fail("Exception was not thrown when given invalid (3 unescaped double quote) input"); + } + + + @Test + public void givenASingleQuoteMarkInArgument_whenExecutingCode_thenExitCode0Returned() throws Exception { + final Process p = new Commandline("echo \"let's go\"").execute(); + // Note, this sleep should be removed when java version reaches Java 8 + Thread.sleep(1000); + assertEquals(0, p.exitValue()); + } + + @Test public void givenASingleQuoteMarkInArgument_whenTranslatingToCmdLineArgs_thenTheQuotationMarkIsNotEscaped() throws Exception { final String command = "echo \"let's go\"";
