Skip to content
Open
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
29 changes: 21 additions & 8 deletions src/main/java/org/jenkinsci/plugins/workflow/steps/EnvStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@

@DataBoundConstructor
public EnvStep(List<String> overrides) {
for (String pair : overrides) {
if (pair.indexOf('=') == -1) {
throw new IllegalArgumentException(pair);
if (overrides == null) {
this.overrides = Collections.emptyList();
} else {
List<String> stored = new ArrayList<>();
for (String pair : overrides) {
if (pair == null || pair.indexOf('=') == -1) {

Check warning on line 57 in src/main/java/org/jenkinsci/plugins/workflow/steps/EnvStep.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 57 is only partially covered, one branch is missing
throw new IllegalArgumentException(String.valueOf(pair));
}
// store a trimmed form to normalize whitespace (keys/values are trimmed later too)
stored.add(pair.trim());
}
this.overrides = stored;
}
this.overrides = new ArrayList<>(overrides);
}

public List<String> getOverrides() {
Expand All @@ -75,16 +82,22 @@

Execution(List<String> overrides, StepContext context) {
super(context);
this.overrides = overrides;
this.overrides = overrides == null ? Collections.emptyList() : overrides;

Check warning on line 85 in src/main/java/org/jenkinsci/plugins/workflow/steps/EnvStep.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 85 is only partially covered, one branch is missing
}

@Override
public boolean start() throws Exception {
Map<String, String> overridesM = new HashMap<>();
for (String pair : overrides) {
int split = pair.indexOf('=');
assert split != -1;
overridesM.put(pair.substring(0, split), pair.substring(split + 1));
if (split == -1) {

Check warning on line 93 in src/main/java/org/jenkinsci/plugins/workflow/steps/EnvStep.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 93 is only partially covered, one branch is missing
// defensive: in case an instance is deserialized from an older form
throw new IllegalStateException("Invalid environment override: " + pair);

Check warning on line 95 in src/main/java/org/jenkinsci/plugins/workflow/steps/EnvStep.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 95 is not covered by tests
}
// trim both key and value to be tolerant of user input like "FOO = bar "
String key = pair.substring(0, split).trim();
String value = pair.substring(split + 1).trim();
overridesM.put(key, value);
}
getContext()
.newBodyInvoker()
Expand Down Expand Up @@ -135,7 +148,7 @@
public Step newInstance(StaplerRequest2 req, JSONObject formData) throws FormException {
String overridesS = formData.getString("overrides");
List<String> overrides = new ArrayList<>();
for (String line : overridesS.split("\r?\n")) {
for (String line : overridesS.split("\\r?\\n")) {
line = line.trim();
if (!line.isEmpty()) {
overrides.add(line);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jenkinsci.plugins.workflow.steps;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import org.junit.jupiter.api.Test;

public class EnvStepUnitTest {

@Test
void constructorAcceptsNull() {
EnvStep s = new EnvStep(null);
assertTrue(s.getOverrides().isEmpty());
}

@Test
void constructorRejectsMalformedEntry() {
assertThrows(IllegalArgumentException.class, () -> new EnvStep(Arrays.asList("BADPAIR")));
}

@Test
void constructorTrimsStoredPair() {
EnvStep s = new EnvStep(Arrays.asList("FOO = bar "));
assertEquals(1, s.getOverrides().size());
assertEquals("FOO = bar", s.getOverrides().get(0));
}
}
Loading