Skip to content

Commit ecdbd77

Browse files
committed
Replaced cucumber.options split on spaces with regex
added a private method (cucumberOptionsSplit to RuntimeOptions to split on spaces or strings encased in single quotes.
1 parent b1d3f48 commit ecdbd77

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

core/src/main/java/cucumber/runtime/RuntimeOptions.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.List;
1717
import java.util.Properties;
1818
import java.util.ResourceBundle;
19+
import java.util.regex.Matcher;
1920
import java.util.regex.Pattern;
2021

2122
import static cucumber.runtime.model.CucumberFeature.load;
@@ -40,7 +41,7 @@ public RuntimeOptions(Properties properties, String... argv) {
4041

4142
parse(new ArrayList<String>(asList(argv)));
4243
if (properties.containsKey("cucumber.options")) {
43-
parse(new ArrayList<String>(asList(properties.getProperty("cucumber.options").split(" "))));
44+
parse(cucumberOptionsSplit(properties.getProperty("cucumber.options")));
4445
}
4546

4647
if (formatters.isEmpty()) {
@@ -54,7 +55,20 @@ public RuntimeOptions(Properties properties, String... argv) {
5455
}
5556
}
5657

57-
private void parse(List<String> args) {
58+
private List<String> cucumberOptionsSplit(String property) {
59+
List<String> matchList = new ArrayList<String>();
60+
Pattern regex = Pattern.compile("[^\\s']+|'([^']*)'");
61+
Matcher regexMatcher = regex.matcher(property);
62+
while (regexMatcher.find()) {
63+
if (regexMatcher.group(1) != null)
64+
matchList.add(regexMatcher.group(1));
65+
else
66+
matchList.add(regexMatcher.group());
67+
}
68+
return matchList;
69+
}
70+
71+
private void parse(List<String> args) {
5872
List<Object> parsedFilters = new ArrayList<Object>();
5973
while (!args.isEmpty()) {
6074
String arg = args.remove(0);

core/src/test/java/cucumber/runtime/RuntimeOptionsTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cucumber.runtime;
22

3+
import net.sourceforge.cobertura.ant.Regex;
4+
35
import org.junit.Test;
46

57
import java.io.File;
@@ -71,7 +73,37 @@ public void name() {
7173
Pattern actualPattern = (Pattern) options.filters.iterator().next();
7274
assertEquals(someName, actualPattern.pattern());
7375
}
74-
76+
77+
@Test
78+
public void name_with_spaces() {
79+
String someName = "some Name";
80+
RuntimeOptions options = new RuntimeOptions(new Properties(), "--name", someName);
81+
Pattern actualPattern = (Pattern) options.filters.iterator().next();
82+
assertEquals(someName, actualPattern.pattern());
83+
}
84+
85+
@Test
86+
public void ensure_name_with_spaces_works_with_cucumber_options() {
87+
String someName = "some Name";
88+
Properties properties = new Properties();
89+
properties.setProperty("cucumber.options", "--name '" + someName + "'");
90+
RuntimeOptions options = new RuntimeOptions(properties);
91+
Pattern actualPattern = (Pattern) options.filters.iterator().next();
92+
assertEquals(someName, actualPattern.pattern());
93+
}
94+
95+
@Test
96+
public void ensure_multiple_cucumber_options_with_spaces_parse_correctly() {
97+
String someName = "some Name";
98+
String somePath = "some file\\path";
99+
Properties properties = new Properties();
100+
properties.setProperty("cucumber.options", "--name '" + someName + "'" + " --dotcucumber '" + somePath + "'");
101+
RuntimeOptions options = new RuntimeOptions(properties);
102+
Pattern actualPattern = (Pattern) options.filters.iterator().next();
103+
assertEquals(someName, actualPattern.pattern());
104+
assertEquals(new File(somePath), options.dotCucumber);
105+
}
106+
75107
@Test
76108
public void name_short() {
77109
String someName = "someName";

0 commit comments

Comments
 (0)