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 c58280d
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 14 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
@@ -1,5 +1,7 @@
package com.adarshr.gradle.testlogger.functional

import java.security.Permission

import static org.gradle.testkit.runner.TaskOutcome.FAILED
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS

Expand Down Expand Up @@ -161,19 +163,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 +348,74 @@ class TestLoggerPluginSpec extends AbstractFunctionalSpec {
result.task(":test").outcome == SUCCESS
}

def "show standard streams from before System exit was called from setupSpec"() {
setup:
def originalSecurityManager = overrideSecurityManager()
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
cleanup:
restoreSecurityManager(originalSecurityManager)
}

def "show standard streams from before System exit was called from setup"() {
setup:
def originalSecurityManager = overrideSecurityManager()
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
cleanup:
restoreSecurityManager(originalSecurityManager)
}

def "hide passed tests"() {
when:
def result = run(
Expand Down Expand Up @@ -692,4 +762,30 @@ class TestLoggerPluginSpec extends AbstractFunctionalSpec {
and:
result.task(':anotherTask').outcome == SUCCESS
}

private static SecurityManager overrideSecurityManager() {
def originalSecurityManager = System.securityManager
// See https://bit.ly/2XXN0xU
System.securityManager = new SecurityManager() {

@Override
void checkPermission(Permission perm) {
}

@Override
void checkPermission(Permission perm, Object context) {
}

@Override
void checkExit(int status) {
throw new IllegalStateException("System exit requested with error $status")
}
}

originalSecurityManager
}

private static void restoreSecurityManager(SecurityManager originalSecurityManager) {
System.securityManager = originalSecurityManager
}
}
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,73 @@
package com.adarshr.test

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

import java.security.Permission

@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"

System.securityManager = new SecurityManager() {

@Override
void checkPermission(Permission perm) {
}

@Override
void checkPermission(Permission perm, Object context) {
}

@Override
void checkExit(int status) {
// throw new IllegalStateException("System exit requested with error $status")
}
}
}

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,73 @@
package com.adarshr.test

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

import java.security.Permission

@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.securityManager = new SecurityManager() {

@Override
void checkPermission(Permission perm) {
}

@Override
void checkPermission(Permission perm, Object context) {
}

@Override
void checkExit(int status) {
// throw new IllegalStateException("System exit requested with error $status")
}
}

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 c58280d

Please sign in to comment.