1{ lua
2, hello
3, wrapLua
4, lib
5, pkgs
6}:
7let
8 runTest = lua: { name, command }:
9 pkgs.runCommandLocal "test-${lua.name}-${name}" ({
10 nativeBuildInputs = [lua];
11 meta.platforms = lua.meta.platforms;
12 }) (''
13 source ${./assert.sh}
14 ''
15 + command
16 + "touch $out"
17 );
18
19 wrappedHello = hello.overrideAttrs(oa: {
20 propagatedBuildInputs = [
21 wrapLua
22 lua.pkgs.cjson
23 ];
24 postFixup = ''
25 wrapLuaPrograms
26 '';
27 });
28
29 luaWithModule = lua.withPackages(ps: [
30 ps.lua-cjson
31 ]);
32
33 golden_LUA_PATHS = {
34
35 # Looking at lua interpreter 'setpath' code
36 # for instance https://github.com/lua/lua/blob/69ea087dff1daba25a2000dfb8f1883c17545b7a/loadlib.c#L599
37 # replace ";;" by ";LUA_PATH_DEFAULT;"
38 "5.1" = ";./?.lua;${lua}/share/lua/5.1/?.lua;${lua}/share/lua/5.1/?/init.lua;${lua}/lib/lua/5.1/?.lua;${lua}/lib/lua/5.1/?/init.lua;";
39 "5.2" = ";${lua}/share/lua/5.2/?.lua;${lua}/share/lua/5.2/?/init.lua;${lua}/lib/lua/5.2/?.lua;${lua}/lib/lua/5.2/?/init.lua;./?.lua;";
40 "5.3" = ";${lua}/share/lua/5.3/?.lua;${lua}/share/lua/5.3/?/init.lua;${lua}/lib/lua/5.3/?.lua;${lua}/lib/lua/5.3/?/init.lua;./?.lua;./?/init.lua;";
41 # lua5.4 seems to be smarter about it and dont add the lua separators when nothing left or right
42 "5.4" = "${lua}/share/lua/5.4/?.lua;${lua}/share/lua/5.4/?/init.lua;${lua}/lib/lua/5.4/?.lua;${lua}/lib/lua/5.4/?/init.lua;./?.lua;./?/init.lua";
43
44 # luajit versions
45 "2.0" = ";./?.lua;${lua}/share/luajit-2.0/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;${lua}/share/lua/5.1/?.lua;${lua}/share/lua/5.1/?/init.lua;";
46 "2.1" = ";./?.lua;${lua}/share/luajit-2.1/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;${lua}/share/lua/5.1/?.lua;${lua}/share/lua/5.1/?/init.lua;";
47 };
48in
49 pkgs.recurseIntoAttrs ({
50
51 checkInterpreterPatch = let
52 golden_LUA_PATH = golden_LUA_PATHS.${lib.versions.majorMinor lua.version};
53 in
54 runTest lua {
55 name = "check-default-lua-path";
56 command = ''
57 export LUA_PATH=";;"
58 generated=$(lua -e 'print(package.path)')
59 assertStringEqual "$generated" "${golden_LUA_PATH}"
60 '';
61 };
62
63 checkWrapping = pkgs.runCommandLocal "test-${lua.name}-wrapping" ({
64 }) (''
65 grep -- 'LUA_PATH=' ${wrappedHello}/bin/hello
66 touch $out
67 '');
68
69 # checks that lua's setup-hook adds dependencies to LUA_PATH
70 # Prevents the following regressions
71 # $ env NIX_PATH=nixpkgs=. nix-shell --pure -Q -p luajitPackages.lua luajitPackages.http
72 # nix-shell$ luajit
73 # > require('http.request')
74 # stdin:1: module 'http.request' not found:
75 checkSetupHook = pkgs.runCommandLocal "test-${lua.name}-setup-hook" ({
76 nativeBuildInputs = [lua];
77 buildInputs = [ lua.pkgs.http ];
78 meta.platforms = lua.meta.platforms;
79 }) (''
80 ${lua}/bin/lua -e "require'http.request'"
81 touch $out
82 '');
83
84 checkRelativeImports = pkgs.runCommandLocal "test-${lua.name}-relative-imports" ({
85 }) (''
86 source ${./assert.sh}
87
88 lua_vanilla_package_path="$(${lua}/bin/lua -e "print(package.path)")"
89 lua_with_module_package_path="$(${luaWithModule}/bin/lua -e "print(package.path)")"
90
91 assertStringContains "$lua_vanilla_package_path" "./?.lua"
92 assertStringContains "$lua_vanilla_package_path" "./?/init.lua"
93
94 assertStringContains "$lua_with_module_package_path" "./?.lua"
95 assertStringContains "$lua_with_module_package_path" "./?/init.lua"
96
97 touch $out
98 '');
99
100
101 /*
102 Check that a lua package's propagatedBuildInputs end up in LUA_PATH
103 */
104 checkPropagatedBuildInputs = pkgs.runCommandLocal "test-${lua.name}-setup-hook" ({
105 # lua-curl is a propagatedBuildInput of rest-nvim has
106 buildInputs = [ lua.pkgs.rest-nvim ];
107 }) (''
108 ${lua}/bin/lua -e "require'cURL'"
109 touch $out
110 '');
111
112})