From de45ddac3b6ac49046be00db496c08bcf502d566 Mon Sep 17 00:00:00 2001 From: "M.P. Korstanje" Date: Thu, 23 Apr 2020 18:17:21 +0200 Subject: [PATCH] Reduce exposed api --- .../options/CommandlineOptionsParser.java | 198 +++++++++--------- .../options/CommandlineOptionsParserTest.java | 14 +- 2 files changed, 107 insertions(+), 105 deletions(-) diff --git a/core/src/main/java/io/cucumber/core/options/CommandlineOptionsParser.java b/core/src/main/java/io/cucumber/core/options/CommandlineOptionsParser.java index 6f8b5635d1..75ca816d1e 100644 --- a/core/src/main/java/io/cucumber/core/options/CommandlineOptionsParser.java +++ b/core/src/main/java/io/cucumber/core/options/CommandlineOptionsParser.java @@ -33,7 +33,7 @@ public final class CommandlineOptionsParser { - static final String VERSION = ResourceBundle.getBundle("io.cucumber.core.version").getString("cucumber-jvm.version"); + private static final String VERSION = ResourceBundle.getBundle("io.cucumber.core.version").getString("cucumber-jvm.version"); // IMPORTANT! Make sure USAGE.txt is always uptodate if this class changes. private static final String USAGE_RESOURCE = "/io/cucumber/core/options/USAGE.txt"; @@ -44,11 +44,108 @@ public CommandlineOptionsParser(OutputStream outputStream) { out = new PrintWriter(outputStream, true); } + public Optional exitStatus() { + return Optional.of(exitCode); + } + + public RuntimeOptionsBuilder parse(String... args) { + return parse(Arrays.asList(args)); + } + + private RuntimeOptionsBuilder parse(List args) { + args = new ArrayList<>(args); + RuntimeOptionsBuilder parsedOptions = new RuntimeOptionsBuilder(); + + while (!args.isEmpty()) { + String arg = args.remove(0).trim(); + + if (arg.equals("--help") || arg.equals("-h")) { + printUsage(); + exitCode = 0; + return parsedOptions; + } else if (arg.equals("--version") || arg.equals("-v")) { + out.println(VERSION); + exitCode = 0; + return parsedOptions; + } else if (arg.equals("--i18n")) { + String nextArg = removeArgFor(arg, args); + exitCode = printI18n(nextArg); + return parsedOptions; + } else if (arg.equals("--threads")) { + int threads = Integer.parseInt(removeArgFor(arg, args)); + if (threads < 1) { + out.println("--threads must be > 0"); + exitCode = 1; + return parsedOptions; + } + parsedOptions.setThreads(threads); + } else if (arg.equals("--glue") || arg.equals("-g")) { + String gluePath = removeArgFor(arg, args); + URI parse = GluePath.parse(gluePath); + parsedOptions.addGlue(parse); + } else if (arg.equals("--tags") || arg.equals("-t")) { + parsedOptions.addTagFilter(removeArgFor(arg, args)); + } else if (arg.equals("--plugin") || arg.equals("-p")) { + parsedOptions.addPluginName(removeArgFor(arg, args)); + } else if (arg.equals("--no-dry-run") || arg.equals("--dry-run") || arg.equals("-d")) { + parsedOptions.setDryRun(!arg.startsWith("--no-")); + } else if (arg.equals("--no-strict") || arg.equals("--strict") || arg.equals("-s")) { + parsedOptions.setStrict(!arg.startsWith("--no-")); + } else if (arg.equals("--no-monochrome") || arg.equals("--monochrome") || arg.equals("-m")) { + parsedOptions.setMonochrome(!arg.startsWith("--no-")); + } else if (arg.equals("--snippets")) { + String nextArg = removeArgFor(arg, args); + parsedOptions.setSnippetType(SnippetTypeParser.parseSnippetType(nextArg)); + } else if (arg.equals("--name") || arg.equals("-n")) { + String nextArg = removeArgFor(arg, args); + Pattern pattern = Pattern.compile(nextArg); + parsedOptions.addNameFilter(pattern); + } else if (arg.equals("--wip") || arg.equals("-w")) { + parsedOptions.setWip(true); + } else if (arg.equals("--order")) { + parsedOptions.setPickleOrder(PickleOrderParser.parse(removeArgFor(arg, args))); + } else if (arg.equals("--count")) { + int count = Integer.parseInt(removeArgFor(arg, args)); + if (count < 1) { + out.println("--count must be > 0"); + exitCode = 1; + return parsedOptions; + } + parsedOptions.setCount(count); + } else if (arg.equals("--object-factory")) { + String objectFactoryClassName = removeArgFor(arg, args); + parsedOptions.setObjectFactoryClass(parseObjectFactory(objectFactoryClassName)); + } else if (arg.startsWith("-")) { + out.println("Unknown option: " + arg); + printUsage(); + exitCode = 1; + return parsedOptions; + } else if (!arg.isEmpty()) { + if (arg.startsWith("@")) { + Path rerunFile = Paths.get(arg.substring(1)); + parsedOptions.addRerun(parseFeatureWithLinesFile(rerunFile)); + } else { + FeatureWithLines featureWithLines = FeatureWithLines.parse(arg); + parsedOptions.addFeature(featureWithLines); + } + } + } + return parsedOptions; + } + + private String removeArgFor(String arg, List args) { + if (!args.isEmpty()) { + return args.remove(0); + } + printUsage(); + throw new CucumberException("Missing argument for " + arg); + } + private void printUsage() { out.println(loadUsageText()); } - String loadUsageText() { + private String loadUsageText() { try ( InputStream usageResourceStream = CommandlineOptionsParser.class.getResourceAsStream(USAGE_RESOURCE); BufferedReader br = new BufferedReader(new InputStreamReader(usageResourceStream, UTF_8)) @@ -149,101 +246,4 @@ private String rightPad(String text, int maxWidth) { return String.format("%" + -width + "s", text); } - - public RuntimeOptionsBuilder parse(String... args) { - return parse(Arrays.asList(args)); - } - - public RuntimeOptionsBuilder parse(List args) { - args = new ArrayList<>(args); - RuntimeOptionsBuilder parsedOptions = new RuntimeOptionsBuilder(); - - while (!args.isEmpty()) { - String arg = args.remove(0).trim(); - - if (arg.equals("--help") || arg.equals("-h")) { - printUsage(); - exitCode = 0; - return parsedOptions; - } else if (arg.equals("--version") || arg.equals("-v")) { - out.println(VERSION); - exitCode = 0; - return parsedOptions; - } else if (arg.equals("--i18n")) { - String nextArg = removeArgFor(arg, args); - exitCode = printI18n(nextArg); - return parsedOptions; - } else if (arg.equals("--threads")) { - int threads = Integer.parseInt(removeArgFor(arg, args)); - if (threads < 1) { - out.println("--threads must be > 0"); - exitCode = 1; - return parsedOptions; - } - parsedOptions.setThreads(threads); - } else if (arg.equals("--glue") || arg.equals("-g")) { - String gluePath = removeArgFor(arg, args); - URI parse = GluePath.parse(gluePath); - parsedOptions.addGlue(parse); - } else if (arg.equals("--tags") || arg.equals("-t")) { - parsedOptions.addTagFilter(removeArgFor(arg, args)); - } else if (arg.equals("--plugin") || arg.equals("-p")) { - parsedOptions.addPluginName(removeArgFor(arg, args)); - } else if (arg.equals("--no-dry-run") || arg.equals("--dry-run") || arg.equals("-d")) { - parsedOptions.setDryRun(!arg.startsWith("--no-")); - } else if (arg.equals("--no-strict") || arg.equals("--strict") || arg.equals("-s")) { - parsedOptions.setStrict(!arg.startsWith("--no-")); - } else if (arg.equals("--no-monochrome") || arg.equals("--monochrome") || arg.equals("-m")) { - parsedOptions.setMonochrome(!arg.startsWith("--no-")); - } else if (arg.equals("--snippets")) { - String nextArg = removeArgFor(arg, args); - parsedOptions.setSnippetType(SnippetTypeParser.parseSnippetType(nextArg)); - } else if (arg.equals("--name") || arg.equals("-n")) { - String nextArg = removeArgFor(arg, args); - Pattern pattern = Pattern.compile(nextArg); - parsedOptions.addNameFilter(pattern); - } else if (arg.equals("--wip") || arg.equals("-w")) { - parsedOptions.setWip(true); - } else if (arg.equals("--order")) { - parsedOptions.setPickleOrder(PickleOrderParser.parse(removeArgFor(arg, args))); - } else if (arg.equals("--count")) { - int count = Integer.parseInt(removeArgFor(arg, args)); - if (count < 1) { - out.println("--count must be > 0"); - exitCode = 1; - return parsedOptions; - } - parsedOptions.setCount(count); - } else if (arg.equals("--object-factory")) { - String objectFactoryClassName = removeArgFor(arg, args); - parsedOptions.setObjectFactoryClass(parseObjectFactory(objectFactoryClassName)); - } else if (arg.startsWith("-")) { - out.println("Unknown option: " + arg); - printUsage(); - exitCode = 1; - return parsedOptions; - } else if (!arg.isEmpty()) { - if (arg.startsWith("@")) { - Path rerunFile = Paths.get(arg.substring(1)); - parsedOptions.addRerun(parseFeatureWithLinesFile(rerunFile)); - } else { - FeatureWithLines featureWithLines = FeatureWithLines.parse(arg); - parsedOptions.addFeature(featureWithLines); - } - } - } - return parsedOptions; - } - - private String removeArgFor(String arg, List args) { - if (!args.isEmpty()) { - return args.remove(0); - } - printUsage(); - throw new CucumberException("Missing argument for " + arg); - } - - public Optional exitStatus() { - return Optional.of(exitCode); - } } diff --git a/core/src/test/java/io/cucumber/core/options/CommandlineOptionsParserTest.java b/core/src/test/java/io/cucumber/core/options/CommandlineOptionsParserTest.java index 79f5ae068d..348e4faab4 100644 --- a/core/src/test/java/io/cucumber/core/options/CommandlineOptionsParserTest.java +++ b/core/src/test/java/io/cucumber/core/options/CommandlineOptionsParserTest.java @@ -27,7 +27,6 @@ import java.net.URI; import java.time.Clock; import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -52,6 +51,7 @@ import static org.hamcrest.collection.IsMapContaining.hasEntry; import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.StringStartsWith.startsWith; +import static org.hamcrest.text.MatchesPattern.matchesPattern; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -117,7 +117,9 @@ public void stop() { @Test void has_version_from_properties_file() { - assertTrue(CommandlineOptionsParser.VERSION.matches("\\d+\\.\\d+\\.\\d+(-RC\\d+)?(-SNAPSHOT)?")); + parser.parse("--version"); + assertThat(output(), matchesPattern("\\d+\\.\\d+\\.\\d+(-RC\\d+)?(-SNAPSHOT)?\n")); + assertThat(parser.exitStatus(), is(Optional.of((byte) 0x0))); } @Test @@ -358,7 +360,7 @@ void clobbers_line_filters_from_cli_if_tags_are_specified_in_env() { @Test void fail_on_unsupported_options() { parser - .parse(asList("-concreteUnsupportedOption", "somewhere", "somewhere_else")) + .parse("-concreteUnsupportedOption", "somewhere", "somewhere_else") .build(); assertThat(output(), startsWith("Unknown option: -concreteUnsupportedOption")); assertThat(parser.exitStatus(), is(Optional.of((byte) 0x1))); @@ -367,7 +369,7 @@ void fail_on_unsupported_options() { @Test void threads_default_1() { RuntimeOptions options = parser - .parse(Collections.emptyList()) + .parse() .build(); assertThat(options.getThreads(), is(1)); } @@ -417,7 +419,7 @@ void set_strict_on_strict_aware_formatters() { @Test void ensure_default_snippet_type_is_underscore() { RuntimeOptions runtimeOptions = parser - .parse(Collections.emptyList()) + .parse() .build(); RuntimeOptions options = new CucumberPropertiesParser() .parse(properties) @@ -428,7 +430,7 @@ void ensure_default_snippet_type_is_underscore() { @Test void order_type_default_none() { RuntimeOptions options = parser - .parse(Collections.emptyList()) + .parse() .build(); Pickle a = createPickle("file:path/file1.feature", "a"); Pickle b = createPickle("file:path/file2.feature", "b");