Skip to content

Commit 63c06e5

Browse files
author
Alan Christie
committed
- Work on 'creates' feature
1 parent ec0395f commit 63c06e5

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

src/groovy/PipelineTester.groovy

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ class Tester {
8484
// i.e. 'arg.volumes' becomes 'volumes' (also contains expanded ranges)
8585
List<String> optionNames = []
8686
def optionDefaults = [:]
87+
// A list of created and uncreated files
88+
// (set if the user's used the corresponding blocks in the test)
89+
List<String> creates = []
90+
List<String> doesNotCreate = []
8791

8892
/**
8993
* The run method.
@@ -102,11 +106,14 @@ class Tester {
102106
for (String path : testFiles) {
103107

104108
// Reset filename and section number
109+
// along with other test-specific objects
105110
currentTestFilename = path.split(File.separator)[-1]
106111
currentTestFilename = currentTestFilename.
107112
take(currentTestFilename.length() - testExt.length())
108113
sectionNumber = 0
109114
testScriptVersion = 0
115+
creates = []
116+
doesNotCreate []
110117

111118
// We must not have duplicate test files -
112119
// this indicates there are pipelines in different projects
@@ -515,6 +522,12 @@ class Tester {
515522
* - see
516523
* An optional list of regular expressions executed against
517524
* the pipeline log.
525+
*
526+
* - creates
527+
* An optional list of file names (regular expressions).
528+
*
529+
* - does_not_create
530+
* An optional list of file names (regular expressions).
518531
**/
519532
def processTest(filename, section) {
520533

@@ -524,10 +537,13 @@ class Tester {
524537
logTest(filename, section)
525538

526539
def command = section.value['command']
527-
def params_block = section.value['params']
528-
def see_block = section.value['see']
540+
def paramsBlock = section.value['params']
541+
def seeBlock = section.value['see']
542+
def createsBlock = section.value['creates']
543+
def doesNotCreateBlock = section.value['does_not_create']
544+
529545
// Enforce conditions on block combinations...
530-
if (command != null && params_block != null) {
546+
if (command != null && paramsBlock != null) {
531547
err('Found "command" and "params". Use one or the other.')
532548
recordFailedTest(section.key)
533549
return
@@ -539,7 +555,7 @@ class Tester {
539555
String pipelineCommand
540556
if (command == null) {
541557

542-
if (!checkAllOptionsHaveBeenUsed(params_block)) {
558+
if (!checkAllOptionsHaveBeenUsed(paramsBlock)) {
543559
recordFailedTest(section.key)
544560
return
545561
}
@@ -548,7 +564,7 @@ class Tester {
548564
String the_command = currentServiceDescriptor.command
549565

550566
// Replace the respective values in the command string...
551-
pipelineCommand = expandTemplate(the_command, params_block)
567+
pipelineCommand = expandTemplate(the_command, paramsBlock)
552568
// Replace newlines with '\n'
553569
pipelineCommand = pipelineCommand.replace(System.lineSeparator(), '\n')
554570

@@ -626,18 +642,20 @@ class Tester {
626642
// Here we look for things like "output*" in the
627643
// redirected output path.
628644
if (testOutputPath != null) {
629-
def outputFiles =
630-
new FileNameFinder().getFileNames(testOutputPath.toString(),
631-
"${outputFileBaseName}*")
645+
def outputFiles = new FileNameFinder().
646+
getFileNames(testOutputPath.toString(), "*")
647+
println "++++++ " + outputFiles
632648
if (outputFiles.size() == 0) {
633-
err("Expected output files '$testOutputFile' but got nothing")
634-
validated = false
635-
}
649+
err("Expected output files '$testOutputFile' but got nothing")
650+
validated = false
651+
}
636652
}
637653

638-
if (validated && see_block != null) {
654+
// Has the user asked us to check text that has been logged?
655+
if (validated && seeBlock != null) {
656+
639657
// Check that we see everything the test tells us to see.
640-
see_block.each { see ->
658+
seeBlock.each { see ->
641659
// Replace spaces in the 'see' string
642660
// with a simple _variable whitespace_ regex (excluding
643661
// line-breaks and form-feeds).
@@ -653,6 +671,7 @@ class Tester {
653671
validated = false
654672
}
655673
}
674+
656675
}
657676

658677
} else {

src/groovy/pipeline.test.template

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,19 @@
9090
// as the regular expression '[ \t]+', absorbing any non-line-breaking
9191
// gaps. You can also use regular expressions.
9292

93-
see: [ 'Computation = 4.5673' ]
93+
see: [ 'Computation = 4.5673' ],
94+
95+
// Files created and not created.
96+
//
97+
// If your pipeline creates files you can and should declare
98+
// their names in a `creates` block. The test utility will make
99+
// sure they exist when your pipeline completes (successfully).
100+
// Additionally, if you are not expecting certain files for the test
101+
// you can also name them. Here we expect to see the file 'output.png'
102+
// but not the file 'output.txt'
103+
104+
creates: [ 'output.png' ],
105+
does_not_create: [ 'output.txt' ]
94106

95107
],
96108

0 commit comments

Comments
 (0)