GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [MSHARED-1014] Optionally inherit system environment variables by Commandline by slawekjaranowski · Pull Request #94 · apache/maven-shared-utils

[MSHARED-1014] Optionally inherit system environment variables by Commandline #94

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.

Sign up for GitHub

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

Merged
slawekjaranowski merged 1 commit into master from MSHARED-1014
Jan 27, 2022

Uh oh!

There was an error while loading. Please reload this page.

Merged

[MSHARED-1014] Optionally inherit system environment variables by Commandline #94

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
e65b7d7
[MSHARED-1014] Optionally inherit system environment variables by Com…
slawekjaranowski Jan 3, 2022
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

38 changes: 33 additions & 5 deletions 38 src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@
public class Commandline
implements Cloneable
{
private final List<Arg> arguments = new Vector<Arg>();
private final List<Arg> arguments = new Vector<>();
Comment thread
slawekjaranowski marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


private final Map<String, String> envVars = Collections.synchronizedMap( new LinkedHashMap<String, String>() );

private Shell shell;

private boolean shellEnvironmentInherited = true;

/**
* Create a new command line object.
* Shell is autodetected from operating system.
Expand Down Expand Up @@ -198,14 +200,13 @@ public void addArguments( String... line )
*/
public void addEnvironment( String name, String value )
{
//envVars.add( name + "=" + value );
envVars.put( name, value );
}

/**
* Add system environment variables.
*/
public void addSystemEnvironment()
private void addSystemEnvironment()
Comment thread
slawekjaranowski marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

{
Properties systemEnvVars = CommandLineUtils.getSystemEnvVars();

Expand All @@ -226,7 +227,11 @@ public void addSystemEnvironment()
*/
public String[] getEnvironmentVariables()
{
addSystemEnvironment();
if ( isShellEnvironmentInherited() )
{
addSystemEnvironment();
}

List<String> environmentVars = new ArrayList<>();
for ( String name : envVars.keySet() )
{
Expand Down Expand Up @@ -297,7 +302,7 @@ public String[] getArguments()
*/
public String[] getArguments( boolean mask )
{
List<String> result = new ArrayList<String>( arguments.size() * 2 );
List<String> result = new ArrayList<>( arguments.size() * 2 );
for ( Arg argument : arguments )
{
Argument arg = (Argument) argument;
Expand Down Expand Up @@ -377,6 +382,29 @@ public void clearArgs()
arguments.clear();
}

/**
* Indicates whether the environment variables of the current process
* should are propagated to the executing Command.
* By default, the current environment variables are inherited by the new Command line execution.
*
* @return <code>true</code> if the environment variables should be propagated, <code>false</code> otherwise.
*/
public boolean isShellEnvironmentInherited()
{
return shellEnvironmentInherited;
}

/**
* Specifies whether the environment variables of the current process should be propagated to the executing Command.
*
* @param shellEnvironmentInherited <code>true</code> if the environment variables should be propagated,
* <code>false</code> otherwise.
*/
public void setShellEnvironmentInherited( boolean shellEnvironmentInherited )
{
this.shellEnvironmentInherited = shellEnvironmentInherited;
}

/**
* Execute the command.
*
Expand Down
34 changes: 20 additions & 14 deletions 34 src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ public void testGetSystemEnvVarsCaseInsensitive()

@Test
public void testEnsureCaseSensitivity()
throws Exception
{
Map<String, String> data = new HashMap<String, String>();
Map<String, String> data = new HashMap<>();
data.put( "abz", "cool" );
assertTrue( CommandLineUtils.ensureCaseSensitivity( data, false ).containsKey( "ABZ" ) );
assertTrue( CommandLineUtils.ensureCaseSensitivity( data, true ).containsKey( "abz" ) );
Expand All @@ -69,7 +68,6 @@ public void testEnsureCaseSensitivity()
*/
@Test
public void testGetSystemEnvVarsWindows()
throws Exception
{
if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
{
Expand Down Expand Up @@ -103,14 +101,9 @@ public void testTranslateCommandline()
assertCmdLineArgs( new String[] { "foo", " ' ", "bar" }, "foo \" ' \" bar" );
}


@Test
public void givenASingleQuoteMarkInArgument_whenExecutingCode_thenNoExceptionIsThrown() throws Exception {
new Commandline("echo \"let's go\"").execute();
}

@Test
public void givenADoubleQuoteMarkInArgument_whenExecutingCode_thenCommandLineExceptionIsThrown() throws Exception {
public void givenADoubleQuoteMarkInArgument_whenExecutingCode_thenCommandLineExceptionIsThrown()
{
try {
new Commandline("echo \"let\"s go\"").execute();
} catch (CommandLineException e) {
Expand All @@ -124,8 +117,7 @@ public void givenADoubleQuoteMarkInArgument_whenExecutingCode_thenCommandLineExc
@Test
public void givenASingleQuoteMarkInArgument_whenExecutingCode_thenExitCode0Returned() throws Exception {
final Process p = new Commandline("echo \"let's go\"").execute();
// Note, this sleep should be removed when java version reaches Java 8
Thread.sleep(1000);
p.waitFor();
assertEquals(0, p.exitValue());
}

Expand Down Expand Up @@ -160,7 +152,9 @@ public void givenAnEscapedSingleQuoteMarkInArgument_whenTranslatingToCmdLineArgs
public void givenAnEscapedDoubleQuoteMarkInArgument_whenTranslatingToCmdLineArgs_thenNoExceptionIsThrown()
throws Exception
{
new Commandline( "echo \"let\\\"s go\"" ).execute();
Process p = new Commandline( "echo \"let\\\"s go\"" ).execute();
p.waitFor();
assertEquals(0, p.exitValue());
}

private void assertCmdLineArgs( final String[] expected, final String cmdLine )
Expand All @@ -185,7 +179,7 @@ public void environmentVariableWithNullShouldNotBeSet() {
}

@Test
public void environmentVariableFromSystemIsCopied() {
public void environmentVariableFromSystemIsCopiedByDefault() {

Commandline commandline = new Commandline();

Expand All @@ -195,6 +189,18 @@ public void environmentVariableFromSystemIsCopied() {
assertThat(environmentVariables, hasItemInArray( "TEST_SHARED_ENV=TestValue" ) );
}

@Test
public void environmentVariableFromSystemIsNotCopiedIfInheritedIsFalse() {

Commandline commandline = new Commandline();
commandline.setShellEnvironmentInherited( false );

String[] environmentVariables = commandline.getEnvironmentVariables();

assertNotNull(environmentVariables);
assertEquals(0, environmentVariables.length );
}

@Test
public void environmentVariableFromSystemIsRemoved() {

Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.