lua.tests: add test for relative import

better error message as well
extracted from https://github.com/NixOS/nixpkgs/pull/273342

written by heyarne
This commit is contained in:
Matthieu Coudron 2024-01-22 23:11:46 +01:00
parent 8656de9673
commit ff5b2d7d00
3 changed files with 31 additions and 8 deletions

View File

@ -71,7 +71,7 @@ let
inherit executable luaversion;
luaOnBuild = luaOnBuildForHost.override { inherit packageOverrides; self = luaOnBuild; };
tests = callPackage ./tests { inherit (luaPackages) wrapLua; };
tests = callPackage ./tests { lua = self; inherit (luaPackages) wrapLua; };
inherit luaAttr;
};

View File

@ -8,9 +8,14 @@ function fail() {
}
function assertStringEqual {
function assertStringEqual() {
if ! diff <(echo "$1") <(echo "$2") ; then
fail "Strings differ"
fail "expected \"$1\" to be equal to \"$2\""
fi
}
function assertStringContains() {
if ! echo "$1" | grep -q "$2" ; then
fail "expected \"$1\" to contain \"$2\""
fi
}

View File

@ -8,7 +8,7 @@
let
runTest = lua: { name, command }:
pkgs.runCommandLocal "test-${lua.name}" ({
pkgs.runCommandLocal "test-${lua.name}-${name}" ({
nativeBuildInputs = [lua];
meta.platforms = lua.meta.platforms;
}) (''
@ -27,6 +27,10 @@ let
wrapLuaPrograms
'';
});
luaWithModule = lua.withPackages(ps: [
ps.lua-cjson
]);
in
pkgs.recurseIntoAttrs ({
@ -36,15 +40,29 @@ in
generated=$(lua -e 'print(package.path)')
golden_LUA_PATH='./share/lua/${lua.luaversion}/?.lua;./?.lua;./?/init.lua'
assertStringEqual "$generated" "$golden_LUA_PATH"
assertStringContains "$generated" "$golden_LUA_PATH"
'';
};
checkWrapping = pkgs.runCommandLocal "test-${lua.name}" ({
checkWrapping = pkgs.runCommandLocal "test-${lua.name}-wrapping" ({
}) (''
grep -- 'LUA_PATH=' ${wrappedHello}/bin/hello
touch $out
'');
})
checkRelativeImports = pkgs.runCommandLocal "test-${lua.name}-relative-imports" ({
}) (''
source ${./assert.sh}
lua_vanilla_package_path="$(${lua}/bin/lua -e "print(package.path)")"
lua_with_module_package_path="$(${luaWithModule}/bin/lua -e "print(package.path)")"
assertStringContains "$lua_vanilla_package_path" "./?.lua"
assertStringContains "$lua_vanilla_package_path" "./?/init.lua"
assertStringContains "$lua_with_module_package_path" "./?.lua"
assertStringContains "$lua_with_module_package_path" "./?/init.lua"
touch $out
'');
})