Skip to content
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

Support running all available patch checks #947

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,7 @@ abstract static class Builder {

abstract Builder importOrganizer(ImportOrganizer importOrganizer);

abstract PatchingOptions autoBuild();

final PatchingOptions build() {

PatchingOptions patchingOptions = autoBuild();

// If anything is specified, then (checkers or refaster) and output must be set.
if ((!patchingOptions.namedCheckers().isEmpty()
|| patchingOptions.customRefactorer().isPresent())
^ patchingOptions.doRefactor()) {
throw new InvalidCommandLineOptionException(
"-XepPatchChecks and -XepPatchLocation must be specified together");
}
return patchingOptions;
}
abstract PatchingOptions build();
}
}

Expand Down Expand Up @@ -390,6 +376,8 @@ public static ErrorProneOptions processArgs(Iterable<String> args) {
* You can pass the IGNORE_UNKNOWN_CHECKS_FLAG to opt-out of that checking. This allows you to
* use command lines from different versions of error-prone interchangeably.
*/
boolean patchLocationSet = false;
boolean patchCheckSet = false;
Builder builder = new Builder();
for (String arg : args) {
switch (arg) {
Expand Down Expand Up @@ -423,6 +411,7 @@ public static ErrorProneOptions processArgs(Iterable<String> args) {
} else if (arg.startsWith(ErrorProneFlags.PREFIX)) {
builder.parseFlag(arg);
} else if (arg.startsWith(PATCH_OUTPUT_LOCATION)) {
patchLocationSet = true;
String remaining = arg.substring(PATCH_OUTPUT_LOCATION.length());
if (remaining.equals("IN_PLACE")) {
builder.patchingOptionsBuilder().inPlace(true);
Expand All @@ -433,6 +422,7 @@ public static ErrorProneOptions processArgs(Iterable<String> args) {
builder.patchingOptionsBuilder().baseDirectory(remaining);
}
} else if (arg.startsWith(PATCH_CHECKS_PREFIX)) {
patchCheckSet = true;
String remaining = arg.substring(PATCH_CHECKS_PREFIX.length());
if (remaining.startsWith("refaster:")) {
// Refaster rule, load from InputStream at file
Expand All @@ -450,7 +440,8 @@ public static ErrorProneOptions processArgs(Iterable<String> args) {
}
});
} else {
Iterable<String> checks = Splitter.on(',').trimResults().split(remaining);
Iterable<String> checks =
Splitter.on(',').trimResults().omitEmptyStrings().split(remaining);
builder.patchingOptionsBuilder().namedCheckers(ImmutableSet.copyOf(checks));
}
} else if (arg.startsWith(PATCH_IMPORT_ORDER_PREFIX)) {
Expand All @@ -470,6 +461,11 @@ public static ErrorProneOptions processArgs(Iterable<String> args) {
}
}

if (patchCheckSet && !patchLocationSet) {
throw new InvalidCommandLineOptionException(
"-XepPatchLocation must be specified when -XepPatchChecks is");
}

return builder.build(remainingArgs.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,6 @@ public void recognizesPatch() {

@Test
public void throwsExceptionWithBadPatchArgs() {
assertThrows(
InvalidCommandLineOptionException.class,
() -> ErrorProneOptions.processArgs(new String[] {"-XepPatchLocation:IN_PLACE"}));
assertThrows(
InvalidCommandLineOptionException.class,
() ->
Expand All @@ -238,6 +235,24 @@ public void recognizesRefaster() {
assertThat(options.patchingOptions().customRefactorer()).isPresent();
}

@Test
public void understandsEmptySetOfNamedCheckers() {
ErrorProneOptions options =
ErrorProneOptions.processArgs(new String[] {"-XepPatchLocation:IN_PLACE"});
assertThat(options.patchingOptions().doRefactor()).isTrue();
assertThat(options.patchingOptions().inPlace()).isTrue();
assertThat(options.patchingOptions().namedCheckers()).isEmpty();
assertThat(options.patchingOptions().customRefactorer()).isAbsent();

options =
ErrorProneOptions.processArgs(
new String[] {"-XepPatchLocation:IN_PLACE", "-XepPatchChecks:"});
assertThat(options.patchingOptions().doRefactor()).isTrue();
assertThat(options.patchingOptions().inPlace()).isTrue();
assertThat(options.patchingOptions().namedCheckers()).isEmpty();
assertThat(options.patchingOptions().customRefactorer()).isAbsent();
}

@Test
public void importOrder_staticFirst() {
ErrorProneOptions options =
Expand Down