@Before public void setup() throws IOException { workspaceRoot = tempFolder.newFolder(); Path changedOutput = tempFolder.newFolder().toPath(); Path target = tempFolder.newFolder().toPath(); uriSupplier = new WorkspaceUriSupplier(workspaceRoot.toPath(), changedOutput); parser = GroovyTreeParser.of(() -> { GroovyWorkspaceCompiler compiler = GroovyWorkspaceCompiler.of(target, workspaceRoot.toPath(), changedOutput); assertEquals(Sets.newHashSet(), compiler.compile(ImmutableSet.of())); return compiler.get(); }, workspaceRoot.toPath(), uriSupplier); }
/** * Creates a new instance of GroovyWorkspaceCompiler. * * @param targetDirectory the directory in which to put generated files * @param workspaceRoot the directory to compile * @param changedFilesRoot the directory in which to temporarily store incrementally changed files * @return the newly created GroovyWorkspaceCompiler */ @SuppressFBWarnings("PT_FINAL_TYPE_RETURN") public static GroovyWorkspaceCompiler of(Path targetDirectory, Path workspaceRoot, Path changedFilesRoot) { checkNotNull(targetDirectory, "targetDirectory must not be null"); checkNotNull(workspaceRoot, "workspaceRoot must not be null"); checkNotNull(changedFilesRoot, "changedFilesRoot must not be null"); checkArgument(targetDirectory.toFile().isDirectory(), "targetDirectory must be a directory"); checkArgument(workspaceRoot.toFile().isDirectory(), "workspaceRoot must be a directory"); checkArgument(changedFilesRoot.toFile().isDirectory(), "changedFilesRoot must be a directory"); CompilerConfiguration config = new CompilerConfiguration(); config.setTargetDirectory(targetDirectory.toFile()); GroovyWorkspaceCompiler workspaceCompiler = new GroovyWorkspaceCompiler(workspaceRoot, changedFilesRoot, config); workspaceCompiler.addAllSourcesToCompilationUnit(); return workspaceCompiler; }
@Override public Set<PublishDiagnosticsParams> compile(Set<URI> files) { try { unit.compile(); } catch (MultipleCompilationErrorsException e) { return parseErrors(e.getErrorCollector()); } return ImmutableSet.of(); }
assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); assertSingleSourceFileUri(catFile, compiler.get().iterator()); compiler.handleFileChanged(catFile.toURI(), Lists.newArrayList(new TextDocumentContentChangeEvent(Ranges.createRange(0, 6, 0, 9), 3, "Dog"))); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); assertSingleSourceFileUri(catChangedFile, compiler.get().iterator()); compiler.handleFileSaved(catFile.toURI(), Optional.absent()); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); assertSingleSourceFileUri(catChangedFile, compiler.get().iterator());
GroovyWorkspaceCompiler compiler = createGroovyWorkspaceCompiler(); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); assertSingleSourceFileUri(catFile, compiler.get().iterator()); compiler.handleFileChanged(catFile.toURI(), Lists.newArrayList(new TextDocumentContentChangeEvent(Ranges.createRange(0, 6, 0, 9), 3, "Dog"))); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); assertSingleSourceFileUri(catChangedFile, compiler.get().iterator()); compiler.handleFileClosed(catFile.toURI()); .isFalse(); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); assertSingleSourceFileUri(catFile, compiler.get().iterator());
assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); assertSingleSourceFileUri(catFile, compiler.get().iterator()); compiler.handleFileChanged(catFile.toURI(), Lists.newArrayList(new TextDocumentContentChangeEvent(Ranges.createRange(0, 6, 0, 9), 3, "Dog"))); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); assertSingleSourceFileUri(catChangedFile, compiler.get().iterator()); compiler.handleChangeWatchedFiles(Lists.newArrayList( new FileEvent( root.getRoot().toPath().relativize(catFile.toPath()).toString(), assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); assertSingleSourceFileUri(catFile, compiler.get().iterator());
GroovyWorkspaceCompiler.of(targetDirectory, workspaceRoot, changedFilesDirectory); TreeParser parser = GroovyTreeParser.of(compiler, workspaceRoot,
@Test public void testHandleChangeWatchedFiles_created() throws IOException { File newFolder1 = root.newFolder(); GroovyWorkspaceCompiler compiler = createGroovyWorkspaceCompiler(); // Compile assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); // Assert the compiler has no source units assertThat(compiler.get().iterator().hasNext()).isFalse(); File catFile = addFileToFolder(newFolder1, "Cat.groovy", "class Cat {\n" + "}\n"); // Call handleChangeWatchedFile with a change saying this file has been changed outside this language server compiler.handleChangeWatchedFiles(Lists.newArrayList( new FileEvent( root.getRoot().toPath().relativize(catFile.toPath()).toString(), FileChangeType.Created))); // Re-compile assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); // Assert the compilation unit now contains cat file assertSingleSourceFileUri(catFile, compiler.get().iterator()); }
GroovyWorkspaceCompiler compiler = createGroovyWorkspaceCompiler(); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); assertSingleSourceFileUri(catFile, compiler.get().iterator()); compiler.handleFileChanged(catFile.toURI(), Lists.newArrayList(new TextDocumentContentChangeEvent(Ranges.createRange(0, 6, 0, 9), 3, "Dog"))); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); assertSingleSourceFileUri(catChangedFile, compiler.get().iterator()); compiler.handleFileChanged(catFile.toURI(), Lists.newArrayList(new TextDocumentContentChangeEvent(Ranges.createRange(0, 6, 0, 9), 6, "Turtle"))); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); assertSingleSourceFileUri(catChangedFile, compiler.get().iterator());
@Test public void testEmptyWorkspace() throws InterruptedException, ExecutionException, IOException { GroovyWorkspaceCompiler compiler = createGroovyWorkspaceCompiler(); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); }
private void resetCompilationUnit() { try { FileUtils.deleteDirectory(config.getTargetDirectory()); } catch (IOException e) { logger.error("Could not delete directory '" + config.getTargetDirectory().toString() + "'", e); throw new RuntimeException("Could not reset compiled files after changes. " + "Make sure you have permission to modify your target directory."); } if (!config.getTargetDirectory().mkdir()) { logger.error("Could not recreate target directory: '{}'", config.getTargetDirectory().getAbsolutePath()); throw new RuntimeException("Could not reset compiled files after changes. " + "User may not have permission to modify target directory."); } unit = new CompilationUnit(config); addAllSourcesToCompilationUnit(); }
private GroovyWorkspaceCompiler createGroovyWorkspaceCompiler() { return GroovyWorkspaceCompiler.of(output.getRoot().toPath(), root.getRoot().toPath(), changedOutput.getRoot().toPath()); }
@Test public void testHandleChangeWatchedFiles_deleted() throws IOException { File newFolder1 = root.newFolder(); File catFile = addFileToFolder(newFolder1, "Cat.groovy", "class Cat {\n" + "}\n"); GroovyWorkspaceCompiler compiler = createGroovyWorkspaceCompiler(); // Compile assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); // Assert the original file is in the compilation unit assertSingleSourceFileUri(catFile, compiler.get().iterator()); // Delete the file assertThat(catFile.delete()).isTrue(); // Call handleChangeWatchedFile with a change saying this file has been deleted outside this language server compiler.handleChangeWatchedFiles(Lists.newArrayList( new FileEvent( root.getRoot().toPath().relativize(catFile.toPath()).toString(), FileChangeType.Changed))); // Re-compile assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); // Assert the compiler has no source units assertThat(compiler.get().iterator().hasNext()).isFalse(); }
@Test public void testCompile_WithJavaExtensionError() throws InterruptedException, ExecutionException, IOException { File test = addFileToFolder(root.getRoot(), "Test.java", "public class Test {" + "Foo foo;" + "}"); GroovyWorkspaceCompiler compiler = createGroovyWorkspaceCompiler(); Set<PublishDiagnosticsParams> diagnostics = compiler.compile(ImmutableSet.of()); assertEquals(Sets.newHashSet(new PublishDiagnosticsParams( test.toPath().toUri().toString(), ImmutableList.of(new Diagnostic(Ranges.createRange(0, 19, 0, 26), "unable to resolve class Foo \n @ line 1, column 20.", DiagnosticSeverity.Error, GroovyConstants.GROOVY_COMPILER)))), diagnostics); }
@Test public void testWorkspaceRootNotFolder() throws IOException { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("workspaceRoot must be a directory"); GroovyWorkspaceCompiler.of(output.getRoot().toPath(), root.newFile().toPath(), changedOutput.getRoot().toPath()); }
@Test public void testCompile_WithJavaExtensionAndGroovySyntax() throws InterruptedException, ExecutionException, IOException { addFileToFolder(root.getRoot(), "ExceptionNewNotSameName.java", "public class ExceptionNew {}"); GroovyWorkspaceCompiler compiler = createGroovyWorkspaceCompiler(); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); }
@Test public void testChangedFilesRootNotFolder() throws IOException { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("changedFilesRoot must be a directory"); GroovyWorkspaceCompiler.of(output.getRoot().toPath(), root.getRoot().toPath(), changedOutput.newFile().toPath()); }
@Test public void testCompile_WithJavaExtension() throws InterruptedException, ExecutionException, IOException { addFileToFolder(root.getRoot(), "test1.groovy", "class Coordinates {\n" + " double latitude\n" + " double longitude\n" + " double getAt(int idx) {\n" + " if (idx == 0) latitude\n" + " else if (idx == 1) longitude\n" + " else throw new ExceptionNew(\"Wrong coordinate index, use 0 or 1\")\n" + " }\n" + "}\n"); addFileToFolder(root.getRoot(), "ExceptionNew.java", "public class ExceptionNew {}"); GroovyWorkspaceCompiler compiler = createGroovyWorkspaceCompiler(); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); }
@Test public void testTargetDirectoryNotFolder() throws IOException { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("targetDirectory must be a directory"); GroovyWorkspaceCompiler.of(output.newFile().toPath(), root.getRoot().toPath(), changedOutput.getRoot().toPath()); }
@Test public void testCompile_withExtraFiles() throws InterruptedException, ExecutionException, IOException { File newFolder1 = root.newFolder(); File newFolder2 = root.newFolder(); addFileToFolder(newFolder1, "coordinates.groovy", "class Coordinates {\n" + " double latitude\n" + " double longitude\n" + " double getAt(int idx) {\n" + " if (idx == 0) latitude\n" + " else if (idx == 1) longitude\n" + " else throw new Exception(\"Wrong coordinate index, use 0 or 1\")\n" + " }\n" + "}\n"); addFileToFolder(newFolder2, "file.txt", "Something that is not groovy"); addFileToFolder(newFolder2, "Test.foo", "public class Test {}\n"); GroovyWorkspaceCompiler compiler = createGroovyWorkspaceCompiler(); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); }