Skip to content

Commit a49305f

Browse files
axel3rdmichael-o
authored andcommitted
Issue #17: Binary PATH execution regression on Windows (since 3.0.15)
This fixes #17
1 parent dd1c85f commit a49305f

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

src/main/java/org/codehaus/plexus/util/cli/Commandline.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,18 +487,24 @@ public String[] getEnvironmentVariables()
487487
for ( Object o : envVars.keySet() )
488488
{
489489
String name = (String) o;
490-
String value = (String) envVars.get( name );
490+
String value = envVars.get( name );
491491
environmentVars[i] = name + "=" + value;
492492
i++;
493493
}
494494
return environmentVars;
495495
}
496496

497497
/**
498-
* Returns the executable and all defined arguments.
498+
* Returns the executable and all defined arguments.<br/>
499+
* For Windows Family, {@link Commandline#getShellCommandline()} is returned
499500
*/
500501
public String[] getCommandline()
501502
{
503+
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
504+
{
505+
return getShellCommandline();
506+
}
507+
502508
final String[] args = getArguments();
503509
String executable = getLiteralExecutable();
504510

src/test/java/org/codehaus/plexus/util/DirectoryScannerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ public void testFollowSymlinks()
196196
assertEquals( 5, includedDirs.size() );
197197
}
198198

199-
200199
private void createTestDirectories()
201200
throws IOException
202201
{

src/test/java/org/codehaus/plexus/util/cli/CommandlineTest.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,29 @@ public void testCommandlineWithCommandInConstructor()
8888
}
8989
}
9090

91+
public void testExecuteBinaryOnPath()
92+
{
93+
try
94+
{
95+
// Maven startup script on PATH is required for this test
96+
Commandline cmd = new Commandline();
97+
cmd.setWorkingDirectory( baseDir );
98+
cmd.setExecutable( "mvn" );
99+
assertEquals( "mvn", cmd.getShell().getOriginalExecutable() );
100+
cmd.createArg().setValue( "-version" );
101+
Process process = cmd.execute();
102+
String out = IOUtil.toString( process.getInputStream() );
103+
assertTrue( out.contains( "Apache Maven" ) );
104+
assertTrue( out.contains( "Maven home:" ) );
105+
assertTrue( out.contains( "Java version:" ) );
106+
assertTrue( out.contains( "Java home:" ) );
107+
}
108+
catch ( Exception e )
109+
{
110+
fail( "Maven startup script seems not on the PATH: " + e.getMessage() );
111+
}
112+
}
113+
91114
public void testExecute()
92115
{
93116
try
@@ -99,21 +122,8 @@ public void testExecute()
99122
assertEquals( "echo", cmd.getShell().getOriginalExecutable() );
100123
cmd.createArgument().setValue( "Hello" );
101124

102-
StringWriter swriter = new StringWriter();
103125
Process process = cmd.execute();
104-
105-
Reader reader = new InputStreamReader( process.getInputStream() );
106-
107-
char[] chars = new char[16];
108-
int read = -1;
109-
while ( ( read = reader.read( chars ) ) > -1 )
110-
{
111-
swriter.write( chars, 0, read );
112-
}
113-
114-
String output = swriter.toString().trim();
115-
116-
assertEquals( "Hello", output );
126+
assertEquals( "Hello", IOUtil.toString( process.getInputStream() ).trim() );
117127
}
118128
catch ( Exception e )
119129
{
@@ -248,7 +258,7 @@ public void testGetShellCommandLineBash()
248258
String expectedShellCmd = "'/bin/echo' 'hello world'";
249259
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
250260
{
251-
expectedShellCmd = "\\bin\\echo \'hello world\'";
261+
expectedShellCmd = "'\\bin\\echo' \'hello world\'";
252262
}
253263
assertEquals( expectedShellCmd, shellCommandline[2] );
254264
}
@@ -307,7 +317,7 @@ public void testGetShellCommandLineBash_WithSingleQuotedArg()
307317
String expectedShellCmd = "'/bin/echo' ''\"'\"'hello world'\"'\"''";
308318
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
309319
{
310-
expectedShellCmd = "\\bin\\echo \'hello world\'";
320+
expectedShellCmd = expectedShellCmd.replace( "/bin/echo", "\\bin\\echo" );
311321
}
312322
assertEquals( expectedShellCmd, shellCommandline[2] );
313323
}
@@ -330,7 +340,7 @@ public void testGetShellCommandLineNonWindows()
330340

331341
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
332342
{
333-
assertEquals( "\\usr\\bin a b", shellCommandline[2] );
343+
assertEquals( "'\\usr\\bin' 'a' 'b'", shellCommandline[2] );
334344
}
335345
else
336346
{

0 commit comments

Comments
 (0)