Skip to content

Commit

Permalink
Apply review suggestions (#26)
Browse files Browse the repository at this point in the history
- improve descriptions for spock tests
- improve arguments trimming in the getTestStepName function
  • Loading branch information
ErikRehmTT committed Oct 26, 2023
1 parent 18a5984 commit 07941f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
42 changes: 21 additions & 21 deletions test/vars/Pipeline2ATXTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Pipeline2ATXTest extends PipelineSpockTestBase {
}

def 'Create json report'() {
given: 'needed arguments'
given: 'all needed information to create a report'
def build = GroovyMock(Run)
def parent = Mock(Project)
parent.getDisplayName() >> 'UnitTests'
Expand All @@ -64,10 +64,10 @@ class Pipeline2ATXTest extends PipelineSpockTestBase {
helper.registerAllowedMethod('getCurrentResult', [Object], {'SUCCESS'})
pipeline2ATX = loadScript(scriptName)

when: 'get json from input'
when: 'json string is generated'
String result = pipeline2ATX.generateJsonReport(build, attributes, teststeps, logfile)

then: 'expect to find the values in the json'
then: 'expect to find the values in the json string'
result.contains('"name": "JenkinsPipeline"')
result.contains('"@type": "testcase"')
result.contains('"name": "UnitTests"') // test testcase name
Expand All @@ -87,7 +87,7 @@ class Pipeline2ATXTest extends PipelineSpockTestBase {
}

def 'Get current build result'() {
given: 'a build'
given: 'a build that will have FAILED result when its in progress and SUCCESS when already finished'
def build = Mock(Build)
build.isInProgress() >> inProgress
build.getResult() >> Result.SUCCESS
Expand Down Expand Up @@ -137,7 +137,7 @@ class Pipeline2ATXTest extends PipelineSpockTestBase {
}

def 'Get execution steps'() {
given:
given: 'a list of rows with different display names'
def build = GroovyMock(WorkflowRun)
def rows = []
['stage', 'stage', 'nostage', 'parallel','stage','test'].each {name ->
Expand All @@ -152,10 +152,10 @@ class Pipeline2ATXTest extends PipelineSpockTestBase {
})
pipeline2ATX = loadScript(scriptName)

when:
when: 'list of rows is evaluated'
def result = pipeline2ATX.getExecutionSteps(build, appendLogs)

then:
then: 'result has the size of the number of `stage` rows that return a nonempty crawl result'
result.size() == expectedSize

where:
Expand All @@ -165,7 +165,7 @@ class Pipeline2ATXTest extends PipelineSpockTestBase {
}

def 'Crawl a row with direct match'() {
given:
given: 'A row with a specific node type and display name'
def row = Mock(FlowGraphTable.Row)
row.getNode() >> node
row.getDisplayName() >> name
Expand All @@ -178,24 +178,24 @@ class Pipeline2ATXTest extends PipelineSpockTestBase {
})
pipeline2ATX = loadScript(scriptName)

when:
when: 'row is crawled'
def result = pipeline2ATX.crawlRows(row, false, insideStage)

then:
then: 'the correct atx item is returned'
result.size() == 2
result['name'] == expectedName
result['@type'] == expectedType

where:
node | name | insideStage | expectedName | expectedType
Mock(AtomNode) | "stage" | true | "creating stage" | "teststep"
Mock(BlockStartNode) | "stage" | true | "creating stage" | "teststep"
Mock(AtomNode) | "parallel" | true | "parallel" | "teststep"
Mock(BlockStartNode) | "stage" | false | "stage" | "teststepfolder"
node | name | insideStage | expectedName | expectedType
Mock(AtomNode) | "stage" | true | "creating stage" | "teststep"
Mock(BlockStartNode) | "stage" | true | "creating stage" | "teststep"
Mock(AtomNode) | "parallel" | true | "parallel" | "teststep"
Mock(BlockStartNode) | "stage" | false | "stage" | "teststepfolder"
}

def 'Crawl a row - edge cases'() {
given:
given: 'A undefined row with or without a crawlable child row'
def outerRow = new FlowGraphTable.Row(Mock(FlowNode))
def innerRow = Mock(FlowGraphTable.Row)
innerRow.getNode() >> Mock(AtomNode)
Expand All @@ -210,10 +210,10 @@ class Pipeline2ATXTest extends PipelineSpockTestBase {
})
pipeline2ATX = loadScript(scriptName)

when:
when: 'row is crawled'
def result = pipeline2ATX.crawlRows(outerRow, false, false)

then:
then: 'crawl result of the child row or an empty result is returned'
result.size() == expectedSize
if (expectedSize > 0) {
result['name'] == 'bat'
Expand Down Expand Up @@ -247,7 +247,7 @@ class Pipeline2ATXTest extends PipelineSpockTestBase {
}

def 'Get TestStep Name from row'() {
given: 'A row opionally with arguments'
given: 'A row optionally with arguments'
def node = Mock(AtomNode)
def row = new FlowGraphTable.Row(node)
node.getDisplayFunctionName() >> 'testnode'
Expand All @@ -259,12 +259,13 @@ class Pipeline2ATXTest extends PipelineSpockTestBase {

then: 'result is the expected string'
result == expectedResult

where:
arguments | expectedResult
'' | 'testnode'
'short' | 'testnode (short)'
'x' * 150 | 'testnode (' + 'x' * 106 + '...)'
' ' * 10 + '\t' * 10 + '\n' * 10 + 'stripped ' | 'testnode ( stripped )'
' ' * 10 + '\t' * 10 + '\n' * 10 + 'stripped ' | 'testnode (stripped)'
}

def 'Create test step item from row'() {
Expand Down Expand Up @@ -297,7 +298,6 @@ class Pipeline2ATXTest extends PipelineSpockTestBase {
}

def 'Create test step folder from row'() {
// TODO: Add child test blocked by https://github.com/jenkinsci/JenkinsPipelineUnit/issues/337
given: 'a row with node'
def outerNode = Mock(BlockStartNode)
def outerRow = new FlowGraphTable.Row(outerNode)
Expand Down
12 changes: 6 additions & 6 deletions vars/pipeline2ATX.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,12 @@ def getTestStepName(row) {

def arguments = ArgumentsAction.getStepArgumentsAsString(row.getNode())
if (arguments) {
// trim arguments, as it could be a long script
arguments = arguments.replaceAll("[\\n\\r\\t]", " ").replaceAll("\\s{2,}", " ")
if (arguments.length() > (allowedSchemaMaxNameLength - name.length() - 3)) {
arguments = arguments.take(allowedSchemaMaxNameLength - name.length() - 6) + "..."
}
name = name + " ("+arguments+")"
arguments = arguments.replaceAll(/\s+/, ' ').trim()

def maxArgumentLength = allowedSchemaMaxNameLength - name.length() - 6
arguments = arguments.length() > maxArgumentLength ? arguments.take(maxArgumentLength) + "..." : arguments

name = "$name ($arguments)"
}

return name
Expand Down

0 comments on commit 07941f3

Please sign in to comment.