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

Check that --should-stop=ifError=FLOW is set when EP is set up using the -Xplugin integration #4644

Merged
merged 1 commit into from
Oct 28, 2024
Merged
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 @@ -84,6 +84,7 @@ static void addTaskListener(
JavacTask javacTask, ScannerSupplier scannerSupplier, ErrorProneOptions errorProneOptions) {
Context context = ((BasicJavacTask) javacTask).getContext();
checkCompilePolicy(Options.instance(context).get("compilePolicy"));
checkShouldStopIfErrorPolicy(Options.instance(context).get("should-stop.ifError"));
setupMessageBundle(context);
RefactoringCollection[] refactoringCollection = {null};
javacTask.addTaskListener(
Expand Down Expand Up @@ -196,21 +197,28 @@ private static ImmutableList<String> setCompilePolicyToByFile(ImmutableList<Stri
return ImmutableList.<String>builder().addAll(args).add("-XDcompilePolicy=simple").build();
}

private static void checkShouldStopIfErrorPolicy(String arg) {
String value = arg.substring(arg.lastIndexOf('=') + 1);
private static void checkShouldStopIfErrorPolicy(String value) {
if (value == null) {
throw new InvalidCommandLineOptionException(
"The default --should-stop=ifError policy (INIT) is not supported by Error Prone,"
+ " pass --should-stop=ifError=FLOW instead");
}
CompileState state = CompileState.valueOf(value);
if (CompileState.FLOW.isAfter(state)) {
throw new InvalidCommandLineOptionException(
String.format(
"%s is not supported by Error Prone, pass --should-stop=ifError=FLOW instead", arg));
"--should-stop=ifError=%s is not supported by Error Prone, pass"
+ " --should-stop=ifError=FLOW instead",
value));
}
}

private static ImmutableList<String> setShouldStopIfErrorPolicyToFlow(
ImmutableList<String> args) {
for (String arg : args) {
if (arg.startsWith("--should-stop=ifError") || arg.startsWith("-XDshould-stop.ifError")) {
checkShouldStopIfErrorPolicy(arg);
String value = arg.substring(arg.lastIndexOf('=') + 1);
checkShouldStopIfErrorPolicy(value);
return args; // don't do anything if a valid policy is already set
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,6 @@ public void stopPolicy_init_xD() {
InvalidCommandLineOptionException.class,
() ->
compiler.compile(new String[] {"-XDshould-stop.ifError=INIT"}, ImmutableList.of()));
assertThat(e).hasMessageThat().contains("-XDshould-stop.ifError=INIT is not supported");
assertThat(e).hasMessageThat().contains("--should-stop=ifError=INIT is not supported");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public void hello() throws IOException {
null,
fileManager,
diagnosticCollector,
ImmutableList.of("-Xplugin:ErrorProne", "-XDcompilePolicy=byfile"),
ImmutableList.of(
"-Xplugin:ErrorProne", "-XDcompilePolicy=byfile", "--should-stop=ifError=FLOW"),
ImmutableList.of(),
fileManager.getJavaFileObjects(source));
assertThat(task.call()).isFalse();
Expand Down Expand Up @@ -144,7 +145,8 @@ public void applyFixes() throws IOException {
ImmutableList.of(
"-Xplugin:ErrorProne"
+ " -XepPatchChecks:MissingOverride -XepPatchLocation:IN_PLACE",
"-XDcompilePolicy=byfile"),
"-XDcompilePolicy=byfile",
"--should-stop=ifError=FLOW"),
ImmutableList.of(),
fileManager.getJavaFileObjects(fileA, fileB));
assertWithMessage(Joiner.on('\n').join(diagnosticCollector.getDiagnostics()))
Expand Down Expand Up @@ -203,7 +205,8 @@ public void applyToPatchFile() throws IOException {
"-Xplugin:ErrorProne"
+ " -XepPatchChecks:MissingOverride -XepPatchLocation:"
+ patchDir,
"-XDcompilePolicy=byfile"),
"-XDcompilePolicy=byfile",
"--should-stop=ifError=FLOW"),
ImmutableList.of(),
fileManager.getJavaFileObjects(fileA, fileB));
assertWithMessage(Joiner.on('\n').join(diagnosticCollector.getDiagnostics()))
Expand Down Expand Up @@ -254,7 +257,8 @@ public void explicitBadPolicyGiven() throws IOException {
new PrintWriter(sw, true),
fileManager,
diagnosticCollector,
ImmutableList.of("-XDcompilePolicy=bytodo", "-Xplugin:ErrorProne"),
ImmutableList.of(
"-XDcompilePolicy=bytodo", "--should-stop=ifError=FLOW", "-Xplugin:ErrorProne"),
ImmutableList.of(),
fileManager.getJavaFileObjects(source));
RuntimeException expected = assertThrows(RuntimeException.class, () -> task.call());
Expand Down Expand Up @@ -375,7 +379,8 @@ public void compilesWithFix() throws IOException {
diagnosticCollector,
ImmutableList.of(
"-Xplugin:ErrorProne -XepDisableAllChecks -Xep:TestCompilesWithFix:ERROR",
"-XDcompilePolicy=byfile"),
"-XDcompilePolicy=byfile",
"--should-stop=ifError=FLOW"),
ImmutableList.of(),
fileManager.getJavaFileObjects(source));
assertThat(task.call()).isFalse();
Expand All @@ -385,4 +390,49 @@ public void compilesWithFix() throws IOException {
.collect(onlyElement());
assertThat(diagnostic.getMessage(ENGLISH)).contains("[TestCompilesWithFix]");
}

@Test
public void noShouldStopIfErrorPolicy() throws IOException {
FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
Path source = fileSystem.getPath("Test.java");
Files.writeString(source, "class Test {}");
JavacFileManager fileManager = new JavacFileManager(new Context(), false, UTF_8);
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
StringWriter sw = new StringWriter();
JavacTask task =
JavacTool.create()
.getTask(
new PrintWriter(sw, true),
fileManager,
diagnosticCollector,
ImmutableList.of("-Xplugin:ErrorProne", "-XDcompilePolicy=byfile"),
ImmutableList.of(),
fileManager.getJavaFileObjects(source));
RuntimeException expected = assertThrows(RuntimeException.class, task::call);
assertThat(expected)
.hasMessageThat()
.contains("The default --should-stop=ifError policy (INIT) is not supported");
}

@Test
public void shouldStopIfErrorPolicyInit() throws IOException {
FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
Path source = fileSystem.getPath("Test.java");
Files.writeString(source, "class Test {}");
JavacFileManager fileManager = new JavacFileManager(new Context(), false, UTF_8);
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
StringWriter sw = new StringWriter();
JavacTask task =
JavacTool.create()
.getTask(
new PrintWriter(sw, true),
fileManager,
diagnosticCollector,
ImmutableList.of(
"-Xplugin:ErrorProne", "-XDcompilePolicy=byfile", "--should-stop=ifError=INIT"),
ImmutableList.of(),
fileManager.getJavaFileObjects(source));
RuntimeException expected = assertThrows(RuntimeException.class, task::call);
assertThat(expected).hasMessageThat().contains("--should-stop=ifError=INIT is not supported");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ public void memoizeCannotAccessTreePath() throws IOException {
ImmutableList.of(
"-Xplugin:ErrorProne -XepDisableAllChecks"
+ " -Xep:CheckThatTriesToMemoizeBasedOnTreePath:ERROR",
"-XDcompilePolicy=byfile"),
"-XDcompilePolicy=byfile",
"--should-stop=ifError=FLOW"),
ImmutableList.of(),
fileManager.getJavaFileObjects(source));
assertThat(task.call()).isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ public void asciiSub() {
null,
fileManager,
diagnosticCollector,
ImmutableList.of("-Xplugin:ErrorProne", "-XDcompilePolicy=simple"),
ImmutableList.of(
"-Xplugin:ErrorProne", "-XDcompilePolicy=simple", "--should-stop=ifError=FLOW"),
ImmutableList.of(),
ImmutableList.of(
new SimpleJavaFileObject(
Expand Down
Loading