/ test / test_format_command.vader
test_format_command.vader
  1  Before:
  2    silent! cd /testplugin/test
  3    silent file top/middle/bottom/dummy.txt
  4  
  5    function! CheckTempFile(filename) abort
  6      " Check every part of the temporary filename, except the random part.
  7      AssertEqual fnamemodify(tempname(), ':h'), fnamemodify(a:filename, ':h:h')
  8      AssertEqual 'dummy.txt', fnamemodify(a:filename, ':t')
  9    endfunction
 10  
 11    runtime autoload/ale/command.vim
 12  
 13    function! ale#command#CreateTempFile(buffer, temporary_file, input) abort
 14      return !empty(a:temporary_file)
 15    endfunction
 16  
 17  After:
 18    unlet! g:result
 19    unlet! g:match
 20  
 21    delfunction CheckTempFile
 22  
 23    runtime autoload/ale/command.vim
 24  
 25  Execute(FormatCommand should do nothing to basic command strings):
 26    AssertEqual
 27    \ ['', 'awesome-linter do something', 0],
 28    \ ale#command#FormatCommand(bufnr('%'), '', 'awesome-linter do something', 0, v:null, v:null, [])
 29  
 30  Execute(FormatCommand should handle %%, and ignore other percents):
 31    AssertEqual
 32    \ ['', '% %%d %%f %x %', 0],
 33    \ ale#command#FormatCommand(bufnr('%'), '', '%% %%%d %%%f %x %', 0, v:null, v:null, [])
 34  
 35  Execute(FormatCommand should convert %s to the current filename):
 36    AssertEqual
 37    \ [
 38    \   '',
 39    \   'foo ' . ale#Escape(expand('%:p')) . ' bar ' . ale#Escape(expand('%:p')),
 40    \   0,
 41    \ ],
 42    \ ale#command#FormatCommand(bufnr('%'), '', 'foo %s bar %s', 0, v:null, v:null, [])
 43  
 44  Execute(FormatCommand should convert %t to a new temporary filename):
 45    let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo %t bar %t', 0, v:null, v:null, [])
 46  
 47    call CheckTempFile(g:result[0])
 48  
 49    let g:match = matchlist(g:result[1], '\v^foo (.*) bar (.*)$')
 50  
 51    Assert !empty(g:match), 'No match found! Result was: ' . g:result[1]
 52    " The first item of the result should be a temporary filename, and it should
 53    " be the same as the escaped name in the command string.
 54    AssertEqual ale#Escape(g:result[0]), g:match[1]
 55    " The two temporary filenames formatted in should be the same.
 56    AssertEqual g:match[1], g:match[2]
 57  
 58  Execute(FormatCommand should not convert %t to a new temporary filename when the input is given as v:false):
 59    let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo %t bar %t', 0, v:false, v:null, [])
 60  
 61    AssertEqual ['', 'foo %t bar %t', 0], g:result
 62  
 63  Execute(FormatCommand should signal that files are created when temporary files are needed):
 64    AssertEqual
 65    \ 1,
 66    \ ale#command#FormatCommand(bufnr('%'), '', 'foo %t', 0, v:null, v:null, [])[2]
 67  
 68    AssertEqual
 69    \ 0,
 70    \ ale#command#FormatCommand(bufnr('%'), '', 'foo %s', 0, v:null, v:null, [])[2]
 71  
 72  Execute(FormatCommand should let you combine %s and %t):
 73    let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo %t bar %s', 0, v:null, v:null, [])
 74  
 75    call CheckTempFile(g:result[0])
 76  
 77    let g:match = matchlist(g:result[1], '\v^foo (.*) bar (.*)$')
 78  
 79    Assert !empty(g:match), 'No match found! Result was: ' . g:result[1]
 80    " The first item of the result should be a temporary filename, and it should
 81    " be the same as the escaped name in the command string.
 82    AssertEqual ale#Escape(g:result[0]), g:match[1]
 83    " The second item should be equal to the original filename.
 84    AssertEqual ale#Escape(expand('%:p')), g:match[2]
 85  
 86  Execute(FormatCommand should replace %e with the escaped executable):
 87    if has('win32')
 88      AssertEqual
 89      \ ['', 'foo foo', 0],
 90      \ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, v:null, v:null, [])
 91      AssertEqual
 92      \ ['', '"foo bar"', 0],
 93      \ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, v:null, v:null, [])
 94      AssertEqual
 95      \ ['', '%e %e', 0],
 96      \ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, v:null, v:null, [])
 97    else
 98      AssertEqual
 99      \ ['', '''foo'' ''foo''', 0],
100      \ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, v:null, v:null, [])
101      AssertEqual
102      \ ['', '''foo bar''', 0],
103      \ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, v:null, v:null, [])
104      AssertEqual
105      \ ['', '%e %e', 0],
106      \ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, v:null, v:null, [])
107    endif
108  
109  Execute(EscapeCommandPart should escape all percent signs):
110    AssertEqual '%%s %%t %%%% %%s %%t %%%%', ale#engine#EscapeCommandPart('%s %t %% %s %t %%')
111  
112  Execute(EscapeCommandPart should pipe in temporary files appropriately):
113    let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo bar', 1, v:null, v:null, [])
114  
115    call CheckTempFile(g:result[0])
116  
117    let g:match = matchlist(g:result[1], '\v^foo bar \< (.*)$')
118    Assert !empty(g:match), 'No match found! Result was: ' . g:result[1]
119    AssertEqual ale#Escape(g:result[0]), g:match[1]
120  
121    let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo bar %t', 1, v:null, v:null, [])
122  
123    call CheckTempFile(g:result[0])
124  
125    let g:match = matchlist(g:result[1], '\v^foo bar (.*)$')
126    Assert !empty(g:match), 'No match found! Result was: ' . g:result[1]
127    AssertEqual ale#Escape(g:result[0]), g:match[1]
128  
129  Execute(FormatCommand should apply filename modifiers to the current file):
130    AssertEqual
131    \ ale#Escape(expand('%:p:h'))
132    \   . ' ' . ale#Escape('dummy.txt')
133    \   . ' ' . ale#Escape(expand('%:p:h:t'))
134    \   . ' ' . ale#Escape('txt')
135    \   . ' ' . ale#Escape(expand('%:p:r')),
136    \ ale#command#FormatCommand(bufnr(''), '', '%s:h %s:t %s:h:t %s:e %s:r', 0, v:null, v:null, [])[1]
137  
138  Execute(FormatCommand should apply filename modifiers to the temporary file):
139    let g:result = ale#command#FormatCommand(bufnr(''), '', '%t:h %t:t %t:h:t %t:e %t:r', 0, v:null, v:null, [])
140  
141    AssertEqual
142    \ ale#Escape(fnamemodify(g:result[0], ':h'))
143    \   . ' ' . ale#Escape('dummy.txt')
144    \   . ' ' . ale#Escape(fnamemodify(g:result[0], ':h:t'))
145    \   . ' ' . ale#Escape('txt')
146    \   . ' ' . ale#Escape(fnamemodify(g:result[0], ':r')),
147    \ g:result[1]
148  
149  Execute(FormatCommand should apply filename mappings the current file):
150    let g:result = ale#command#FormatCommand(bufnr('%'), '', '%s', 0, v:null, v:null, [
151    \ [expand('%:p:h'), '/foo/bar'],
152    \])
153  
154    Assert g:result[1] =~# '/foo/bar'
155  
156  Execute(FormatCommand should apply filename mappings to temporary files):
157    let g:result = ale#command#FormatCommand(bufnr('%'), '', '%t', 0, v:null, v:null, [
158    \ [fnamemodify(tempname(), ':h:h'), '/foo/bar']
159    \])
160  
161    Assert g:result[1] =~# '/foo/bar'
162  
163  Execute(FormatCommand should apply filename modifiers to mapped filenames):
164    let g:result = ale#command#FormatCommand(bufnr('%'), '', '%s:h', 0, v:null, v:null, [
165    \ [expand('%:p:h'), '/foo/bar'],
166    \])
167  
168    AssertEqual ale#Escape('/foo/bar'), g:result[1]
169  
170    let g:result = ale#command#FormatCommand(bufnr('%'), '', '%t:h:h:h', 0, v:null, v:null, [
171    \ [fnamemodify(tempname(), ':h:h'), '/foo/bar']
172    \])
173  
174    AssertEqual ale#Escape('/foo/bar'), g:result[1]
175  
176  Execute(FormatCommand should apply regular cwd paths):
177    AssertEqual
178    \ 'cd ' . (has('unix') ? '' : '/d ') . ale#Escape('/foo /bar') . ' && abc',
179    \ ale#command#FormatCommand(bufnr('%'), '', 'abc', 0, v:null, '/foo /bar', [])[1]
180    \
181  Execute(FormatCommand should apply cwd substitution and formatting):
182    call ale#test#SetFilename('foo.txt')
183  
184    AssertEqual
185    \ 'cd ' . (has('unix') ? '' : '/d ') . ale#Escape(getcwd()) . ' && abc',
186    \ ale#command#FormatCommand(bufnr('%'), '', 'abc', 0, v:null, '%s:h', [])[1]