Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fileExists mock with helper methods #312

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,17 @@ You can take a look at the `BasePipelineTest` class to have the short list of al
Some tricky methods such as `load` and `parallel` are implemented directly in the helper.
If you want to override those, make sure that you extend the `PipelineTestHelper` class.

### Mocking readFile
### Mocking readFile and fileExists

The `readFile` step can be mocked to return a specific string for a given file name. This can be useful for testing
pipelines which must read data from a file which is required for subsequent steps. An example of such a testing scenario
follows:
The `readFile` and `fileExists` steps can be mocked to return a specific result for a given file name. This can be
useful for testing pipelines for which file operations can influence subsequent steps. An example of such a testing
scenario follows:

```groovy
// Jenkinsfile
node {
stage('Process output') {
if (readFile('output') == 'FAILED!!!') {
if (fileExists('output') && readFile('output') == 'FAILED!!!') {
currentBuild.result = 'FAILURE'
error 'Build failed'
}
Expand All @@ -206,6 +206,7 @@ node {
```groovy
@Test
void exampleReadFileTest() {
helper.addFileExistsMock('fileExists', true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be
helper.addFileExistsMock('output', true)?

Copy link
Contributor Author

@nre-ableton nre-ableton Mar 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, I think this is a typo. See #350.

helper.addReadFileMock('output', 'FAILED!!!')
runScript("Jenkinsfile")
assertJobStatusFailure()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ abstract class BasePipelineTest {
]
})
helper.registerAllowedMethod("buildDiscarder", [Object])
helper.registerAllowedMethod("skipStagesAfterUnstable")
helper.registerAllowedMethod("checkout", [Map])
helper.registerAllowedMethod("choice", [Map])
helper.registerAllowedMethod('cifsPublisher', [Map], {true})
Expand All @@ -157,6 +156,7 @@ abstract class BasePipelineTest {
println(message)
})
helper.registerAllowedMethod("error", [String], { updateBuildStatus('FAILURE') })
helper.registerAllowedMethod('fileExists', [String], { String arg -> helper.fileExists(arg) })
helper.registerAllowedMethod("gatlingArchive")
helper.registerAllowedMethod("gitlabBuilds", [Map, Closure])
helper.registerAllowedMethod("gitlabCommitStatus", [String, Closure], { String name, Closure c ->
Expand All @@ -172,7 +172,7 @@ abstract class BasePipelineTest {
helper.getLibLoader().loadImplicitLibraries()
helper.getLibLoader().loadLibrary(expression)
helper.setGlobalVars(binding)
return new LibClassLoader(helper,null)
return new LibClassLoader(helper, null)
})
helper.registerAllowedMethod("logRotator", [Map])
helper.registerAllowedMethod('mail', [Map])
Expand All @@ -183,8 +183,8 @@ abstract class BasePipelineTest {
helper.registerAllowedMethod("properties", [List])
helper.registerAllowedMethod("pwd", [], { 'workspaceDirMocked' })
helper.registerAllowedMethod("pwd", [Map], { 'tempDirMocked' })
helper.registerAllowedMethod('readFile', [Map], { args -> helper.readFile(args )})
helper.registerAllowedMethod('readFile', [String], { args -> helper.readFile(args )})
helper.registerAllowedMethod('readFile', [Map], { args -> helper.readFile(args) })
helper.registerAllowedMethod('readFile', [String], { args -> helper.readFile(args) })
helper.registerAllowedMethod("retry", [Integer, Closure], { Integer count, Closure c ->
def attempts = 0
while (attempts <= count) {
Expand All @@ -203,6 +203,7 @@ abstract class BasePipelineTest {
helper.registerAllowedMethod('sh', [String], { args -> helper.runSh(args) })
helper.registerAllowedMethod('sh', [Map], { args -> helper.runSh(args) })
helper.registerAllowedMethod('skipDefaultCheckout')
helper.registerAllowedMethod("skipStagesAfterUnstable")
helper.registerAllowedMethod('sleep')
helper.registerAllowedMethod('specific', [String])
helper.registerAllowedMethod('sshPublisher', [Map], {true})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.lesfurets.jenkins.unit

import com.sun.org.apache.xpath.internal.operations.Bool

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @nre-ableton !
Why this unused import com.sun.org.apache.xpath.internal.operations.Bool here?


import static com.lesfurets.jenkins.unit.MethodSignature.method

import java.lang.reflect.Method
Expand Down Expand Up @@ -108,6 +110,8 @@ class PipelineTestHelper {
/** Let scripts and library classes access global vars (env, currentBuild) */
protected Binding binding

Map<String, Boolean> mockFileExistsResults = [:]

Map<String, String> mockReadFileOutputs = [:]

/**
Expand Down Expand Up @@ -299,6 +303,7 @@ class PipelineTestHelper {
gse.setConfig(configuration)

mockScriptOutputs.clear()
mockFileExistsResults.clear()
mockReadFileOutputs.clear()
return this
}
Expand Down Expand Up @@ -568,6 +573,14 @@ class PipelineTestHelper {
}
}

void addFileExistsMock(String file, Boolean result) {
mockFileExistsResults[file] = result
}

Boolean fileExists(String arg) {
return mockFileExistsResults[arg] ?: false
}

void addReadFileMock(String file, String contents) {
mockReadFileOutputs[file] = contents
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,32 @@ class PipelineTestHelperTest {
}

@Test
void readFile() throws Exception {
void readFile() {
// given:
def helper = new PipelineTestHelper()
helper.addReadFileMock('test', 'contents')
helper.addFileExistsMock('test', true)

// when:
def output = helper.readFile('test')
def result = helper.fileExists('test')

// then:
Assertions.assertThat(output).isEqualTo('contents')
Assertions.assertThat(result).isTrue()
}

@Test
void readFileNotMocked() {
// given:
def helper = new PipelineTestHelper()

// when:
def result = helper.fileExists('test')

// then:
Assertions.assertThat(result).isFalse()
}

@Test
void readFileWithMap() throws Exception {
void readFileWithMap() {
// given:
def helper = new PipelineTestHelper()
helper.addReadFileMock('test', 'contents')
Expand All @@ -62,7 +74,7 @@ class PipelineTestHelperTest {
}

@Test
void readFileWithNoMockOutput() throws Exception {
void readFileWithNoMockOutput() {
// given:
def helper = new PipelineTestHelper()

Expand All @@ -74,7 +86,7 @@ class PipelineTestHelperTest {
}

@Test
void runSh() throws Exception {
void runSh() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock('pwd', '/foo/bar', 0)
Expand All @@ -87,7 +99,7 @@ class PipelineTestHelperTest {
}

@Test(expected = Exception)
void runShWithScriptFailure() throws Exception {
void runShWithScriptFailure() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock('evil', '/foo/bar', 666)
Expand All @@ -99,7 +111,7 @@ class PipelineTestHelperTest {
}

@Test
void runShWithStdout() throws Exception {
void runShWithStdout() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock('pwd', '/foo/bar', 0)
Expand All @@ -112,7 +124,7 @@ class PipelineTestHelperTest {
}

@Test
void runShWithReturnCode() throws Exception {
void runShWithReturnCode() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock('pwd', '/foo/bar', 0)
Expand All @@ -125,7 +137,7 @@ class PipelineTestHelperTest {
}

@Test
void runShWithNonZeroReturnCode() throws Exception {
void runShWithNonZeroReturnCode() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock('evil', '/foo/bar', 666)
Expand All @@ -138,7 +150,7 @@ class PipelineTestHelperTest {
}

@Test
void runShWithCallback() throws Exception {
void runShWithCallback() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock('pwd') { script ->
Expand All @@ -153,7 +165,7 @@ class PipelineTestHelperTest {
}

@Test(expected = Exception)
void runShWithCallbackScriptFailure() throws Exception {
void runShWithCallbackScriptFailure() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock('evil') { script ->
Expand All @@ -167,7 +179,7 @@ class PipelineTestHelperTest {
}

@Test
void runShWithCallbackStdout() throws Exception {
void runShWithCallbackStdout() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock('pwd') { script ->
Expand All @@ -182,7 +194,7 @@ class PipelineTestHelperTest {
}

@Test
void runShWithCallbackReturnCode() throws Exception {
void runShWithCallbackReturnCode() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock('pwd') { script ->
Expand All @@ -197,7 +209,7 @@ class PipelineTestHelperTest {
}

@Test
void runShWithCallbackNonZeroReturnCode() throws Exception {
void runShWithCallbackNonZeroReturnCode() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock('pwd') { script ->
Expand All @@ -212,7 +224,7 @@ class PipelineTestHelperTest {
}

@Test(expected = IllegalArgumentException)
void runShWithCallbackOutputNotMap() throws Exception {
void runShWithCallbackOutputNotMap() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock('pwd') { script ->
Expand All @@ -226,7 +238,7 @@ class PipelineTestHelperTest {
}

@Test(expected = IllegalArgumentException)
void runShWithCallbackNoStdoutKey() throws Exception {
void runShWithCallbackNoStdoutKey() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock('pwd') { script ->
Expand All @@ -240,7 +252,7 @@ class PipelineTestHelperTest {
}

@Test(expected = IllegalArgumentException)
void runShWithCallbackNoExitValueKey() throws Exception {
void runShWithCallbackNoExitValueKey() {
// given:
def helper = new PipelineTestHelper()
helper.addShMock('pwd') { script ->
Expand All @@ -254,7 +266,7 @@ class PipelineTestHelperTest {
}

@Test()
void runShWithoutMockOutput() throws Exception {
void runShWithoutMockOutput() {
// given:
def helper = new PipelineTestHelper()

Expand All @@ -266,7 +278,7 @@ class PipelineTestHelperTest {
}

@Test()
void runShWithoutMockOutputAndReturnStatus() throws Exception {
void runShWithoutMockOutputAndReturnStatus() {
// given:
def helper = new PipelineTestHelper()

Expand All @@ -278,7 +290,7 @@ class PipelineTestHelperTest {
}

@Test()
void runShWithoutMockOutputForGitRevParse() throws Exception {
void runShWithoutMockOutputForGitRevParse() {
// given:
def helper = new PipelineTestHelper()

Expand All @@ -290,7 +302,7 @@ class PipelineTestHelperTest {
}

@Test(expected = IllegalArgumentException)
void runShWithBothStatusAndStdout() throws Exception {
void runShWithBothStatusAndStdout() {
// given:
def helper = new PipelineTestHelper()

Expand Down