Skip to content

Commit 6355f37

Browse files
authored
Migrate from RestartableJenkinsRule to JenkinsSessionRule (#360)
1 parent a259e64 commit 6355f37

File tree

2 files changed

+36
-49
lines changed

2 files changed

+36
-49
lines changed

src/test/java/org/jenkinsci/plugins/workflow/multibranch/SCMVarTest.java

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,51 +49,45 @@
4949
import static org.junit.Assert.*;
5050
import org.junit.ClassRule;
5151
import org.junit.Rule;
52-
import org.junit.runners.model.Statement;
5352
import org.jvnet.hudson.test.BuildWatcher;
5453
import org.jvnet.hudson.test.Issue;
55-
import org.jvnet.hudson.test.RestartableJenkinsRule;
54+
import org.jvnet.hudson.test.JenkinsSessionRule;
5655

5756
public class SCMVarTest {
5857

5958
@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
60-
@Rule public RestartableJenkinsRule story = new RestartableJenkinsRule();
59+
@Rule public JenkinsSessionRule story = new JenkinsSessionRule();
6160
@Rule public GitSampleRepoRule sampleGitRepo = new GitSampleRepoRule();
6261

63-
@Test public void scmPickle() {
64-
story.addStep(new Statement() {
65-
@Override public void evaluate() throws Throwable {
62+
@Test public void scmPickle() throws Throwable {
63+
story.then(j -> {
6664
sampleGitRepo.init();
6765
sampleGitRepo.write("Jenkinsfile", "def _scm = scm; semaphore 'wait'; node {checkout _scm; echo readFile('file')}");
6866
sampleGitRepo.write("file", "initial content");
6967
sampleGitRepo.git("add", "Jenkinsfile");
7068
sampleGitRepo.git("commit", "--all", "--message=flow");
71-
WorkflowMultiBranchProject mp = story.j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
69+
WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
7270
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
7371
WorkflowJob p = WorkflowMultiBranchProjectTest.scheduleAndFindBranchProject(mp, "master");
7472
SemaphoreStep.waitForStart("wait/1", null);
7573
WorkflowRun b1 = p.getLastBuild();
7674
assertNotNull(b1);
77-
}
7875
});
79-
story.addStep(new Statement() {
80-
@Override public void evaluate() throws Throwable {
76+
story.then(j -> {
8177
SemaphoreStep.success("wait/1", null);
82-
WorkflowJob p = story.j.jenkins.getItemByFullName("p/master", WorkflowJob.class);
78+
WorkflowJob p = j.jenkins.getItemByFullName("p/master", WorkflowJob.class);
8379
assertNotNull(p);
8480
WorkflowRun b1 = p.getLastBuild();
8581
assertNotNull(b1);
8682
assertEquals(1, b1.getNumber());
87-
story.j.assertLogContains("initial content", story.j.waitForCompletion(b1));
83+
j.assertLogContains("initial content", j.waitForCompletion(b1));
8884
SCMBinderTest.assertRevisionAction(b1);
89-
}
9085
});
9186
}
9287

9388
@Issue("JENKINS-30222")
94-
@Test public void globalVariable() {
95-
story.addStep(new Statement() {
96-
@Override public void evaluate() throws Throwable {
89+
@Test public void globalVariable() throws Throwable {
90+
story.then(j -> {
9791
// Set up a standardJob definition:
9892
File lib = new File(Jenkins.get().getRootDir(), "somelib");
9993
LibraryConfiguration cfg = new LibraryConfiguration("somelib", new LocalRetriever(lib));
@@ -121,12 +115,11 @@ public class SCMVarTest {
121115
sampleGitRepo.git("add", "resource");
122116
sampleGitRepo.git("commit", "--all", "--message=flow");
123117
// And run:
124-
WorkflowMultiBranchProject mp = story.j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
118+
WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
125119
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
126120
WorkflowJob p = WorkflowMultiBranchProjectTest.scheduleAndFindBranchProject(mp, "master");
127-
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
128-
story.j.assertLogContains("loaded resource content", b);
129-
}
121+
WorkflowRun b = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
122+
j.assertLogContains("loaded resource content", b);
130123
});
131124
}
132125

@@ -145,19 +138,17 @@ private static final class LocalRetriever extends LibraryRetriever {
145138
}
146139

147140
@Issue("JENKINS-31386")
148-
@Test public void standaloneProject() {
149-
story.addStep(new Statement() {
150-
@Override public void evaluate() throws Throwable {
141+
@Test public void standaloneProject() throws Throwable {
142+
story.then(j -> {
151143
sampleGitRepo.init();
152144
sampleGitRepo.write("Jenkinsfile", "node {checkout scm; echo readFile('file')}");
153145
sampleGitRepo.write("file", "some content");
154146
sampleGitRepo.git("add", "Jenkinsfile");
155147
sampleGitRepo.git("commit", "--all", "--message=flow");
156-
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
148+
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
157149
p.setDefinition(new CpsScmFlowDefinition(new GitStep(sampleGitRepo.toString()).createSCM(), "Jenkinsfile"));
158-
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
159-
story.j.assertLogContains("some content", b);
160-
}
150+
WorkflowRun b = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
151+
j.assertLogContains("some content", b);
161152
});
162153
}
163154

src/test/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowBranchProjectFactoryTest.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,20 @@
3737
import static org.junit.Assert.*;
3838
import org.junit.ClassRule;
3939
import org.junit.Rule;
40-
import org.junit.runners.model.Statement;
4140
import org.jvnet.hudson.test.BuildWatcher;
4241
import org.jvnet.hudson.test.Issue;
43-
import org.jvnet.hudson.test.RestartableJenkinsRule;
42+
import org.jvnet.hudson.test.JenkinsRule;
43+
import org.jvnet.hudson.test.JenkinsSessionRule;
4444

4545
public class WorkflowBranchProjectFactoryTest {
4646

4747
@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
48-
@Rule public RestartableJenkinsRule story = new RestartableJenkinsRule();
48+
@Rule public JenkinsSessionRule story = new JenkinsSessionRule();
4949
@Rule public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();
5050

5151
@Issue("JENKINS-30744")
52-
@Test public void slashyBranches() {
53-
story.addStep(new Statement() {
54-
@Override public void evaluate() throws Throwable {
52+
@Test public void slashyBranches() throws Throwable {
53+
story.then(j -> {
5554
sampleRepo.init();
5655
sampleRepo.git("checkout", "-b", "dev/main");
5756
String script =
@@ -63,41 +62,38 @@ public class WorkflowBranchProjectFactoryTest {
6362
sampleRepo.write("Jenkinsfile", script);
6463
sampleRepo.git("add", "Jenkinsfile");
6564
sampleRepo.git("commit", "--all", "--message=flow");
66-
WorkflowMultiBranchProject mp = story.j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
65+
WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
6766
GitSCMSource source = new GitSCMSource(sampleRepo.toString());
6867
source.setTraits(Collections.singletonList(new BranchDiscoveryTrait()));
6968
mp.getSourcesList().add(new BranchSource(source));
7069
WorkflowJob p = scheduleAndFindBranchProject(mp, "dev%2Fmain");
7170
assertEquals(1, mp.getItems().size());
72-
story.j.waitUntilNoActivity();
71+
j.waitUntilNoActivity();
7372
WorkflowRun b1 = p.getLastBuild();
7473
assertEquals(1, b1.getNumber());
75-
story.j.assertLogContains("branch=dev/main", b1);
76-
story.j.assertLogContains("workspace=dev_main", b1);
77-
verifyProject(p);
74+
j.assertLogContains("branch=dev/main", b1);
75+
j.assertLogContains("workspace=dev_main", b1);
76+
verifyProject(j, p);
7877
sampleRepo.write("Jenkinsfile", script.replace("branch=", "Branch="));
79-
}
8078
});
81-
story.addStep(new Statement() {
82-
@Override public void evaluate() throws Throwable {
83-
WorkflowJob p = story.j.jenkins.getItemByFullName("p/dev%2Fmain", WorkflowJob.class);
79+
story.then(j -> {
80+
WorkflowJob p = j.jenkins.getItemByFullName("p/dev%2Fmain", WorkflowJob.class);
8481
assertNotNull(p);
8582
sampleRepo.git("commit", "--all", "--message=Flow");
86-
sampleRepo.notifyCommit(story.j);
83+
sampleRepo.notifyCommit(j);
8784
WorkflowRun b2 = p.getLastBuild();
8885
assertEquals(2, b2.getNumber());
89-
story.j.assertLogContains("Branch=dev/main", b2);
90-
story.j.assertLogContains("workspace=dev_main", b2);
91-
verifyProject(p);
92-
}
86+
j.assertLogContains("Branch=dev/main", b2);
87+
j.assertLogContains("workspace=dev_main", b2);
88+
verifyProject(j, p);
9389
});
9490
}
95-
private void verifyProject(WorkflowJob p) throws Exception {
91+
private static void verifyProject(JenkinsRule j, WorkflowJob p) throws Exception {
9692
assertEquals("dev%2Fmain", p.getName());
9793
assertEquals("dev/main", p.getDisplayName());
9894
assertEquals("p/dev%2Fmain", p.getFullName());
9995
assertEquals("p » dev/main", p.getFullDisplayName());
100-
story.j.createWebClient().getPage(p);
96+
j.createWebClient().getPage(p);
10197
assertEquals(new File(new File(p.getParent().getRootDir(), "branches"), "dev-main.k31kdj"), p.getRootDir());
10298
}
10399

0 commit comments

Comments
 (0)