Skip to content

Commit 6798f30

Browse files
roxspringslachiewicz
authored andcommitted
[MSHARED-297] - Minor code cleanup
1 parent f751e61 commit 6798f30

4 files changed

Lines changed: 14 additions & 32 deletions

File tree

src/main/java/org/apache/maven/shared/utils/cli/shell/BourneShell.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,8 @@ protected String getExecutionPreamble()
105105
}
106106

107107
String dir = getWorkingDirectoryAsString();
108-
StringBuilder sb = new StringBuilder();
109-
sb.append( "cd " );
110108

111-
sb.append( quoteOneItem( dir, false ) );
112-
sb.append( " && " );
113-
114-
return sb.toString();
109+
return "cd " + quoteOneItem( dir, false ) + " && ";
115110
}
116111

117112
/**
@@ -138,10 +133,6 @@ protected String quoteOneItem( String path, boolean isExecutable )
138133
return null;
139134
}
140135

141-
StringBuilder sb = new StringBuilder();
142-
sb.append( "'" );
143-
sb.append( path.replace( "'", "'\"'\"'" ) );
144-
sb.append( "'" );
145-
return sb.toString();
136+
return "'" + path.replace( "'", "'\"'\"'" ) + "'";
146137
}
147138
}

src/main/java/org/apache/maven/shared/utils/cli/shell/Shell.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ String[] getShellArgs()
110110
}
111111
else
112112
{
113-
return shellArgs.toArray( new String[shellArgs.size()] );
113+
return shellArgs.toArray( new String[0] );
114114
}
115115
}
116116

@@ -146,7 +146,7 @@ List<String> getCommandLine( String executableParameter, String... argumentsPara
146146
*/
147147
List<String> getRawCommandLine( String executableParameter, String... argumentsParameter )
148148
{
149-
List<String> commandLine = new ArrayList<String>();
149+
List<String> commandLine = new ArrayList<>();
150150
StringBuilder sb = new StringBuilder();
151151

152152
if ( executableParameter != null )
@@ -280,7 +280,7 @@ char getExecutableQuoteDelimiter()
280280
public List<String> getShellCommandLine( String... arguments )
281281
{
282282

283-
List<String> commandLine = new ArrayList<String>();
283+
List<String> commandLine = new ArrayList<>();
284284

285285
if ( getShellCommand() != null )
286286
{

src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ public void givenAnEscapedDoubleQuoteMarkInArgument_whenTranslatingToCmdLineArgs
148148
public void givenAnEscapedSingleQuoteMarkInArgument_whenTranslatingToCmdLineArgs_thenTheQuotationMarkRemainsEscaped()
149149
throws Exception
150150
{
151-
final String command = "echo \"let\\\'s go\"";
152-
final String[] expected = new String[] { "echo", "let\\\'s go" };
151+
final String command = "echo \"let\\'s go\"";
152+
final String[] expected = new String[] { "echo", "let\\'s go"};
153153
assertCmdLineArgs( expected, command );
154154
}
155155

@@ -168,5 +168,4 @@ private void assertCmdLineArgs( final String[] expected, final String cmdLine )
168168
assertEquals( expected.length, actual.length );
169169
assertEquals( Arrays.asList( expected ), Arrays.asList( actual ) );
170170
}
171-
172171
}

src/test/java/org/apache/maven/shared/utils/cli/shell/BourneShellTest.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void testQuoteWorkingDirectoryAndExecutable()
4242
sh.setWorkingDirectory( "/usr/local/bin" );
4343
sh.setExecutable( "chmod" );
4444

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

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

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

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

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

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

81-
final String[] args = { "\"some arg with spaces\"" };
82-
83-
List<String> shellCommandLine = sh.getShellCommandLine( args );
81+
List<String> shellCommandLine = sh.getShellCommandLine("\"some arg with spaces\"");
8482

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

97-
String[] args = { "some arg with spaces" };
98-
99-
List<String> shellCommandLine = sh.getShellCommandLine( args );
95+
List<String> shellCommandLine = sh.getShellCommandLine("some arg with spaces");
10096

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

113-
String[] args = { "arg'withquote" };
114-
115-
List<String> shellCommandLine = sh.getShellCommandLine( args );
109+
List<String> shellCommandLine = sh.getShellCommandLine("arg'withquote");
116110

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

130-
String[] args = { ";some&argwithunix$chars" };
131-
132-
List<String> shellCommandLine = sh.getShellCommandLine( args );
124+
List<String> shellCommandLine = sh.getShellCommandLine(";some&argwithunix$chars");
133125

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

0 commit comments

Comments
 (0)