WIP: Coverage

This commit is contained in:
James Dinsdale 2017-02-27 12:57:15 +00:00
parent b990a2e143
commit d25522fe67
No known key found for this signature in database
GPG key ID: 3D8EDF4F3967CD54
5 changed files with 82 additions and 6 deletions

View file

@ -4,3 +4,6 @@ directories:
output: tests/_output
support: tests/_support
time_limit: 15
coverage:
include:
- src/**/*.zsh

View file

@ -11,10 +11,13 @@ setopt EXTENDED_GLOB
# Print each of the source files into the target, removing any comments
# and blank lines from the compiled executable
cat src/**/(^zunit).zsh | grep -v -E '^(\s*#.*[^"]|\s*)$' >> zunit
cat src/**/*.zsh | grep -v -E '^(\s*#.*[^"]|\s*)$' >> zunit
# Print the main command last
cat src/zunit.zsh | grep -v -E '^(\s*#.*[^"]|\s*)$' >> zunit
# cat src/zunit.zsh | grep -v -E '^(\s*#.*[^"]|\s*)$' >> zunit
# Execute the command at the bottom of the file
echo '_zunit "$@"' >> zunit
# Make sure the file is executable
chmod u+x zunit

View file

@ -17,6 +17,7 @@ function _zunit_run_usage() {
echo " --output-text Print results to a text log, in TAP compatible format"
echo " --output-html Print results to a HTML page"
echo " --allow-risky Supress warnings generated for risky tests"
echo " --coverage Enable calculation of code coverage"
}
###
@ -127,6 +128,9 @@ function _zunit_execute_test() {
return 126
fi
if [[ -n $coverage ]]; then
_zunit_coverage_start
fi
# Check if a time limit has been specified. We only do this if
# the ZSH version is at least 5.1.0, since older versions of ZSH
@ -174,6 +178,11 @@ function _zunit_execute_test() {
# Output the result to the user
state=$?
if [[ -n $coverage ]]; then
_zunit_coverage_end
fi
if [[ $state -eq 48 ]]; then
_zunit_skip $output
@ -204,6 +213,8 @@ function _zunit_execute_test() {
function _zunit_encode_test_name() {
echo "$1" | tr A-Z a-z \
| tr _ ' ' \
| tr / ' ' \
| tr . ' ' \
| tr - ' ' \
| tr -s ' ' \
| sed 's/\- /-/' \
@ -415,7 +426,7 @@ function _zunit_parse_argument() {
###
function _zunit_run() {
local -a arguments testfiles
local fail_fast tap allow_risky
local fail_fast tap allow_risky coverage
local output_text logfile_text output_html logfile_html
# Load the datetime module, and record the start time
@ -429,7 +440,8 @@ function _zunit_run() {
t=tap -tap=tap \
-output-text=output_text \
-output-html=output_html \
-allow-risky=allow_risky
-allow-risky=allow_risky \
-coverage=coverage
# TAP output is enabled
if [[ -n $tap ]] || [[ "$zunit_config_tap" = "true" ]]; then
@ -498,6 +510,10 @@ function _zunit_run() {
fi
fi
if [[ -n $coverage ]]; then
_zunit_coverage_build_file_list
fi
arguments=("$@")
testfiles=()

55
src/coverage.zsh Normal file
View file

@ -0,0 +1,55 @@
###############################
# Functions for code coverage #
###############################
function _zunit_coverage_start() {
trap '_zunit_mark_covered $testfile $LINENO' DEBUG
}
function _zunit_coverage_end() {
trap '' DEBUG
echo ${_zunit_coverage_data_${testfile}}
}
function _zunit_coverage_calculate() {
}
function _zunit_coverage_output() {
}
function _zunit_coverage_mark() {
local file="$1" line="$2"
safe_name=$(_zunit_encode_test_name $file)
_zunit_coverage_data_${safe_name}[$line]=1
}
function _zunit_coverage_build_file_list() {
setopt EXTENDED_GLOB
if [[ -n $zunit_config_coverage_include ]]; then
for glob in $zunit_config_coverage_include; do
for file in ${~glob}; do
safe_name=$(_zunit_encode_test_name $file)
typeset -gA _zunit_coverage_data_${safe_name}
_zunit_coverage_data_${safe_name}=()
lines=($(cat $file))
i=1
for line in $lines; do
if [[ $line =~ '(\s+)?#.*' || $line = $'\n' ]]; then
_zunit_coverage_data_${safe_name}[$i]=2
continue
fi
_zunit_coverage_data_${safe_name}[$i]=0
i=$(( i + 1 ))
done
done
done
fi
}

View file

@ -23,6 +23,7 @@ function _zunit_usage() {
echo " --output-text Print results to a text log, in TAP compatible format"
echo " --output-html Print results to a HTML page"
echo " --allow-risky Supress warnings generated for risky tests"
echo " --coverage Enable calculation of code coverage"
}
###
@ -121,5 +122,3 @@ function _zunit() {
;;
esac
}
_zunit "$@"