1+ package hudson .plugins .git ;
2+
3+ import org .jenkinsci .plugins .workflow .cps .CpsFlowDefinition ;
4+ import org .jenkinsci .plugins .workflow .job .WorkflowJob ;
5+ import org .jenkinsci .plugins .workflow .job .WorkflowRun ;
6+ import org .junit .jupiter .api .Test ;
7+ import org .junit .jupiter .api .BeforeEach ;
8+ import org .junit .jupiter .api .AfterEach ;
9+ import org .jvnet .hudson .test .JenkinsRule ;
10+ import org .jvnet .hudson .test .junit .jupiter .WithJenkins ;
11+ import jenkins .plugins .git .GitSampleRepoRule ;
12+
13+ @ WithJenkins
14+ public class RelativeTargetDirectoryPipelineTest {
15+
16+ public GitSampleRepoRule sampleRepo = new GitSampleRepoRule ();
17+
18+ @ BeforeEach
19+ public void setupRepo () throws Throwable {
20+ sampleRepo .before ();
21+ }
22+
23+ @ AfterEach
24+ public void tearDownRepo () {
25+ sampleRepo .after ();
26+ }
27+
28+ @ Test
29+ public void warningIsLoggedWhenUsedInPipeline (JenkinsRule j ) throws Exception {
30+ // 1. Init a git repo
31+ sampleRepo .init ();
32+ sampleRepo .write ("Jenkinsfile" , "echo 'hello'" );
33+ sampleRepo .git ("add" , "Jenkinsfile" );
34+ sampleRepo .git ("commit" , "--all" , "-m" , "init" );
35+
36+ // 2. Create Pipeline Job
37+ WorkflowJob job = j .createProject (WorkflowJob .class , "test-job" );
38+ String repoUrl = sampleRepo .toString ();
39+
40+ // 3. Script that uses the bad feature
41+ String pipelineScript =
42+ "node {\n " +
43+ " checkout([$class: 'GitSCM',\n " +
44+ " branches: [[name: '*/master']],\n " +
45+ " userRemoteConfigs: [[url: '" + repoUrl + "']],\n " +
46+ " extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdir']]\n " +
47+ " ])\n " +
48+ "}\n " ;
49+ job .setDefinition (new CpsFlowDefinition (pipelineScript , true ));
50+
51+ // 4. Run and Assert
52+ WorkflowRun run = j .buildAndAssertSuccess (job );
53+ j .assertLogContains ("'Check out to a sub-directory' is not intended for use with Pipeline jobs" , run );
54+ }
55+ }
0 commit comments