-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Add command line option support for Android #597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SierraGolf
merged 10 commits into
cucumber:master
from
friederbluemle:android-cmd-line-options
Nov 2, 2013
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a1d2478
Remove trailing whitespace
friederbluemle f64b1c3
Add command line option support for Android
friederbluemle 08a99c5
Add tests for InstrumentationArguments
friederbluemle f1bfa94
Add support for 'count' option
friederbluemle c55f0d0
Add support for 'debug' option
friederbluemle be024b8
Remove duplicate code for 'debug' option
friederbluemle b518300
Add support for 'coverage' option
friederbluemle 72500a0
Add support for 'log' option
friederbluemle 88a0451
Move option parsing to InstrumentationArguments
friederbluemle ce52f30
Add tests for new argument options
friederbluemle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
android/src/main/java/cucumber/runtime/android/InstrumentationArguments.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package cucumber.runtime.android; | ||
|
||
import android.os.Bundle; | ||
|
||
/** | ||
* This is a wrapper class around the command line arguments that were supplied | ||
* when the instrumentation was started. | ||
*/ | ||
public final class InstrumentationArguments { | ||
private static final String KEY_DEBUG = "debug"; | ||
private static final String KEY_LOG = "log"; | ||
private static final String KEY_COUNT = "count"; | ||
private static final String KEY_COVERAGE = "coverage"; | ||
private static final String KEY_COVERAGE_FILE_PATH = "coverageFile"; | ||
private static final String VALUE_SEPARATOR = "--"; | ||
|
||
private Bundle arguments; | ||
|
||
public InstrumentationArguments(Bundle arguments) { | ||
this.arguments = arguments != null ? arguments : new Bundle(); | ||
} | ||
|
||
private boolean getBooleanArgument(String tag) { | ||
String tagString = arguments.getString(tag); | ||
return tagString != null && Boolean.parseBoolean(tagString); | ||
} | ||
|
||
private void appendOption(StringBuilder sb, String optionKey, String optionValue) { | ||
for (String value : optionValue.split(VALUE_SEPARATOR)) { | ||
sb.append(sb.length() == 0 || optionKey.isEmpty() ? "" : " ").append(optionKey).append(optionValue.isEmpty() ? "" : " " + value); | ||
} | ||
} | ||
|
||
/** | ||
* Returns a Cucumber options compatible string based on the argument extras found. | ||
* <p /> | ||
* The bundle <em>cannot</em> contain multiple entries for the same key, | ||
* however certain Cucumber options can be passed multiple times (e.g. | ||
* {@code --tags}). The solution is to pass values separated by | ||
* {@link InstrumentationArguments#VALUE_SEPARATOR} which will result | ||
* in multiple {@code --key value} pairs being created. | ||
* | ||
* @return the cucumber options string | ||
*/ | ||
public String getCucumberOptionsString() { | ||
String cucumberOptions = arguments.getString("cucumberOptions"); | ||
if (cucumberOptions != null) { | ||
return cucumberOptions; | ||
} | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
String features = ""; | ||
for (String key : arguments.keySet()) { | ||
if ("glue".equals(key)) { | ||
appendOption(sb, "--glue", arguments.getString(key)); | ||
} else if ("format".equals(key)) { | ||
appendOption(sb, "--format", arguments.getString(key)); | ||
} else if ("tags".equals(key)) { | ||
appendOption(sb, "--tags", arguments.getString(key)); | ||
} else if ("name".equals(key)) { | ||
appendOption(sb, "--name", arguments.getString(key)); | ||
} else if ("dryRun".equals(key) && getBooleanArgument(key)) { | ||
appendOption(sb, "--dry-run", ""); | ||
} else if ("log".equals(key) && getBooleanArgument(key)) { | ||
appendOption(sb, "--dry-run", ""); | ||
} else if ("noDryRun".equals(key) && getBooleanArgument(key)) { | ||
appendOption(sb, "--no-dry-run", ""); | ||
} else if ("monochrome".equals(key) && getBooleanArgument(key)) { | ||
appendOption(sb, "--monochrome", ""); | ||
} else if ("noMonochrome".equals(key) && getBooleanArgument(key)) { | ||
appendOption(sb, "--no-monochrome", ""); | ||
} else if ("strict".equals(key) && getBooleanArgument(key)) { | ||
appendOption(sb, "--strict", ""); | ||
} else if ("noStrict".equals(key) && getBooleanArgument(key)) { | ||
appendOption(sb, "--no-strict", ""); | ||
} else if ("snippets".equals(key)) { | ||
appendOption(sb, "--snippets", arguments.getString(key)); | ||
} else if ("dotcucumber".equals(key)) { | ||
appendOption(sb, "--dotcucumber", arguments.getString(key)); | ||
} else if ("features".equals(key)) { | ||
features = arguments.getString(key); | ||
} | ||
} | ||
// Even though not strictly required, wait until everything else | ||
// has been added before adding any feature references | ||
appendOption(sb, "", features); | ||
return sb.toString(); | ||
} | ||
|
||
public boolean isDebugEnabled() { | ||
return getBooleanArgument(KEY_DEBUG); | ||
} | ||
|
||
public boolean isLogEnabled() { | ||
return getBooleanArgument(KEY_LOG); | ||
} | ||
|
||
public boolean isCountEnabled() { | ||
return getBooleanArgument(KEY_COUNT); | ||
} | ||
|
||
public boolean isCoverageEnabled() { | ||
return getBooleanArgument(KEY_COVERAGE); | ||
} | ||
|
||
public String getCoverageFilePath() { | ||
return arguments.getString(KEY_COVERAGE_FILE_PATH); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a little confused now on what the consensus is about multiple/single argument, but wanted to give a hint about this line of code anyway. I think you can inline this method by simply using
arguments.getBoolean(tag)
in case the default should be false, or usearguments.getBoolean(tag, true)
in case the default should be true.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind, I just double checked and found out that the Bundle helper methods are not working in this context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, unfortunately
arguments.getBoolean(tag, true)
cannot be used here, since it casts the value to a Boolean instead of parsing a String.