Skip to content

Back to Shell usage for Windows command line (+ minor Win UT fix) #20

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

Closed
wants to merge 1 commit into from
Closed
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: 14 additions & 8 deletions src/main/java/org/codehaus/plexus/util/cli/Commandline.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public class Commandline
* Create a new command line object.
* Shell is autodetected from operating system
*
* Shell usage is only desirable when generating code for remote execution.
* For Linux, shell usage is only desirable when generating code for remote execution.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not Linux, but Unix-like. There are other OSes too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michael-o : Good catch, you are right. Do you want I update the PR (there is 6 mentioned in file) ? (I'm not sure to understand, you have closed the PR).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have already merged a fixed version of the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, thanks

*
* @param toProcess
*/
Expand Down Expand Up @@ -170,7 +170,7 @@ public Commandline( String toProcess, Shell shell )
* Create a new command line object.
* Shell is autodetected from operating system
*
* Shell usage is only desirable when generating code for remote execution.
* For Linux, shell usage is only desirable when generating code for remote execution.
*/
public Commandline( Shell shell )
{
Expand Down Expand Up @@ -417,7 +417,7 @@ public String getLiteralExecutable()
/**
* Return an executable name, quoted for shell use.
*
* Shell usage is only desirable when generating code for remote execution.
* For Linux, shell usage is only desirable when generating code for remote execution.
*
* @return Executable to be run, quoted for shell interpretation
*/
Expand Down Expand Up @@ -487,18 +487,24 @@ public String[] getEnvironmentVariables()
for ( Object o : envVars.keySet() )
{
String name = (String) o;
String value = (String) envVars.get( name );
String value = envVars.get( name );
environmentVars[i] = name + "=" + value;
i++;
}
return environmentVars;
}

/**
* Returns the executable and all defined arguments.
* Returns the executable and all defined arguments.<br/>
* For Windows Family, {@link Commandline#getShellCommandline()} is returned
*/
public String[] getCommandline()
{
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
{
return getShellCommandline();
}

final String[] args = getArguments();
String executable = getLiteralExecutable();

Expand All @@ -515,7 +521,7 @@ public String[] getCommandline()
/**
* Returns the shell, executable and all defined arguments.
*
* Shell usage is only desirable when generating code for remote execution.
* For Linux, shell usage is only desirable when generating code for remote execution.
*/
public String[] getShellCommandline()
{
Expand Down Expand Up @@ -703,7 +709,7 @@ public Properties getSystemEnvVars()
/**
* Allows to set the shell to be used in this command line.
*
* Shell usage is only desirable when generating code for remote execution.
* For Linux, shell usage is only desirable when generating code for remote execution.
*
* @param shell
* @since 1.2
Expand All @@ -716,7 +722,7 @@ public void setShell( Shell shell )
/**
* Get the shell to be used in this command line.
*
* Shell usage is only desirable when generating code for remote execution.
* For Linux, shell usage is only desirable when generating code for remote execution.
* @since 1.2
*/
public Shell getShell()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ public void testFollowSymlinksFalse()

private void assertAlwaysIncluded( List<String> included )
{
assertTrue( included.contains( "aRegularDir/aRegularFile.txt" ) );
assertTrue( included.contains( "targetDir/targetFile.txt" ) );
assertTrue( included.contains( "aRegularDir" + File.separator + "aRegularFile.txt" ) );
assertTrue( included.contains( "targetDir" + File.separator + "targetFile.txt" ) );
assertTrue( included.contains( "fileR.txt" ) );
assertTrue( included.contains( "fileW.txt" ) );
assertTrue( included.contains( "fileX.txt" ) );
Expand All @@ -183,8 +183,8 @@ public void testFollowSymlinks()
ds.scan();
List<String> included = Arrays.asList( ds.getIncludedFiles() );
assertAlwaysIncluded( included );
assertTrue( included.contains( "symDir/targetFile.txt" ) );
assertTrue( included.contains( "symLinkToDirOnTheOutside/FileInDirOnTheOutside.txt" ) );
assertTrue( included.contains( "symDir" + File.separator + "targetFile.txt" ) );
assertTrue( included.contains( "symLinkToDirOnTheOutside" + File.separator + "FileInDirOnTheOutside.txt" ) );
assertEquals( 11, included.size() );

List<String> includedDirs = Arrays.asList( ds.getIncludedDirectories() );
Expand All @@ -196,7 +196,6 @@ public void testFollowSymlinks()
assertEquals( 5, includedDirs.size() );
}


private void createTestDirectories()
throws IOException
{
Expand Down
44 changes: 27 additions & 17 deletions src/test/java/org/codehaus/plexus/util/cli/CommandlineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@ public void testCommandlineWithCommandInConstructor()
}
}

public void testExecuteBinaryOnPath()
{
try
{
// Maven binary on PATH is required for this test
Commandline cmd = new Commandline();
cmd.setWorkingDirectory( baseDir );
cmd.setExecutable( "mvn" );
assertEquals( "mvn", cmd.getShell().getOriginalExecutable() );
cmd.createArg().setValue( "-version" );
Process process = cmd.execute();
String out = IOUtil.toString( process.getInputStream() );
assertTrue( out.contains( "Apache Maven" ) );
assertTrue( out.contains( "Maven home:" ) );
assertTrue( out.contains( "Java version:" ) );
assertTrue( out.contains( "Java home:" ) );
}
catch ( Exception e )
{
fail( "Maven binary seems not on the PATH: " + e.getMessage() );
}
}

public void testExecute()
{
try
Expand All @@ -99,21 +122,8 @@ public void testExecute()
assertEquals( "echo", cmd.getShell().getOriginalExecutable() );
cmd.createArgument().setValue( "Hello" );

StringWriter swriter = new StringWriter();
Process process = cmd.execute();

Reader reader = new InputStreamReader( process.getInputStream() );

char[] chars = new char[16];
int read = -1;
while ( ( read = reader.read( chars ) ) > -1 )
{
swriter.write( chars, 0, read );
}

String output = swriter.toString().trim();

assertEquals( "Hello", output );
assertEquals( "Hello", IOUtil.toString( process.getInputStream() ).trim() );
}
catch ( Exception e )
{
Expand Down Expand Up @@ -248,7 +258,7 @@ public void testGetShellCommandLineBash()
String expectedShellCmd = "'/bin/echo' 'hello world'";
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
{
expectedShellCmd = "\\bin\\echo \'hello world\'";
expectedShellCmd = "'\\bin\\echo' \'hello world\'";
}
assertEquals( expectedShellCmd, shellCommandline[2] );
}
Expand Down Expand Up @@ -307,7 +317,7 @@ public void testGetShellCommandLineBash_WithSingleQuotedArg()
String expectedShellCmd = "'/bin/echo' ''\"'\"'hello world'\"'\"''";
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
{
expectedShellCmd = "\\bin\\echo \'hello world\'";
expectedShellCmd = expectedShellCmd.replace( "/bin/echo", "\\bin\\echo" );
}
assertEquals( expectedShellCmd, shellCommandline[2] );
}
Expand All @@ -330,7 +340,7 @@ public void testGetShellCommandLineNonWindows()

if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
{
assertEquals( "\\usr\\bin a b", shellCommandline[2] );
assertEquals( "'\\usr\\bin' 'a' 'b'", shellCommandline[2] );
}
else
{
Expand Down