Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #817 Allow specifying global extension order #818

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@ -27,17 +27,32 @@
* Maintains a registry of global Spock extensions and their configuration objects,
* which can be used to configure other extensions.
*
* <p>If an extension is specified in two or more {@code IGlobalExtension} files on the classpath, it will only
* be processed for the first time it is discovered.
* This allows processing other extensions (e.g. Spring) before custom ones by listing the order of extensions
* explicitly in the file.
*
* <p>
* For instance this will load and run {@code SpringExtension} before {@code MyExtension}:
* <pre>{@code
* org.spockframework.spring.SpringExtension
* com.example.MyExtension}</pre>
* </p>
*
*
* @author Peter Niederwieser
*/
public class GlobalExtensionRegistry implements IExtensionRegistry, IConfigurationRegistry {
private final List<Class<?>> globalExtensionClasses;
private final Set<Class<?>> globalExtensionClasses;
private final Map<Class<?>, Object> configurationsByType = new HashMap<>();
private final Map<String, Object> configurationsByName = new HashMap<>();

private final List<IGlobalExtension> globalExtensions = new ArrayList<>();
private final Set<IGlobalExtension> globalExtensions = new LinkedHashSet<>();

GlobalExtensionRegistry(List<Class<?>> globalExtensionClasses, List<?> initialConfigurations) {
this.globalExtensionClasses = globalExtensionClasses;
// Only add an extension the first time it is encountered, i.e
// turns [A, A, B, A,] into [A,B]
this.globalExtensionClasses = new LinkedHashSet<>(globalExtensionClasses);
initializeConfigurations(initialConfigurations);
}

Expand Down Expand Up @@ -72,7 +87,7 @@ public Object getConfigurationByName(String name) {
}

@Override
public List<IGlobalExtension> getGlobalExtensions() {
public Set<IGlobalExtension> getGlobalExtensions() {
return globalExtensions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import org.spockframework.runtime.extension.IGlobalExtension;

import java.util.List;
import java.util.Set;

public interface IExtensionRegistry {
List<IGlobalExtension> getGlobalExtensions();
Set<IGlobalExtension> getGlobalExtensions();
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ class GlobalExtensionRegistrySpec extends Specification {
e.message.contains("unknown configuration class")
}

// See https://github.com/spockframework/spock/issues/817
def "Extensions discovered later in the classpath are ignored if already processed earlier"() {
when:
def registry = new GlobalExtensionRegistry(extensionClasses, [])
registry.initializeGlobalExtensions()

then:
registry.globalExtensions*.class == expectedExtensionClasses

where:
// Ignore subsequent additions, even if they appear later in the list
extensionClasses || expectedExtensionClasses
[MyExtension, MyExtension] || [MyExtension]
[SpringExtension, MyExtension, SpringExtension] || [SpringExtension, MyExtension]
[SpringExtension, SpringExtension, MyExtension, SpringExtension] || [SpringExtension, MyExtension]
}

static class SpringExtension extends AbstractGlobalExtension {

}

static class MyExtension extends AbstractGlobalExtension {
static instantiated = false

Expand Down