Skip to content

Commit

Permalink
Add match-all addShMock invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartine-ias committed Apr 26, 2024
1 parent 75bad9d commit 84c149a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ class TestExampleJob extends BasePipelineTest {
}
```

After calling `super.setUp()`, the test `helper` instance is available, as well as many
helper methods. The test helper already provides basic variables such as a very simple
After calling `super.setUp()`, the test `helper` instance is available, as well as many
helper methods. The test helper already provides basic variables such as a very simple
`currentBuild` definition. You can redefine them as you wish.

Note that `super.setUp()` must be called prior to using most features. This is commonly done
Expand Down Expand Up @@ -335,7 +335,7 @@ void setUp() {
// - generate an error on unexpected calls
// - ignore any echo (debug) outputs, they are not relevant
// - all further shell mocks are configured in the test
helper.addShMock(~/(?s).*/) { throw new Exception('Unexpected sh call') }
helper.addShMock() { throw new Exception('Unexpected sh call') }
helper.addShMock(~/echo\s.*/, '', 0)
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class PipelineTestHelper {
this.callback = callback
}

MockScriptHandler(Closure callback) {
this.fullMatchFilter = null
this.regexpMatchFilter = null
this.callback = callback
}

/**
* match a script invocation against our filter
*/
Expand Down Expand Up @@ -793,7 +799,7 @@ class PipelineTestHelper {
/**
* Configure mock output for the `sh` command. This function should be called before
* attempting to call `JenkinsMocks.sh()`.
* @param filter Script command to mock or null if any command is matched.
* @param filter Script command to mock
* @param stdout Standard output text to return for the given command.
* @param exitValue Exit value for the command.
*/
Expand All @@ -804,7 +810,7 @@ class PipelineTestHelper {
/**
* Configure mock output for the `sh` command. This function should be called before
* attempting to call `JenkinsMocks.sh()`.
* @param filter Regexp pattern to mock or null if any command is matched.
* @param filter Regexp pattern to mock
* @param stdout Standard output text to return for the given command.
* @param exitValue Exit value for the command.
*/
Expand All @@ -815,7 +821,7 @@ class PipelineTestHelper {
/**
* Configure mock callback for the `sh` command. This function should be called before
* attempting to call `JenkinsMocks.sh()`.
* @param filter Script command to mock or null if any command is matched.
* @param filter Script command to mock
* @param callback Closure to be called when the mock is executed. This closure will be
* passed the script call which is being executed, and
* <strong>must</strong> return a {@code Map} with the following
Expand All @@ -832,7 +838,7 @@ class PipelineTestHelper {
/**
* Configure mock callback for the `sh` command. This function should be called before
* attempting to call `JenkinsMocks.sh()`.
* @param filter Regexp pattern to mock or null if any command is matched.
* @param filter Regexp pattern to mock
* @param callback Closure to be called when the mock is executed. This closure will be
* passed the script call which is being executed, and
* <strong>must</strong> return a {@code Map} with the following
Expand All @@ -846,6 +852,22 @@ class PipelineTestHelper {
mockShHandlers << new MockScriptHandler(filter, callback)
}

/**
* Configure match-all mock callback for the `sh` command. This function should be called
* before attempting to call `JenkinsMocks.sh()`.
* @param callback Closure to be called when the mock is executed. This closure will be
* passed the script call which is being executed, and
* <strong>must</strong> return a {@code Map} with the following
* key/value pairs:
* <ul>
* <li>{@code stdout}: {@code String} with the mocked output.</li>
* <li>{@code exitValue}: {@code int} with the mocked exit value.</li>
* </ul>
*/
void addShMock(Closure callback) {
mockShHandlers << new MockScriptHandler(callback)
}

@SuppressWarnings('ThrowException')
def runSh(def args) {
return runScript(args, mockShHandlers)
Expand All @@ -854,7 +876,7 @@ class PipelineTestHelper {
/**
* Configure mock output for the `bat` command. This function should be called before
* attempting to call `JenkinsMocks.bat()`.
* @param filter Script command to mock or null if any command is matched.
* @param filter Script command to mock
* @param stdout Standard output text to return for the given command.
* @param exitValue Exit value for the command.
*/
Expand All @@ -865,7 +887,7 @@ class PipelineTestHelper {
/**
* Configure mock output for the `bat` command. This function should be called before
* attempting to call `JenkinsMocks.bat()`.
* @param filter Regexp pattern to mock or null if any command is matched.
* @param filter Regexp pattern to mock
* @param stdout Standard output text to return for the given command.
* @param exitValue Exit value for the command.
*/
Expand All @@ -876,7 +898,7 @@ class PipelineTestHelper {
/**
* Configure mock callback for the `bat` command. This function should be called before
* attempting to call `JenkinsMocks.bat()`.
* @param filter Script command to mock or null if any command is matched.
* @param filter Script command to mock
* @param callback Closure to be called when the mock is executed. This closure will be
* passed the script call which is being executed, and
* <strong>must</strong> return a {@code Map} with the following
Expand All @@ -893,7 +915,7 @@ class PipelineTestHelper {
/**
* Configure mock callback for the `bat` command. This function should be called before
* attempting to call `JenkinsMocks.bat()`.
* @param filter Regexp pattern to mock or null if any command is matched.
* @param filter Regexp pattern to mock
* @param callback Closure to be called when the mock is executed. This closure will be
* passed the script call which is being executed, and
* <strong>must</strong> return a {@code Map} with the following
Expand All @@ -907,6 +929,22 @@ class PipelineTestHelper {
mockBatHandlers << new MockScriptHandler(filter, callback)
}

/**
* Configure match-all mock callback for the `bat` command. This function should be called
* before attempting to call `JenkinsMocks.bat()`.
* @param callback Closure to be called when the mock is executed. This closure will be
* passed the script call which is being executed, and
* <strong>must</strong> return a {@code Map} with the following
* key/value pairs:
* <ul>
* <li>{@code stdout}: {@code String} with the mocked output.</li>
* <li>{@code exitValue}: {@code int} with the mocked exit value.</li>
* </ul>
*/
void addBatMock(Closure callback) {
mockBatHandlers << new MockScriptHandler(callback)
}

@SuppressWarnings('ThrowException')
def runBat(def args) {
return runScript(args, mockBatHandlers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,56 @@ class PipelineTestHelperTest {
assertThat(status).isEqualTo(2)
}

@Test()
void runShWithNoArgs() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock() { script ->
return [stdout: '', exitValue: 2]
}

// when:
def status = helper.runSh(returnStatus: true, script: 'echo foo')

// then:
assertThat(status).isEqualTo(2)
}

@Test()
void runBatWithNoArgs() {
// given:
def helper = new PipelineTestHelper()
helper.addBatMock() { script ->
return [stdout: '', exitValue: 2]
}

// when:
def status = helper.runBat(returnStatus: true, script: 'echo foo')

// then:
assertThat(status).isEqualTo(2)
}

@Test()
void runShWithNoArgsMultiline() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock() { script ->
return [stdout: '', exitValue: 2]
}

// when:
def status = helper.runSh(returnStatus: true, script:
'''echo foo &&\
echo bar
'''
)

// then:
assertThat(status).isEqualTo(2)
}


@Test()
void runShWithoutMockOutput() {
// given:
Expand Down

0 comments on commit 84c149a

Please sign in to comment.