common.perl 5.6 KB
Newer Older
1 2 3 4 5 6 7
#!/bin/false /bin/bash
##
# Common functions for Perl.
#

set -eo pipefail

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Defaults for the tools we shall use
perl_tool_coverage='cover'
perl_tool_lint='perlcritic'

# Default versions of the tools
perl_toolversion_cover='1.25'
perl_toolversion_perlcritic='1.126'

# And the definitions of what we need in requirements
perl_tool_coverage_1_25='Devel::Cover@1.25'
perl_tool_perlcritic_1_126="
PPI@1.220
Perl::Critic@1.126
Perl::Critic::Pulp@93
"
23 24 25 26 27 28 29 30 31 32 33 34

##
# Is perl active for this action?
#
# @param $1     the action we are attempting to perform
function perl_active() {
    local action="$1"

    local files=''
    if [[ "$action" == 'test' || "$action" == 'coverage' ]] ; then
        files="$(config -e test_perl_files '*-test.pl')"
    elif [[ "$action" == 'lint' ]] ; then
35
        files="$(config -e lint_perl_files '{perl}')"
36 37 38 39 40 41 42 43 44 45 46 47 48
    else
        # Unrecognised type
        return 1
    fi

    if [[ -n "$files" ]] ; then
        return 0
    fi

    return 1
}


49 50 51 52 53 54 55 56 57 58
##
# Matching functions for filename expansion.
function perl_match_magic() {
    echo -n 'Perl script'
}
function perl_match_extensions() {
    echo -n 'pl pm'
}


59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
##
# Help messages
function perl_help_lint() {
    echo "--checks    List all the enabled perlcritic checks"
}


##
# List what options would be enabled
function perl_switch_lint_checks() {
    perl_environment_lint
    echo "Perl Critic checks enabled:"
    perlcritic -profile "${root}/perlcriticrc" --list-enabled \
        | sed 's/^/  /'
    exit
}


##
# Set up the environment for Perl.
#
# @param $@ CPAN requirements to install (either filenames, or names of modules preceeded by a '+')
function perl_environment() {
82 83 84 85 86
    # Taking input from /dev/null means that the input isn't a tty, so we avoid
    # printing messages.
    "${scripts}/perl-env-setup" -l "${log_dir}/cpan_output.txt" \
                                -e "${environment_dir}/perllib" "$@" < /dev/null
    source "${scripts}/perl-env" -e "${environment_dir}/perllib"
87 88
}

89 90 91 92 93 94 95 96 97 98
function perl_tool_requirements() {
    local action="$1"
    local reqs
    reqs="$(tool_config_expansion perl "$action")"
    local req
    for req in $reqs ; do
        echo -n " +$req"
    done
}

99 100 101 102 103 104 105 106
function perl_environment_test() {
    perl_environment +PadWalker@2.2 \
                     "${root}/cpan.txt" \
                     "${root}/cpan-test.txt" \
                     "${scripts}/cpan-test.txt"
}

function perl_environment_coverage() {
107
    perl_environment +PadWalker@2.2 \
108 109 110 111
                     "${root}/cpan.txt" \
                     "${root}/cpan-test.txt" \
                     "${root}/cpan-coverage.txt" \
                     "${scripts}/cpan-test.txt" \
112 113
                     "${scripts}/cpan-coverage.txt" \
                     $(perl_tool_requirements coverage)
114 115 116
}

function perl_environment_lint() {
117
    perl_environment +PadWalker@2.2 \
118
                     "${root}/cpan.txt" \
119
                     "${root}/cpan-test.txt" \
120
                     "${root}/cpan-lint.txt" \
121 122 123
                     "${scripts}/cpan-test.txt" \
                     "${scripts}/cpan-lint.txt" \
                     $(perl_tool_requirements lint)
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
}


function perl_setup_test() {
    : # Nothing to do
}

function perl_setup_coverage() {
    local ignore
    local ignore_options=''
    local pattern
    ignore="$(config coverage_perl_ignore '-test JFTest')"

    for pattern in ${ignore} ; do
        pattern=${pattern//+/[+]}
        pattern=${pattern//-/[-]}
        ignore_options="${ignore_options},+ignore_re,${pattern}"
    done

143
    export PERL5OPT="MDevel::Cover=-db,${log_dir}/cover_db$ignore_options,-silent,1"
144 145 146 147 148 149 150 151 152
}

function perl_setup_lint() {
    : # Nothing to do
}


function perl_run_test() {
    for test in $(config -e test_perl_files '*-test.pl') ; do
153
        if perl "$test" 2>&1 | output_filter "Test '$test'..." ; then
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
            tests_passed=$(( tests_passed + 1 ))
        else
            echo "  Test '$test' failed"
            tests_failed=$(( tests_failed + 1 ))
        fi
        tests_total=$(( tests_total + 1 ))
    done
}

# Running the actual coverage is just calling the tests
function perl_run_coverage() {
    perl_run_test "$@"
}

function perl_run_lint() {
169 170 171
    local file
    local message="Compiling Perl files"

172
    for file in $(config -e lint_perl_files '{perl}') ; do
173
        if ! perl -c "$file" 2>&1 | output_filter "$message" ; then
174 175 176 177
            lint_failed=$(( lint_failed+1 ))
        else
            lint_passed=$(( lint_passed+1 ))
        fi
178
        message="+"
179
        lint_total=$(( lint_total+1 ))
180 181
    done

182
    if ! perlcritic --profile "${root}/perlcriticrc" \
183
                    --color \
184
                    $(config -e lint_perl_files '{perl}') \
185
         2>&1 | output_filter "Linting Perl files"; then
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        lint_failed=$(( lint_failed+1 ))
    else
        lint_passed=$(( lint_passed+1 ))
    fi
    lint_total=$(( lint_total+1 ))
}


function perl_process_test() {
    : # Nothing to do
}

function perl_process_coverage() {
    export PERL5OPT=''

    echo '--- Generating HTML coverage report ---'
    cover -report html_basic \
203
          -outputdir "${artifact_dir}/perl-coverage" \
204
          +ignore_re 'test-' \
205
          "${log_dir}/cover_db" > "${artifact_dir}/perl-coverage.txt"
206 207 208

    echo '--- Generating text coverage report ---'
    cover -report text \
209
          -outputdir "${artifact_dir}/perl-coverage" \
210
          +ignore_re 'test-' \
211
          "${log_dir}/cover_db" > "${artifact_dir}/perl-coveragefull.txt"
212 213

    # And read the amount of code covered
214
    coverage_percentage=$(grep Total "${artifact_dir}/perl-coverage.txt" | awk 'END { print $3 }')
215 216 217 218 219 220
    echo "Perl Coverage: ${coverage_percentage}%"
}

function perl_process_lint() {
    : # Nothing to do
}