Skip to content

Commit 8f01484

Browse files
committed
Factor shellwords out to separate class, ref cucumber#597
1 parent 4ed29b0 commit 8f01484

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.util.ArrayList;
1818
import java.util.List;
1919
import java.util.ResourceBundle;
20-
import java.util.regex.Matcher;
2120
import java.util.regex.Pattern;
2221

2322
import static cucumber.runtime.model.CucumberFeature.load;
@@ -26,7 +25,6 @@
2625
public class RuntimeOptions {
2726
public static final String VERSION = ResourceBundle.getBundle("cucumber.version").getString("cucumber-jvm.version");
2827
public static final String USAGE = FixJava.readResource("/cucumber/runtime/USAGE.txt");
29-
private static final Pattern SHELLWORDS_PATTERN = Pattern.compile("[^\\s']+|'([^']*)'");
3028

3129
private final List<String> glue = new ArrayList<String>();
3230
private final List<Object> filters = new ArrayList<Object>();
@@ -48,7 +46,7 @@ public class RuntimeOptions {
4846
* @param argv the arguments
4947
*/
5048
public RuntimeOptions(String argv) {
51-
this(new FormatterFactory(), shellWords(argv));
49+
this(new FormatterFactory(), Shellwords.parse(argv));
5250
}
5351

5452
/**
@@ -78,7 +76,7 @@ public RuntimeOptions(Env env, FormatterFactory formatterFactory, List<String> a
7876

7977
String cucumberOptionsFromEnv = env.get("cucumber.options");
8078
if (cucumberOptionsFromEnv != null) {
81-
parse(shellWords(cucumberOptionsFromEnv));
79+
parse(Shellwords.parse(cucumberOptionsFromEnv));
8280
}
8381
filters.addAll(lineFilters);
8482

@@ -88,19 +86,6 @@ public RuntimeOptions(Env env, FormatterFactory formatterFactory, List<String> a
8886
setFormatterOptions();
8987
}
9088

91-
private static List<String> shellWords(String cmdline) {
92-
List<String> matchList = new ArrayList<String>();
93-
Matcher shellwordsMatcher = SHELLWORDS_PATTERN.matcher(cmdline);
94-
while (shellwordsMatcher.find()) {
95-
if (shellwordsMatcher.group(1) != null) {
96-
matchList.add(shellwordsMatcher.group(1));
97-
} else {
98-
matchList.add(shellwordsMatcher.group());
99-
}
100-
}
101-
return matchList;
102-
}
103-
10489
private void parse(List<String> args) {
10590
List<Object> parsedFilters = new ArrayList<Object>();
10691
List<Object> parsedLineFilters = new ArrayList<Object>();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cucumber.runtime;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
public class Shellwords {
9+
private static final Pattern SHELLWORDS_PATTERN = Pattern.compile("[^\\s']+|'([^']*)'");
10+
11+
public static List<String> parse(String cmdline) {
12+
List<String> matchList = new ArrayList<String>();
13+
Matcher shellwordsMatcher = SHELLWORDS_PATTERN.matcher(cmdline);
14+
while (shellwordsMatcher.find()) {
15+
if (shellwordsMatcher.group(1) != null) {
16+
matchList.add(shellwordsMatcher.group(1));
17+
} else {
18+
matchList.add(shellwordsMatcher.group());
19+
}
20+
}
21+
return matchList;
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cucumber.runtime;
2+
3+
import org.junit.Ignore;
4+
import org.junit.Test;
5+
6+
import static java.util.Arrays.asList;
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class ShellwordsTest {
10+
@Test
11+
public void parses_single_quoted_strings() {
12+
assertEquals(asList("--name", "The Fox"), Shellwords.parse("--name 'The Fox'"));
13+
}
14+
15+
@Ignore("TODO: fixme")
16+
@Test
17+
public void parses_double_quoted_strings() {
18+
assertEquals(asList("--name", "The Fox"), Shellwords.parse("--name \"The Fox\""));
19+
}
20+
21+
@Ignore("TODO: fixme")
22+
@Test
23+
public void parses_both_single_and_double_quoted_strings() {
24+
assertEquals(asList("--name", "The Fox", "--fur", "Brown White"), Shellwords.parse("--name \"The Fox\" --fur 'Brown White'"));
25+
}
26+
}

0 commit comments

Comments
 (0)