|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +help() |
| 4 | +{ |
| 5 | + echo "Usage: The script returns untagged tests if any in the terraform-provider-oci repository. The script only takes one option at a time." |
| 6 | + echo "Checks for untagged tests in all test files if no option passed." |
| 7 | + echo |
| 8 | + echo "Syntax: ./scripts/check-untagged-tests.sh [-h|s|f]" |
| 9 | + echo "options:" |
| 10 | + echo "h print help" |
| 11 | + echo "s check for untagged tests in specific service e.g. ./check-untagged-tests -s ocvp" |
| 12 | + echo "f check for untagged tests in specific test file e.g. ./check-untagged-tests -f ocvp_esxi_host_test.go" |
| 13 | + echo |
| 14 | + echo "make command syntax: make SERVICE=ocvp check-untagged-tests OR make check-untagged-tests" |
| 15 | + echo |
| 16 | +} |
| 17 | + |
| 18 | +checkFn () { |
| 19 | + SEARCH_PATTERN=$1 |
| 20 | + echo "SEARCH_PATTERN: $SEARCH_PATTERN" |
| 21 | + # create temp files |
| 22 | + tmpfile=$(mktemp /tmp/check-untagged-tests.XXXXXX) |
| 23 | + tmpfile2=$(mktemp /tmp/check-untagged-tests.XXXXXX) |
| 24 | + trap 'rm -f $tmpfile $tmpfile2' 0 2 3 15 |
| 25 | + |
| 26 | + echo "==> Checking for untagged test functions" |
| 27 | + # grep test function header with tagging information |
| 28 | + grep -B 1 -h '^func Test.*{$' $SEARCH_PATTERN > $tmpfile |
| 29 | + if [[ $? -ne 0 ]] |
| 30 | + then |
| 31 | + echo "grep command failed for SEARCH_PATTERN" $SEARCH_PATTERN |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | + # remove the tagged tests from the grep output |
| 35 | + vi -es '+g/issue-routing-tag/-1,+1d' '+w !tee' '+wq!' $tmpfile |
| 36 | + # parse the untagged test function names |
| 37 | + grep -oE '^func Test[^(]+\(' $tmpfile | sed 's/(//; s/func //' > $tmpfile2 |
| 38 | + |
| 39 | + if [ -s "$tmpfile2" ] |
| 40 | + then |
| 41 | + echo "==> Following untagged test functions found:" |
| 42 | + echo "________________________________________________________________________" |
| 43 | + cat $tmpfile2 |
| 44 | + echo "________________________________________________________________________" |
| 45 | + echo "==> [Action Required] Please provide correct tagging information for above test functions" |
| 46 | + exit 1 |
| 47 | + else |
| 48 | + echo "untagged test functions not found!" |
| 49 | + exit 0 |
| 50 | + fi |
| 51 | +} |
| 52 | + |
| 53 | +if [ $# -eq 0 ] |
| 54 | + then |
| 55 | + echo "Checking all test files" |
| 56 | + checkFn "oci/*_test.go" |
| 57 | +fi |
| 58 | + |
| 59 | +# Get the options |
| 60 | +while getopts "hs:f:" option; do |
| 61 | + case $option in |
| 62 | + h) # check for untagged tests in specific service |
| 63 | + help |
| 64 | + exit 0;; |
| 65 | + s) # check for untagged tests in specific service |
| 66 | + checkFn "oci/$OPTARG*_test.go";; |
| 67 | + f) # check for untagged tests in specific test file |
| 68 | + checkFn "oci/$OPTARG";; |
| 69 | + *) # invalid option |
| 70 | + echo "Error: Invalid option" |
| 71 | + help |
| 72 | + exit 1;; |
| 73 | + esac |
| 74 | +done |
0 commit comments