1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-28 05:47:08 +02:00

fix(groovy): handle NPE in utils unstash (#4969)

* Fix NPE in utils unstash

* fix test

---------

Co-authored-by: Vijayan T <vijayanjay@gmail.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
This commit is contained in:
radianer 2024-07-09 09:40:29 +02:00 committed by GitHub
parent 8e962a7729
commit 4d32de1c1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 13 deletions

View File

@ -90,7 +90,6 @@ boolean isInsidePod(Script script) {
}
def unstash(name, msg = "Unstash failed:") {
def unstashedContent = []
try {
echo "Unstash content: ${name}"
@ -98,7 +97,7 @@ def unstash(name, msg = "Unstash failed:") {
unstashedContent += name
} catch (e) {
echo "$msg $name (${e.getMessage()})"
if (e.getMessage().contains("JNLP4-connect")) {
if (e.getMessage() != null && e.getMessage().contains("JNLP4-connect")) {
sleep(3) // Wait 3 seconds in case it has been a network hiccup
try {
echo "[Retry JNLP4-connect issue] Unstashing content: ${name}"

View File

@ -227,6 +227,29 @@ class UtilsTest extends BasePiperTest {
assert(stashResult == [])
}
@Test
void testUnstashFailsNoExceptionMessage() {
def logMessages = []
def examinee = newExaminee(
unstashClosure: {
def stashName -> throw new RuntimeException()
},
echoClosure: {
// coerce to java.lang.String, we might have GStrings.
// comparism with java.lang.String might fail.
message -> logMessages << message.toString()
}
)
def stashResult = examinee.unstash('a')
// in case unstash fails (maybe the stash does not exist, or we cannot unstash due to
// some colliding files in conjunction with file permissions) we emit a log message
// and continue silently instead of failing. In that case we get an empty array back
// instead an array containing the name of the unstashed stash.
assertThat(logMessages, hasItem('Unstash failed: a (null)'))
assert(stashResult == [])
}
private Utils newExaminee(Map parameters) {
def examinee = new Utils()
examinee.steps = [
@ -301,7 +324,7 @@ class UtilsTest extends BasePiperTest {
examinee.stash('test')
assertEquals(expected, stashProperties)
examinee.stash(name: 'test')
assertEquals(expected, stashProperties)
}
@ -310,10 +333,10 @@ class UtilsTest extends BasePiperTest {
void testStash_simpleSignature2Params() {
final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties()
Map expected = [name: 'test', includes: 'includesX', excludes: '']
examinee.stash('test', 'includesX')
assertEquals(expected, stashProperties)
examinee.stash(name: 'test', includes: 'includesX')
assertEquals(expected, stashProperties)
}
@ -322,10 +345,10 @@ class UtilsTest extends BasePiperTest {
void testStash_simpleSignature3Params() {
final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties()
Map expected = [name: 'test', includes: 'includesX', excludes: 'excludesX']
examinee.stash('test', 'includesX', 'excludesX')
assertEquals(expected, stashProperties)
examinee.stash(name: 'test', includes: 'includesX', excludes: 'excludesX')
assertEquals(expected, stashProperties)
}
@ -334,10 +357,10 @@ class UtilsTest extends BasePiperTest {
void testStash_simpleSignature4Params() {
final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties()
Map expected = [name: 'test', includes: 'includesX', excludes: 'excludesX', useDefaultExcludes: false]
examinee.stash('test', 'includesX', 'excludesX', false)
assertEquals(expected, stashProperties)
examinee.stash(name: 'test', includes: 'includesX', excludes: 'excludesX', useDefaultExcludes: false)
assertEquals(expected, stashProperties)
}
@ -346,10 +369,10 @@ class UtilsTest extends BasePiperTest {
void testStash_simpleSignature5Params() {
final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties()
Map expected = [name: 'test', includes: 'includesX', excludes: 'excludesX', useDefaultExcludes: false, allowEmpty: true]
examinee.stash('test', 'includesX', 'excludesX', false, true)
assertEquals(expected, stashProperties)
examinee.stash(name: 'test', includes: 'includesX', excludes: 'excludesX', useDefaultExcludes: false, allowEmpty: true)
assertEquals(expected, stashProperties)
}
@ -358,10 +381,10 @@ class UtilsTest extends BasePiperTest {
void testStash_explicitDefaults() {
final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties()
Map expected = [name: 'test', includes: 'includesX', excludes: 'excludesX']
examinee.stash('test', 'includesX', 'excludesX', true, false)
assertEquals(expected, stashProperties)
examinee.stash(name: 'test', includes: 'includesX', excludes: 'excludesX', useDefaultExcludes: true, allowEmpty: false)
assertEquals(expected, stashProperties)
}