Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert usage of cmd on Windows #109

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/main/java/org/codehaus/plexus/util/cli/Commandline.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,34 +491,32 @@ public String[] getEnvironmentVariables()
}

/**
* Warning: For built-in commands like 'echo' on {@link Os#FAMILY_WINDOWS}, use <code>cmd /X /C echo</code>
* @deprecated Use {@link org.codehaus.plexus.util.cli.Commandline#getRawCommandline()} method instead.
* @return Returns the executable and all defined arguments.
* For Windows Family, {@link Commandline#getShellCommandline()} is returned
*/
@Deprecated
public String[] getCommandline()
{
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
{
return getShellCommandline();
}

return getRawCommandline();
}

/**
* Returns the executable and all defined arguments.
* Returns the executable and all defined arguments.<br>
* Warning: For built-in commands like 'echo' on {@link Os#FAMILY_WINDOWS}, use <code>cmd /X /C echo</code>
* @return the command line as array not escaped neither quoted
*/
public String[] getRawCommandline()
{
final String[] args = getArguments();
String executable = getLiteralExecutable();
String executableTmp = getLiteralExecutable();

if ( executable == null )
if ( executableTmp == null )
{
return args;
}
final String[] result = new String[args.length + 1];
result[0] = executable;
result[0] = executableTmp;
System.arraycopy( args, 0, result, 1, args.length );
return result;
}
Expand Down Expand Up @@ -568,7 +566,7 @@ public String toString()

public int size()
{
return getCommandline().length;
return getRawCommandline().length;
}

@Override
Expand Down Expand Up @@ -668,7 +666,7 @@ public Process execute()
{
if ( workingDir == null )
{
process = Runtime.getRuntime().exec( getCommandline(), environment, workingDir );
process = Runtime.getRuntime().exec( getRawCommandline(), environment, workingDir );
}
else
{
Expand Down
29 changes: 23 additions & 6 deletions src/test/java/org/codehaus/plexus/util/cli/CommandlineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ public void testExecuteBinaryOnPath()
throws Exception
{
// Maven startup script on PATH is required for this test
String binary = "mvn";
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
{
binary += ".cmd";
}
Commandline cmd = new Commandline();
cmd.setWorkingDirectory( baseDir );
cmd.setExecutable( "mvn" );
assertEquals( "mvn", cmd.getShell().getOriginalExecutable() );
cmd.setExecutable( binary );
assertEquals( binary, cmd.getShell().getOriginalExecutable() );
cmd.createArg().setValue( "-version" );
Process process = cmd.execute();
String out = IOUtil.toString( process.getInputStream() );
Expand All @@ -94,11 +99,20 @@ public void testExecute()
throws Exception
{
// allow it to detect the proper shell here.
String executable = "echo";
Commandline cmd = new Commandline();
cmd.setWorkingDirectory( baseDir );
cmd.setExecutable( "echo" );
assertEquals( "echo", cmd.getShell().getOriginalExecutable() );
cmd.createArgument().setValue( "Hello" );
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
{
cmd.createArg().setValue( "Hello" );
executable = "cmd";
cmd.createArg().setValue("/X");
cmd.createArg().setValue("/C");
cmd.createArg().setValue("echo");
}
cmd.setExecutable( executable );
assertEquals( executable, cmd.getShell().getOriginalExecutable() );
cmd.createArg().setValue( "Hello" );

Process process = cmd.execute();
assertEquals( "Hello", IOUtil.toString( process.getInputStream() ).trim() );
Expand Down Expand Up @@ -460,7 +474,10 @@ public void testDollarSignInArgumentPath()
cmd.setExecutable( "cat" );
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
{
cmd.setExecutable( "dir" );
cmd.setExecutable( "cmd" );
cmd.createArg().setLine( "/X" );
cmd.createArg().setLine( "/C" );
cmd.createArg().setLine( "dir" );
}
cmd.setWorkingDirectory( dir );
cmd.createArg().setLine( "test$1.txt" );
Expand Down