Skip to content
Permalink
Browse files

[JENKINS-26100] Change buildEnvVars to take Run (#55)

  • Loading branch information...
casz committed Jun 6, 2017
1 parent c6bf0c5 commit 5e3008ecc47a6f31930e81223f2b5699121f31cb
@@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.23</version>
<version>2.29</version>
<relativePath />
</parent>

@@ -42,6 +42,12 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.8.9</version>
<scope>test</scope>
</dependency>
</dependencies>

<scm>
@@ -318,10 +318,14 @@ public AccurevSCMDescriptor getDescriptor() {
* @param env enviroments
* @since 0.6.9
*/
// TODO: 2.60+ Delete this override.
@Override
public void buildEnvVars(AbstractBuild<?, ?> build, Map<String, String> env) {
// call super even though SCM.buildEnvVars currently does nothing - this could change
super.buildEnvVars(build, env);
buildEnvironment(build, env);
}

// TODO: 2.60+ - add @Override.
public void buildEnvironment(Run<?,?> build, Map<String, String> env) {
AbstractModeDelegate delegate = AccurevMode.findDelegate(this);
delegate.buildEnvVars(build, env);
}
@@ -3,7 +3,6 @@
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.TaskListener;
@@ -314,7 +313,8 @@ protected boolean populate(Run<?, ?> build) throws IOException, InterruptedExcep
return populate(build, isPopulateRequired());
}

public void buildEnvVars(AbstractBuild<?, ?> build, Map<String, String> env) {
// TODO: As part of rewrite I should get accurev transaction onto environment as part of this
public void buildEnvVars(Run<?, ?> build, Map<String, String> env) {
try {
setup(null, null, TaskListener.NULL);
} catch (IOException | InterruptedException ex) {
@@ -367,7 +367,7 @@ public void buildEnvVars(AbstractBuild<?, ?> build, Map<String, String> env) {
buildEnvVarsCustom(build, env);
}

protected void buildEnvVarsCustom(AbstractBuild<?, ?> build, Map<String, String> env) {
protected void buildEnvVarsCustom(Run<?, ?> build, Map<String, String> env) {
// override to put implementation specific values
}

@@ -1,6 +1,5 @@
package hudson.plugins.accurev.delegates;

import hudson.model.AbstractBuild;
import hudson.model.Job;
import hudson.model.Run;
import hudson.plugins.accurev.*;
@@ -183,7 +182,7 @@ protected boolean validateCheckout(Run<?, ?> build) {
}

@Override
protected void buildEnvVarsCustom(AbstractBuild<?, ?> build, Map<String, String> env) {
protected void buildEnvVarsCustom(Run<?, ?> build, Map<String, String> env) {
env.put("ACCUREV_REFTREE", scm.getReftree());
}

@@ -1,6 +1,5 @@
package hudson.plugins.accurev.delegates;

import hudson.model.AbstractBuild;
import hudson.model.Job;
import hudson.model.Run;
import hudson.plugins.accurev.*;
@@ -167,7 +166,7 @@ protected String getChangeLogStream() {
}

@Override
protected void buildEnvVarsCustom(AbstractBuild<?, ?> build, Map<String, String> env) {
protected void buildEnvVarsCustom(Run<?, ?> build, Map<String, String> env) {
env.put("ACCUREV_WORKSPACE", scm.getWorkspace());
}

@@ -0,0 +1,46 @@
package hudson.plugins.accurev;

import hudson.model.AbstractBuild;
import hudson.model.Run;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class AccurevSCMTest {

@SuppressWarnings("deprecation")
@Test
public void shouldSetEnvironmentVariablesWithAccurevSCM() throws IOException {
AccurevSCM scm = mockSCMForBuildEnvVars();

AccurevSCM.AccurevServer server = new AccurevSCM.AccurevServer(null, "test", "test", 5050, "user", "pass");
when(scm.getServer()).thenReturn(server);

when(scm.getStream()).thenReturn("testStream");

AbstractBuild build = mock(AbstractBuild.class);
Map<String, String> environment = new HashMap<>();
scm.buildEnvVars(build, environment);

assertThat(environment.get("ACCUREV_SERVER_HOSTNAME"), is("test"));
assertThat(environment.get("ACCUREV_SERVER_PORT"), is("5050"));
assertThat(environment.get("ACCUREV_STREAM"), is("testStream"));
}

private AccurevSCM mockSCMForBuildEnvVars() {
AccurevSCM scm = mock(AccurevSCM.class);
doCallRealMethod().when(scm).buildEnvVars(any(AbstractBuild.class), anyMap());
doCallRealMethod().when(scm).buildEnvironment(any(Run.class), anyMap());
return scm;
}
}

0 comments on commit 5e3008e

Please sign in to comment.
You can’t perform that action at this time.