|
| 1 | +package org.javacs; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.nullValue; |
| 5 | +import static org.hamcrest.Matchers.notNullValue; |
| 6 | + |
| 7 | +import com.google.gson.JsonArray; |
| 8 | +import com.google.gson.JsonObject; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import org.javacs.lsp.Diagnostic; |
| 14 | +import org.javacs.lsp.DiagnosticSeverity; |
| 15 | +import org.javacs.lsp.DidChangeConfigurationParams; |
| 16 | +import org.junit.Before; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +public class ExtraCompilerArgsTest { |
| 20 | + private static final Path WORKSPACE_ROOT = Paths.get("src/test/examples/compiler-args-project").normalize(); |
| 21 | + private static final Path FILE = WORKSPACE_ROOT.resolve("UsesPreviewStringTemplate.java").normalize(); |
| 22 | + |
| 23 | + private final List<Diagnostic> diagnostics = new ArrayList<>(); |
| 24 | + |
| 25 | + @Before |
| 26 | + public void clearDiagnostics() { |
| 27 | + diagnostics.clear(); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void reportsErrorWithoutExtraCompilerArgs() { |
| 32 | + var server = LanguageServerFixture.getJavaLanguageServer(WORKSPACE_ROOT, diagnostics::add); |
| 33 | + |
| 34 | + server.lint(List.of(FILE)); |
| 35 | + |
| 36 | + assertThat(firstError(), notNullValue()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void appliesExtraCompilerArgsFromSettings() { |
| 41 | + var server = LanguageServerFixture.getJavaLanguageServer(WORKSPACE_ROOT, diagnostics::add); |
| 42 | + var change = new DidChangeConfigurationParams(); |
| 43 | + var settings = new JsonObject(); |
| 44 | + var java = new JsonObject(); |
| 45 | + var args = new JsonArray(); |
| 46 | + args.add("--enable-preview"); |
| 47 | + args.add("-source"); |
| 48 | + args.add("21"); |
| 49 | + java.add("extraCompilerArgs", args); |
| 50 | + settings.add("java", java); |
| 51 | + change.settings = settings; |
| 52 | + |
| 53 | + server.didChangeConfiguration(change); |
| 54 | + server.lint(List.of(FILE)); |
| 55 | + |
| 56 | + assertThat(firstError(), nullValue()); |
| 57 | + } |
| 58 | + |
| 59 | + private Diagnostic firstError() { |
| 60 | + Diagnostic error = null; |
| 61 | + for (var diagnostic : diagnostics) { |
| 62 | + if (diagnostic.severity == DiagnosticSeverity.Error) { |
| 63 | + error = diagnostic; |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + return error; |
| 68 | + } |
| 69 | +} |
0 commit comments