Skip to content

Commit

Permalink
Reduce exposed api
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje committed Apr 23, 2020
1 parent e62d83a commit de45dda
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -44,11 +44,108 @@ public CommandlineOptionsParser(OutputStream outputStream) {
out = new PrintWriter(outputStream, true);
}

public Optional<Byte> exitStatus() {
return Optional.of(exitCode);
}

public RuntimeOptionsBuilder parse(String... args) {
return parse(Arrays.asList(args));
}

private RuntimeOptionsBuilder parse(List<String> 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<String> 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))
Expand Down Expand Up @@ -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<String> 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<String> args) {
if (!args.isEmpty()) {
return args.remove(0);
}
printUsage();
throw new CucumberException("Missing argument for " + arg);
}

public Optional<Byte> exitStatus() {
return Optional.of(exitCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)));
Expand All @@ -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));
}
Expand Down Expand Up @@ -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)
Expand All @@ -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");
Expand Down

0 comments on commit de45dda

Please sign in to comment.