Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #30 from alexsomai/symbol-annotation
[JENKINS-37054] Add symbol annotation
  • Loading branch information
olivergondza committed Nov 10, 2016
2 parents 4751cd2 + 2a9c7f5 commit 9e1004b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 18 deletions.
26 changes: 20 additions & 6 deletions pom.xml
Expand Up @@ -14,9 +14,12 @@
<url>http://wiki.jenkins-ci.org/display/JENKINS/Workspace+Cleanup+Plugin</url>

<properties>
<jenkins.version>1.609</jenkins.version>
<java.level>6</java.level>
<workflow.version>1.4.3</workflow.version>
<jenkins.version>1.642.1</jenkins.version>
<java.level>7</java.level>
<workflow-job.version>2.4</workflow-job.version>
<workflow-cps.version>2.10</workflow-cps.version>
<workflow-basic-steps.version>2.1</workflow-basic-steps.version>
<workflow-durable-task-step.version>2.4</workflow-durable-task-step.version>
</properties>

<developers>
Expand Down Expand Up @@ -45,6 +48,11 @@
<version>0.3</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
Expand All @@ -54,19 +62,25 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow.version}</version>
<version>${workflow-job.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow.version}</version>
<version>${workflow-cps.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>${workflow.version}</version>
<version>${workflow-basic-steps.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>${workflow-durable-task-step.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/hudson/plugins/ws_cleanup/WsCleanup.java
Expand Up @@ -19,6 +19,7 @@
import jenkins.tasks.SimpleBuildStep;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -253,7 +254,8 @@ public boolean needsToRunAfterFinalized() {
public boolean isMatrixProject(Object o) {
return o instanceof MatrixProject;
}


@Symbol("cleanWs")
@Extension(ordinal=-9999)
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {

Expand Down
83 changes: 72 additions & 11 deletions src/test/java/hudson/plugins/ws_cleanup/CleanupTest.java
Expand Up @@ -215,9 +215,8 @@ public void pipelineWorkspaceCleanup() throws Exception {
" } \n" +
" } \n" +
"}"));
WorkflowRun build = p.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(build);
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done", build);
WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done", run);

assertThat(ws.getRoot().listFiles(), nullValue());
}
Expand All @@ -236,14 +235,10 @@ public void pipelineWorkspaceCleanupUsingPattern() throws Exception {
" } \n" +
" } \n" +
"}"));
WorkflowRun build = p.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(build);
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done", build);
WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done", run);

File[] files = ws.getRoot().listFiles();
assertThat(files, notNullValue());
assertThat(files, arrayWithSize(1));
assertThat(files[0].getName(), is("foo.txt"));
verifyFileExists("foo.txt");
}

@Test @Issue("JENKINS-28454")
Expand All @@ -266,10 +261,76 @@ public void pipelineWorkspaceCleanupUnlessBuildFails() throws Exception {
j.assertBuildStatus(Result.FAILURE, build);
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] Skipped based on build state FAILURE", build);

verifyFileExists("foo.txt");
}

@Test @Issue("JENKINS-37054")
public void symbolAnnotationWorkspaceCleanup() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("" +
"node { \n" +
" ws ('" + ws.getRoot() + "') { \n" +
" try { \n" +
" writeFile file: 'foo.txt', text: 'foobar' \n" +
" } finally { \n" +
" cleanWs() \n" +
" } \n" +
" } \n" +
"}"));
WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done", run);

assertThat(ws.getRoot().listFiles(), nullValue());
}

@Test @Issue("JENKINS-37054")
public void symbolWorkspaceCleanupAnnotationUsingPattern() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("" +
"node { \n" +
" ws ('" + ws.getRoot() + "') { \n" +
" try { \n" +
" writeFile file: 'foo.txt', text: 'first file' \n" +
" writeFile file: 'bar.txt', text: 'second file' \n" +
" } finally { \n" +
" cleanWs patterns: [[pattern: 'bar.*', type: 'INCLUDE']] \n" +
" } \n" +
" } \n" +
"}"));
WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done", run);

verifyFileExists("foo.txt");
}

@Test @Issue("JENKINS-37054")
public void symbolAnnotationWorkspaceCleanupUnlessBuildFails() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("" +
"node { \n" +
" ws ('" + ws.getRoot() + "'){ \n" +
" try { \n" +
" writeFile file: 'foo.txt', text: 'foobar' \n" +
" throw new Exception() \n" +
" } catch (err) { \n" +
" currentBuild.result = 'FAILURE' \n" +
" } finally { \n" +
" cleanWs cleanWhenFailure: false \n" +
" } \n" +
" } \n" +
"}"));
WorkflowRun run = p.scheduleBuild2(0).get();
j.assertBuildStatus(Result.FAILURE, run);
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] Skipped based on build state FAILURE", run);

verifyFileExists("foo.txt");
}

private void verifyFileExists(String fileName) {
File[] files = ws.getRoot().listFiles();
assertThat(files, notNullValue());
assertThat(files, arrayWithSize(1));
assertThat(files[0].getName(), is("foo.txt"));
assertThat(files[0].getName(), is(fileName));
}

public static WsCleanup wipeoutPublisher() {
Expand Down

0 comments on commit 9e1004b

Please sign in to comment.