[Tool] Lua code coverage run script

I’m releasing this simple script and setup instructions I wrote for Lua code coverage, we’re no longer using Lua as it’s not well suited to large projects such as what we’re working on DistrictRP, but for those using Lua I thought I’d put this here in the hope of encouraging unit testing and the likes for more robust code!

Anyway here it goes:

[Linux] run_coverage.sh (copy the code below and paste into a file with this name)

#!/bin/bash
# ------------------------------------------------------------------
# [Edward McKnight (EM-Creations.co.uk)] Run coverage
#          Run Lua code coverage
# ------------------------------------------------------------------
echo "Running coverage.."

echo "Cleaning up.."
rm *.report.out
rm *.stats.out

echo "Collecting coverage data.."
find . -type f -name '*.lua' -exec lua -lluacov {} 2> /dev/null \;

echo "Generating report.."
luacov

echo "Finished!"

[Windows] run_coverage.bat (copy the code below and paste into a file with this name)

@echo off
REM Author: Edward McKnight (EM-Creations.co.uk)
echo Running coverage..

echo Cleaning up..
del "*.report.out"
del "*.stats.out"

echo Collecting coverage data..
FOR /R %%G IN (*.lua) DO (
  lua -lluacov %%G >NUL 2>&1
)

echo Generating report..
luacov

echo Finished!

Caveat
Lua doesn’t seem to have the same level of support for this as other languages (such as Java). At the moment the coverage package we’re using just highlights how many times a piece of code is called, rather than linking it directly to unit tests. This is still useful however as it highlights areas of code that are accessed alot so extra attention should be given to these highlighted errors to ensure that they’re thoroughly tested.

Requirements:

  • Lua installed locally
  • luarocks installed locally
  • The above two programs available on the command line or terminal

Step-by-step guide:

  • Install “luacov” (Lua Coverage) luarocks install luacov
  • Run the code coverage script from the top-level directory (Linux) bash run_coverage.sh or (Windows) run_coverage.bat
  • Open the top-level directory “luacoverage.report.out” file to see the code coverage report

I hope this is of some help to some people!

1 Like

Moved to #development:releases :wink:

1 Like

Much appreciated, thank you :slight_smile: