Skip to content

Commit

Permalink
Show output from before System.exit(5) was called
Browse files Browse the repository at this point in the history
  • Loading branch information
radarsh committed Mar 13, 2019
1 parent 3433241 commit 8e1bbf4
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.adarshr.gradle.testlogger.util.TimeUtils
import groovy.transform.CompileStatic
import org.gradle.api.tasks.testing.TestResult

import static org.gradle.api.tasks.testing.TestResult.ResultType.SKIPPED

@CompileStatic
class TestResultWrapper {

Expand All @@ -18,7 +20,7 @@ class TestResultWrapper {

boolean isLoggable() {
testLoggerExtension.showPassed && testResult.successfulTestCount ||
testLoggerExtension.showSkipped && testResult.skippedTestCount ||
testLoggerExtension.showSkipped && (testResult.resultType == SKIPPED || testResult.skippedTestCount) ||
testLoggerExtension.showFailed && testResult.failedTestCount
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification

import static java.lang.System.lineSeparator
import static org.apache.commons.io.FileUtils.copyDirectoryToDirectory

@SuppressWarnings('GrMethodMayBeStatic')
Expand All @@ -28,10 +29,12 @@ abstract class AbstractFunctionalSpec extends Specification {
private static final def SUITE_MARKER_REGEX = $/${SUITE_MARKER}(.*)__/$
private static final def TEST_MARKER_REGEX = $/${TEST_MARKER}(.*)__/$

private static final def FILTER_PATTERN = $/(?ms)${lineSeparator()}Unexpected exception thrown.*> Task :test FAILED${lineSeparator()}/$

private AnsiTextRenderer ansi = new AnsiTextRenderer()

protected TestLoggerOutput getLoggerOutput(String text) {
def allLines = text.readLines()
def allLines = text.replaceAll(FILTER_PATTERN, '').readLines()
def lines = allLines
.subList(allLines.indexOf(START_MARKER) + 1, allLines.indexOf(SUMMARY_MARKER))
.findAll { !it.startsWith(TEST_MARKER) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,19 @@ class TestLoggerPluginSpec extends AbstractFunctionalSpec {
and:
lines.size() == 14
lines[0] == render('')
lines[1] == render('[erase-ahead,bold]com.adarshr.test.NestedTest$NestedTestsetOne[/]')
lines[1] == render('[erase-ahead,bold]com.adarshr.test.NestedTest$NestedTestsetThree[/]')
lines[2] == render('')
lines[3] == render('[erase-ahead,bold] Test [bold-off]secondTestOfNestedTestsetOne()[green] PASSED[/]')
lines[4] == render('[erase-ahead,bold] Test [bold-off]firstTestOfNestedTestsetOne()[green] PASSED[/]')
lines[5] == render('')
lines[6] == render('[erase-ahead,bold]com.adarshr.test.NestedTest$NestedTestsetThree[/]')
lines[7] == render('')
lines[8] == render('[erase-ahead,bold] Test [bold-off]firstTestOfNestedTestsetThree()[green] PASSED[/]')
lines[3] == render('[erase-ahead,bold] Test [bold-off]firstTestOfNestedTestsetThree()[green] PASSED[/]')
lines[4] == render('')
lines[5] == render('[erase-ahead,bold]com.adarshr.test.NestedTest$NestedTestsetTwo[/]')
lines[6] == render('')
lines[7] == render('[erase-ahead,bold] Test [bold-off]secondTestOfNestedTestsetTwo()[green] PASSED[/]')
lines[8] == render('[erase-ahead,bold] Test [bold-off]firstTestOfNestedTestsetTwo()[green] PASSED[/]')
lines[9] == render('')
lines[10] == render('[erase-ahead,bold]com.adarshr.test.NestedTest$NestedTestsetTwo[/]')
lines[10] == render('[erase-ahead,bold]com.adarshr.test.NestedTest$NestedTestsetOne[/]')
lines[11] == render('')
lines[12] == render('[erase-ahead,bold] Test [bold-off]secondTestOfNestedTestsetTwo()[green] PASSED[/]')
lines[13] == render('[erase-ahead,bold] Test [bold-off]firstTestOfNestedTestsetTwo()[green] PASSED[/]')
lines[12] == render('[erase-ahead,bold] Test [bold-off]secondTestOfNestedTestsetOne()[green] PASSED[/]')
lines[13] == render('[erase-ahead,bold] Test [bold-off]firstTestOfNestedTestsetOne()[green] PASSED[/]')
and:
result.task(":test").outcome == SUCCESS
}
Expand Down Expand Up @@ -346,6 +346,66 @@ class TestLoggerPluginSpec extends AbstractFunctionalSpec {
result.task(":test").outcome == SUCCESS
}

def "show standard streams from before System exit was called from setupSpec"() {
when:
def result = run(
'sample-spock-tests-system-exit',
'''
testlogger {
showStandardStreams true
}
''',
'clean test --tests *SecondSpec'
)
then:
def lines = getLoggerOutput(result.output).lines
and:
lines.size() == 4
lines[0] == render('[default]')
lines[1] == render(' SecondSpec - stdout setupSpec')
lines[2] == render(' SecondSpec - stderr setupSpec[/]')
lines[3] == render('')
and:
result.task(":test").outcome == FAILED
}

def "show standard streams from before System exit was called from setup"() {
when:
def result = run(
'sample-spock-tests-system-exit',
'''
testlogger {
showStandardStreams true
}
''',
'clean test --tests *FirstSpec'
)
then:
def output = getLoggerOutput(result.output)
def lines = output.lines
def summary = output.summary
and:
lines.size() == 12
lines[0] == render('[default]')
lines[1] == render(' FirstSpec - stdout setupSpec')
lines[2] == render(' FirstSpec - stderr setupSpec[/]')
lines[3] == render('')
lines[4] == render('')
lines[5] == render('[erase-ahead,bold]com.adarshr.test.FirstSpec[/]')
lines[6] == render('')
lines[7] == render('[erase-ahead,bold] Test [bold-off]this test should pass[yellow] SKIPPED[/]')
lines[8] == render('[default]')
lines[9] == render(' FirstSpec - this test should pass - stdout setup')
lines[10] == render(' FirstSpec - this test should pass - stderr setup[/]')
and:
summary[0] == render('')
summary[1].startsWith render('[erase-ahead,bold,green]SUCCESS: [default]Executed 1 tests in')
summary[1].endsWith render('(1 skipped)[/]')
summary[2] == render('')
and:
result.task(":test").outcome == FAILED
}

def "hide passed tests"() {
when:
def result = run(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
id 'groovy'
id 'com.adarshr.test-logger'
}

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
testCompile 'org.codehaus.groovy:groovy-all:2.4.12'
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.adarshr.test

import spock.lang.Ignore
import spock.lang.Narrative
import spock.lang.Specification
import spock.lang.Stepwise

@Stepwise
@Narrative('Test that calls System.exit from setup method')
class FirstSpec extends Specification {

def setupSpec() {
println "${getClass().simpleName} - stdout setupSpec"
System.err.println "${getClass().simpleName} - stderr setupSpec"
}

def cleanupSpec() {
println "${getClass().simpleName} - stdout cleanupSpec"
System.err.println "${getClass().simpleName} - stderr cleanupSpec"
}

def setup() {
println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stdout setup"
System.err.println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stderr setup"

System.exit(5)
}

def cleanup() {
println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stdout cleanup"
System.err.println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stderr cleanup"
}

def "this test should pass"() {
expect:
println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stdout expect"
System.err.println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stderr expect"
1 == 1
}

def "this test should fail"() {
expect:
println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stdout expect"
System.err.println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stderr expect"
1 == 2
}

@Ignore
def "this test should be skipped"() {
expect:
println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stdout expect"
System.err.println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stderr expect"
1 == 2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.adarshr.test

import spock.lang.Ignore
import spock.lang.Narrative
import spock.lang.Specification
import spock.lang.Stepwise

@Stepwise
@Narrative('Test that calls System.exit from setupSpec method')
class SecondSpec extends Specification {

def setupSpec() {
println "${getClass().simpleName} - stdout setupSpec"
System.err.println "${getClass().simpleName} - stderr setupSpec"

System.exit(5)
}

def cleanupSpec() {
println "${getClass().simpleName} - stdout cleanupSpec"
System.err.println "${getClass().simpleName} - stderr cleanupSpec"
}

def setup() {
println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stdout setup"
System.err.println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stderr setup"
}

def cleanup() {
println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stdout cleanup"
System.err.println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stderr cleanup"
}

def "this test should pass"() {
expect:
println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stdout expect"
System.err.println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stderr expect"
1 == 1
}

def "this test should fail"() {
expect:
println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stdout expect"
System.err.println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stderr expect"
1 == 2
}

@Ignore
def "this test should be skipped"() {
expect:
println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stdout expect"
System.err.println "${getClass().simpleName} - ${specificationContext.currentFeature.name} - stderr expect"
1 == 2
}
}
6 changes: 3 additions & 3 deletions src/test-functional/resources/test-marker.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ tasks.withType(Test) {
boolean started = false
def suites = []

beforeTest { suite ->
if (suite.className.startsWith('com.adarshr') && !started) {
beforeSuite { suite ->
if (suite.className?.startsWith('com.adarshr') && !started) {
started = true
logger.lifecycle '__START__'
}
if (suite.className.startsWith('com.adarshr') && !suites.contains(suite.className)) {
if (suite.className?.startsWith('com.adarshr') && !suites.contains(suite.className)) {
logger.lifecycle "__SUITE=${suite.className}__"
suites << suite.className
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class TestResultWrapperSpec extends Specification {
testLoggerExtensionMock.showPassed >> showPassed
testLoggerExtensionMock.showSkipped >> showSkipped
testLoggerExtensionMock.showFailed >> showFailed
and:
testResultMock.resultType >> resultType
testResultMock.testCount >> totalCount
testResultMock.successfulTestCount >> successfulCount
testResultMock.skippedTestCount >> skippedCount
Expand All @@ -28,6 +30,7 @@ class TestResultWrapperSpec extends Specification {
resultType | totalCount | successfulCount | skippedCount | failedCount | showPassed | showSkipped | showFailed
SUCCESS | 1 | 1 | 0 | 0 | true | false | false
SKIPPED | 1 | 0 | 1 | 0 | false | true | false
SKIPPED | 0 | 0 | 0 | 0 | false | true | false
FAILURE | 1 | 0 | 0 | 1 | false | false | true
}

Expand Down

0 comments on commit 8e1bbf4

Please sign in to comment.