Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import au.com.origin.snapshots.SnapshotVerifier
import au.com.origin.snapshots.config.PropertyResolvingSnapshotConfig
import au.com.origin.snapshots.config.SnapshotConfig
import au.com.origin.snapshots.config.SnapshotConfigInjector
import groovy.transform.CompileStatic
import org.spockframework.runtime.extension.AbstractAnnotationDrivenExtension
import org.spockframework.runtime.model.FeatureInfo
import org.spockframework.runtime.model.SpecInfo

@CompileStatic
class SnapshotExtension extends AbstractAnnotationDrivenExtension<EnableSnapshots> implements SnapshotConfigInjector {

SnapshotVerifier snapshotVerifier;
Expand All @@ -17,12 +20,14 @@ class SnapshotExtension extends AbstractAnnotationDrivenExtension<EnableSnapshot

void visitSpec(SpecInfo spec) {
def snapshotMethodInterceptor = new SnapshotMethodInterceptor(snapshotVerifier)
spec.allFeatures.featureMethod*.addInterceptor(snapshotMethodInterceptor)
for (FeatureInfo feature : spec.allFeatures) {
feature.featureMethod.addInterceptor(snapshotMethodInterceptor)
}
spec.addCleanupSpecInterceptor(snapshotMethodInterceptor)
}

@Override
SnapshotConfig getSnapshotConfig() {
return new PropertyResolvingSnapshotConfig()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import au.com.origin.snapshots.Expect
import au.com.origin.snapshots.SnapshotVerifier
import au.com.origin.snapshots.logging.LoggingHelper
import au.com.origin.snapshots.utils.ReflectionUtils
import groovy.transform.CompileStatic
import java.lang.reflect.Field
import java.lang.reflect.Method
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.spockframework.runtime.extension.AbstractMethodInterceptor
import org.spockframework.runtime.extension.IMethodInvocation

// Based on this issue: https://github.com/spockframework/spock/issues/652
@CompileStatic
class SnapshotMethodInterceptor extends AbstractMethodInterceptor {
private log = LoggerFactory.getLogger( SnapshotMethodInterceptor.class )
private final Logger log = LoggerFactory.getLogger( SnapshotMethodInterceptor.class )
private final SnapshotVerifier snapshotVerifier;

SnapshotMethodInterceptor(SnapshotVerifier snapshotVerifier) {
Expand All @@ -28,8 +32,9 @@ class SnapshotMethodInterceptor extends AbstractMethodInterceptor {
System.arraycopy invocation.arguments, 0, newArguments, 0, invocation.arguments.length
invocation.arguments = newArguments
}
invocation.method.reflection.parameterTypes.eachWithIndex { type, i ->
if (Expect.class == type) {
def parameterTypes = invocation.method.reflection.parameterTypes
for (int i = 0; i < parameterTypes.length; i++) {
if (Expect.class == parameterTypes[i]) {
LoggingHelper.deprecatedV5(log, "Injecting 'Expect' via method a argument is no longer recommended. Consider using instance variable injection instead.")
invocation.arguments[i] = new Expect(snapshotVerifier, invocation.feature.featureMethod.reflection)
}
Expand All @@ -38,8 +43,8 @@ class SnapshotMethodInterceptor extends AbstractMethodInterceptor {
}

private void updateInstanceVariable(Object testInstance, Method testMethod) {
ReflectionUtils.findFieldByPredicate(testInstance.class, { field -> field.getType() == Expect.class })
.ifPresent({ field ->
ReflectionUtils.findFieldByPredicate(testInstance.class, { Field field -> field.getType() == Expect.class })
.ifPresent({ Field field ->
Expect expect = Expect.of(snapshotVerifier, testMethod);
ReflectionUtils.makeAccessible(field);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package au.com.origin.snapshots.spock

import spock.lang.Specification

class GroovyRuntimeCompatibilitySpec extends Specification {

private static final String REMOVED_IN_GROOVY_6 = 'org/codehaus/groovy/runtime/callsite/CallSiteArray'

def "extension classes do not reference the call site cache that Groovy 6 removed"() {
given:
def classesRoot = new File(SnapshotExtension.protectionDomain.codeSource.location.toURI())
def extensionClasses = []
classesRoot.eachFileRecurse { file ->
if (file.name.endsWith('.class')) {
extensionClasses << file
}
}

expect:
!extensionClasses.empty
extensionClasses.findAll { new String(it.bytes, 'ISO-8859-1').contains(REMOVED_IN_GROOVY_6) }.empty
}
}
Loading