|
| 1 | +{ pkgs, helpers, ... }: |
| 2 | +let |
| 3 | + isByteCompiledFun = '' |
| 4 | + local function is_byte_compiled(filename) |
| 5 | + local f = assert(io.open(filename, "rb")) |
| 6 | + local data = assert(f:read("*a")) |
| 7 | + -- Assume that file is binary if it contains null bytes |
| 8 | + for i = 1, #data do |
| 9 | + if data:byte(i) == 0 then |
| 10 | + return true |
| 11 | + end |
| 12 | + end |
| 13 | + return false |
| 14 | + end |
| 15 | +
|
| 16 | + local function test_rtp_file(name, is_compiled) |
| 17 | + local file = assert(vim.api.nvim_get_runtime_file(name, false)[1], "file " .. name .. " not found in runtime") |
| 18 | + if is_compiled then |
| 19 | + assert(is_byte_compiled(file), name .. " is expected to be byte compiled, but it's not") |
| 20 | + else |
| 21 | + assert(not is_byte_compiled(file), name .. " is not expected to be byte compiled, but it is") |
| 22 | + end |
| 23 | + end |
| 24 | + ''; |
| 25 | +in |
| 26 | +{ |
| 27 | + default.module = |
| 28 | + { config, ... }: |
| 29 | + { |
| 30 | + performance.byteCompileLua.enable = true; |
| 31 | + |
| 32 | + extraFiles = { |
| 33 | + "plugin/file_text.lua".text = "vim.opt.tabstop = 2"; |
| 34 | + "plugin/file_source.lua".source = helpers.writeLua "file_source.lua" "vim.opt.tabstop = 2"; |
| 35 | + "plugin/test.vim".text = "set tabstop=2"; |
| 36 | + "plugin/test.json".text = builtins.toJSON { a = 1; }; |
| 37 | + }; |
| 38 | + |
| 39 | + files = { |
| 40 | + "plugin/file.lua" = { |
| 41 | + opts.tabstop = 2; |
| 42 | + }; |
| 43 | + "plugin/file.vim" = { |
| 44 | + opts.tabstop = 2; |
| 45 | + }; |
| 46 | + }; |
| 47 | + |
| 48 | + extraPlugins = [ pkgs.vimPlugins.nvim-lspconfig ]; |
| 49 | + |
| 50 | + extraConfigLua = '' |
| 51 | + -- The test will search for this string in nixvim-print-init output: VALIDATING_STRING. |
| 52 | + -- Since this is the comment, it won't appear in byte compiled file. |
| 53 | + ''; |
| 54 | + |
| 55 | + # Using plugin for the test code to avoid infinite recursion |
| 56 | + extraFiles."plugin/test.lua".text = '' |
| 57 | + ${isByteCompiledFun} |
| 58 | +
|
| 59 | + -- vimrc is byte compiled |
| 60 | + local init = vim.env.MYVIMRC or vim.fn.getscriptinfo({name = "init.lua"})[1].name |
| 61 | + assert(is_byte_compiled(init), "MYVIMRC is expected to be byte compiled, but it's not") |
| 62 | +
|
| 63 | + -- nixvim-print-init prints text |
| 64 | + local init_content = vim.fn.system("${config.printInitPackage}/bin/nixvim-print-init") |
| 65 | + assert(init_content:find("VALIDATING_STRING"), "nixvim-print-init's output is byte compiled") |
| 66 | +
|
| 67 | + -- extraFiles |
| 68 | + test_rtp_file("plugin/file_text.lua", false) |
| 69 | + test_rtp_file("plugin/file_source.lua", false) |
| 70 | + test_rtp_file("plugin/test.vim", false) |
| 71 | + test_rtp_file("plugin/test.json", false) |
| 72 | +
|
| 73 | + -- files |
| 74 | + test_rtp_file("plugin/file.lua", false) |
| 75 | + test_rtp_file("plugin/file.vim", false) |
| 76 | +
|
| 77 | + -- Plugins and neovim runtime aren't byte compiled by default |
| 78 | + test_rtp_file("lua/vim/lsp.lua", false) |
| 79 | + test_rtp_file("lua/lspconfig.lua", false) |
| 80 | + ''; |
| 81 | + |
| 82 | + }; |
| 83 | + |
| 84 | + disabled.module = |
| 85 | + { config, ... }: |
| 86 | + { |
| 87 | + performance.byteCompileLua.enable = false; |
| 88 | + |
| 89 | + extraFiles."plugin/test1.lua".text = "vim.opt.tabstop = 2"; |
| 90 | + |
| 91 | + files."plugin/test2.lua".opts.tabstop = 2; |
| 92 | + |
| 93 | + extraPlugins = [ pkgs.vimPlugins.nvim-lspconfig ]; |
| 94 | + |
| 95 | + extraConfigLua = '' |
| 96 | + -- The test will search for this string in nixvim-print-init output: VALIDATING_STRING. |
| 97 | + -- Since this is the comment, it won't appear in byte compiled file. |
| 98 | + ''; |
| 99 | + |
| 100 | + # Using plugin for the test code to avoid infinite recursion |
| 101 | + extraFiles."plugin/test.lua".text = '' |
| 102 | + ${isByteCompiledFun} |
| 103 | +
|
| 104 | + -- vimrc |
| 105 | + local init = vim.env.MYVIMRC or vim.fn.getscriptinfo({name = "init.lua"})[1].name |
| 106 | + assert(not is_byte_compiled(init), "MYVIMRC is not expected to be byte compiled, but it is") |
| 107 | +
|
| 108 | + -- nixvim-print-init prints text |
| 109 | + local init_content = vim.fn.system("${config.printInitPackage}/bin/nixvim-print-init") |
| 110 | + assert(init_content:find("VALIDATING_STRING"), "nixvim-print-init's output is byte compiled") |
| 111 | +
|
| 112 | + -- Nothing is byte compiled |
| 113 | + -- extraFiles |
| 114 | + test_rtp_file("plugin/test1.lua", false) |
| 115 | + -- files |
| 116 | + test_rtp_file("plugin/test2.lua", false) |
| 117 | + -- Plugins |
| 118 | + test_rtp_file("lua/lspconfig.lua", false) |
| 119 | + -- Neovim runtime |
| 120 | + test_rtp_file("lua/vim/lsp.lua", false) |
| 121 | + ''; |
| 122 | + |
| 123 | + }; |
| 124 | + |
| 125 | + init-lua-disabled = { |
| 126 | + performance.byteCompileLua = { |
| 127 | + enable = true; |
| 128 | + initLua = false; |
| 129 | + }; |
| 130 | + |
| 131 | + extraConfigLuaPost = '' |
| 132 | + ${isByteCompiledFun} |
| 133 | +
|
| 134 | + -- vimrc is not byte compiled |
| 135 | + local init = vim.env.MYVIMRC or vim.fn.getscriptinfo({name = "init.lua"})[1].name |
| 136 | + assert(not is_byte_compiled(init), "MYVIMRC is not expected to be byte compiled, but it is") |
| 137 | + ''; |
| 138 | + }; |
| 139 | +} |
0 commit comments