From 5b47c65705964197d84dc8e54dfc2480e3e5a089 Mon Sep 17 00:00:00 2001 From: Stanislav Asunkin <1353637+stasjok@users.noreply.github.com> Date: Tue, 29 Apr 2025 17:22:26 +0300 Subject: [PATCH] tests/modules/performance/byte-compile-lua: improve bytecode detection Previously, to determine if a file is byte-compiled, a simple binary file detection was used, specifically checking if it contained any null bytes. This commit updates the check to read the file header and compare it with a known LuaJIT header. --- .../hm-extra-files-byte-compiling.nix | 10 +++-- .../modules/performance/byte-compile-lua.nix | 40 +++++++++---------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/tests/platforms/hm-extra-files-byte-compiling.nix b/tests/platforms/hm-extra-files-byte-compiling.nix index 197069e4..a7cadd5d 100644 --- a/tests/platforms/hm-extra-files-byte-compiling.nix +++ b/tests/platforms/hm-extra-files-byte-compiling.nix @@ -57,17 +57,19 @@ let }).config.home-files; in pkgs.runCommand "home-manager-extra-files-byte-compiling" { } '' - is_binary() { - ! grep -qI . "$1" + is_byte_compiled() { + # LuaJIT bytecode header is: ESC L J version + # https://github.com/LuaJIT/LuaJIT/blob/v2.1/src/lj_bcdump.h + [[ $(head -c3 "$1") = $'\x1bLJ' ]] } test_byte_compiled() { - if ! is_binary "$home_files/.config/nvim/$1"; then + if ! is_byte_compiled "$home_files/.config/nvim/$1"; then echo "File $1 is expected to be byte compiled, but it's not" exit 1 fi } test_not_byte_compiled() { - if is_binary "$home_files/.config/nvim/$1"; then + if is_byte_compiled "$home_files/.config/nvim/$1"; then echo "File $1 is not expected to be byte compiled, but it is" exit 1 fi diff --git a/tests/test-sources/modules/performance/byte-compile-lua.nix b/tests/test-sources/modules/performance/byte-compile-lua.nix index a3041a5e..73ad3697 100644 --- a/tests/test-sources/modules/performance/byte-compile-lua.nix +++ b/tests/test-sources/modules/performance/byte-compile-lua.nix @@ -1,27 +1,27 @@ { pkgs, ... }: let - isByteCompiledFun = '' - local function is_byte_compiled(filename) - local f = assert(io.open(filename, "rb")) - local data = assert(f:read("*a")) - -- Assume that file is binary if it contains null bytes - for i = 1, #data do - if data:byte(i) == 0 then - return true + isByteCompiledFun = # lua + '' + -- LuaJIT bytecode header is: ESC L J version + -- https://github.com/LuaJIT/LuaJIT/blob/v2.1/src/lj_bcdump.h + -- We are comparing the first three bytes of the file (until version) + local expected_header = string.char(0x1b, 0x4c, 0x4a) + local function is_byte_compiled(filename) + local f = assert(io.open(filename, "rb")) + local data = assert(f:read(3)) + f:close() + return data == expected_header + end + + local function test_rtp_file(name, is_compiled) + local file = assert(vim.api.nvim_get_runtime_file(name, false)[1], "file " .. name .. " not found in runtime") + if is_compiled then + assert(is_byte_compiled(file), name .. " is expected to be byte compiled, but it's not") + else + assert(not is_byte_compiled(file), name .. " is not expected to be byte compiled, but it is") end end - return false - end - - local function test_rtp_file(name, is_compiled) - local file = assert(vim.api.nvim_get_runtime_file(name, false)[1], "file " .. name .. " not found in runtime") - if is_compiled then - assert(is_byte_compiled(file), name .. " is expected to be byte compiled, but it's not") - else - assert(not is_byte_compiled(file), name .. " is not expected to be byte compiled, but it is") - end - end - ''; + ''; in { default =