summaryrefslogtreecommitdiff
path: root/src/bin/luacov
blob: 7aed2d26f474c348231ef70b878557f5d3a0fa2a (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env lua
local runner = require("luacov.runner")

local patterns = {}
local configfile
local reporter

local help_message = [[
luacov - coverage analyzer for Lua scripts

Usage:
   luacov [options] [pattern...]

   Launch your Lua programs with -lluacov to perform accounting.

   Launch this script to generate a report from collected stats.

   By default it reports on every Lua file encountered running your
   script. To filter filenames, pass one or more patterns in the
   command line, or use a config.

Options:
   -c filename, --config filename

   Use a config file, .luacov by default. For details see
   luacov.defaults module.

   -r name, --reporter name

   Use a custom reporter - a module in luacov.reporter.* namespace.

Example:
   luacov foo/bar
   
   This will report only on modules in the foo/bar subtree.
]]

local function read_key(i)
   if arg[i]:sub(1, 1) ~= "-" or #arg[i] == 1 then
      return nil, arg[i], i + 1
   end

   if arg[i]:sub(2, 2) == "-" then
      local key, value = arg[i]:match("^%-%-([^=]+)=(.*)$")
      if key then
         return key, value, i + 1
      else
         return arg[i]:sub(3), arg[i + 1], i + 2
      end
   else
      local key = arg[i]:sub(2, 2)
      local value = arg[i]:sub(3)
      if #value == 0 then
         i = i + 1
         value = arg[i]
      elseif value:sub(1, 1) == "=" then
         value = value:sub(2)
      end
      return key, value, i + 1
   end
end

local function norm_pat(p)
   return (p:gsub("[/\\]", "."):gsub("%.lua$", ""))
end

local i = 1
while arg[i] do
   local key, value
   key, value, i = read_key(i)
   if key then
      if (key == "h") or (key == "help") then
         print(help_message)
         os.exit(0)
      elseif (key == "c") or (key == "config") then
         configfile = value
      elseif (key == "r") or (key == "reporter") then
         reporter = value
      end
   else
      table.insert(patterns, norm_pat(value))
   end
end

-- will load configfile specified, or defaults otherwise
local configuration = runner.load_config(configfile)

configuration.include = configuration.include or {}
configuration.exclude = configuration.exclude or {}

-- add elements specified on commandline to config
for _, patt in ipairs(patterns) do
   table.insert(configuration.include, patt)
end

configuration.reporter = reporter or configuration.reporter
runner.run_report(configuration)