Skip to content

Commit 22bf537

Browse files
committed
Dispose line filters when feature paths are clobbered
Also dispose line filters when feature paths are clobbered with -Dcucumber.options.
1 parent 6c2ba2f commit 22bf537

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

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

+19-16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class RuntimeOptions {
3131

3232
private final List<String> glue = new ArrayList<String>();
3333
private final List<Object> filters = new ArrayList<Object>();
34+
private final List<Object> lineFilters = new ArrayList<Object>();
3435
private final List<Formatter> formatters = new ArrayList<Formatter>();
3536
private final List<String> featurePaths = new ArrayList<String>();
3637
private final FormatterFactory formatterFactory;
@@ -47,12 +48,13 @@ public RuntimeOptions(Env env, String... argv) {
4748
RuntimeOptions(Env env, FormatterFactory formatterFactory, String... argv) {
4849
this.formatterFactory = formatterFactory;
4950

50-
parse(new ArrayList<String>(asList(argv)), false);
51+
parse(new ArrayList<String>(asList(argv)));
5152

5253
String cucumberOptionsFromEnv = env.get("cucumber.options");
5354
if (cucumberOptionsFromEnv != null) {
54-
parse(shellWords(cucumberOptionsFromEnv), true);
55+
parse(shellWords(cucumberOptionsFromEnv));
5556
}
57+
filters.addAll(lineFilters);
5658

5759
if (formatters.isEmpty()) {
5860
formatters.add(formatterFactory.create("progress"));
@@ -73,15 +75,12 @@ private List<String> shellWords(String cmdline) {
7375
return matchList;
7476
}
7577

76-
private void parse(List<String> args, boolean clobberFeaturePathsAndGlue) {
78+
private void parse(List<String> args) {
7779
List<Object> parsedFilters = new ArrayList<Object>();
80+
List<Object> parsedLineFilters = new ArrayList<Object>();
81+
List<String> parsedFeaturePaths = new ArrayList<String>();
82+
List<String> parsedGlue = new ArrayList<String>();
7883

79-
List<String> oldFeaturePaths = new ArrayList<String>(featurePaths);
80-
List<String> oldGlue = new ArrayList<String>(glue);
81-
if (clobberFeaturePathsAndGlue) {
82-
featurePaths.clear();
83-
glue.clear();
84-
}
8584
while (!args.isEmpty()) {
8685
String arg = args.remove(0).trim();
8786

@@ -93,7 +92,7 @@ private void parse(List<String> args, boolean clobberFeaturePathsAndGlue) {
9392
System.exit(0);
9493
} else if (arg.equals("--glue") || arg.equals("-g")) {
9594
String gluePath = args.remove(0);
96-
glue.add(gluePath);
95+
parsedGlue.add(gluePath);
9796
} else if (arg.equals("--tags") || arg.equals("-t")) {
9897
parsedFilters.add(args.remove(0));
9998
} else if (arg.equals("--format") || arg.equals("-f")) {
@@ -119,19 +118,23 @@ private void parse(List<String> args, boolean clobberFeaturePathsAndGlue) {
119118
throw new CucumberException("Unknown option: " + arg);
120119
} else {
121120
PathWithLines pathWithLines = new PathWithLines(arg);
122-
featurePaths.add(pathWithLines.path);
123-
parsedFilters.addAll(pathWithLines.lines);
121+
parsedFeaturePaths.add(pathWithLines.path);
122+
parsedLineFilters.addAll(pathWithLines.lines);
124123
}
125124
}
126125
if (!parsedFilters.isEmpty()) {
127126
filters.clear();
128127
filters.addAll(parsedFilters);
129128
}
130-
if (featurePaths.isEmpty()) {
131-
featurePaths.addAll(oldFeaturePaths);
129+
if (!parsedFeaturePaths.isEmpty()) {
130+
featurePaths.clear();
131+
lineFilters.clear();
132+
featurePaths.addAll(parsedFeaturePaths);
133+
lineFilters.addAll(parsedLineFilters);
132134
}
133-
if (glue.isEmpty()) {
134-
glue.addAll(oldGlue);
135+
if (!parsedGlue.isEmpty()) {
136+
glue.clear();
137+
glue.addAll(parsedGlue);
135138
}
136139
}
137140

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

+24-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import cucumber.api.SnippetType;
44
import org.junit.Test;
5-
65
import cucumber.runtime.formatter.ColorAware;
76
import cucumber.runtime.formatter.FormatterFactory;
87
import cucumber.runtime.formatter.StrictAware;
9-
108
import gherkin.formatter.Formatter;
119

1210
import java.net.MalformedURLException;
1311
import java.net.URL;
12+
import java.util.Arrays;
1413
import java.util.Properties;
1514
import java.util.regex.Pattern;
1615

@@ -41,6 +40,20 @@ public void assigns_feature_paths() {
4140
assertEquals(asList("somewhere_else"), options.getFeaturePaths());
4241
}
4342

43+
@Test
44+
public void assigns_line_filters_from_feature_paths() {
45+
RuntimeOptions options = new RuntimeOptions(new Env(), "--glue", "somewhere", "somewhere_else:3");
46+
assertEquals(asList("somewhere_else"), options.getFeaturePaths());
47+
assertEquals(asList(3L), options.getFilters());
48+
}
49+
50+
@Test
51+
public void assigns_filters_and_line_filters_from_feature_paths() {
52+
RuntimeOptions options = new RuntimeOptions(new Env(), "--tags", "@keep_this", "somewhere_else:3");
53+
assertEquals(asList("somewhere_else"), options.getFeaturePaths());
54+
assertEquals(Arrays.<Object>asList("@keep_this", 3L), options.getFilters());
55+
}
56+
4457
@Test
4558
public void strips_options() {
4659
RuntimeOptions options = new RuntimeOptions(new Env(), " --glue ", "somewhere", "somewhere_else");
@@ -166,6 +179,15 @@ public void preserves_features_from_cli_if_features_not_specified_in_cucumber_op
166179
assertEquals(asList("old", "older"), runtimeOptions.getFeaturePaths());
167180
}
168181

182+
@Test
183+
public void clobbers_line_filters_from_cli_if_features_specified_in_cucumber_options_property() {
184+
Properties properties = new Properties();
185+
properties.setProperty("cucumber.options", "new newer");
186+
RuntimeOptions runtimeOptions = new RuntimeOptions(new Env(properties), "--tags", "@keep_this", "path/file1.feature:1");
187+
assertEquals(asList("new", "newer"), runtimeOptions.getFeaturePaths());
188+
assertEquals(asList("@keep_this"), runtimeOptions.getFilters());
189+
}
190+
169191
@Test
170192
public void allows_removal_of_strict_in_cucumber_options_property() {
171193
Properties properties = new Properties();

0 commit comments

Comments
 (0)