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