GuyBrush | 13a6605 | 2024-11-12 20:18:14 +0100 | [diff] [blame^] | 1 | " Tests for GetLatestVimScripts plugin |
| 2 | |
| 3 | " vim feature |
| 4 | set nocp |
| 5 | set cpo&vim |
| 6 | |
| 7 | " constants |
| 8 | const s:dotvim= has("win32") ? "vimfiles" : ".vim" |
| 9 | const s:scriptdir = $"{$HOME}/{s:dotvim}/GetLatest" |
| 10 | const s:vimdir = expand("<script>:h:h:h") |
| 11 | const s:packages = { |
| 12 | \ 'vmb': { |
| 13 | \ 'spec': '4979 1 :AutoInstall: AnsiEsc.vim', |
| 14 | \ 'files': ['plugin/AnsiEscPlugin.vim', 'autoload/AnsiEsc.vim'] |
| 15 | \ }, |
| 16 | \ 'vim.bz2': { |
| 17 | \ 'spec': '514 1 :AutoInstall: mrswin.vim', |
| 18 | \ 'files': ['plugin/mrswin.vim'] |
| 19 | \ }, |
| 20 | \ 'vba.gz': { |
| 21 | \ 'spec': '120 1 :AutoInstall: Decho.vim', |
| 22 | \ 'package': 'GetLatest/Installed/Decho.vba', |
| 23 | \ 'files': ['plugin/Decho.vim', 'syntax/Decho.vim'] |
| 24 | \ }, |
| 25 | \ 'tar.xz': { |
| 26 | \ 'spec': '5632 1 :AutoInstall: dumpx', |
| 27 | \ 'package': 'GetLatest/Installed/dumpx.tar', |
| 28 | \ 'files': ['dumpx/plugin/dumpx.vim', 'dumpx/doc/dumpx.txt'] |
| 29 | \ } |
| 30 | \ } |
| 31 | |
| 32 | " Before each test recreate the .vim dir structure expected by GLVS and load the plugin |
| 33 | func SetUp() |
| 34 | |
| 35 | " add the required GetLatest dir (note $HOME is a dummy) |
| 36 | call mkdir(s:scriptdir, "p") |
| 37 | let &runtimepath = $"{$HOME}/{s:dotvim},{s:vimdir}/runtime" |
| 38 | |
| 39 | " add plugin dir |
| 40 | call mkdir($"{$HOME}/{s:dotvim}/plugin") |
| 41 | |
| 42 | " doc file is required for the packages which use :helptags |
| 43 | let docdir = $"{$HOME}/{s:dotvim}/doc" |
| 44 | call mkdir(docdir, "p") |
| 45 | exe $"split {docdir}/tags" |
| 46 | w! |
| 47 | bwipe! |
| 48 | |
| 49 | " load required plugins, getscript.vim would be loaded manually by the test |
| 50 | " (instead of relying on autoload) because set up depends on shell selection |
| 51 | runtime plugin/vimballPlugin.vim |
| 52 | runtime plugin/getscriptPlugin.vim |
| 53 | |
| 54 | " define tools location |
| 55 | if has('win32') |
| 56 | |
| 57 | if executable('git') |
| 58 | let git_path = trim(system('powershell -Command "Split-Path (Split-Path (gcm git).Source)"')) |
| 59 | endif |
| 60 | |
| 61 | if executable('bunzip2') |
| 62 | let g:GetLatestVimScripts_bunzip2= "bunzip2" |
| 63 | elseif executable('7z') |
| 64 | let g:GetLatestVimScripts_bunzip2= "7z x" |
| 65 | elseif exists('git_path') |
| 66 | let g:GetLatestVimScripts_bunzip2= $'{git_path}\usr\bin\bunzip2' |
| 67 | else |
| 68 | call assert_report("Missing tool to decompress .bz2 files") |
| 69 | endif |
| 70 | |
| 71 | if executable('gunzip') |
| 72 | let g:GetLatestVimScripts_gunzip= "gunzip" |
| 73 | elseif executable('7z') |
| 74 | let g:GetLatestVimScripts_gunzip="7z e" |
| 75 | elseif exists('git_path') |
| 76 | let g:GetLatestVimScripts_gunzip= $'{git_path}\usr\bin\gunzip' |
| 77 | else |
| 78 | call assert_report("Missing tool to decompress .gz files") |
| 79 | endif |
| 80 | |
| 81 | if executable('unxz') |
| 82 | let g:GetLatestVimScripts_unxz= "unxz" |
| 83 | elseif executable('7z') |
| 84 | let g:GetLatestVimScripts_unxz="7z x" |
| 85 | elseif exists('git_path') |
| 86 | let g:GetLatestVimScripts_unxz= $'{git_path}\mingw64\bin\unxz' |
| 87 | else |
| 88 | call assert_report("Missing tool to decompress .xz files") |
| 89 | endif |
| 90 | |
| 91 | endif |
| 92 | |
| 93 | endfunc |
| 94 | |
| 95 | " After each test remove the contents of the .vim dir and reset the script |
| 96 | func TearDown() |
| 97 | call delete($"{$HOME}/{s:dotvim}", "rf") |
| 98 | |
| 99 | " getscript.vim include guard |
| 100 | unlet! g:loaded_getscript g:loaded_getscriptPlugin |
| 101 | " remove all globals (shell dependents) |
| 102 | let script_globals = keys(g:) |
| 103 | call filter(script_globals, 'v:val =~ "GetLatestVimScripts_"') |
| 104 | if len(script_globals) |
| 105 | call map(script_globals, '"g:" . v:val') |
| 106 | exe "unlet " . script_globals->join() |
| 107 | endif |
| 108 | endfunc |
| 109 | |
| 110 | " Ancillary functions |
| 111 | |
| 112 | func SetShell(shell) |
| 113 | |
| 114 | " select different shells |
| 115 | if a:shell == "default" |
| 116 | set shell& shellcmdflag& shellxquote& shellpipe& shellredir& |
| 117 | elseif a:shell == "powershell" " help dos-powershell |
| 118 | " powershell desktop is windows only |
| 119 | if !has("win32") |
| 120 | throw 'Skipped: powershell desktop is missing' |
| 121 | endif |
| 122 | set shell=powershell shellcmdflag=-NoProfile\ -Command shellxquote=\" |
| 123 | set shellpipe=2>&1\ \|\ Out-File\ -Encoding\ default shellredir=2>&1\ \|\ Out-File\ -Encoding\ default |
| 124 | elseif a:shell == "pwsh" " help dos-powershell |
| 125 | " powershell core works crossplatform |
| 126 | if !executable("pwsh") |
| 127 | throw 'Skipped: powershell core is missing' |
| 128 | endif |
| 129 | set shell=pwsh shellcmdflag=-NoProfile\ -c shellpipe=>%s\ 2>&1 shellredir=>%s\ 2>&1 |
| 130 | if has("win32") |
| 131 | set shellxquote=\" |
| 132 | else |
| 133 | set shellxquote= |
| 134 | endif |
| 135 | else |
| 136 | call assert_report("Trying to select and unknown shell") |
| 137 | endif |
| 138 | |
| 139 | " reload script to force new shell options |
| 140 | runtime autoload/getscript.vim |
| 141 | |
| 142 | endfunc |
| 143 | |
| 144 | func SelectScript(package) |
| 145 | |
| 146 | " add the corresponding file |
| 147 | exe $"split {s:scriptdir}/GetLatestVimScripts.dat" |
| 148 | let scripts =<< trim END |
| 149 | ScriptID SourceID Filename |
| 150 | -------------------------- |
| 151 | END |
| 152 | call setline(1, scripts) |
| 153 | call append(line('$'), s:packages[a:package]['spec']) |
| 154 | w! |
| 155 | bwipe! |
| 156 | |
| 157 | endfunc |
| 158 | |
| 159 | func ValidateInstall(package) |
| 160 | " check the package is expected |
| 161 | call assert_true(s:packages->has_key(a:package), "This package is unexpected") |
| 162 | |
| 163 | " check if installation work out |
| 164 | if s:packages[a:package]->has_key('package') |
| 165 | let check = filereadable($"{$HOME}/{s:dotvim}/".s:packages[a:package]['package']) |
| 166 | call assert_true(check, "The plugin was not downloaded") |
| 167 | endif |
| 168 | |
| 169 | call assert_true(s:packages[a:package]->has_key('files'), "This package lacks validation files") |
| 170 | for file in s:packages[a:package]['files'] |
| 171 | let check = filereadable($"{$HOME}/{s:dotvim}/".file) |
| 172 | call assert_true(check, "The plugin was not installed") |
| 173 | endfor |
| 174 | endfunc |
| 175 | |
| 176 | " Tests |
| 177 | " |
| 178 | func Test_glvs_default_vmb() |
| 179 | |
| 180 | " select different shells |
| 181 | call SetShell('default') |
| 182 | |
| 183 | " add the corresponding script |
| 184 | call SelectScript('vmb') |
| 185 | |
| 186 | " load the plugins specified |
| 187 | GLVS |
| 188 | |
| 189 | call ValidateInstall('vmb') |
| 190 | |
| 191 | endfunc |
| 192 | |
| 193 | func Test_glvs_pwsh_vmb() |
| 194 | |
| 195 | " select different shells |
| 196 | call SetShell('pwsh') |
| 197 | |
| 198 | " add the corresponding script |
| 199 | call SelectScript('vmb') |
| 200 | |
| 201 | " load the plugins specified |
| 202 | GLVS |
| 203 | |
| 204 | call ValidateInstall('vmb') |
| 205 | |
| 206 | endfunc |
| 207 | |
| 208 | func Test_glvs_powershell_vmb() |
| 209 | |
| 210 | " select different shells |
| 211 | call SetShell('powershell') |
| 212 | |
| 213 | " add the corresponding script |
| 214 | call SelectScript('vmb') |
| 215 | |
| 216 | " load the plugins specified |
| 217 | GLVS |
| 218 | |
| 219 | call ValidateInstall('vmb') |
| 220 | |
| 221 | endfunc |
| 222 | |
| 223 | func Test_glvs_default_vim_bz2() |
| 224 | |
| 225 | " select different shells |
| 226 | call SetShell('default') |
| 227 | |
| 228 | " add the corresponding script |
| 229 | call SelectScript('vim.bz2') |
| 230 | |
| 231 | " load the plugins specified |
| 232 | GLVS |
| 233 | |
| 234 | call ValidateInstall('vim.bz2') |
| 235 | |
| 236 | endfunc |
| 237 | |
| 238 | func Test_glvs_powershell_vim_bz2() |
| 239 | |
| 240 | " select different shells |
| 241 | call SetShell('powershell') |
| 242 | |
| 243 | " add the corresponding script |
| 244 | call SelectScript('vim.bz2') |
| 245 | |
| 246 | " load the plugins specified |
| 247 | GLVS |
| 248 | |
| 249 | call ValidateInstall('vim.bz2') |
| 250 | |
| 251 | endfunc |
| 252 | |
| 253 | func Test_glvs_pwsh_vim_bz2() |
| 254 | |
| 255 | " select different shells |
| 256 | call SetShell('pwsh') |
| 257 | |
| 258 | " add the corresponding script |
| 259 | call SelectScript('vim.bz2') |
| 260 | |
| 261 | " load the plugins specified |
| 262 | GLVS |
| 263 | |
| 264 | call ValidateInstall('vim.bz2') |
| 265 | |
| 266 | endfunc |
| 267 | |
| 268 | func Test_glvs_default_vba_gz() |
| 269 | |
| 270 | " select different shells |
| 271 | call SetShell('default') |
| 272 | |
| 273 | " add the corresponding script |
| 274 | call SelectScript('vba.gz') |
| 275 | |
| 276 | " load the plugins specified |
| 277 | GLVS |
| 278 | |
| 279 | call ValidateInstall('vba.gz') |
| 280 | |
| 281 | endfunc |
| 282 | |
| 283 | func Test_glvs_powershell_vba_gz() |
| 284 | |
| 285 | " select different shells |
| 286 | call SetShell('powershell') |
| 287 | |
| 288 | " add the corresponding script |
| 289 | call SelectScript('vba.gz') |
| 290 | |
| 291 | " load the plugins specified |
| 292 | GLVS |
| 293 | |
| 294 | call ValidateInstall('vba.gz') |
| 295 | |
| 296 | endfunc |
| 297 | |
| 298 | func Test_glvs_pwsh_vba_gz() |
| 299 | |
| 300 | " select different shells |
| 301 | call SetShell('pwsh') |
| 302 | |
| 303 | " add the corresponding script |
| 304 | call SelectScript('vba.gz') |
| 305 | |
| 306 | " load the plugins specified |
| 307 | GLVS |
| 308 | |
| 309 | call ValidateInstall('vba.gz') |
| 310 | |
| 311 | endfunc |
| 312 | |
| 313 | func Test_glvs_default_tar_xz() |
| 314 | |
| 315 | " select different shells |
| 316 | call SetShell('default') |
| 317 | |
| 318 | " add the corresponding script |
| 319 | call SelectScript('tar.xz') |
| 320 | |
| 321 | " load the plugins specified |
| 322 | GLVS |
| 323 | |
| 324 | call ValidateInstall('tar.xz') |
| 325 | |
| 326 | endfunc |
| 327 | |
| 328 | func Test_glvs_powershell_tar_xz() |
| 329 | |
| 330 | " select different shells |
| 331 | call SetShell('powershell') |
| 332 | |
| 333 | " add the corresponding script |
| 334 | call SelectScript('tar.xz') |
| 335 | |
| 336 | " load the plugins specified |
| 337 | GLVS |
| 338 | |
| 339 | call ValidateInstall('tar.xz') |
| 340 | |
| 341 | endfunc |
| 342 | |
| 343 | func Test_glvs_pwsh_tar_xz() |
| 344 | |
| 345 | " select different shells |
| 346 | call SetShell('pwsh') |
| 347 | |
| 348 | " add the corresponding script |
| 349 | call SelectScript('tar.xz') |
| 350 | |
| 351 | " load the plugins specified |
| 352 | GLVS |
| 353 | |
| 354 | call ValidateInstall('tar.xz') |
| 355 | |
| 356 | endfunc |