Skip to content

Commit

Permalink
[JENKINS-26100] Change buildEnvVars to take Run (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
jetersen committed Jun 6, 2017
1 parent c6bf0c5 commit 5e3008e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 10 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.23</version>
<version>2.29</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -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>
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/hudson/plugins/accurev/AccurevSCM.java
Expand Up @@ -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);
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down
@@ -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.*;
Expand Down Expand Up @@ -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());
}

Expand Down
@@ -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.*;
Expand Down Expand Up @@ -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());
}

Expand Down
46 changes: 46 additions & 0 deletions src/test/java/hudson/plugins/accurev/AccurevSCMTest.java
@@ -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.