1/*
2 run tests with `nix-build -A neovim.tests`
3
4 The attrset exposes both the wrapped neovim and the associated test for easier debugging
5
6 Here are some common neovim flags used in the tests:
7 -e runs neovim in `:h Ex-mode` which returns an exit code != 0 when hitting an error
8 -i NONE gets rid of shada warnings
9*/
10{
11 vimUtils,
12 writeText,
13 neovim,
14 vimPlugins,
15 neovimUtils,
16 wrapNeovimUnstable,
17 neovim-unwrapped,
18 fetchFromGitLab,
19 runCommandLocal,
20 testers,
21 pkgs,
22}:
23let
24 inherit (neovimUtils) makeNeovimConfig;
25
26 plugins = with vimPlugins; [
27 {
28 plugin = vim-obsession;
29 config = ''
30 map <Leader>$ <Cmd>Obsession<CR>
31 '';
32 }
33 ];
34
35 packagesWithSingleLineConfigs = with vimPlugins; [
36 {
37 plugin = vim-obsession;
38 config = ''map <Leader>$ <Cmd>Obsession<CR>'';
39 }
40 {
41 plugin = trouble-nvim;
42 config = ''" placeholder config'';
43 }
44 ];
45
46 nvimConfSingleLines = makeNeovimConfig {
47 plugins = packagesWithSingleLineConfigs;
48 customRC = ''
49 " just a comment
50 '';
51 };
52
53 nvimConfNix = makeNeovimConfig {
54 inherit plugins;
55 customRC = ''
56 " just a comment
57 '';
58 };
59
60 nvim-with-luasnip = wrapNeovim2 "-with-luasnip" (makeNeovimConfig {
61 plugins = [
62 {
63 plugin = vimPlugins.luasnip;
64 }
65 ];
66 });
67
68 # build should fail with a wrong
69 nvim-run-failing-check =
70 (wrapNeovimUnstable neovim-unwrapped {
71 luaRcContent = "this is an invalid lua statement to break the build";
72 }).overrideAttrs
73 ({
74 doCheck = true;
75 });
76
77 nvimAutoDisableWrap = makeNeovimConfig { };
78
79 wrapNeovim2 =
80 suffix: config:
81 wrapNeovimUnstable neovim-unwrapped (
82 config
83 // {
84 extraName = suffix;
85 }
86 );
87
88 nmt = fetchFromGitLab {
89 owner = "rycee";
90 repo = "nmt";
91 rev = "d2cc8c1042b1c2511f68f40e2790a8c0e29eeb42";
92 sha256 = "1ykcvyx82nhdq167kbnpgwkgjib8ii7c92y3427v986n2s5lsskc";
93 };
94
95 /*
96 neovim-drv must be a wrapped neovim
97 - exposes lua config in $luarcGeneric
98 - exposes vim config in $vimrcGeneric
99 */
100
101 runTest =
102 neovim-drv: buildCommand:
103 runCommandLocal "test-${neovim-drv.name}"
104 ({
105 nativeBuildInputs = [ ];
106 meta.platforms = neovim-drv.meta.platforms;
107 })
108 (
109 ''
110 source ${nmt}/bash-lib/assertions.sh
111 vimrc="${writeText "test-${neovim-drv.name}-init.vim" neovim-drv.initRc}"
112 luarc="${writeText "test-${neovim-drv.name}-init.lua" neovim-drv.luaRcContent}"
113 luarcGeneric="$out/patched.lua"
114 vimrcGeneric="$out/patched.vim"
115 mkdir $out
116 export HOME=$TMPDIR
117 ${pkgs.perl}/bin/perl -pe "s|\Q$NIX_STORE\E/[a-z0-9]{32}-|$NIX_STORE/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-|g" < "$vimrc" > "$vimrcGeneric"
118 ${pkgs.perl}/bin/perl -pe "s|\Q$NIX_STORE\E/[a-z0-9]{32}-|$NIX_STORE/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-|g" < "$luarc" > "$luarcGeneric"
119 ''
120 + buildCommand
121 );
122
123 nvim_with_rocks_nvim = (
124 wrapNeovimUnstable neovim-unwrapped {
125 extraName = "with-rocks-nvim";
126 wrapperArgs = "--set NVIM_APPNAME test-rocks-nvim";
127 plugins = [ vimPlugins.rocks-nvim ];
128 }
129 );
130in
131pkgs.recurseIntoAttrs (rec {
132
133 inherit nmt;
134
135 failed_check = testers.testBuildFailure nvim-run-failing-check;
136
137 vim_empty_config = vimUtils.vimrcFile {
138 beforePlugins = "";
139 customRC = "";
140 };
141
142 ### neovim tests
143 ##################
144 nvim_with_plugins = wrapNeovim2 "-with-plugins" nvimConfNix;
145 nvim_singlelines = wrapNeovim2 "-single-lines" nvimConfSingleLines;
146
147 # test that passthru.initRc hasn't changed
148 passthruInitRc = runTest nvim_singlelines ''
149 INITRC=${
150 pkgs.writeTextFile {
151 name = "initrc";
152 text = nvim_singlelines.passthru.initRc;
153 }
154 }
155 assertFileContent \
156 $INITRC \
157 "${./init-single-lines.vim}"
158 '';
159
160 # test single line concatenation
161 singlelinesconfig = runTest nvim_singlelines ''
162 assertFileContains \
163 "$luarcGeneric" \
164 "vim.cmd.source \"/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-init.vim"
165 assertFileContent \
166 "$vimrcGeneric" \
167 "${./init-single-lines.vim}"
168 '';
169
170 nvim_via_override = neovim.override {
171 extraName = "-via-override";
172 configure = {
173 packages.foo.start = [ vimPlugins.ale ];
174 customRC = ''
175 :help ale
176 '';
177 };
178 };
179
180 nvim_with_aliases = neovim.override {
181 extraName = "-with-aliases";
182 vimAlias = true;
183 viAlias = true;
184 };
185
186 # test it still works with vim-plug
187 nvim_with_plug = neovim.override {
188 extraName = "-with-plug";
189 configure.packages.plugins = with pkgs.vimPlugins; {
190 start = [
191 (base16-vim.overrideAttrs (old: {
192 pname = old.pname + "-unique-for-tests-please-dont-use";
193 }))
194 ];
195 };
196 configure.customRC = ''
197 color base16-tomorrow-night
198 set background=dark
199 '';
200 };
201
202 run_nvim_with_plug = runTest nvim_with_plug ''
203 ${nvim_with_plug}/bin/nvim -V3log.txt -i NONE -c 'color base16-tomorrow-night' +quit! -e
204 '';
205
206 nvim_with_autoconfigure = pkgs.neovim.overrideAttrs (oa: {
207 plugins = [
208 vimPlugins.unicode-vim
209 vimPlugins.fzf-hoogle-vim
210 ];
211 autoconfigure = true;
212 # legacy wrapper sets it to false
213 wrapRc = true;
214 });
215
216 nvim_with_runtimeDeps = pkgs.neovim.overrideAttrs ({
217 plugins = [
218 pkgs.vimPlugins.hex-nvim
219 ];
220 autowrapRuntimeDeps = true;
221 # legacy wrapper sets it to false
222 wrapRc = true;
223 });
224
225 nvim_with_ftplugin =
226 let
227 # this plugin checks that it's ftplugin/vim.tex is loaded before $VIMRUNTIME/ftplugin/vim.tex
228 # $VIMRUNTIME/ftplugin/vim.tex sources $VIMRUNTIME/ftplugin/initex.vim which sets b:did_ftplugin
229 # we save b:did_ftplugin's value in a `plugin_was_loaded_too_late` file
230 texFtplugin =
231 (pkgs.runCommandLocal "tex-ftplugin" { } ''
232 mkdir -p $out/ftplugin
233 echo 'call system("echo ". exists("b:did_ftplugin") . " > plugin_was_loaded_too_late")' >> $out/ftplugin/tex.vim
234 echo ':q!' >> $out/ftplugin/tex.vim
235 '')
236 // {
237 pname = "test-ftplugin";
238 };
239 in
240
241 neovim.override {
242 extraName = "-with-ftplugin";
243 configure.packages.plugins = {
244 start = [
245 texFtplugin
246 ];
247 };
248 };
249
250 # regression test that ftplugin files from plugins are loaded before the ftplugin
251 # files from $VIMRUNTIME
252 run_nvim_with_ftplugin = runTest nvim_with_ftplugin ''
253 echo '\documentclass{article}' > main.tex
254
255 ${nvim_with_ftplugin}/bin/nvim -i NONE -V3log.txt main.tex -c "set ft?" -c quit
256 ls -l $TMPDIR
257 # check the saved value b:did_ftplugin then our plugin has been loaded instead of neovim's
258 result="$(cat plugin_was_loaded_too_late)"
259 echo $result
260 [ "$result" = 0 ]
261 '';
262
263 # Generate a neovim wrapper with only a init.lua and no init.vim file
264 nvim_with_only_init_lua = wrapNeovim2 "-only-lua-init-file" {
265 luaRcContent = "-- some text";
266 };
267
268 # check that we do not generate an init.vim file if it is not needed
269 no_init_vim_file = runTest nvim_with_only_init_lua ''
270 ${nvim_with_only_init_lua}/bin/nvim -i NONE -e --headless -c 'if len(getscriptinfo({"name":"init.vim"})) == 0 | quit | else | cquit | fi'
271 # This does now work because the lua file is sourced via loadfile() which
272 # does not add the file name to :scriptnames and getscriptinfo().
273 #${nvim_with_only_init_lua}/bin/nvim -i NONE -e --headless -c 'if len(getscriptinfo({"name":"init.lua"})) == 1 | quit | else | cquit | fi'
274
275 assertFileRegex ${nvim_with_only_init_lua}/bin/nvim 'VIMINIT=.*init.lua'
276 '';
277
278 # check that the vim-doc hook correctly generates the tag
279 # we know for a fact packer has a doc folder
280 checkForTags = vimPlugins.packer-nvim.overrideAttrs (oldAttrs: {
281 doInstallCheck = true;
282 installCheckPhase = ''
283 [ -f $out/doc/tags ]
284 '';
285 });
286
287 # check that the vim-doc hook correctly generates the tag
288 # for neovim packages from luaPackages
289 # we know for a fact gitsigns-nvim has a doc folder and comes from luaPackages
290 checkForTagsLuaPackages = vimPlugins.gitsigns-nvim.overrideAttrs (oldAttrs: {
291 doInstallCheck = true;
292 installCheckPhase = ''
293 [ -f $out/doc/tags ]
294 '';
295 });
296
297 nvim_with_gitsigns_plugin = neovim.override {
298 extraName = "-with-gitsigns-plugin";
299 configure.packages.plugins = {
300 start = [
301 vimPlugins.gitsigns-nvim
302 ];
303 };
304 };
305
306 checkHelpLuaPackages = runTest nvim_with_gitsigns_plugin ''
307 ${nvim_with_gitsigns_plugin}/bin/nvim -i NONE -c 'help gitsigns' +quitall! -e
308 '';
309
310 # nixpkgs should detect that no wrapping is necessary
311 nvimShouldntWrap = wrapNeovim2 "-should-not-wrap" nvimAutoDisableWrap;
312
313 # this will generate a neovimRc content but we disable wrapping
314 nvimDontWrap = wrapNeovim2 "-forced-nowrap" (makeNeovimConfig {
315 wrapRc = false;
316 customRC = ''
317 " this shouldn't trigger the creation of an init.vim
318 '';
319 });
320
321 force-nowrap = runTest nvimDontWrap ''
322 ! grep -F -- ' -u' ${nvimDontWrap}/bin/nvim
323 '';
324
325 nvim_via_override-test = runTest nvim_via_override ''
326 assertFileContent \
327 "$vimrcGeneric" \
328 "${./init-override.vim}"
329 '';
330
331 checkAliases = runTest nvim_with_aliases ''
332 folder=${nvim_with_aliases}/bin
333 assertFileIsExecutable "$folder/vi"
334 assertFileIsExecutable "$folder/vim"
335 '';
336
337 # having no RC generated should autodisable init.vim wrapping
338 nvim_autowrap = runTest nvim_via_override ''
339 ! grep ${nvimShouldntWrap}/bin/nvim
340 '';
341
342 # system remote plugin manifest should be generated, deoplete should be usable
343 # without the user having to do `UpdateRemotePlugins`. To test, launch neovim
344 # and do `:call deoplete#enable()`. It will print an error if the remote
345 # plugin is not registered.
346 test_nvim_with_remote_plugin = neovim.override {
347 extraName = "-remote";
348 configure.packages.foo.start = with vimPlugins; [ deoplete-nvim ];
349 };
350
351 nvimWithLuaPackages = wrapNeovim2 "-with-lua-packages" (makeNeovimConfig {
352 extraLuaPackages = ps: [ ps.mpack ];
353 customRC = ''
354 lua require("mpack")
355 '';
356 });
357
358 nvim_with_lua_packages = runTest nvimWithLuaPackages ''
359 ${nvimWithLuaPackages}/bin/nvim -V3log.txt -i NONE --noplugin +quitall! -e
360 '';
361
362 # nixpkgs should install optional packages in the opt folder
363 nvim_with_opt_plugin = neovim.override {
364 extraName = "-with-opt-plugin";
365 configure.packages.opt-plugins = with pkgs.vimPlugins; {
366 opt = [
367 (dashboard-nvim.overrideAttrs (old: {
368 pname = old.pname + "-unique-for-tests-please-dont-use-opt";
369 }))
370 ];
371 };
372 configure.customRC = ''
373 " Load all autoloaded plugins
374 packloadall
375
376 " Try to run Dashboard, and throw if it succeeds
377 try
378 Dashboard
379 echo "Dashboard found, throwing error"
380 cquit 1
381 catch /^Vim\%((\a\+)\)\=:E492/
382 echo "Dashboard not found"
383 endtry
384
385 " Load Dashboard as an optional
386 packadd dashboard-nvim-unique-for-tests-please-dont-use-opt
387
388 " Try to run Dashboard again, and throw if it fails
389 let res = exists(':Dashboard')
390 if res == 0
391 echo "Dashboard not found, throwing error"
392 cquit 1
393 endif
394 cquit 0
395 '';
396 };
397
398 run_nvim_with_opt_plugin = runTest nvim_with_opt_plugin ''
399 ${nvim_with_opt_plugin}/bin/nvim -i NONE +quit! -e
400 '';
401
402 autoconfigure = runTest nvim_with_autoconfigure ''
403 assertFileContains \
404 "$luarc" \
405 '${vimPlugins.unicode-vim.passthru.initLua}'
406 '';
407
408 autowrap_runtime_deps = runTest nvim_with_runtimeDeps ''
409 assertFileContains \
410 "${nvim_with_runtimeDeps}/bin/nvim" \
411 '${pkgs.xxd}/bin'
412 '';
413
414 inherit nvim-with-luasnip;
415 # check that bringing in one plugin with lua deps makes those deps visible from wrapper
416 # for instance luasnip has a dependency on jsregexp
417 can_require_transitive_deps = runTest nvim-with-luasnip ''
418 ${nvim-with-luasnip}/bin/nvim -i NONE --cmd "lua require'jsregexp'" -e +quitall!
419 '';
420
421 inherit nvim_with_rocks_nvim;
422 rocks_install_plenary = runTest nvim_with_rocks_nvim ''
423 ${nvim_with_rocks_nvim}/bin/nvim -V3log.txt -i NONE +'Rocks install plenary.nvim' +quit! -e
424 '';
425
426 inherit (vimPlugins) corePlugins;
427})