From c5cce5a3a36b046ce0734423d92ce256157a9bd3 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Thu, 6 Sep 2018 09:25:53 +0200 Subject: [PATCH 1/4] Remove not needed merge method from ConfigurationMerger The other merge method called in the body of that method here does not exist. So any call to this method would end up in some method not found exception or similar. --- src/com/sap/piper/ConfigurationMerger.groovy | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/com/sap/piper/ConfigurationMerger.groovy b/src/com/sap/piper/ConfigurationMerger.groovy index bfefbced9..0e95a782c 100644 --- a/src/com/sap/piper/ConfigurationMerger.groovy +++ b/src/com/sap/piper/ConfigurationMerger.groovy @@ -23,13 +23,4 @@ class ConfigurationMerger { merged = merge(parameters, parameterKeys, merged) return merged } - - @NonCPS - static Map merge( - def script, def stepName, - Map parameters, Set parameterKeys, - Set stepConfigurationKeys - ) { - merge(script, stepName, parameters, parameterKeys, [:], stepConfigurationKeys) - } } From cce1eb0f9abb9ec5128c86d86127106b99f88abd Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 7 Sep 2018 14:28:26 +0200 Subject: [PATCH 2/4] Execute closures when evaluating shell calls This allows us to e.g. also throw exceptions (e.g. hudson.AbortException) when dealing with mocked shell calls. --- test/groovy/util/JenkinsShellCallRule.groovy | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/groovy/util/JenkinsShellCallRule.groovy b/test/groovy/util/JenkinsShellCallRule.groovy index f9d53a313..e227ee514 100644 --- a/test/groovy/util/JenkinsShellCallRule.groovy +++ b/test/groovy/util/JenkinsShellCallRule.groovy @@ -61,14 +61,18 @@ class JenkinsShellCallRule implements TestRule { shell.add(m.script.replaceAll(/\s+/," ").trim()) if (m.returnStdout || m.returnStatus) { def unifiedScript = unify(m.script) + def result = null for(def e : returnValues.entrySet()) { if(e.key.type == Type.REGEX && unifiedScript =~ e.key.script) { - return e.value + result = e.value + break } else if(e.key.type == Type.PLAIN && unifiedScript.equals(e.key.script)) { - return e.value + result = e.value + break } } - return null + if(result instanceof Closure) result = result() + return result } }) From 68ab5bacd6758be2a24df539a6c03b673ee489c2 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 7 Sep 2018 15:49:51 +0200 Subject: [PATCH 3/4] mock GitUtils if we don't mock the GitUtils here changes inside GitUtils affecting the git commands issued at the command line requires changes here. In fact we have to react on implementation details of the GitUtils here. It is better to be independent from that implementation details here since this leads to more focused and smaller commits. --- test/groovy/ArtifactSetVersionTest.groovy | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/test/groovy/ArtifactSetVersionTest.groovy b/test/groovy/ArtifactSetVersionTest.groovy index 095eb0234..7dd6d59ac 100644 --- a/test/groovy/ArtifactSetVersionTest.groovy +++ b/test/groovy/ArtifactSetVersionTest.groovy @@ -1,9 +1,13 @@ #!groovy + import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.rules.ExpectedException import org.junit.rules.RuleChain + +import com.sap.piper.GitUtils + import util.BasePiperTest import util.JenkinsDockerExecuteRule import util.JenkinsEnvironmentRule @@ -26,6 +30,16 @@ import static org.junit.Assert.assertEquals class ArtifactSetVersionTest extends BasePiperTest { Map dockerParameters + def GitUtils gitUtils = new GitUtils() { + boolean insideWorkTree() { + return true + } + + String getGitCommitIdOrNull() { + return 'testCommitId' + } + } + def sshAgentList = [] private ExpectedException thrown = ExpectedException.none() @@ -61,10 +75,8 @@ class ArtifactSetVersionTest extends BasePiperTest { return closure() }) - jscr.setReturnValue('git rev-parse HEAD', 'testCommitId') jscr.setReturnValue("date --universal +'%Y%m%d%H%M%S'", '20180101010203') jscr.setReturnValue('git diff --quiet HEAD', 0) - jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0) helper.registerAllowedMethod('fileExists', [String.class], {true}) } From 9e378d6b461502a832575fbe85d2183ccb01c89e Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Mon, 10 Sep 2018 13:44:30 +0200 Subject: [PATCH 4/4] Remove obsolete code: mockHelper in fact the MockHelper seems not to be used anymore. Apparently fully superseded by our JenkinsRules. --- test/groovy/util/BasePiperTest.groovy | 3 - test/groovy/util/BasePiperTestContext.groovy | 6 - test/groovy/util/MockHelper.groovy | 145 ------------------- 3 files changed, 154 deletions(-) delete mode 100644 test/groovy/util/MockHelper.groovy diff --git a/test/groovy/util/BasePiperTest.groovy b/test/groovy/util/BasePiperTest.groovy index 05228b3f1..4c343b8ef 100644 --- a/test/groovy/util/BasePiperTest.groovy +++ b/test/groovy/util/BasePiperTest.groovy @@ -17,9 +17,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner @TestExecutionListeners(listeners = [LibraryLoadingTestExecutionListener.class], mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS) abstract class BasePiperTest extends BasePipelineTest { - @Autowired - MockHelper mockHelper - @Autowired Script nullScript diff --git a/test/groovy/util/BasePiperTestContext.groovy b/test/groovy/util/BasePiperTestContext.groovy index d7b012fe0..4687e6748 100644 --- a/test/groovy/util/BasePiperTestContext.groovy +++ b/test/groovy/util/BasePiperTestContext.groovy @@ -36,10 +36,4 @@ class BasePiperTestContext { LibraryLoadingTestExecutionListener.prepareObjectInterceptors(mockUtils) return mockUtils } - - @Bean - MockHelper mockHelper() { - return new MockHelper() - } - } diff --git a/test/groovy/util/MockHelper.groovy b/test/groovy/util/MockHelper.groovy deleted file mode 100644 index abf745720..000000000 --- a/test/groovy/util/MockHelper.groovy +++ /dev/null @@ -1,145 +0,0 @@ -#!groovy -package util - -import groovy.json.JsonBuilder -import groovy.json.JsonSlurper -import hudson.tasks.junit.TestResult -import org.yaml.snakeyaml.Yaml - -/** - * This is a Helper class for mocking. - * - * It can be used to load test data or to mock Jenkins or Maven specific objects. - **/ - -class MockHelper { - - /** - * load properties from resources for mocking return value of readProperties method - * @param path to properties - * @return properties file - */ - Properties loadProperties( String path ){ - Properties p = new Properties() - File pFile = new File( path ) - p.load( pFile.newDataInputStream() ) - return p - } - - /** - * load JSON from resources for mocking return value of readJSON method - * @param path to json file - * @return json file - */ - Object loadJSON( String path ){ - def js = new JsonSlurper() - def reader = new BufferedReader(new FileReader( path )) - def j = js.parse(reader) - return j - } - - /** - * load YAML from resources for mocking return value of readYaml method - * @param path to yaml file - * @return yaml file - */ - Object loadYAML( String path ){ - return new Yaml().load(new FileReader(path)) - } - - /** - * creates HTTP response for mocking return value of httpRequest method - * @param text - text to parse into json object - * @return json Object - */ - Object createResponse( String text ){ - def response = new JsonBuilder(new JsonSlurper().parseText( text )) - return response - } - - /** - * load File from resources for mocking return value of readFile method - * @param path to file - * @return File - */ - File loadFile( String path ){ - return new File( path ) - } - - /** - * load POM from resources for mocking return value of readMavenPom method - * @param path to pom file - * @return Pom class - */ - MockPom loadPom(String path ){ - return new MockPom( path ) - } - - /** - * Inner class to mock maven descriptor - */ - class MockPom { - def f - def pom - MockPom(String path){ - this.f = new File( path ) - if ( f.exists() ){ - this.pom = new XmlSlurper().parse(f) - } - else { - throw new FileNotFoundException( 'Failed to find file: ' + path ) - } - } - String getVersion(){ - return pom.version - } - String getGroupId(){ - return pom.groupId - } - String getArtifactId(){ - return pom.artifactId - } - String getPackaging(){ - return pom.packaging - } - String getName(){ - return pom.name - } - } - - MockBuild loadMockBuild(){ - return new MockBuild() - } - - MockBuild loadMockBuild(TestResult result){ - return new MockBuild(result) - } - - /** - * Inner class to mock Jenkins' currentBuild return object in scripts - */ - class MockBuild { - TestResult testResult - MockBuild(){} - MockBuild(TestResult result){ - testResult = result - } - MockLibrary getAction(Class c){ - println("MockLibrary -> getAction - arg: " + c.toString() ) - return new MockLibrary() - } - - class MockLibrary { - MockLibrary(){} - // return default - List getLibraries(){ - println("MockLibrary -> getLibraries") - return [ [name: 'default-library', version: 'default-master', trusted: true] ] - } - TestResult getResult() { - println("MockLibrary -> getResult") - return testResult - } - } - } -}