| 1 | # Contributed by Yavor Lulchev @RookieWookiee |
| 2 | # Improved by @thoni56 |
| 3 | |
| 4 | # Could not find a way to do these three in a general function (bash isn't my native language...) |
| 5 | _removeFromOptions() { |
| 6 | new_array=() |
| 7 | for value in "${options[@]}" |
| 8 | do |
| 9 | [[ $value != $1 ]] && new_array+=($value) |
| 10 | done |
| 11 | options=("${new_array[@]}") |
| 12 | unset new_array |
| 13 | } |
| 14 | |
| 15 | _removeFromLibraries() { |
| 16 | new_array=() |
| 17 | for value in "${libraries[@]}" |
| 18 | do |
| 19 | [[ $value != $1 ]] && new_array+=($value) |
| 20 | done |
| 21 | libraries=("${new_array[@]}") |
| 22 | unset new_array |
| 23 | } |
| 24 | |
| 25 | _removeFromTests() { |
| 26 | new_array=() |
| 27 | for value in "${tests[@]}" |
| 28 | do |
| 29 | [[ $value != $1 ]] && new_array+=($value) |
| 30 | done |
| 31 | tests=("${new_array[@]}") |
| 32 | unset new_array |
| 33 | } |
| 34 | |
| 35 | _discover_tests() |
| 36 | { |
| 37 | if test -f $word ; then |
| 38 | local raw=$(nm -f posix $word | grep -o -E 'CgreenSpec\w*?\b') |
| 39 | local specs=( $(echo "$raw" | sed 's/^CgreenSpec__//g' | sed 's/__$//g') ) |
| 40 | # specs should be an array with all <SUT>__<testname> from the library in $word |
| 41 | local SUTs=( $(printf "%s\n" "${specs[@]}" | awk -F '__' '{ print $1 }' | sort | uniq) ) |
| 42 | # SUTs should now contain all SUTs present in the library |
| 43 | |
| 44 | for SUT in "${SUTs[@]}"; do |
| 45 | sut_tests=() |
| 46 | for spec in "${specs[@]}"; do |
| 47 | if [[ $spec == "$SUT"* ]] ; then |
| 48 | sut_tests+=( $spec ) |
| 49 | fi |
| 50 | done |
| 51 | if test $SUT = "default" ; then |
| 52 | # Tests have no SUT in its name |
| 53 | tests+=( ${sut_tests[@]/default__/} ) |
| 54 | else |
| 55 | tests+=( ${sut_tests[@]/__/:} ) |
| 56 | fi |
| 57 | done |
| 58 | fi |
| 59 | } |
| 60 | |
| 61 | case $OSTYPE in |
| 62 | darwin* ) |
| 63 | LIBEXT=dylib |
| 64 | ;; |
| 65 | linux* ) |
| 66 | LIBEXT=so |
| 67 | ;; |
| 68 | cygwin ) |
| 69 | LIBEXT=dll |
| 70 | ;; |
| 71 | esac |
| 72 | |
| 73 | _cgreen_runner_completion() |
| 74 | { |
| 75 | local options libraries tests |
| 76 | options=("--colours" "--no-colours" "--xml" "--suite" "--verbose" "--no-run" "--help" " --version") |
| 77 | libraries=() |
| 78 | tests=() |
| 79 | |
| 80 | # Look for words in the command given so far |
| 81 | for word in ${COMP_WORDS[@]:1}; do |
| 82 | # Matching loadable libraries? |
| 83 | if compgen -G "$word*.$LIBEXT" > /dev/null; then |
| 84 | # Add that pattern |
| 85 | libraries+=($word*.$LIBEXT) |
| 86 | fi |
| 87 | # Is it a library? |
| 88 | if echo $word | grep -q -E "\b\.$LIBEXT\b"; then |
| 89 | _discover_tests |
| 90 | fi |
| 91 | done |
| 92 | |
| 93 | # Remove all suggestions already used |
| 94 | for word in ${COMP_WORDS[@]:1}; do |
| 95 | if [[ "$word" != "${COMP_WORDS[0]}" ]] ; then |
| 96 | _removeFromOptions $word |
| 97 | _removeFromLibraries $word |
| 98 | _removeFromTests $word |
| 99 | fi |
| 100 | done |
| 101 | |
| 102 | completion_word=${COMP_WORDS[$COMP_CWORD]} |
| 103 | expansions="${options[@]} ${libraries[@]} ${tests[@]}" |
| 104 | |
| 105 | COMPREPLY=($(compgen -W "$expansions" -- "$completion_word")) |
| 106 | } |
| 107 | |
| 108 | _cgreen_debug_completion() |
| 109 | { |
| 110 | local options libraries tests |
| 111 | options=("--debugger") |
| 112 | libraries=() |
| 113 | tests=() |
| 114 | |
| 115 | # Look for words in the command given so far |
| 116 | for word in ${COMP_WORDS[@]}; do |
| 117 | # Matching loadable libraries? |
| 118 | if compgen -G "$word*.$LIBEXT" > /dev/null; then |
| 119 | # Add that pattern |
| 120 | libraries+=($word*.$LIBEXT) |
| 121 | fi |
| 122 | if echo $word | grep -q -E "\b\.$LIBEXT\b"; then |
| 123 | _discover_tests |
| 124 | fi |
| 125 | done |
| 126 | |
| 127 | # Remove libraries and tests if already used (only one library and one test allowed) |
| 128 | for word in ${COMP_WORDS[@]}; do |
| 129 | if [[ $word == *".$LIBEXT" ]]; then |
| 130 | # Only one library allowed |
| 131 | libraries=() |
| 132 | fi |
| 133 | if [[ $word == *"\\:"* ]]; then |
| 134 | if [[ "${COMP_WORDS[$COMP_CWORD]}" == "" ]]; then |
| 135 | # Only one test allowed |
| 136 | tests=() |
| 137 | fi |
| 138 | fi |
| 139 | if [[ "$word" != "${COMP_WORDS[0]}" ]] ; then |
| 140 | _removeFromOptions $word |
| 141 | fi |
| 142 | done |
| 143 | |
| 144 | completion_word=${COMP_WORDS[$COMP_CWORD]} |
| 145 | expansions="${options[@]} ${libraries[@]} ${tests[@]}" |
| 146 | |
| 147 | COMPREPLY=($(compgen -W "$expansions" -- "$completion_word")) |
| 148 | } |
| 149 | |
| 150 | complete -o nosort -o dirnames -A directory -F _cgreen_runner_completion cgreen-runner |
| 151 | complete -o nosort -A directory -F _cgreen_debug_completion cgreen-debug |