/
RuntimeOptions.java
297 lines (258 loc) · 10.2 KB
/
RuntimeOptions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package cucumber.runtime;
import cucumber.api.SnippetType;
import cucumber.api.StepDefinitionReporter;
import cucumber.runtime.formatter.ColorAware;
import cucumber.runtime.formatter.PluginFactory;
import cucumber.runtime.formatter.StrictAware;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.model.CucumberFeature;
import gherkin.I18n;
import cucumber.runtime.model.PathWithLines;
import gherkin.formatter.Formatter;
import gherkin.formatter.Reporter;
import gherkin.util.FixJava;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.regex.Pattern;
import static cucumber.runtime.model.CucumberFeature.load;
// IMPORTANT! Make sure USAGE.txt is always uptodate if this class changes.
public class RuntimeOptions {
public static final String VERSION = ResourceBundle.getBundle("cucumber.version").getString("cucumber-jvm.version");
public static final String USAGE = FixJava.readResource("/cucumber/api/cli/USAGE.txt");
private final List<String> glue = new ArrayList<String>();
private final List<Object> filters = new ArrayList<Object>();
private final List<String> featurePaths = new ArrayList<String>();
private final List<String> pluginNames = new ArrayList<String>();
private final PluginFactory pluginFactory;
private final List<Object> plugins = new ArrayList<Object>();
private boolean dryRun;
private boolean strict = false;
private boolean monochrome = false;
private SnippetType snippetType = SnippetType.UNDERSCORE;
private boolean pluginNamesInstantiated;
/**
* Create a new instance from a string of options, for example:
* <p/>
* <pre<{@code "--name 'the fox' --plugin pretty --strict"}</pre>
*
* @param argv the arguments
*/
public RuntimeOptions(String argv) {
this(new PluginFactory(), Shellwords.parse(argv));
}
/**
* Create a new instance from a list of options, for example:
* <p/>
* <pre<{@code Arrays.asList("--name", "the fox", "--plugin", "pretty", "--strict");}</pre>
*
* @param argv the arguments
*/
public RuntimeOptions(List<String> argv) {
this(new PluginFactory(), argv);
}
public RuntimeOptions(Env env, List<String> argv) {
this(env, new PluginFactory(), argv);
}
public RuntimeOptions(PluginFactory pluginFactory, List<String> argv) {
this(new Env("cucumber"), pluginFactory, argv);
}
public RuntimeOptions(Env env, PluginFactory pluginFactory, List<String> argv) {
this.pluginFactory = pluginFactory;
argv = new ArrayList<String>(argv); // in case the one passed in is unmodifiable.
parse(argv);
String cucumberOptionsFromEnv = env.get("cucumber.options");
if (cucumberOptionsFromEnv != null) {
parse(Shellwords.parse(cucumberOptionsFromEnv));
}
if (pluginNames.isEmpty()) {
pluginNames.add("progress");
}
}
private void parse(List<String> args) {
List<Object> parsedFilters = new ArrayList<Object>();
List<String> parsedFeaturePaths = new ArrayList<String>();
List<String> parsedGlue = new ArrayList<String>();
while (!args.isEmpty()) {
String arg = args.remove(0).trim();
if (arg.equals("--help") || arg.equals("-h")) {
printUsage();
System.exit(0);
} else if (arg.equals("--version") || arg.equals("-v")) {
System.out.println(VERSION);
System.exit(0);
} else if (arg.equals("--i18n")) {
String nextArg = args.remove(0);
System.exit(printI18n(nextArg));
} else if (arg.equals("--glue") || arg.equals("-g")) {
String gluePath = args.remove(0);
parsedGlue.add(gluePath);
} else if (arg.equals("--tags") || arg.equals("-t")) {
parsedFilters.add(args.remove(0));
} else if (arg.equals("--plugin") || arg.equals("-p")) {
pluginNames.add(args.remove(0));
} else if (arg.equals("--format") || arg.equals("-f")) {
System.err.println("WARNING: Cucumber-JVM's --format option is deprecated. Please use --plugin instead.");
pluginNames.add(args.remove(0));
} else if (arg.equals("--no-dry-run") || arg.equals("--dry-run") || arg.equals("-d")) {
dryRun = !arg.startsWith("--no-");
} else if (arg.equals("--no-strict") || arg.equals("--strict") || arg.equals("-s")) {
strict = !arg.startsWith("--no-");
} else if (arg.equals("--no-monochrome") || arg.equals("--monochrome") || arg.equals("-m")) {
monochrome = !arg.startsWith("--no-");
} else if (arg.equals("--snippets")) {
String nextArg = args.remove(0);
snippetType = SnippetType.fromString(nextArg);
} else if (arg.equals("--name") || arg.equals("-n")) {
String nextArg = args.remove(0);
Pattern patternFilter = Pattern.compile(nextArg);
parsedFilters.add(patternFilter);
} else if (arg.startsWith("-")) {
printUsage();
throw new CucumberException("Unknown option: " + arg);
} else {
parsedFeaturePaths.add(arg);
}
}
if (!parsedFilters.isEmpty() || haveLineFilters(parsedFeaturePaths)) {
filters.clear();
filters.addAll(parsedFilters);
if (parsedFeaturePaths.isEmpty() && !featurePaths.isEmpty()) {
stripLinesFromFeaturePaths(featurePaths);
}
}
if (!parsedFeaturePaths.isEmpty()) {
featurePaths.clear();
featurePaths.addAll(parsedFeaturePaths);
}
if (!parsedGlue.isEmpty()) {
glue.clear();
glue.addAll(parsedGlue);
}
}
private boolean haveLineFilters(List<String> parsedFeaturePaths) {
for (String pathName : parsedFeaturePaths) {
if (pathName.startsWith("@") || PathWithLines.hasLineFilters(pathName)) {
return true;
}
}
return false;
}
private void stripLinesFromFeaturePaths(List<String> featurePaths) {
List<String> newPaths = new ArrayList<String>();
for (String pathName : featurePaths) {
newPaths.add(PathWithLines.stripLineFilters(pathName));
}
featurePaths.clear();
featurePaths.addAll(newPaths);
}
private void printUsage() {
System.out.println(USAGE);
}
private int printI18n(String language) {
List<I18n> all = I18n.getAll();
if (language.equalsIgnoreCase("help")) {
for (I18n i18n : all) {
System.out.println(i18n.getIsoCode());
}
return 0;
} else {
return printKeywordsFor(language, all);
}
}
private int printKeywordsFor(String language, List<I18n> all) {
for (I18n i18n : all) {
if (i18n.getIsoCode().equalsIgnoreCase(language)) {
System.out.println(i18n.getKeywordTable());
return 0;
}
}
System.err.println("Unrecognised ISO language code");
return 1;
}
public List<CucumberFeature> cucumberFeatures(ResourceLoader resourceLoader) {
return load(resourceLoader, featurePaths, filters, System.out);
}
List<Object> getPlugins() {
if (!pluginNamesInstantiated) {
for (String pluginName : pluginNames) {
Object plugin = pluginFactory.create(pluginName);
plugins.add(plugin);
setMonochromeOnColorAwarePlugins(plugin);
setStrictOnStrictAwarePlugins(plugin);
}
pluginNamesInstantiated = true;
}
return plugins;
}
public Formatter formatter(ClassLoader classLoader) {
return pluginProxy(classLoader, Formatter.class);
}
public Reporter reporter(ClassLoader classLoader) {
return pluginProxy(classLoader, Reporter.class);
}
public StepDefinitionReporter stepDefinitionReporter(ClassLoader classLoader) {
return pluginProxy(classLoader, StepDefinitionReporter.class);
}
/**
* Creates a dynamic proxy that multiplexes method invocations to all plugins of the same type.
*
* @param classLoader used to create the proxy
* @param type proxy type
* @param <T> generic proxy type
* @return a proxy
*/
public <T> T pluginProxy(ClassLoader classLoader, final Class<T> type) {
Object proxy = Proxy.newProxyInstance(classLoader, new Class<?>[]{type}, new InvocationHandler() {
@Override
public Object invoke(Object target, Method method, Object[] args) throws Throwable {
for (Object plugin : getPlugins()) {
if (type.isInstance(plugin)) {
Utils.invoke(plugin, method, 0, args);
}
}
return null;
}
});
return type.cast(proxy);
}
private void setMonochromeOnColorAwarePlugins(Object plugin) {
if (plugin instanceof ColorAware) {
ColorAware colorAware = (ColorAware) plugin;
colorAware.setMonochrome(monochrome);
}
}
private void setStrictOnStrictAwarePlugins(Object plugin) {
if (plugin instanceof StrictAware) {
StrictAware strictAware = (StrictAware) plugin;
strictAware.setStrict(strict);
}
}
public List<String> getGlue() {
return glue;
}
public boolean isStrict() {
return strict;
}
public boolean isDryRun() {
return dryRun;
}
public List<String> getFeaturePaths() {
return featurePaths;
}
public void addPlugin(Object plugin) {
plugins.add(plugin);
}
public List<Object> getFilters() {
return filters;
}
public boolean isMonochrome() {
return monochrome;
}
public SnippetType getSnippetType() {
return snippetType;
}
}