Skip to content

Commit

Permalink
#28: Allow clearing of environment
Browse files Browse the repository at this point in the history
  • Loading branch information
fleipold committed Jan 23, 2022
1 parent c319def commit bc9cc0b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ assertTrue(result.getOutputString().contains("var2=val 2\n"));
assertEquals("bash -c env", result.getCommandLine());
~~~

The environment can be cleared of values inherited from the parent process:

~~~ .java
Map<String, String> envVariables = new HashMap<>();
envVariables.put("var1", "val 1");
envVariables.put("var2", "val 2");
ProcResult result = new ProcBuilder("bash")
.withArgs("-c", "env")
.clearEnvironment()
.withVars(envVariables).run();

String[] outputLines = result.getOutputString().split("\n");
assertEquals("var1=val 1", outputLines[0]);
assertEquals("var2=val 2", outputLines[1]);
// Note: environment is not going to be completely empty, as there are some variables that every process needs
// thus we only assert on the first two lines.

assertEquals("bash -c env", result.getCommandLine());
~~~

By default the new program is spawned in the working directory of
the parent process. This can be overidden:

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/buildobjects/process/Proc.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Proc implements EventSink {
public Proc(String command,
List<String> args,
Map<String, String> env,
boolean clearEnvironment,
InputStream stdin,
Object stdout,
File directory,
Expand All @@ -54,6 +55,10 @@ public Proc(String command,
ProcessBuilder builder = new ProcessBuilder(cmdArray)
.directory(directory);

if (clearEnvironment) {
builder.environment().clear();
}

builder.environment().putAll(env);
process = builder.start();

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/buildobjects/process/ProcBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ProcBuilder {

private StreamConsumer outputConsumer;
private StreamConsumer errorConsumer;
private boolean clearEnvironment;


/** Creates a new ProcBuilder
Expand Down Expand Up @@ -219,7 +220,7 @@ public ProcResult run() throws StartupException, TimeoutException, ExternalProce
}

try {
Proc proc = new Proc(command, args, env, stdin, outputConsumer != null ? outputConsumer : stdout , directory, timoutMillis, errorConsumer != null ? errorConsumer : stderr);
Proc proc = new Proc(command, args, env, clearEnvironment, stdin, outputConsumer != null ? outputConsumer : stdout , directory, timoutMillis, errorConsumer != null ? errorConsumer : stderr);

final ByteArrayOutputStream output = defaultStdout == stdout && outputConsumer == null ? defaultStdout : null;

Expand Down Expand Up @@ -267,6 +268,12 @@ public static String filter(String input, String cmd, String... args) {
return builder.run().getOutputString();
}

/** Clears the environment before setting new variables. */
public ProcBuilder clearEnvironment() {
this.clearEnvironment = true;
return this;
}

/**
* Add a variable to the process's environment
*
Expand Down Expand Up @@ -353,4 +360,5 @@ public String getProcString() {
public String getCommandLine() {
return Proc.formatCommandLine(command, args);
}

}
24 changes: 23 additions & 1 deletion src/test/java/org/buildobjects/process/ProcBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* <dependency>
* <groupId>org.buildobjects</groupId>
* <artifactId>jproc</artifactId>
* <version>2.2.0</version>
* <version>2.7.0</version>
* </dependency>
* ~~~
*/
Expand Down Expand Up @@ -167,6 +167,28 @@ public void testPassingInMultipleVariables() {
assertEquals("bash -c env", result.getCommandLine());
}

/**
* The environment can be cleared of values inherited from the parent process:
*/
@Test
public void testClearEnvironment() {
Map<String, String> envVariables = new HashMap<>();
envVariables.put("var1", "val 1");
envVariables.put("var2", "val 2");
ProcResult result = new ProcBuilder("bash")
.withArgs("-c", "env")
.clearEnvironment()
.withVars(envVariables).run();

String[] outputLines = result.getOutputString().split("\n");
assertEquals("var1=val 1", outputLines[0]);
assertEquals("var2=val 2", outputLines[1]);
// Note: environment is not going to be completely empty, as there are some variables that every process needs
// thus we only assert on the first two lines.

assertEquals("bash -c env", result.getCommandLine());
}


/**
* By default the new program is spawned in the working directory of
Expand Down

0 comments on commit bc9cc0b

Please sign in to comment.