aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/test_format_command.vader
blob: 27674cff692d8f448189cfa5a5686479998db2e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
Before:
  silent! cd /testplugin/test
  silent file top/middle/bottom/dummy.txt

  " Return the base temp directory safely. Vim 9.0+ uses a flat tempname()
  " format (e.g. C:\tmp\VIMXXXXXXXX) instead of C:\tmp\VIMxxx\NNN, so
  " fnamemodify(tempname(), ':h:h') may resolve to a filesystem root.
  function! GetTempBase() abort
    let l:base = fnamemodify(tempname(), ':h:h')

    if l:base is# fnamemodify(l:base, ':h')
      return fnamemodify(tempname(), ':h')
    endif

    return l:base
  endfunction

  function! CheckTempFile(filename) abort
    " Check every part of the temporary filename, except the random part.
    AssertEqual fnamemodify(tempname(), ':h'), fnamemodify(a:filename, ':h:h')
    AssertEqual 'dummy.txt', fnamemodify(a:filename, ':t')
  endfunction

  runtime autoload/ale/command.vim

  function! ale#command#CreateTempFile(buffer, temporary_file, input) abort
    return !empty(a:temporary_file)
  endfunction

After:
  unlet! g:result
  unlet! g:match

  delfunction CheckTempFile
  delfunction GetTempBase

  runtime autoload/ale/util.vim
  runtime autoload/ale/command.vim

Execute(FormatCommand should do nothing to basic command strings):
  AssertEqual
  \ ['', 'awesome-linter do something', 0],
  \ ale#command#FormatCommand(bufnr('%'), '', 'awesome-linter do something', 0, v:null, v:null, [])

Execute(FormatCommand should handle %%, and ignore other percents):
  AssertEqual
  \ ['', '% %%d %%f %x %', 0],
  \ ale#command#FormatCommand(bufnr('%'), '', '%% %%%d %%%f %x %', 0, v:null, v:null, [])

Execute(FormatCommand should convert %s to the current filename):
  AssertEqual
  \ [
  \   '',
  \   'foo ' . ale#Escape(expand('%:p')) . ' bar ' . ale#Escape(expand('%:p')),
  \   0,
  \ ],
  \ ale#command#FormatCommand(bufnr('%'), '', 'foo %s bar %s', 0, v:null, v:null, [])

Execute(FormatCommand should convert %t to a new temporary filename):
  let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo %t bar %t', 0, v:null, v:null, [])

  call CheckTempFile(g:result[0])

  let g:match = matchlist(g:result[1], '\v^foo (.*) bar (.*)$')

  Assert !empty(g:match), 'No match found! Result was: ' . g:result[1]
  " The first item of the result should be a temporary filename, and it should
  " be the same as the escaped name in the command string.
  AssertEqual ale#Escape(g:result[0]), g:match[1]
  " The two temporary filenames formatted in should be the same.
  AssertEqual g:match[1], g:match[2]

Execute(FormatCommand should normalize mixed Windows temporary paths):
  if has('win32')
    function! ale#util#Tempname() abort
      return 'D:/Personal/Temp/V6UA83E.tmp'
    endfunction

    let g:result = ale#command#FormatCommand(bufnr('%'), '', '%t', 0, v:null, v:null, [])

    AssertEqual 'D:\Personal\Temp\V6UA83E.tmp\dummy.txt', g:result[0]
    AssertEqual ale#Escape(g:result[0]), g:result[1]
  endif

Execute(FormatCommand should not convert %t to a new temporary filename when the input is given as v:false):
  let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo %t bar %t', 0, v:false, v:null, [])

  AssertEqual ['', 'foo %t bar %t', 0], g:result

Execute(FormatCommand should signal that files are created when temporary files are needed):
  AssertEqual
  \ 1,
  \ ale#command#FormatCommand(bufnr('%'), '', 'foo %t', 0, v:null, v:null, [])[2]

  AssertEqual
  \ 0,
  \ ale#command#FormatCommand(bufnr('%'), '', 'foo %s', 0, v:null, v:null, [])[2]

Execute(FormatCommand should let you combine %s and %t):
  let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo %t bar %s', 0, v:null, v:null, [])

  call CheckTempFile(g:result[0])

  let g:match = matchlist(g:result[1], '\v^foo (.*) bar (.*)$')

  Assert !empty(g:match), 'No match found! Result was: ' . g:result[1]
  " The first item of the result should be a temporary filename, and it should
  " be the same as the escaped name in the command string.
  AssertEqual ale#Escape(g:result[0]), g:match[1]
  " The second item should be equal to the original filename.
  AssertEqual ale#Escape(expand('%:p')), g:match[2]

Execute(FormatCommand should replace %e with the escaped executable):
  if has('win32')
    AssertEqual
    \ ['', 'foo foo', 0],
    \ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, v:null, v:null, [])
    AssertEqual
    \ ['', '"foo bar"', 0],
    \ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, v:null, v:null, [])
    AssertEqual
    \ ['', '%e %e', 0],
    \ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, v:null, v:null, [])
  else
    AssertEqual
    \ ['', '''foo'' ''foo''', 0],
    \ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, v:null, v:null, [])
    AssertEqual
    \ ['', '''foo bar''', 0],
    \ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, v:null, v:null, [])
    AssertEqual
    \ ['', '%e %e', 0],
    \ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, v:null, v:null, [])
  endif

Execute(EscapeCommandPart should escape all percent signs):
  AssertEqual '%%s %%t %%%% %%s %%t %%%%', ale#engine#EscapeCommandPart('%s %t %% %s %t %%')

Execute(EscapeCommandPart should pipe in temporary files appropriately):
  let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo bar', 1, v:null, v:null, [])

  call CheckTempFile(g:result[0])

  let g:match = matchlist(g:result[1], '\v^foo bar \< (.*)$')
  Assert !empty(g:match), 'No match found! Result was: ' . g:result[1]
  AssertEqual ale#Escape(g:result[0]), g:match[1]

  let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo bar %t', 1, v:null, v:null, [])

  call CheckTempFile(g:result[0])

  let g:match = matchlist(g:result[1], '\v^foo bar (.*)$')
  Assert !empty(g:match), 'No match found! Result was: ' . g:result[1]
  AssertEqual ale#Escape(g:result[0]), g:match[1]

Execute(FormatCommand should apply filename modifiers to the current file):
  AssertEqual
  \ ale#Escape(expand('%:p:h'))
  \   . ' ' . ale#Escape('dummy.txt')
  \   . ' ' . ale#Escape(expand('%:p:h:t'))
  \   . ' ' . ale#Escape('txt')
  \   . ' ' . ale#Escape(expand('%:p:r')),
  \ ale#command#FormatCommand(bufnr(''), '', '%s:h %s:t %s:h:t %s:e %s:r', 0, v:null, v:null, [])[1]

Execute(FormatCommand should apply filename modifiers to the temporary file):
  let g:result = ale#command#FormatCommand(bufnr(''), '', '%t:h %t:t %t:h:t %t:e %t:r', 0, v:null, v:null, [])

  AssertEqual
  \ ale#Escape(fnamemodify(g:result[0], ':h'))
  \   . ' ' . ale#Escape('dummy.txt')
  \   . ' ' . ale#Escape(fnamemodify(g:result[0], ':h:t'))
  \   . ' ' . ale#Escape('txt')
  \   . ' ' . ale#Escape(fnamemodify(g:result[0], ':r')),
  \ g:result[1]

Execute(FormatCommand should apply filename mappings the current file):
  let g:result = ale#command#FormatCommand(bufnr('%'), '', '%s', 0, v:null, v:null, [
  \ [expand('%:p:h'), '/foo/bar'],
  \])

  Assert g:result[1] =~# '/foo/bar'

Execute(FormatCommand should apply filename mappings to temporary files):
  let g:result = ale#command#FormatCommand(bufnr('%'), '', '%t', 0, v:null, v:null, [
  \ [GetTempBase(), '/foo/bar']
  \])

  Assert g:result[1] =~# '/foo/bar'

Execute(FormatCommand should apply filename modifiers to mapped filenames):
  let g:mapped_filename = '/foo/bar/dummy.txt'
  let g:result = ale#command#FormatCommand(bufnr('%'), '', '%s:h', 0, v:null, v:null, [
  \ [expand('%:p'), g:mapped_filename],
  \])

  AssertEqual ale#Escape('/foo/bar'), g:result[1]

  " Compute the number of :h modifiers needed to get from the temp file
  " (tempname()/dummy.txt) back to GetTempBase(). This varies by platform:
  " NeoVim uses nested temp dirs, Vim 9.x Windows uses flat ones.
  let g:tbase = GetTempBase()
  let g:tname = ale#util#Tempname()
  let g:rel = substitute(g:tname, '^\V' . escape(g:tbase, '\'), '', '')
  " +1 for dummy.txt appended by TemporaryFilename
  let g:depth = len(split(g:rel, '[/\\]')) + 1
  let g:modifiers = repeat(':h', g:depth)

  let g:result = ale#command#FormatCommand(bufnr('%'), '', '%t' . g:modifiers, 0, v:null, v:null, [
  \ [GetTempBase(), '/foo/bar']
  \])

  AssertEqual ale#Escape('/foo/bar'), g:result[1]

Execute(FormatCommand should apply regular cwd paths):
  AssertEqual
  \ 'cd ' . (has('unix') ? '' : '/d ') . ale#Escape('/foo /bar') . ' && abc',
  \ ale#command#FormatCommand(bufnr('%'), '', 'abc', 0, v:null, '/foo /bar', [])[1]
  \
Execute(FormatCommand should apply cwd substitution and formatting):
  call ale#test#SetFilename('foo.txt')

  AssertEqual
  \ 'cd ' . (has('unix') ? '' : '/d ') . ale#Escape(getcwd()) . ' && abc',
  \ ale#command#FormatCommand(bufnr('%'), '', 'abc', 0, v:null, '%s:h', [])[1]