Skip to content

Commit

Permalink
lua.tests: add test for relative import
Browse files Browse the repository at this point in the history
better error message as well
extracted from NixOS#273342

written by heyarne
  • Loading branch information
teto committed Feb 6, 2024
1 parent 8656de9 commit ff5b2d7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkgs/development/interpreters/lua-5/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
11 changes: 8 additions & 3 deletions pkgs/development/interpreters/lua-5/tests/assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 22 additions & 4 deletions pkgs/development/interpreters/lua-5/tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}) (''
Expand All @@ -27,6 +27,10 @@ let
wrapLuaPrograms
'';
});

luaWithModule = lua.withPackages(ps: [
ps.lua-cjson
]);
in
pkgs.recurseIntoAttrs ({

Expand All @@ -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
'');
})

0 comments on commit ff5b2d7

Please sign in to comment.