Fix #438 Create Java .class files for javac in a temporary directory

This commit is contained in:
w0rp 2017-03-31 20:14:53 +01:00
parent 7a8dbe1139
commit 7c736579b7
5 changed files with 78 additions and 0 deletions

View File

@ -9,8 +9,12 @@ function! ale_linters#java#javac#GetCommand(buffer) abort
\ ? '-cp ' . g:ale_java_javac_classpath \ ? '-cp ' . g:ale_java_javac_classpath
\ : '' \ : ''
" Create .class files in a temporary directory, which we will delete later.
let l:class_file_directory = ale#engine#CreateDirectory(a:buffer)
return 'javac -Xlint ' return 'javac -Xlint '
\ . l:cp_option \ . l:cp_option
\ . ' -d ' . fnameescape(l:class_file_directory)
\ . ' ' . g:ale_java_javac_options \ . ' ' . g:ale_java_javac_options
\ . ' %t' \ . ' %t'
endfunction endfunction

View File

@ -152,6 +152,17 @@ function! ale#engine#ManageDirectory(buffer, directory) abort
call add(g:ale_buffer_info[a:buffer].temporary_directory_list, a:directory) call add(g:ale_buffer_info[a:buffer].temporary_directory_list, a:directory)
endfunction endfunction
" Create a new temporary directory and manage it in one go.
function! ale#engine#CreateDirectory(buffer) abort
let l:temporary_directory = tempname()
" Create the temporary directory for the file, unreadable by 'other'
" users.
call mkdir(l:temporary_directory, '', 0750)
call ale#engine#ManageDirectory(a:buffer, l:temporary_directory)
return l:temporary_directory
endfunction
function! ale#engine#RemoveManagedFiles(buffer) abort function! ale#engine#RemoveManagedFiles(buffer) abort
if !has_key(g:ale_buffer_info, a:buffer) if !has_key(g:ale_buffer_info, a:buffer)
return return

View File

@ -637,6 +637,16 @@ ale#Queue(delay, [linting_flag]) *ale#Queue()*
run. Linters with `lint_file` set to `1` are not run by default. run. Linters with `lint_file` set to `1` are not run by default.
ale#engine#CreateDirectory(buffer) *ale#engine#CreateDirectory()*
Create a new temporary directory with a unique name, and manage that
directory with |ale#engine#ManageDirectory()|, so it will be removed as
soon as possible.
It is advised to only call this function from a callback function for
returning a linter command to run.
ale#engine#EscapeCommandPart(command_part) *ale#engine#EscapeCommandPart()* ale#engine#EscapeCommandPart(command_part) *ale#engine#EscapeCommandPart()*
Given a |String|, return a |String| with all `%` characters replaced with Given a |String|, return a |String| with all `%` characters replaced with

View File

@ -0,0 +1,33 @@
Before:
runtime ale_linters/java/javac.vim
call ale#engine#InitBufferInfo(bufnr(''))
After:
call ale#linter#Reset()
" We need to clean up the buffer to remove the temporary directories created
" for the command.
call ale#cleanup#Buffer(bufnr(''))
let g:ale_java_javac_options = ''
let g:ale_java_javac_classpath = ''
Execute(The javac callback should return the correct default value):
let b:command = ale_linters#java#javac#GetCommand(bufnr(''))
Assert match(b:command, '\v^javac +-Xlint +-d +/tmp/[0-9a-zA-Z/]+ +\%t$') >= 0,
\ 'Invalid command string: ' . b:command
Execute(The javac callback should use g:ale_java_javac_classpath correctly):
let g:ale_java_javac_classpath = 'foo.jar'
let b:command = ale_linters#java#javac#GetCommand(bufnr(''))
Assert match(b:command, '\v^javac +-Xlint -cp foo\.jar +-d +/tmp/[0-9a-zA-Z/]+ +\%t$') >= 0,
\ 'Invalid command string: ' . b:command
Execute(The javac callback should use g:ale_java_javac_options correctly):
let g:ale_java_javac_options = '--anything --else'
let b:command = ale_linters#java#javac#GetCommand(bufnr(''))
Assert match(b:command, '\v^javac +-Xlint +-d +/tmp/[0-9a-zA-Z/]+ --anything --else +\%t$') >= 0,
\ 'Invalid command string: ' . b:command

View File

@ -81,3 +81,23 @@ Execute(ALE should delete managed files when the buffer is removed):
Assert !filereadable(g:filename), 'The tempoary file was not deleted' Assert !filereadable(g:filename), 'The tempoary file was not deleted'
Assert !isdirectory(g:directory), 'The tempoary directory was not deleted' Assert !isdirectory(g:directory), 'The tempoary directory was not deleted'
Assert isdirectory(g:preserved_directory), 'The tempoary directory was not kept' Assert isdirectory(g:preserved_directory), 'The tempoary directory was not kept'
Execute(ALE should create and delete directories for ale#engine#CreateDirectory()):
call ale#engine#InitBufferInfo(bufnr('%'))
let b:dir = ale#engine#CreateDirectory(bufnr('%'))
let b:dir2 = ale#engine#CreateDirectory(bufnr('%'))
Assert isdirectory(b:dir), 'The directory was not created'
" We should get the correct file permissions.
" We want to ensure that the directory is not readable by 'other'
AssertEqual 'rwxr-x---', getfperm(b:dir)
" The two directories shouldn't be the same.
AssertNotEqual b:dir2, b:dir
call ale#cleanup#Buffer(bufnr('%'))
Assert !isdirectory(b:dir), 'The directory was not deleted'
Assert !isdirectory(b:dir2), 'The second directory was not deleted'