Skip to content

Commit f175e4e

Browse files
authored
Warn when glue paths is a well known project source directory (#2547)
1 parent b106ab0 commit f175e4e

File tree

6 files changed

+102
-10
lines changed

6 files changed

+102
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1717

1818
### Fixed
1919

20+
## [7.3.5] (2022-05-10)
21+
22+
### Added
23+
* [Core] Warn when glue path is passed as file scheme instead of classpath ([#2547](https://github.com/cucumber/cucumber-jvm/pull/2547) M.P. Korstanje)
24+
2025
## [7.3.4] (2022-05-02)
2126

2227
### Fixed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ We appreciate that. Do keep the following in mind:
1515
To automatically format the java source code run
1616

1717
```
18-
mvn spotless:apply
18+
mvn initialize spotless:apply
1919
```
2020

2121
To configure Intelij IDEA/Eclipse use the configuration files in `.spotless/`.

core/src/main/java/io/cucumber/core/feature/GluePath.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package io.cucumber.core.feature;
22

3+
import io.cucumber.core.logging.Logger;
4+
import io.cucumber.core.logging.LoggerFactory;
5+
36
import java.io.File;
47
import java.net.URI;
58
import java.net.URISyntaxException;
9+
import java.util.regex.Matcher;
10+
import java.util.regex.Pattern;
611

712
import static io.cucumber.core.resource.ClasspathSupport.CLASSPATH_SCHEME;
813
import static io.cucumber.core.resource.ClasspathSupport.CLASSPATH_SCHEME_PREFIX;
@@ -29,6 +34,11 @@
2934
*/
3035
public class GluePath {
3136

37+
private static final Logger log = LoggerFactory.getLogger(GluePath.class);
38+
39+
private static final Pattern WELL_KNOWN_PROJECT_SOURCE_DIRECTORIES = Pattern
40+
.compile("src/(?:main|test)/(?:java|kotlin|scala|groovy)(|/|/.+)");
41+
3242
private GluePath() {
3343

3444
}
@@ -70,6 +80,8 @@ private static String replaceNonStandardPathSeparator(String featureIdentifier)
7080
private static URI parseAssumeClasspathScheme(String gluePath) {
7181
URI uri = URI.create(gluePath);
7282

83+
warnWhenWellKnownProjectSourceDirectory(gluePath);
84+
7385
String schemeSpecificPart = uri.getSchemeSpecificPart();
7486
if (!isValidIdentifier(schemeSpecificPart)) {
7587
throw new IllegalArgumentException("The glue path contained invalid identifiers " + uri);
@@ -92,6 +104,30 @@ private static URI parseAssumeClasspathScheme(String gluePath) {
92104
return uri;
93105
}
94106

107+
private static void warnWhenWellKnownProjectSourceDirectory(String gluePath) {
108+
Matcher matcher = WELL_KNOWN_PROJECT_SOURCE_DIRECTORIES.matcher(gluePath);
109+
if (!matcher.matches()) {
110+
return;
111+
}
112+
log.warn(() -> {
113+
String classPathResource = matcher.group(1);
114+
if (classPathResource.startsWith("/")) {
115+
classPathResource = classPathResource.substring(1);
116+
}
117+
if (classPathResource.endsWith("/")) {
118+
classPathResource = classPathResource.substring(0, classPathResource.length() - 1);
119+
}
120+
String packageName = classPathResource.replaceAll("/", ".");
121+
String message = "" +
122+
"Consider replacing glue path '%s' with '%s'.\n'" +
123+
"\n" +
124+
"The current glue path points to a source directory in your project. However " +
125+
"cucumber looks for glue (i.e. step definitions) on the classpath. By using a " +
126+
"package name you can avoid this ambiguity.";
127+
return String.format(message, gluePath, packageName);
128+
});
129+
}
130+
95131
private static boolean isProbablyPackage(String gluePath) {
96132
return gluePath.contains(PACKAGE_SEPARATOR_STRING)
97133
&& !gluePath.contains(RESOURCE_SEPARATOR_STRING);

core/src/test/java/io/cucumber/core/feature/GluePathTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
package io.cucumber.core.feature;
22

3+
import io.cucumber.core.logging.LogRecordListener;
4+
import io.cucumber.core.logging.LoggerFactory;
5+
import org.hamcrest.Matcher;
36
import org.junit.jupiter.api.Test;
47
import org.junit.jupiter.api.condition.EnabledOnOs;
58
import org.junit.jupiter.api.condition.OS;
69
import org.junit.jupiter.api.function.Executable;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.Arguments;
12+
import org.junit.jupiter.params.provider.MethodSource;
713

814
import java.net.URI;
15+
import java.util.logging.LogRecord;
16+
import java.util.stream.Stream;
917

18+
import static org.hamcrest.CoreMatchers.containsString;
19+
import static org.hamcrest.CoreMatchers.nullValue;
1020
import static org.hamcrest.MatcherAssert.assertThat;
1121
import static org.hamcrest.core.Is.is;
1222
import static org.hamcrest.core.IsEqual.equalTo;
1323
import static org.junit.jupiter.api.Assertions.assertAll;
1424
import static org.junit.jupiter.api.Assertions.assertThrows;
25+
import static org.junit.jupiter.params.provider.Arguments.arguments;
1526

1627
class GluePathTest {
1728

@@ -114,4 +125,49 @@ void absolute_windows_path_form_is_not_valid() {
114125
"The glue path must have a classpath scheme C:/com/example/app")));
115126
}
116127

128+
@ParameterizedTest
129+
@MethodSource("warn_when_glue_as_filesystem_path_examples")
130+
void when_when_glue_path_is_well_known_source_directory(String gluePath, Matcher<String> logPattern) {
131+
// warn when 'src/{test,main}/{java,kotlin,scala,groovy}' is used
132+
133+
LogRecordListener logRecordListener = new LogRecordListener();
134+
LoggerFactory.addListener(logRecordListener);
135+
136+
GluePath.parse(gluePath);
137+
138+
LoggerFactory.removeListener(logRecordListener);
139+
140+
String logMessage = logRecordListener.getLogRecords()
141+
.stream()
142+
.findFirst()
143+
.map(LogRecord::getMessage)
144+
.orElse(null);
145+
146+
assertThat(logMessage, logPattern);
147+
}
148+
149+
static Stream<Arguments> warn_when_glue_as_filesystem_path_examples() {
150+
return Stream.of(
151+
arguments("src/main/java/com/example/package",
152+
equalTo("" +
153+
"Consider replacing glue path " +
154+
"'src/main/java/com/example/package' with " +
155+
"'com.example.package'.\n" +
156+
"'\n" +
157+
"The current glue path points to a source " +
158+
"directory in your project. However cucumber " +
159+
"looks for glue (i.e. step definitions) on the " +
160+
"classpath. By using a package name you can " +
161+
"avoid this ambiguity.")),
162+
arguments("src/main/java", containsString("with ''")),
163+
arguments("src/main/java/", containsString("with ''")),
164+
arguments("src/main/java_other", nullValue()),
165+
arguments("src/main/other", nullValue()),
166+
arguments("src/main/java/com", containsString("with 'com'")),
167+
arguments("src/main/java/com/", containsString("with 'com'")),
168+
arguments("src/main/groovy/com", containsString("with 'com'")),
169+
arguments("src/main/java/com/example", containsString("with 'com.example'")),
170+
arguments("src/main/java/com/example/", containsString("with 'com.example'")));
171+
}
172+
117173
}

core/src/test/java/io/cucumber/core/options/CucumberOptionsAnnotationParserTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import io.cucumber.core.backend.ObjectFactory;
44
import io.cucumber.core.exception.CucumberException;
5-
import io.cucumber.core.plugin.DefaultSummaryPrinter;
65
import io.cucumber.core.plugin.HtmlFormatter;
76
import io.cucumber.core.plugin.NoPublishFormatter;
87
import io.cucumber.core.plugin.PluginFactory;
@@ -13,7 +12,6 @@
1312
import io.cucumber.core.snippets.SnippetType;
1413
import io.cucumber.plugin.Plugin;
1514
import io.cucumber.tagexpressions.TagExpressionException;
16-
import org.junit.jupiter.api.Disabled;
1715
import org.junit.jupiter.api.Test;
1816
import org.junit.jupiter.api.function.Executable;
1917

core/src/test/java/io/cucumber/core/runtime/FeaturePathFeatureSupplierTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
import static java.util.Collections.singletonList;
1717
import static org.hamcrest.CoreMatchers.containsString;
18+
import static org.hamcrest.CoreMatchers.equalTo;
1819
import static org.hamcrest.CoreMatchers.is;
1920
import static org.hamcrest.CoreMatchers.startsWith;
2021
import static org.hamcrest.MatcherAssert.assertThat;
21-
import static org.junit.jupiter.api.Assertions.assertAll;
2222
import static org.junit.jupiter.api.Assertions.assertThrows;
2323

2424
class FeaturePathFeatureSupplierTest {
@@ -40,15 +40,12 @@ void tearDown() {
4040

4141
@Test
4242
void logs_message_if_no_features_are_found() {
43-
Options featureOptions = () -> singletonList(FeaturePath.parse("src/test/resources/io/cucumber/core/options"));
43+
Options featureOptions = () -> singletonList(FeaturePath.parse("classpath:io/cucumber/core/options"));
4444

4545
FeaturePathFeatureSupplier supplier = new FeaturePathFeatureSupplier(classLoader, featureOptions, parser);
4646
supplier.get();
47-
assertAll(
48-
() -> assertThat(logRecordListener.getLogRecords().get(1).getMessage(),
49-
containsString("No features found at file:")),
50-
() -> assertThat(logRecordListener.getLogRecords().get(1).getMessage(),
51-
containsString("src/test/resources/io/cucumber/core/options")));
47+
assertThat(logRecordListener.getLogRecords().get(1).getMessage(),
48+
equalTo("No features found at classpath:io/cucumber/core/options"));
5249
}
5350

5451
@Test

0 commit comments

Comments
 (0)