Skip to content

Commit

Permalink
Add fileExists mock
Browse files Browse the repository at this point in the history
This mock is similar to the readFile mock in that mock files can be
added to the PipelineTestHelper. If no filename is registered with the
helper, then the mock returns false by default.
  • Loading branch information
nre-ableton committed Oct 17, 2020
1 parent 88d0c72 commit 4279125
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
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)
helper.addReadFileMock('output', 'FAILED!!!')
runScript("Jenkinsfile")
assertJobStatusFailure()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,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 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

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 @@ -39,13 +39,25 @@ class PipelineTestHelperTest {
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
Expand Down

0 comments on commit 4279125

Please sign in to comment.