Skip to content

Commit 2afa02c

Browse files
committed
Fix missing test slice configuration
Test slices are now longer registered in spring.factories, but TestSliceMetadata still looked there for them to generate documentation. Fixed this so that TestSliceMetadata now looks in the right places for test slices. See gh-29873
1 parent 940558c commit 2afa02c

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

buildSrc/src/main/java/org/springframework/boot/build/test/autoconfigure/TestSliceMetadata.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,11 +21,14 @@
2121
import java.io.FileWriter;
2222
import java.io.IOException;
2323
import java.io.Reader;
24+
import java.io.UncheckedIOException;
2425
import java.net.MalformedURLException;
2526
import java.net.URL;
2627
import java.net.URLClassLoader;
2728
import java.nio.file.Files;
2829
import java.nio.file.Path;
30+
import java.util.ArrayList;
31+
import java.util.List;
2932
import java.util.Properties;
3033
import java.util.SortedSet;
3134
import java.util.TreeSet;
@@ -102,13 +105,54 @@ private Properties readTestSlices() throws IOException {
102105
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(classLoader);
103106
Properties springFactories = readSpringFactories(
104107
new File(this.sourceSet.getOutput().getResourcesDir(), "META-INF/spring.factories"));
108+
readTestSlicesDirectory(springFactories,
109+
new File(this.sourceSet.getOutput().getResourcesDir(), "META-INF/spring-boot/"));
105110
for (File classesDir : this.sourceSet.getOutput().getClassesDirs()) {
106111
addTestSlices(testSlices, classesDir, metadataReaderFactory, springFactories);
107112
}
108113
}
109114
return testSlices;
110115
}
111116

117+
/**
118+
* Reads files from the given directory and puts them in springFactories. The key is
119+
* the file name, the value is the file contents, split by line, delimited with comma.
120+
*
121+
* This is done to mimic the spring.factories structure.
122+
* @param springFactories spring.factories parsed as properties
123+
* @param directory directory to scan
124+
*/
125+
private void readTestSlicesDirectory(Properties springFactories, File directory) {
126+
File[] files = directory.listFiles();
127+
if (files == null) {
128+
return;
129+
}
130+
for (File file : files) {
131+
try {
132+
List<String> lines = removeComments(Files.readAllLines(file.toPath()));
133+
springFactories.setProperty(file.getName(), StringUtils.collectionToCommaDelimitedString(lines));
134+
}
135+
catch (IOException ex) {
136+
throw new UncheckedIOException("Failed to read file " + file, ex);
137+
}
138+
}
139+
}
140+
141+
private List<String> removeComments(List<String> lines) {
142+
List<String> result = new ArrayList<>();
143+
for (String line : lines) {
144+
int commentIndex = line.indexOf('#');
145+
if (commentIndex > -1) {
146+
line = line.substring(0, commentIndex);
147+
}
148+
line = line.trim();
149+
if (!line.isEmpty()) {
150+
result.add(line);
151+
}
152+
}
153+
return result;
154+
}
155+
112156
private URL toURL(File file) {
113157
try {
114158
return file.toURI().toURL();

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/developing-auto-configuration.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Additional `@Conditional` annotations are used to constrain when the auto-config
1818
Usually, auto-configuration classes use `@ConditionalOnClass` and `@ConditionalOnMissingBean` annotations.
1919
This ensures that auto-configuration applies only when relevant classes are found and when you have not declared your own `@Configuration`.
2020

21-
You can browse the source code of {spring-boot-autoconfigure-module-code}[`spring-boot-autoconfigure`] to see the `@Configuration` classes that Spring provides (see the {spring-boot-code}/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories[`META-INF/spring.factories`] file).
21+
You can browse the source code of {spring-boot-autoconfigure-module-code}[`spring-boot-autoconfigure`] to see the `@Configuration` classes that Spring provides (see the {spring-boot-code}/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring-boot/org.springframework.boot.autoconfigure.AutoConfiguration[`META-INF/spring-boot/org.springframework.boot.autoconfigure.AutoConfiguration`] file).
2222

2323

2424

0 commit comments

Comments
 (0)