• Stars
    star
    295
  • Rank 140,902 (Top 3 %)
  • Language
    Lua
  • License
    MIT License
  • Created about 15 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

LuaCov is a simple coverage analyzer for Lua code.

LuaCov

Coverage analyzer for Lua

Build Status Windows build status


Overview

LuaCov is a simple coverage analyzer for Lua scripts. When a Lua script is run with the luacov module loaded, it generates a stats file with the number of executions of each line of the script and its loaded modules. The luacov command-line script then processes this file generating a report file which allows one to visualize which code paths were not traversed, which is useful for verifying the effectiveness of a test suite.

LuaCov is free software and, like Lua, is released under the MIT License.

Download and Installation

LuaCov can be downloaded from its Github downloads page.

It can also be installed using Luarocks:

luarocks install luacov

In order to additionally install experimental C extensions that improve performance and analysis accuracy install CLuaCov package instead:

luarocks install cluacov

LuaCov is written in pure Lua and has no external dependencies.

Instructions

Using LuaCov consists of two steps: running your script to collect coverage data, and then running luacov on the collected data to generate a report (see configuration below for other options).

To collect coverage data, your script needs to load the luacov Lua module. This can be done from the command-line, without modifying your script, like this:

lua -lluacov test.lua

Alternatively, you can add require("luacov") to the first line of your script.

Once the script is run, a file called luacov.stats.out is generated. If the file already exists, statistics are added to it. This is useful, for example, for making a series of runs with different input parameters in a test suite. To start the accounting from scratch, just delete the stats file.

To generate a report, just run the luacov command-line script. It expects to find a file named luacov.stats.out in the current directory, and outputs a file named luacov.report.out. The script takes the following parameters:

luacov [-c=configfile] [filename...]

For the -c option see below at configuration. The filenames (actually Lua patterns) indicate the files to include in the report, specifying them here equals to adding them to the include list in the configuration file, with .lua extension stripped.

This is an example output of the report file:

==============================================================================
test.lua
==============================================================================
 1 if 10 > 100 then
*0    print("I don't think this line will execute.")
   else
 1    print("Hello, LuaCov!")
   end

Note that to generate this report, luacov reads the source files. Therefore, it expects to find them in the same location they were when the luacov module ran (the stats file stores the filenames, but not the sources themselves).

To silence missed line reporting for a group of lines, place inline options luacov: disable and luacov: enable in short comments around them:

if SOME_DEBUG_CONDITION_THAT_IS_ALWAYS_FALSE_IN_TESTS then
   -- luacov: disable

   -- Lines here are not marked as missed even though they are not covered.

   -- luacov: enable
end

LuaCov saves its stats upon normal program termination. If your program is a daemon -- in other words, if it does not terminate normally -- you can use the luacov.tick module or tick configuration option, which periodically saves the stats file. For example, to run (on Unix systems) LuaCov on Xavante, just modify the first line of xavante_start.lua so it reads:

#!/usr/bin/env lua -lluacov.tick

or add

tick = true

to .luacov config file.

Configuration

LuaCov includes several configuration options, which have their defaults stored in src/luacov/defaults.lua. These are the global defaults. To use project specific configuration, create a Lua script setting options as globals or returning a table with some options and store it as .luacov in the project directory from where luacov is being run. For example, this config informs LuaCov that only foo module and its submodules should be covered and that they are located inside src directory:

modules = {
   ["foo"] = "src/foo/init.lua",
   ["foo.*"] = "src"
}

For a full list of options, see luacov.defaults documentation.

Html reporter

To generate report file as html document, adjust the .luacov parameters to

reporter = "html"
reportfile = "luacov.report.html"

LuaCov Html Reporter

Custom reporter engines

LuaCov supports custom reporter engines, which are distributed as separate packages. Check them out!

Using development version

After cloning this repo, these commands may be useful:

  • luarocks make to install LuaCov from local sources;
  • busted to run tests using busted.
  • ldoc . to regenerate documentation using LDoc.
  • luacheck . to lint using Luacheck.

Credits

LuaCov was designed and implemented by Hisham Muhammad as a tool for testing LuaRocks.

More Repositories

1

Penlight

A set of pure Lua libraries focusing on input data handling (such as reading configuration files), functional programming (such as map, reduce, placeholder expressions,etc), and OS path management. Much of the functionality is inspired by the Python standard libraries.
Lua
1,883
star
2

luasocket

Network support for the Lua language
HTML
1,814
star
3

busted

Elegant Lua unit testing.
Lua
1,387
star
4

luafilesystem

LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution.
C
891
star
5

ldoc

LDoc is a LuaDoc-compatible documentation generator which can also process C extension source. Markdown may be optionally used to render comments, as well as integrated readme documentation and pretty-printed example files.
Lua
774
star
6

luasql

LuaSQL is a simple interface from Lua to a DBMS.
Lua
536
star
7

luasec

LuaSec
C
364
star
8

lua-compat-5.3

Compatibility module providing Lua-5.3-style APIs for Lua 5.2 and 5.1
C
225
star
9

luassert

Assertion library for Lua
Lua
202
star
10

copas

Copas is a dispatcher based on coroutines that can be used by TCP/IP servers.
Lua
202
star
11

lua_cliargs

A command-line argument parsing module for Lua.
Lua
120
star
12

md5

MD5 offers basic cryptographic facilities for Lua 5.1.
C
109
star
13

cgilua

CGILua is a tool for creating dynamic HTML pages and manipulating input data from Web forms.
Lua
93
star
14

lua-iconv

Lua bindings for POSIX iconv
Lua
90
star
15

lualogging

A simple API to use logging features in Lua
Lua
51
star
16

say

Lua string hashing library, useful for internationalization
Lua
45
star
17

luaexpat

LuaExpat is a SAX XML parser based on the Expat library
Lua
23
star
18

luasystem

Platform independent system calls for Lua
C
19
star
19

lua-mimetypes

fixed repo of https://luarocks.org/modules/luarocks/mimetypes
Lua
6
star
20

luasyslog

Addon for lualogging to log to the system log on unix systems
Lua
6
star
21

moon-sand

Sandbox to mess with CIβ€Œ deployments using LunarModules CI key(s)
Lua
2
star
22

expadom

An XML DOM Level 2 Core implementation in Lua, based on the (Lua)Expat parser.
Lua
2
star
23

lunarmodules

providing the profile page (README.md) for the Lunar Modules organization
2
star
24

.github

Lunarmodules profile page and reusable workflows
2
star
25

lua-lace

Lua Access Control Engine
Lua
2
star