From d25522fe67dd619ddb932a7b4a481cf13d7c88aa Mon Sep 17 00:00:00 2001 From: James Dinsdale Date: Mon, 27 Feb 2017 12:57:15 +0000 Subject: [PATCH] WIP: Coverage --- .zunit.yml | 3 +++ build.zsh | 7 ++++-- src/commands/run.zsh | 20 ++++++++++++++-- src/coverage.zsh | 55 ++++++++++++++++++++++++++++++++++++++++++++ src/zunit.zsh | 3 +-- 5 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 src/coverage.zsh diff --git a/.zunit.yml b/.zunit.yml index e761e4f..b926b9d 100644 --- a/.zunit.yml +++ b/.zunit.yml @@ -4,3 +4,6 @@ directories: output: tests/_output support: tests/_support time_limit: 15 +coverage: + include: + - src/**/*.zsh diff --git a/build.zsh b/build.zsh index cf8a616..98d1e46 100755 --- a/build.zsh +++ b/build.zsh @@ -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 diff --git a/src/commands/run.zsh b/src/commands/run.zsh index 044e98a..fd0f844 100644 --- a/src/commands/run.zsh +++ b/src/commands/run.zsh @@ -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=() diff --git a/src/coverage.zsh b/src/coverage.zsh new file mode 100644 index 0000000..7b14287 --- /dev/null +++ b/src/coverage.zsh @@ -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 +} diff --git a/src/zunit.zsh b/src/zunit.zsh index f48c2b5..4a8719b 100755 --- a/src/zunit.zsh +++ b/src/zunit.zsh @@ -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 "$@"