diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/PipelineTestHelper.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/PipelineTestHelper.groovy index a5aefa3f..98b998e3 100644 --- a/src/main/groovy/com/lesfurets/jenkins/unit/PipelineTestHelper.groovy +++ b/src/main/groovy/com/lesfurets/jenkins/unit/PipelineTestHelper.groovy @@ -683,11 +683,12 @@ class PipelineTestHelper { exitValue = output.exitValue } - if (!returnStdout) { + // Jenkins also prints the output from sh when returnStdout is true if the script fails + if (!returnStdout || exitValue != 0) { println stdout } - if (returnStdout) { + if (returnStdout && exitValue == 0) { return stdout } if (returnStatus) { diff --git a/src/test/groovy/com/lesfurets/jenkins/unit/PipelineTestHelperTest.groovy b/src/test/groovy/com/lesfurets/jenkins/unit/PipelineTestHelperTest.groovy index 45a41486..a26e399b 100644 --- a/src/test/groovy/com/lesfurets/jenkins/unit/PipelineTestHelperTest.groovy +++ b/src/test/groovy/com/lesfurets/jenkins/unit/PipelineTestHelperTest.groovy @@ -123,6 +123,18 @@ class PipelineTestHelperTest { Assertions.assertThat(output).isEqualTo('/foo/bar') } + @Test(expected = Exception) + void runShWithStdoutFailure() { + // given: + def helper = new PipelineTestHelper() + helper.addShMock('pwd', '/foo/bar', 1) + + // when: + helper.runSh(returnStdout: true, script: 'pwd') + + // then: Exception raised + } + @Test void runShWithReturnCode() { // given: