summaryrefslogtreecommitdiff
path: root/src/luacov/hook.lua
blob: 0c070c13afbeb14de6e4638ccabc4ea99bd821ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
------------------------
-- Hook module, creates debug hook used by LuaCov.
-- @class module
-- @name luacov.hook
local hook = {}

----------------------------------------------------------------
--- Creates a new debug hook.
-- @param runner runner module.
-- @return debug hook function that uses runner fields and functions
-- and sets `runner.data`.
function hook.new(runner)
   local ignored_files = {}
   local steps_after_save = 0

   return function(_, line_nr, level)
      -- Do not use string metamethods within the debug hook:
      -- they may be absent if it's called from a sandboxed environment
      -- or because of carelessly implemented monkey-patching.
      level = level or 2
      if not runner.initialized then
         return
      end

      -- Get name of processed file.
      local name = debug.getinfo(level, "S").source
      local prefixed_name = string.match(name, "^@(.*)")
      if prefixed_name then
         name = prefixed_name
      elseif not runner.configuration.codefromstrings then
         -- Ignore Lua code loaded from raw strings by default.
         return
      end

      local data = runner.data
      local file = data[name]

      if not file then
         -- New or ignored file.
         if ignored_files[name] then
            return
         elseif runner.file_included(name) then
            file = {max = 0, max_hits = 0}
            data[name] = file
         else
            ignored_files[name] = true
            return
         end
      end

      if line_nr > file.max then
         file.max = line_nr
      end

      local hits = (file[line_nr] or 0) + 1
      file[line_nr] = hits

      if hits > file.max_hits then
         file.max_hits = hits
      end

      if runner.tick then
         steps_after_save = steps_after_save + 1

         if steps_after_save == runner.configuration.savestepsize then
            steps_after_save = 0

            if not runner.paused then
               runner.save_stats()
            end
         end
      end
   end
end

return hook