Skip to content

Commit

Permalink
JENKINS-49423 Upgrade from 0.17 incomplete when scriptOnlyIfSuccess a…
Browse files Browse the repository at this point in the history
…nd scriptOnlyIfFailure are both false
  • Loading branch information
Daniel Heid committed Feb 9, 2018
1 parent 8a061f0 commit 32371bc
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 135 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@ work
*.iws
.idea
jenkins-agent
out
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -89,6 +89,14 @@ changes without needing to run to `package` phase.

## Release Notes

### Version 2.4.0

The old option to build only on success or on failure will now be migrated differently: If both is not selected, the
behavior of versions previous to 1.0.0 was to build it independent of the actual build result. Previous versions (after
1.0.0) simply migrate that to do nothing. With version 2.4.0 every build result will be selected in such cases.

* JENKINS-49423 - Upgrade from 0.17 incomplete when scriptOnlyIfSuccess and scriptOnlyIfFailure are both false

### Version 2.3.0

* JENKINS-48931 - PostBuildScript Execute Script Execute Shell Textbox Formatting Broken
Expand Down
19 changes: 16 additions & 3 deletions pom.xml
Expand Up @@ -4,13 +4,13 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.2</version>
<version>3.4</version>
</parent>

<artifactId>postbuildscript</artifactId>
<packaging>hpi</packaging>
<name>Jenkins PostBuildScript Plugin</name>
<version>2.3.1-SNAPSHOT</version>
<version>2.4.0-SNAPSHOT</version>
<url>http://wiki.jenkins-ci.org/display/JENKINS/PostBuildScript+Plugin</url>
<inceptionYear>2011</inceptionYear>

Expand Down Expand Up @@ -114,7 +114,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.11.0</version>
<version>2.13.0</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -144,6 +144,19 @@
<compatibleSinceVersion>1.0.0</compatibleSinceVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Expand Up @@ -148,6 +148,13 @@ private Set<String> migrateResults() {
if (scriptOnlyIfSuccess != null && scriptOnlyIfSuccess) {
results.add(Result.SUCCESS.toString());
}
if (scriptOnlyIfFailure != null && scriptOnlyIfSuccess != null && !scriptOnlyIfSuccess && !scriptOnlyIfFailure) {
results.add(Result.SUCCESS.toString());
results.add(Result.UNSTABLE.toString());
results.add(Result.FAILURE.toString());
results.add(Result.NOT_BUILT.toString());
results.add(Result.ABORTED.toString());
}
return results;
}

Expand Down
@@ -0,0 +1,162 @@
package org.jenkinsci.plugins.postbuildscript;

import hudson.Functions;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.tasks.BuildStep;
import org.jenkinsci.plugins.postbuildscript.model.PostBuildStep;
import org.jenkinsci.plugins.postbuildscript.model.Script;
import org.jenkinsci.plugins.postbuildscript.model.ScriptFile;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class PostBuildScriptIT {

private static final Set<String> SUCCESS_RESULTS = Collections.singleton("SUCCESS");

@Rule
public final JenkinsRule jenkinsRule = new JenkinsRule();
private File outFile;
private Collection<ScriptFile> scriptFiles;
private PostBuildScript postBuildScript;
private FreeStyleBuild build;

@Test
public void executesShellScriptFile() throws Exception {
assumeFalse(Functions.isWindows());

givenOutfile();
givenScriptFiles("/script.sh"); //NON-NLS
postBuildScript = new PostBuildScript(
scriptFiles,
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
false
);

whenBuilt();

thenSuccessfulBuild();
thenWroteHelloWorldToFile();
}

@Test
public void executesGroovyScriptFile() throws Exception {

givenOutfile();
givenScriptFiles("/script.groovy"); //NON-NLS
postBuildScript = new PostBuildScript(
Collections.emptyList(),
scriptFiles,
Collections.emptyList(),
Collections.emptyList(),
false
);

whenBuilt();

thenSuccessfulBuild();
thenWroteHelloWorldToFile();
}

@Test
public void executesGroovyScript() throws Exception {
assumeFalse(Functions.isWindows());

givenOutfile();
String scriptContent = String.format("def out = new File(\"%s\")%nout << \"Hello world\"", outFile.getPath()); //NON-NLS
Script script = new Script(SUCCESS_RESULTS, scriptContent);
Collection<Script> scripts = Collections.singleton(script);
postBuildScript = new PostBuildScript(
Collections.emptyList(),
Collections.emptyList(),
scripts,
Collections.emptyList(),
false
);

whenBuilt();

thenSuccessfulBuild();
thenWroteHelloWorldToFile();
}

@Test
public void executesPostBuildStep() throws Exception {

BuildStep buildStep = mock(BuildStep.class);
given(buildStep.perform(any(AbstractBuild.class), any(Launcher.class), any(BuildListener.class))).willReturn(true);
Collection<BuildStep> buildSteps = Collections.singleton(buildStep);
PostBuildStep step = new PostBuildStep(SUCCESS_RESULTS, buildSteps);
Collection<PostBuildStep> steps = Collections.singleton(step);
postBuildScript = new PostBuildScript(
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
steps,
false
);

whenBuilt();

thenSuccessfulBuild();
verify(buildStep).perform(eq(build), any(Launcher.class), any(BuildListener.class));

}

private void givenOutfile() throws Exception {
outFile = File.createTempFile(getClass().getName(), ".out");
outFile.deleteOnExit();
}

private void givenScriptFiles(String scriptFileLocation) throws URISyntaxException {
String scriptFilePath = getClass().getResource(scriptFileLocation).toURI().getPath();
String command = scriptFilePath + " " + outFile.getPath();
ScriptFile scriptFile = new ScriptFile(SUCCESS_RESULTS, command);
scriptFiles = Collections.singleton(scriptFile);
}

private void whenBuilt() throws IOException, InterruptedException, java.util.concurrent.ExecutionException {
FreeStyleProject project = jenkinsRule.createFreeStyleProject();
project.getPublishersList().add(postBuildScript);
build = project.scheduleBuild2(0).get();
}

private void thenWroteHelloWorldToFile() throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(outFile.toURI()));
String outFileContent = new String(encoded, Charset.forName("UTF-8"));
assertThat(outFileContent, startsWith("Hello world"));
}

private void thenSuccessfulBuild() {
assertThat(build.getResult(), is(Result.SUCCESS));
}


}

0 comments on commit 32371bc

Please sign in to comment.