summaryrefslogtreecommitdiff
path: root/tests/cli.lua
blob: 528aa02a210861a402ab0b41192871f46d79902c (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
local lua = arg[-1] or "lua"
local slash = lua:find("/")

-- Correct lua path so that it can be used from test directories.
if slash and slash ~= 1 then
   lua = "../../" .. lua
end

local ntests = 0

local function exec(cmd)
   local err_msg = ("CLI test #%d failed (%s)"):format(ntests, cmd)
   local ok = assert(os.execute(cmd), err_msg)
   assert(ok == 0 or ok == true, err_msg)
end

local function read(file)
   local handler = assert(io.open(file))
   local contents = assert(handler:read("*a"))
   handler:close()
   return contents
end

-- dir must be a subdir of tests/ containing expected.out or expected_file.
-- The file can contain 'H' to match any number of hits.
-- flags will be passed to luacov.
local function test(dir, expected_file, flags)
   ntests = ntests + 1
   local test_dir = "tests/" .. dir
   expected_file = expected_file or "expected.out"
   flags = flags or ""

   os.remove(test_dir .. "/luacov.stats.out")
   os.remove(test_dir .. "/luacov.report.out")
   local init_lua = "package.path=[[?.lua;../../src/?.lua;]]..package.path; corowrap = coroutine.wrap"
   exec(("cd %q && %q -e %q -lluacov test.lua"):format(
      test_dir, lua, init_lua))
   exec(("cd %q && %q -e %q ../../src/bin/luacov %s"):format(
      test_dir, lua, init_lua, flags))

   expected_file = test_dir .. "/" .. expected_file
   local expected = read(expected_file)
   local actual_file = test_dir .. "/luacov.report.out"
   local actual = read(actual_file)

   local ok

   if expected:find("H") then
      local expected_pattern = expected:gsub("%p", "%%%0"):gsub("H", "%%d+")
      ok = actual:match("^" .. expected_pattern .. "$")
   else
      ok = actual == expected
   end

   assert(ok, ("CLI test #%d failed (%s ~= %s)"):format(ntests, actual_file, expected_file))
   os.remove(test_dir .. "/luacov.stats.out")
   os.remove(test_dir .. "/luacov.report.out")
end

test("simple")

test("filefilter")
test("filefilter", "expected2.out", "-c 2.luacov")

test("coroutines")

test("hook")

test("nested")

print(("%d CLI tests passed."):format(ntests))