|
27 | 27 | import org.gradle.api.file.FileTree;
|
28 | 28 | import org.gradle.api.tasks.Input;
|
29 | 29 | import org.gradle.api.tasks.OutputFile;
|
| 30 | +import org.gradle.api.tasks.SourceSet; |
| 31 | +import org.gradle.api.tasks.SourceSetContainer; |
30 | 32 | import org.gradle.api.tasks.TaskAction;
|
31 | 33 | import org.gradle.api.tasks.testing.Test;
|
32 | 34 | import org.gradle.api.tasks.util.PatternFilterable;
|
@@ -122,6 +124,23 @@ public void naming(Closure<TestingConventionRule> action) {
|
122 | 124 | naming.configure(action);
|
123 | 125 | }
|
124 | 126 |
|
| 127 | + @Input |
| 128 | + public Set<String> getMainClassNamedLikeTests() { |
| 129 | + SourceSetContainer javaSourceSets = Boilerplate.getJavaSourceSets(getProject()); |
| 130 | + if (javaSourceSets.findByName(SourceSet.MAIN_SOURCE_SET_NAME) == null) { |
| 131 | + // some test projects don't have a main source set |
| 132 | + return Collections.emptySet(); |
| 133 | + } |
| 134 | + return javaSourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME) |
| 135 | + .getOutput().getClassesDirs().getAsFileTree() |
| 136 | + .getFiles().stream() |
| 137 | + .filter(file -> file.getName().endsWith(".class")) |
| 138 | + .map(File::getName) |
| 139 | + .map(name -> name.substring(0, name.length() - 6)) |
| 140 | + .filter(this::implementsNamingConvention) |
| 141 | + .collect(Collectors.toSet()); |
| 142 | + } |
| 143 | + |
125 | 144 | @TaskAction
|
126 | 145 | public void doCheck() throws IOException {
|
127 | 146 | final String problems;
|
@@ -235,10 +254,12 @@ public void doCheck() throws IOException {
|
235 | 254 | );
|
236 | 255 | }).sorted()
|
237 | 256 | .collect(Collectors.joining("\n"))
|
238 |
| - ) |
| 257 | + ), |
239 | 258 | // TODO: check that the testing tasks are included in the right task based on the name ( from the rule )
|
240 |
| - // TODO: check to make sure that the main source set doesn't have classes that match |
241 |
| - // the naming convention (just the names, don't load classes) |
| 259 | + checkNoneExists( |
| 260 | + "Classes matching the test naming convention should be in test not main", |
| 261 | + getMainClassNamedLikeTests() |
| 262 | + ) |
242 | 263 | );
|
243 | 264 | }
|
244 | 265 |
|
@@ -296,6 +317,18 @@ private String checkNoneExists(String message, Stream<? extends Class<?>> stream
|
296 | 317 | }
|
297 | 318 | }
|
298 | 319 |
|
| 320 | + private String checkNoneExists(String message, Set<? extends String> candidates) { |
| 321 | + String problem = candidates.stream() |
| 322 | + .map(each -> " * " + each) |
| 323 | + .sorted() |
| 324 | + .collect(Collectors.joining("\n")); |
| 325 | + if (problem.isEmpty() == false) { |
| 326 | + return message + ":\n" + problem; |
| 327 | + } else { |
| 328 | + return ""; |
| 329 | + } |
| 330 | + } |
| 331 | + |
299 | 332 | private String checkAtLeastOneExists(String message, Stream<? extends Class<?>> stream) {
|
300 | 333 | if (stream.findAny().isPresent()) {
|
301 | 334 | return "";
|
@@ -337,10 +370,14 @@ private boolean seemsLikeATest(Class<?> clazz) {
|
337 | 370 | }
|
338 | 371 |
|
339 | 372 | private boolean implementsNamingConvention(Class<?> clazz) {
|
| 373 | + return implementsNamingConvention(clazz.getName()); |
| 374 | + } |
| 375 | + |
| 376 | + private boolean implementsNamingConvention(String className) { |
340 | 377 | if (naming.stream()
|
341 | 378 | .map(TestingConventionRule::getSuffix)
|
342 |
| - .anyMatch(suffix -> clazz.getName().endsWith(suffix))) { |
343 |
| - getLogger().debug("{} is a test because it matches the naming convention", clazz.getName()); |
| 379 | + .anyMatch(suffix -> className.endsWith(suffix))) { |
| 380 | + getLogger().debug("{} is a test because it matches the naming convention", className); |
344 | 381 | return true;
|
345 | 382 | }
|
346 | 383 | return false;
|
|
0 commit comments