lunarmodules / luacov

LuaCov is a simple coverage analyzer for Lua code.
http://lunarmodules.github.io/luacov/
MIT License
300 stars 68 forks source link

How to use LuaCov with Luvit? #39

Closed ghost closed 8 years ago

ghost commented 8 years ago

[I already described the issue in luvit/luvit#843 and am just copying the relevant part here.]

I am trying to make a coverage analysis of the tests of a module of mine written using Luvit using LuaCov. However, the coverage stats only include measures of luvipath.lua and luvibundle.lua, but completely miss anything else.

Example with testthis.lua:

function testthis()
        print("testthis")
end

testthis()
# luvit -e 'jit.off(); jit.flush(); local luacov = require("luacov");' testthis.lua
testthis
# cat luacov.stats.out
107:/home/tim/luvi/src/lua/luvipath.lua
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 11 31 20 0 11 0 0 5 2 0 3 0 0 0 0 5 5 0 0 0 5 5 11 22 11 6 0 0 0 5 4 0 0 0 5 16 11 31 20 0 11 0 0 0 5 5 25 20 20 20 1 19 1 0 18 0 0 0 0 0 5 13 8 8 0 0 5 5

I compared this with plain LuaJIT:

# rm luacov.stats.out
# luajit -e 'jit.off(); jit.flush(); local luacov = require("luacov");' testthis.lua
testthis
# cat luacov.stats.out
5:testthis.lua
1 1 2 0 2

At first I assumed that all of this is caused by LuaJIT, as described for LuaTrace:

luatrace runs under "plain" Lua and LuaJIT with the -joff option (LuaJIT doesn't call hooks in compiled code, and luatrace loses track of where it's up to)

However, even with the JIT enabled, plain LuaJIT still creates the desired output in my very simple test-case:

# rm luacov.stats.out
# luajit -e 'jit.on(); local luacov = require("luacov");' testthis.lua
testthis
# cat luacov.stats.out
5:testthis.lua
1 1 2 0 2 

So it is unclear what happens, but somehow Luvit prevents LuaCov from being seeing the interpreter progress.

The same behaviour can be observed for LuaTrace.

# luvit -e 'jit.off(); jit.flush(); local luatrace = require("luatrace"); luatrace.tron();' tests/run.lua

So apparently they both use the same path, which is blocked by Luvit.

Could you please help me to figure out why LuaCov does not work in this case?

mpeterv commented 8 years ago

It seems that unlike Lua and LuaJIT Luvit does not prepend '@' to filenames when setting chunk name for a loaded file. Setting codefromstrings option to true in .luacov config should work. E.g.

return {
   codefromstrings = true,
   include = {"testthis$"}
}
ghost commented 8 years ago

Thanks! That works!