Skip to content

Commit

Permalink
[MSHARED-297] - Minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
roxspring authored and slachiewicz committed Jun 22, 2020
1 parent f751e61 commit 6798f30
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,8 @@ protected String getExecutionPreamble()
}

String dir = getWorkingDirectoryAsString();
StringBuilder sb = new StringBuilder();
sb.append( "cd " );

sb.append( quoteOneItem( dir, false ) );
sb.append( " && " );

return sb.toString();
return "cd " + quoteOneItem( dir, false ) + " && ";
}

/**
Expand All @@ -138,10 +133,6 @@ protected String quoteOneItem( String path, boolean isExecutable )
return null;
}

StringBuilder sb = new StringBuilder();
sb.append( "'" );
sb.append( path.replace( "'", "'\"'\"'" ) );
sb.append( "'" );
return sb.toString();
return "'" + path.replace( "'", "'\"'\"'" ) + "'";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ String[] getShellArgs()
}
else
{
return shellArgs.toArray( new String[shellArgs.size()] );
return shellArgs.toArray( new String[0] );
}
}

Expand Down Expand Up @@ -146,7 +146,7 @@ List<String> getCommandLine( String executableParameter, String... argumentsPara
*/
List<String> getRawCommandLine( String executableParameter, String... argumentsParameter )
{
List<String> commandLine = new ArrayList<String>();
List<String> commandLine = new ArrayList<>();
StringBuilder sb = new StringBuilder();

if ( executableParameter != null )
Expand Down Expand Up @@ -280,7 +280,7 @@ char getExecutableQuoteDelimiter()
public List<String> getShellCommandLine( String... arguments )
{

List<String> commandLine = new ArrayList<String>();
List<String> commandLine = new ArrayList<>();

if ( getShellCommand() != null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public void givenAnEscapedDoubleQuoteMarkInArgument_whenTranslatingToCmdLineArgs
public void givenAnEscapedSingleQuoteMarkInArgument_whenTranslatingToCmdLineArgs_thenTheQuotationMarkRemainsEscaped()
throws Exception
{
final String command = "echo \"let\\\'s go\"";
final String[] expected = new String[] { "echo", "let\\\'s go" };
final String command = "echo \"let\\'s go\"";
final String[] expected = new String[] { "echo", "let\\'s go"};
assertCmdLineArgs( expected, command );
}

Expand All @@ -168,5 +168,4 @@ private void assertCmdLineArgs( final String[] expected, final String cmdLine )
assertEquals( expected.length, actual.length );
assertEquals( Arrays.asList( expected ), Arrays.asList( actual ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void testQuoteWorkingDirectoryAndExecutable()
sh.setWorkingDirectory( "/usr/local/bin" );
sh.setExecutable( "chmod" );

String executable = StringUtils.join( sh.getShellCommandLine( new String[]{} ).iterator(), " " );
String executable = StringUtils.join( sh.getShellCommandLine().iterator(), " " );

assertEquals( "/bin/sh -c cd '/usr/local/bin' && 'chmod'", executable );
}
Expand All @@ -54,7 +54,7 @@ public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes()
sh.setWorkingDirectory( "/usr/local/'something else'" );
sh.setExecutable( "chmod" );

String executable = StringUtils.join( sh.getShellCommandLine( new String[]{} ).iterator(), " " );
String executable = StringUtils.join( sh.getShellCommandLine().iterator(), " " );

assertEquals( "/bin/sh -c cd '/usr/local/'\"'\"'something else'\"'\"'' && 'chmod'", executable );
}
Expand All @@ -66,7 +66,7 @@ public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes_Backsl
sh.setWorkingDirectory( "\\usr\\local\\'something else'" );
sh.setExecutable( "chmod" );

String executable = StringUtils.join( sh.getShellCommandLine( new String[]{} ).iterator(), " " );
String executable = StringUtils.join( sh.getShellCommandLine().iterator(), " " );

assertEquals( "/bin/sh -c cd '\\usr\\local\\'\"'\"'something else'\"'\"'' && 'chmod'", executable );
}
Expand All @@ -78,9 +78,7 @@ public void testPreserveSingleQuotesOnArgument()
sh.setWorkingDirectory( "/usr/bin" );
sh.setExecutable( "chmod" );

final String[] args = { "\"some arg with spaces\"" };

List<String> shellCommandLine = sh.getShellCommandLine( args );
List<String> shellCommandLine = sh.getShellCommandLine("\"some arg with spaces\"");

String cli = StringUtils.join( shellCommandLine.iterator(), " " );
System.out.println( cli );
Expand All @@ -94,9 +92,7 @@ public void testAddSingleQuotesOnArgumentWithSpaces()
sh.setWorkingDirectory( "/usr/bin" );
sh.setExecutable( "chmod" );

String[] args = { "some arg with spaces" };

List<String> shellCommandLine = sh.getShellCommandLine( args );
List<String> shellCommandLine = sh.getShellCommandLine("some arg with spaces");

String cli = StringUtils.join( shellCommandLine.iterator(), " " );
System.out.println( cli );
Expand All @@ -110,9 +106,7 @@ public void testAddArgumentWithSingleQuote()
sh.setWorkingDirectory( "/usr/bin" );
sh.setExecutable( "chmod" );

String[] args = { "arg'withquote" };

List<String> shellCommandLine = sh.getShellCommandLine( args );
List<String> shellCommandLine = sh.getShellCommandLine("arg'withquote");

assertEquals("cd '/usr/bin' && 'chmod' 'arg'\"'\"'withquote'", shellCommandLine.get(shellCommandLine.size() - 1));
}
Expand All @@ -127,9 +121,7 @@ public void testArgumentsWithSemicolon()
sh.setWorkingDirectory( "/usr/bin" );
sh.setExecutable( "chmod" );

String[] args = { ";some&argwithunix$chars" };

List<String> shellCommandLine = sh.getShellCommandLine( args );
List<String> shellCommandLine = sh.getShellCommandLine(";some&argwithunix$chars");

String cli = StringUtils.join( shellCommandLine.iterator(), " " );
System.out.println( cli );
Expand Down

0 comments on commit 6798f30

Please sign in to comment.