Run shellcheck on bash completion
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Dr. Stefan Schimanski · committed by Dr. Stefan Schimanski · parent 60267ae · Browse files · 3 files·+51 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+51 added−15 removed
- other:+51 added−15 removed
Provenance
- Commit
defeccc- Tree
7c0ebe9- Author
Dr. Stefan Schimanski <stefan.schimanski@gmail.com>- Committer
Dr. Stefan Schimanski <stefan.schimanski@gmail.com>- Parent
60267ae
3 files changed+51 added−15 removed
modified:.travis.yml+5 added-1 removed
2 unmodified lines34567891011812132 unmodified lines - 1.3.3 - 1.4.2 - 1.5.1 - tipbefore_install: - mkdir -p bin - curl -Lso bin/shellcheck https://github.com/caarlos0/shellcheck-docker/releases/download/v0.4.3/shellcheck - chmod +x bin/shellcheckscript: - go test -v ./... - PATH=$PATH:$PWD/bin go test -v ./... - go buildmodified:bash_completions_test.go+30 added
2 unmodified lines3 import (4 "bytes"5 "fmt"6 "os"7+ "os/exec"8 "strings"9 "testing"10 )11 11 unmodified lines23 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)24 }25 }26 27+func runShellCheck(s string) error {28+ excluded := []string{29+ "SC2034", // PREFIX appears unused. Verify it or export it.30+ }31+ cmd := exec.Command("shellcheck", "-s", "bash", "-", "-e", strings.Join(excluded, ","))32+ cmd.Stderr = os.Stderr33+ cmd.Stdout = os.Stdout34+35+ stdin, err := cmd.StdinPipe()36+ if err != nil {37+ return err38+ }39+ go func() {40+ defer stdin.Close()41+ stdin.Write([]byte(s))42+ }()43+44+ return cmd.Run()45+}46+47 // World worst custom function, just keep telling you to enter hello!48 const (49 bashCompletionFunc = `__custom_func() {50 COMPREPLY=( "hello" )76 unmodified lines127 // check for subdirs_in_dir flags128 check(t, str, `flags_completion+=("__handle_subdirs_in_dir_flag themes")`)129 130 checkOmit(t, str, cmdDeprecated.Name())131+132+ // if available, run shellcheck against the script133+ if err := exec.Command("which", "shellcheck").Run(); err != nil {134+ return135+ }136+ err := runShellCheck(str)137+ if err != nil {138+ t.Fatalf("shellcheck failed: %v", err)139+ }140 }modified:bash_completions.go+16 added-14 removed
59 unmodified lines60 }61 62 __handle_reply()63 {64- __debug "${FUNCNAME}"64+ __debug "${FUNCNAME[0]}"65 case $cur in66 -*)67 if [[ $(type -t compopt) = "builtin" ]]; then68 compopt -o nospace5 unmodified lines74 allflags=("${flags[*]} ${two_word_flags[*]}")75 fi76 COMPREPLY=( $(compgen -W "${allflags[*]}" -- "$cur") )77 if [[ $(type -t compopt) = "builtin" ]]; then78- [[ $COMPREPLY == *= ]] || compopt +o nospace78+ [[ "${COMPREPLY[0]}" == *= ]] || compopt +o nospace79 fi80 81 # complete after --flag=abc82 if [[ $cur == *=* ]]; then4 unmodified lines87 local index flag88 flag="${cur%%=*}"89 __index_of_word "${flag}" "${flags_with_completion[@]}"90 if [[ ${index} -ge 0 ]]; then91- COMPREPLY=() PREFIX="" cur="${cur#*=}"91+ COMPREPLY=()92+ PREFIX=""93+ cur="${cur#*=}"94 ${flags_completion[${index}]}95 if [ -n "${ZSH_VERSION}" ]; then96 # zfs completion needs --flag= prefix95- eval COMPREPLY=( "\${COMPREPLY[@]/#/${flag}=}" )97+ eval "COMPREPLY=( \"\${COMPREPLY[@]/#/${flag}=}\" )"98 fi99 fi100 fi101 return 0;48 unmodified lines150 }151 152 __handle_flag()153 {152- __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"154+ __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"155 156 # if a command required a flag, and we found it, unset must_have_one_flag()157 local flagname=${words[c]}158 local flagvalue2 unmodified lines161 flagvalue=${flagname#*=} # take in as flagvalue after the =162 flagname=${flagname%%=*} # strip everything after the =163 flagname="${flagname}=" # but put the = back164 fi163- __debug "${FUNCNAME}: looking for ${flagname}"165+ __debug "${FUNCNAME[0]}: looking for ${flagname}"166 if __contains_word "${flagname}" "${must_have_one_flag[@]}"; then167 must_have_one_flag=()168 fi169 170 # keep flag value with flagname as flaghash169- if [ ${flagvalue} ] ; then171+ if [ -n "${flagvalue}" ] ; then172 flaghash[${flagname}]=${flagvalue}171- elif [ ${words[ $((c+1)) ]} ] ; then173+ elif [ -n "${words[ $((c+1)) ]}" ] ; then174 flaghash[${flagname}]=${words[ $((c+1)) ]}175 else176 flaghash[${flagname}]="true" # pad "true" for bool flag177 fi12 unmodified lines190 }191 192 __handle_noun()193 {192- __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"194+ __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"195 196 if __contains_word "${words[c]}" "${must_have_one_noun[@]}"; then197 must_have_one_noun=()198 elif __contains_word "${words[c]}" "${noun_aliases[@]}"; then5 unmodified lines204 }205 206 __handle_command()207 {206- __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"208+ __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"209 210 local next_command211 if [[ -n ${last_command} ]]; then212 next_command="_${last_command}_${words[c]//:/__}"213 else214 if [[ $c -eq 0 ]]; then213- next_command="_$(basename ${words[c]//:/__})"215+ next_command="_$(basename "${words[c]//:/__}")"216 else217 next_command="_${words[c]//:/__}"218 fi219 fi220 c=$((c+1))219- __debug "${FUNCNAME}: looking for ${next_command}"221+ __debug "${FUNCNAME[0]}: looking for ${next_command}"222 declare -F $next_command >/dev/null && $next_command223 }224 225 __handle_word()1 unmodified line227 if [[ $c -ge $cword ]]; then228 __handle_reply229 return230 fi229- __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"231+ __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"232 if [[ "${words[c]}" == -* ]]; then233 __handle_flag234 elif __contains_word "${words[c]}" "${commands[@]}"; then235 __handle_command234- elif [[ $c -eq 0 ]] && __contains_word "$(basename ${words[c]})" "${commands[@]}"; then236+ elif [[ $c -eq 0 ]] && __contains_word "$(basename "${words[c]}")" "${commands[@]}"; then237 __handle_command238 else239 __handle_noun240 fi362 unmodified lines