Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
if (relativeTargetDir == null || relativeTargetDir.length() == 0 || relativeTargetDir.equals(".")) {
return workspace;
}
//fixing issue : #3068 : Warn if used in Pipeline
if ( context != null && context.getClass().getName().equals("org.jenkinsci.plugins.workflow.job.WorkflowJob")){

Check warning on line 42 in src/main/java/hudson/plugins/git/extensions/impl/RelativeTargetDirectory.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 42 is only partially covered, one branch is missing
listener.getLogger().println("[Warning] 'Check out to a sub-directory' is not intended for use with Pipeline jobs. Please use the 'dir' step instead. ");
}
return workspace.child(environment.expand(relativeTargetDir));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package hudson.plugins.git;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import jenkins.plugins.git.GitSampleRepoRule;

@WithJenkins
public class RelativeTargetDirectoryPipelineTest {

public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();

@BeforeEach
public void setupRepo() throws Throwable {
sampleRepo.before();
}

@AfterEach
public void tearDownRepo() {
sampleRepo.after();
}

@Test
public void warningIsLoggedWhenUsedInPipeline(JenkinsRule j) throws Exception {
// 1. Init a git repo
sampleRepo.init();
sampleRepo.write("Jenkinsfile", "echo 'hello'");
sampleRepo.git("add", "Jenkinsfile");
sampleRepo.git("commit", "--all", "-m", "init");

// 2. Create Pipeline Job
WorkflowJob job = j.createProject(WorkflowJob.class, "test-job");
String repoUrl = sampleRepo.toString();

// 3. Script that uses the bad feature
String pipelineScript =
"node {\n" +
" checkout([$class: 'GitSCM',\n" +
" branches: [[name: '*/master']],\n" +
" userRemoteConfigs: [[url: '" + repoUrl + "']],\n" +
" extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdir']]\n" +
" ])\n" +
"}\n";
job.setDefinition(new CpsFlowDefinition(pipelineScript, true));

// 4. Run and Assert
WorkflowRun run = j.buildAndAssertSuccess(job);
j.assertLogContains("'Check out to a sub-directory' is not intended for use with Pipeline jobs", run);
}
}
Loading