Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIXED JENKINS-44263] - Get rid of explict cached value access.
In EnvInject 3.1 I was doing refactoring for better Pipeline Support, and I have somehow removed cache calculation in the Build lazy loading chain. It caused regression (missing data) in EnvInjectPluginAction#getEnvInjectVarList(), which was always reading the cache value instead of invoking the Getter method.
- Loading branch information
1 parent
5066aee
commit 994a02b
Showing
2 changed files
with
67 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/test/java/org/jenkinsci/plugins/envinject/EnvInjectVarListTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.jenkinsci.plugins.envinject; | ||
|
||
import hudson.model.FreeStyleBuild; | ||
import hudson.model.FreeStyleProject; | ||
import hudson.model.Run; | ||
import java.util.Map; | ||
import javax.annotation.Nonnull; | ||
import org.junit.Assert; | ||
import static org.junit.Assert.assertEquals; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.SingleFileSCM; | ||
|
||
/** | ||
* Tests for {@link EnvInjectVarList}. | ||
* @author Oleg Nenashev | ||
*/ | ||
public class EnvInjectVarListTest { | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Test | ||
@Issue("JENKINS-44263") | ||
public void envVarsShouldBeCachedProperlyAfterReload() throws Exception { | ||
final FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "project"); | ||
p.getBuildersList().add(new EnvInjectBuilder(null, "TEXT_VAR=tvalue")); | ||
final FreeStyleBuild build = j.buildAndAssertSuccess(p); | ||
|
||
// Check vars before the reload | ||
{ | ||
EnvInjectVarList varList = getVarListOrFail(build); | ||
Map<String, String> envMap = varList.getEnvMap(); | ||
Assert.assertNotNull("EnvInject vars list is null for the run before reload", envMap); | ||
Assert.assertTrue("TEXT_VAR is not present in the list", envMap.containsKey("TEXT_VAR")); | ||
} | ||
|
||
// Reload and check vars | ||
// build.reload() does not assign parents for RunAction2, hence we apply a workaround | ||
p.doReload(); | ||
final Run<?, ?> reloadedBuild = p.getBuildByNumber(build.getNumber()); | ||
{ | ||
EnvInjectVarList varList = getVarListOrFail(reloadedBuild); | ||
Map<String, String> envMap = varList.getEnvMap(); | ||
Assert.assertNotNull("EnvInject vars list is null for the run after the reload", envMap); | ||
Assert.assertTrue("TEXT_VAR is not present in the list", envMap.containsKey("TEXT_VAR")); | ||
Assert.assertEquals("TEXT_VAR has wrong value", "tvalue", envMap.get("TEXT_VAR")); | ||
} | ||
} | ||
|
||
@Nonnull | ||
private EnvInjectVarList getVarListOrFail(@Nonnull Run<?, ?> run) throws AssertionError { | ||
EnvInjectPluginAction action = run.getAction(EnvInjectPluginAction.class); | ||
Assert.assertNotNull("EnvInject action is not set for the run " + run, action); | ||
EnvInjectVarList list = (EnvInjectVarList)action.getTarget(); | ||
Assert.assertNotNull("Unexpected state. EnvInject Var List is nul for the run " + run, list); | ||
return list; | ||
} | ||
} |