github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/PipelineExecuteTest.groovy (about) 1 import util.BasePiperTest 2 import util.Rules 3 4 import org.junit.Before 5 import org.junit.Rule 6 import org.junit.Test 7 import org.junit.rules.ExpectedException 8 import org.junit.rules.RuleChain 9 10 import util.JenkinsReadYamlRule 11 import util.JenkinsStepRule 12 13 class PipelineExecuteTest extends BasePiperTest { 14 15 private ExpectedException thrown = new ExpectedException().none() 16 private JenkinsStepRule stepRule = new JenkinsStepRule(this) 17 18 @Rule 19 public RuleChain ruleChain = Rules 20 .getCommonRules(this) 21 .around(new JenkinsReadYamlRule(this)) 22 .around(thrown) 23 .around(stepRule) 24 25 def pipelinePath 26 def checkoutParameters = [:] 27 def load 28 29 @Before 30 void init() { 31 32 helper.registerAllowedMethod('deleteDir', [], null) 33 helper.registerAllowedMethod('checkout', [Map], { m -> 34 checkoutParameters.branch = m.branches[0].name 35 checkoutParameters.repoUrl = m.userRemoteConfigs[0].url 36 checkoutParameters.credentialsId = m.userRemoteConfigs[0].credentialsId 37 checkoutParameters.path = m.extensions[0].sparseCheckoutPaths[0].path 38 }) 39 helper.registerAllowedMethod('load', [String], { s -> load = s }) 40 } 41 42 43 @Test 44 void straightForwardTest() { 45 46 stepRule.step.pipelineExecute(repoUrl: "https://test.com/myRepo.git") 47 48 assert load == "Jenkinsfile" 49 assert checkoutParameters.branch == 'master' 50 assert checkoutParameters.repoUrl == "https://test.com/myRepo.git" 51 assert checkoutParameters.credentialsId == '' 52 assert checkoutParameters.path == 'Jenkinsfile' 53 } 54 55 @Test 56 void parameterizeTest() { 57 58 stepRule.step.pipelineExecute(repoUrl: "https://test.com/anotherRepo.git", 59 branch: 'feature', 60 path: 'path/to/Jenkinsfile', 61 credentialsId: 'abcd1234') 62 63 assert load == "path/to/Jenkinsfile" 64 assert checkoutParameters.branch == 'feature' 65 assert checkoutParameters.repoUrl == "https://test.com/anotherRepo.git" 66 assert checkoutParameters.credentialsId == 'abcd1234' 67 assert checkoutParameters.path == 'path/to/Jenkinsfile' 68 } 69 70 @Test 71 void noRepoUrlTest() { 72 73 thrown.expect(Exception) 74 thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR repoUrl") 75 76 stepRule.step.pipelineExecute() 77 } 78 }