From c4b5c2574b6877d84a7bf5c2e57c85fc6cdd41bb Mon Sep 17 00:00:00 2001 From: Matthieu Coudron Date: Fri, 1 Apr 2022 04:27:53 +0200 Subject: [PATCH] lua: test the interpreter Test with: nix-build -A lua.tests pass Tests are very limited for now, goal is mostly to put the infra into place and enrich the tests when dealing with lua issues. add luaPath/luaCpath as passthrough makes it easier to generate LUA_PATH/LUA_CPATH --- .../interpreters/lua-5/default.nix | 2 + .../interpreters/lua-5/tests/assert.sh | 16 ++++++ .../interpreters/lua-5/tests/default.nix | 50 +++++++++++++++++++ .../interpreters/lua-5/wrapper.nix | 2 + 4 files changed, 70 insertions(+) create mode 100644 pkgs/development/interpreters/lua-5/tests/assert.sh create mode 100644 pkgs/development/interpreters/lua-5/tests/default.nix diff --git a/pkgs/development/interpreters/lua-5/default.nix b/pkgs/development/interpreters/lua-5/default.nix index d236252e5bc7..478c5bdb34db 100644 --- a/pkgs/development/interpreters/lua-5/default.nix +++ b/pkgs/development/interpreters/lua-5/default.nix @@ -69,6 +69,8 @@ let inherit executable luaversion sourceVersion; luaOnBuild = luaOnBuildForHost.override { inherit packageOverrides; self = luaOnBuild; }; + tests = callPackage ./tests { inherit (luaPackages) wrapLua; }; + inherit luaAttr; }; diff --git a/pkgs/development/interpreters/lua-5/tests/assert.sh b/pkgs/development/interpreters/lua-5/tests/assert.sh new file mode 100644 index 000000000000..fe5582a0b062 --- /dev/null +++ b/pkgs/development/interpreters/lua-5/tests/assert.sh @@ -0,0 +1,16 @@ +# Always failing assertion with a message. +# +# Example: +# fail "It should have been but it wasn't to be" +function fail() { + echo "$1" + exit 1 +} + + +function assertStringEqual { + + if ! diff <(echo "$1") <(echo "$2") ; then + fail "Strings differ" + fi +} diff --git a/pkgs/development/interpreters/lua-5/tests/default.nix b/pkgs/development/interpreters/lua-5/tests/default.nix new file mode 100644 index 000000000000..38479af5f207 --- /dev/null +++ b/pkgs/development/interpreters/lua-5/tests/default.nix @@ -0,0 +1,50 @@ +{ lua +, hello +, wrapLua +, lib, fetchFromGitHub +, fetchFromGitLab +, pkgs +}: +let + + runTest = lua: { name, command }: + pkgs.runCommandLocal "test-${lua.name}" ({ + nativeBuildInputs = [lua]; + meta.platforms = lua.meta.platforms; + }) ('' + source ${./assert.sh} + '' + + command + + "touch $out" + ); + + wrappedHello = hello.overrideAttrs(oa: { + propagatedBuildInputs = [ + wrapLua + lua.pkgs.cjson + ]; + postFixup = '' + wrapLuaPrograms + ''; + }); +in + pkgs.recurseIntoAttrs ({ + + checkAliases = runTest lua { + name = "check-aliases"; + command = '' + generated=$(lua -e 'print(package.path)') + golden_LUA_PATH='./share/lua/${lua.luaversion}/?.lua;./?.lua;./?/init.lua' + + assertStringEqual "$generated" "$golden_LUA_PATH" + ''; + }; + + checkWrapping = pkgs.runCommandLocal "test-${lua.name}" ({ + }) ('' + grep -- 'LUA_PATH=' ${wrappedHello}/bin/hello + touch $out + ''); + +}) + diff --git a/pkgs/development/interpreters/lua-5/wrapper.nix b/pkgs/development/interpreters/lua-5/wrapper.nix index b9ac255d2432..9431522b71e5 100644 --- a/pkgs/development/interpreters/lua-5/wrapper.nix +++ b/pkgs/development/interpreters/lua-5/wrapper.nix @@ -60,6 +60,8 @@ let passthru = lua.passthru // { interpreter = "${env}/bin/lua"; inherit lua; + luaPath = lua.pkgs.lib.genLuaPathAbsStr env; + luaCpath = lua.pkgs.lib.genLuaCPathAbsStr env; env = stdenv.mkDerivation { name = "interactive-${lua.name}-environment"; nativeBuildInputs = [ env ];