Skip to content

Commit

Permalink
handle empty and spaces a little differently
Browse files Browse the repository at this point in the history
After reviewing the help, the last sentence states:

```
If the Value is empty or whitespace-only, it will not be added to the environment, nor will it override or unset any environment variable with the same name that may already exist (e.g. a variable defined by the system).
```

This wasn't true prior to my changes nor any of the commits up to this point. I think this covers all the cases.
  • Loading branch information
darinpope committed Dec 4, 2024
1 parent 52fce38 commit d38fc0c
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,20 @@ private Entry(Map.Entry<String, String> e) {

@DataBoundConstructor
public Entry(String key, String value) {
this.key = Util.fixNull(Util.fixEmptyAndTrim(key));
this.value = Util.fixNull(Util.fixEmptyAndTrim(value));
this.key = Util.fixEmptyAndTrim(key);
this.value = Util.fixEmptyAndTrim(value);
}
}

private static EnvVars toMap(List<Entry> entries) {
EnvVars map = new EnvVars();
if (entries != null)
for (Entry entry : entries)
map.put(entry.key, entry.value);
if (entries != null) {
for (Entry entry : entries) {
if(entry.key != null && entry.value != null) {
map.put(entry.key, entry.value);
}
}
}
return map;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hudson.slaves;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
Expand Down Expand Up @@ -167,14 +168,14 @@ public void testEmptyForController() throws Exception {

Map<String, String> envVars = executeBuild(j.jenkins);

assertEquals("", envVars.get("KEY"));
assertNull(envVars.get("KEY"));
}

@Test
public void testEmptyForAgent() throws Exception {
setVariables(agent, new EnvironmentVariablesNodeProperty.Entry("KEY", ""));
Map<String, String> envVars = executeBuild(agent);
assertEquals("", envVars.get("KEY"));
assertNull(envVars.get("KEY"));
}

@Test
Expand All @@ -185,14 +186,142 @@ public void testOnlySpacesForController() throws Exception {

Map<String, String> envVars = executeBuild(j.jenkins);

assertEquals("", envVars.get("KEY"));
assertNull(envVars.get("KEY"));
}

@Test
public void testOnlySpacesForAgent() throws Exception {
setVariables(agent, new EnvironmentVariablesNodeProperty.Entry("KEY", " "));
Map<String, String> envVars = executeBuild(agent);
assertEquals("", envVars.get("KEY"));
assertNull(envVars.get("KEY"));
}

@Test
public void testFormRoundTripForControllerWithEmpty() throws Exception {
j.jenkins.getGlobalNodeProperties().replaceBy(
Set.of(new EnvironmentVariablesNodeProperty(
new EnvironmentVariablesNodeProperty.Entry("", ""))));

WebClient webClient = j.createWebClient();
HtmlPage page = webClient.getPage(j.jenkins, "configure");
HtmlForm form = page.getFormByName("config");
j.submit(form);

assertEquals(1, j.jenkins.getGlobalNodeProperties().toList().size());

EnvironmentVariablesNodeProperty prop = j.jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class);
assertEquals(0, prop.getEnvVars().size());
}

@Test
public void testFormRoundTripForAgentWithEmpty() throws Exception {
setVariables(agent, new EnvironmentVariablesNodeProperty.Entry("", ""));

WebClient webClient = j.createWebClient();
HtmlPage page = webClient.getPage(agent, "configure");
HtmlForm form = page.getFormByName("config");
j.submit(form);

assertEquals(1, agent.getNodeProperties().toList().size());

EnvironmentVariablesNodeProperty prop = agent.getNodeProperties().get(EnvironmentVariablesNodeProperty.class);
assertEquals(0, prop.getEnvVars().size());
}

@Test
public void testFormRoundTripForControllerWithSpaces() throws Exception {
j.jenkins.getGlobalNodeProperties().replaceBy(
Set.of(new EnvironmentVariablesNodeProperty(
new EnvironmentVariablesNodeProperty.Entry(" ", " "))));

WebClient webClient = j.createWebClient();
HtmlPage page = webClient.getPage(j.jenkins, "configure");
HtmlForm form = page.getFormByName("config");
j.submit(form);

assertEquals(1, j.jenkins.getGlobalNodeProperties().toList().size());

EnvironmentVariablesNodeProperty prop = j.jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class);
assertEquals(0, prop.getEnvVars().size());
}

@Test
public void testFormRoundTripForAgentWithSpaces() throws Exception {
setVariables(agent, new EnvironmentVariablesNodeProperty.Entry(" ", " "));

WebClient webClient = j.createWebClient();
HtmlPage page = webClient.getPage(agent, "configure");
HtmlForm form = page.getFormByName("config");
j.submit(form);

assertEquals(1, agent.getNodeProperties().toList().size());

EnvironmentVariablesNodeProperty prop = agent.getNodeProperties().get(EnvironmentVariablesNodeProperty.class);
assertEquals(0, prop.getEnvVars().size());
}

@Test
public void testFormRoundTripForControllerWithSpacesAndKey() throws Exception {
j.jenkins.getGlobalNodeProperties().replaceBy(
Set.of(new EnvironmentVariablesNodeProperty(
new EnvironmentVariablesNodeProperty.Entry("KEY", " "))));

WebClient webClient = j.createWebClient();
HtmlPage page = webClient.getPage(j.jenkins, "configure");
HtmlForm form = page.getFormByName("config");
j.submit(form);

assertEquals(1, j.jenkins.getGlobalNodeProperties().toList().size());

EnvironmentVariablesNodeProperty prop = j.jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class);
assertEquals(0, prop.getEnvVars().size());
}

@Test
public void testFormRoundTripForAgentWithSpacesAndKey() throws Exception {
setVariables(agent, new EnvironmentVariablesNodeProperty.Entry("KEY", " "));

WebClient webClient = j.createWebClient();
HtmlPage page = webClient.getPage(agent, "configure");
HtmlForm form = page.getFormByName("config");
j.submit(form);

assertEquals(1, agent.getNodeProperties().toList().size());

EnvironmentVariablesNodeProperty prop = agent.getNodeProperties().get(EnvironmentVariablesNodeProperty.class);
assertEquals(0, prop.getEnvVars().size());
}

@Test
public void testFormRoundTripForControllerWithEmptyAndKey() throws Exception {
j.jenkins.getGlobalNodeProperties().replaceBy(
Set.of(new EnvironmentVariablesNodeProperty(
new EnvironmentVariablesNodeProperty.Entry("KEY", ""))));

WebClient webClient = j.createWebClient();
HtmlPage page = webClient.getPage(j.jenkins, "configure");
HtmlForm form = page.getFormByName("config");
j.submit(form);

assertEquals(1, j.jenkins.getGlobalNodeProperties().toList().size());

EnvironmentVariablesNodeProperty prop = j.jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class);
assertEquals(0, prop.getEnvVars().size());
}

@Test
public void testFormRoundTripForAgentWithEmptyAndKey() throws Exception {
setVariables(agent, new EnvironmentVariablesNodeProperty.Entry("KEY", ""));

WebClient webClient = j.createWebClient();
HtmlPage page = webClient.getPage(agent, "configure");
HtmlForm form = page.getFormByName("config");
j.submit(form);

assertEquals(1, agent.getNodeProperties().toList().size());

EnvironmentVariablesNodeProperty prop = agent.getNodeProperties().get(EnvironmentVariablesNodeProperty.class);
assertEquals(0, prop.getEnvVars().size());
}

// //////////////////////// setup //////////////////////////////////////////
Expand Down

0 comments on commit d38fc0c

Please sign in to comment.