siffiejoe/lua-testy

Option to import a shim module before tests

Opened this issue · 0 comments

idbrii commented

I use testy on a project where we embed lua into another exe (and testy generally works great, thanks!).

We expose functions to lua and testy chokes on these functions. I've created a shim lua file that re-implements these functions in pure lua so they can run from tests. Often, require-ing this shim file from tests is enough, but for anything defined at file scope it is not:

local FileLoader = require "fileloader" -- failure occurs here
...
local test_Flowers()
    require "nativeshims"  -- never reach this point
    assert(MakeFlower().size == 10)
end

I don't think it's possible to solve this outside of testy without restructuring the entire project. I think it'd be helpful for testy to have an option to add modules to require before running any tests. Looks like it could require them right after it requires testy.extra.

An alternative would be to test for a testy-defined global, but I think that's a much messier solution and can fail if we make _G strict:

if _G.testy_assert then -- feels antithetical to testy's unintrusive approach.
    require "nativeshims"
end
local FileLoader = require "fileloader" -- failure occurs here

I imagine something it might be invoked like this:

testy.lua -m nativeshims flower.lua