From f5981b91455f1e9235c1ecd47aa9fa9d888a9bed Mon Sep 17 00:00:00 2001 From: Virgil Date: Tue, 23 Apr 2019 16:38:38 +1000 Subject: [PATCH] Linting with shellcheck (#32) * [SC2196] Use grep -Eegrep is non-standard and deprecated. * [SC2086] Double quoting Double quote to prevent globbing and word splitting. * [SC2059] printf syntax Don't use variables in the printf format string. Use printf "..%s.." "$foo". Update matching test to not assume whitespace is * [SC2002] Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead. * Update Unicode PASS/FAIL output - [x] NEW variables for 'checkmark' and 'fancy x' and tput colours - [x] eg: `checkmark` = `\342\234\223` - [x] eg: `PASS=${txtbold}${txtgreen}${checkmark}${txtreset}` - [x] update `printf` statements to use `%b` for unicode variables. - [x] Using readonly variables for - [x] $BINARY_NAME and $VERSION - [x] $PASS and $FAIL - [x] refactor `.hasrc` file reading - [x] [SC2002] Useless cat - [x] Add new BATS tests for Unicode output * Repo scaffolding - shellscript filetype - [x] Add shellscripts to `.editorconfig` - [x] Add shellscripts to `.gitattributes` * Refactored tests to use consistent unicode glyphs. --- .editorconfig | 10 ++-- .gitattributes | 10 +++- .hastest.bats | 57 ++++++++++++++++------ has | 129 +++++++++++++++++++++++++++---------------------- 4 files changed, 127 insertions(+), 79 deletions(-) diff --git a/.editorconfig b/.editorconfig index 803935a..4ec16b7 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,19 +1,23 @@ # http://editorconfig.org/ - root = true [*] indent_style = space -indent_size = 2 - end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true +[*.{sh,bash,bats}] +indent_size = 2 +indent_style = space + +# Markdown [*.md] +indent_size = 4 trim_trailing_whitespace = false +# Makefiles always use tabs for indentation [{Makefile, makefile}] indent_style = tab indent_size = 4 diff --git a/.gitattributes b/.gitattributes index 2125666..40ccdcb 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,9 @@ -* text=auto \ No newline at end of file +# Auto detect text files and perform LF normalization +# http://davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/ +* text=auto eol=lf +# +# The above will handle all files NOT found below +# +*.sh text diff=sh eol=lf +*.bash text diff=sh eol=lf +*.md text eol=lf diff --git a/.hastest.bats b/.hastest.bats index e580cc7..d4c3a1e 100644 --- a/.hastest.bats +++ b/.hastest.bats @@ -2,6 +2,8 @@ INSTALL_DIR= BATS_TMPDIR=${BATS_TMPDIR:-/tmp} +fancyx='✗' +checkmark='✓' ## We need to create a new directory so that .hasrc file in the root does not get read by the `has` instance under test setup() { @@ -29,11 +31,11 @@ teardown() { run "${INSTALL_DIR}/bin/has" [ "$status" -eq 0 ] [ "${lines[0]%% *}" == 'has' ] - [ "${lines[1]%% *}" == 'USAGE:' ] + [ "${lines[1]%%:*}" == 'USAGE' ] rm -rf ${INSTALL_DIR} } -@test "..even if has is missing from directory" { +@test "..even if 'has' is missing from directory" { INSTALL_DIR="${BATS_TEST_TMPDIR}/system_local" cd "${BATS_TEST_DIRNAME}" mv has has-been @@ -48,6 +50,7 @@ teardown() { @test "make update runs git fetch" { cd "${BATS_TEST_DIRNAME}" run make update + [[ "$status" -eq 0 ]] [[ "${lines[@]}" =~ "git fetch --verbose" ]] } @@ -59,33 +62,34 @@ teardown() { [[ "$(echo "${output}" | grep "USAGE:")" ]] [[ "$(echo "${output}" | grep "EXAMPLE:")" ]] } + @test "works with single command check" { run bash has git [[ "$status" -eq 0 ]] - [[ "$(echo "${output}" | grep "✔" | grep "git")" ]] + [[ "$(echo "${output}" | grep ${checkmark} | grep "git")" ]] } @test "safely tells about tools not configured" { run bash has foobar [[ "$status" -eq 1 ]] - [[ "$(echo "${output}" | grep "✘" | grep "foobar not understood")" ]] + [[ "$(echo "${output}" | grep ${fancyx} | grep "foobar not understood")" ]] } @test "env var lets override safety check" { HAS_ALLOW_UNSAFE=y run bash has foobar [[ "$status" -eq 1 ]] - [[ "$(echo "${output}" | grep "✘" | grep "foobar")" ]] + [[ "$(echo "${output}" | grep ${fancyx} | grep "foobar")" ]] } @test "status code reflects number of failed commands" { HAS_ALLOW_UNSAFE=y run bash has foobar bc git barbaz [[ "$status" -eq 2 ]] - [[ "$(echo "${output}" | grep "✘" | grep "foobar")" ]] - [[ "$(echo "${output}" | grep "✘" | grep "barbaz")" ]] + [[ "$(echo "${output}" | grep ${fancyx} | grep "foobar")" ]] + [[ "$(echo "${output}" | grep ${fancyx} | grep "barbaz")" ]] } @test "status code reflects number of failed commands upto 126" { @@ -94,7 +98,6 @@ teardown() { [[ "$status" -eq 126 ]] } - @test "loads commands from .hasrc file and excludes comments" { printf "bash\n#comment\nmake\n" >> .hasrc @@ -102,19 +105,41 @@ teardown() { [[ "$status" -eq 0 ]] - [[ "$(echo "${output}" | grep "✔" | grep "bash")" ]] - [[ "$(echo "${output}" | grep "✔" | grep "make")" ]] + [[ "$(echo "${output}" | grep ${checkmark} | grep "bash")" ]] + [[ "$(echo "${output}" | grep ${checkmark} | grep "make")" ]] } -@test "loads commands from .hasrc file and honors cli args as well" { +@test "loads commands from .hasrc file and honors CLI args as well" { printf "bash\nmake\ngit" >> .hasrc - HAS_ALLOW_UNSAFE=y run bash has git bc [[ "$status" -eq 0 ]] - [[ "$(echo "${output}" | grep "✔" | grep "bash")" ]] - [[ "$(echo "${output}" | grep "✔" | grep "make")" ]] - [[ "$(echo "${output}" | grep "✔" | grep "git")" ]] - [[ "$(echo "${output}" | grep "✔" | grep "bc")" ]] + [[ "$(echo "${output}" | grep ${checkmark} | grep "bash")" ]] + [[ "$(echo "${output}" | grep ${checkmark} | grep "make")" ]] + [[ "$(echo "${output}" | grep ${checkmark} | grep "git")" ]] + [[ "$(echo "${output}" | grep ${checkmark} | grep "bc")" ]] } + +@test "testing PASS output with unicode" { + run bash has git + + [[ "$status" -eq 0 ]] + [[ "printf '%b\n' ${lines[0]}" =~ '✓' ]] +} + +@test "testing FAIL output with unicode" { + run bash has foobar + + [[ "$status" -eq 1 ]] + [[ "printf '%b\n' ${lines[0]}" =~ '✗' ]] +} + +@test "fail count 3: testing output with and without unicode" { + run bash has git foobar barbaz barfoo + + [[ "$status" -eq 3 ]] + [[ "printf '%b\n' ${lines[0]}" =~ "${checkmark}" ]] + [[ "printf '%b\n' ${lines[2]}" =~ '✗' ]] +} + diff --git a/has b/has index 7bd4d85..328bba2 100755 --- a/has +++ b/has @@ -1,13 +1,31 @@ #!/usr/bin/env bash - ## source: https://github.com/kdabir/has ## Important so that version is not extracted for failed commands (not found) set -o pipefail +readonly BINARY_NAME="has" +readonly VERSION="v1.4.0" + ## constant - symbols for success failure -PASS="\e[1m\e[38;5;2m✔\e[m" -FAIL="\e[1m\e[38;5;1m✘\e[m" +readonly txtreset="$(tput sgr0)" +readonly txtbold="$(tput bold)" +readonly txtblack="$(tput setaf 0)" +readonly txtred="$(tput setaf 1)" +readonly txtgreen="$(tput setaf 2)" +readonly txtyellow="$(tput setaf 3)" +readonly txtblue="$(tput setaf 4)" +readonly txtpurple="$(tput setaf 5)" +readonly txtcyan="$(tput setaf 6)" +readonly txtwhite="$(tput setaf 7)" +# unicode "✗" +readonly fancyx='\342\234\227' +# unicode "✓" +readonly checkmark='\342\234\223' +# PASS="\e[1m\e[38;5;2m✔\e[m" +# FAIL="\e[1m\e[38;5;1m✘\e[m" +readonly PASS="${txtbold}${txtgreen}${checkmark}${txtreset}" +readonly FAIL="${txtbold}${txtred}${fancyx}${txtreset}" ## These variables are used to keep track of passed and failed commands OK=0 @@ -19,38 +37,38 @@ REGEX_SIMPLE_VERSION="([[:digit:]]+\.?){2,3}" ## RC file can contain commands to be tested RC_FILE=".hasrc" -# try to extract version by executing $1 with $2 arg +# try to extract version by executing "${1}" with "${2}" arg __dynamic_detect(){ - cmd=$1 - params=$2 - version=$(eval ${cmd} ${params} "2>&1" | egrep -o "$REGEX_SIMPLE_VERSION" | head -1) + cmd="${1}" + params="${2}" + version=$(eval "${cmd}" "${params}" "2>&1" | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1) status=$? } # commands that use `--version` flag __dynamic_detect--version(){ - __dynamic_detect $1 "--version" + __dynamic_detect "${1}" "--version" } # commands that use `-version` flag __dynamic_detect-version(){ - __dynamic_detect $1 "-version" + __dynamic_detect "${1}" "-version" } # commands that use `-v` flag __dynamic_detect-v(){ - __dynamic_detect $1 "-v" + __dynamic_detect "${1}" "-v" } # commands that use `version` argument __dynamic_detect-arg_version(){ - __dynamic_detect $1 "version" + __dynamic_detect "${1}" "version" } ## the main function __detect(){ - name=$1 + name="${1}" # setup aliases maps commonly used name to exact command name case ${name} in @@ -66,74 +84,73 @@ __detect(){ *) command=${name} ;; esac - case ${command} in + case "${command}" in # commands that need --version flag - bash|zsh) __dynamic_detect--version ${command} ;; - git|hg|svn|bzr) __dynamic_detect--version ${command} ;; - gcc|make) __dynamic_detect--version ${command} ;; - curl|wget|http) __dynamic_detect--version ${command} ;; - vim|emacs|nano|subl) __dynamic_detect--version ${command} ;; - bats|tree|ack|autojump) __dynamic_detect--version ${command} ;; - jq|ag|brew) __dynamic_detect--version ${command} ;; - - R) __dynamic_detect--version ${command} ;; - node|npm|yarn) __dynamic_detect--version ${command} ;; - grunt|brunch) __dynamic_detect--version ${command} ;; - ruby|gem|rake|bundle) __dynamic_detect--version ${command} ;; - python|python3) __dynamic_detect--version ${command} ;; - perl|perl6|php|php5) __dynamic_detect--version ${command} ;; - groovy|gradle|mvn) __dynamic_detect--version ${command} ;; - lein) __dynamic_detect--version ${command} ;; - aws|eb|sls|gcloud) __dynamic_detect--version ${command} ;; + bash|zsh) __dynamic_detect--version "${command}" ;; + git|hg|svn|bzr) __dynamic_detect--version "${command}" ;; + gcc|make) __dynamic_detect--version "${command}" ;; + curl|wget|http) __dynamic_detect--version "${command}" ;; + vim|emacs|nano|subl) __dynamic_detect--version "${command}" ;; + bats|tree|ack|autojump) __dynamic_detect--version "${command}" ;; + jq|ag|brew) __dynamic_detect--version "${command}" ;; + R) __dynamic_detect--version "${command}" ;; + node|npm|yarn) __dynamic_detect--version "${command}" ;; + grunt|brunch) __dynamic_detect--version "${command}" ;; + ruby|gem|rake|bundle) __dynamic_detect--version "${command}" ;; + python|python3) __dynamic_detect--version "${command}" ;; + perl|perl6|php|php5) __dynamic_detect--version "${command}" ;; + groovy|gradle|mvn) __dynamic_detect--version "${command}" ;; + lein) __dynamic_detect--version "${command}" ;; + aws|eb|sls|gcloud) __dynamic_detect--version "${command}" ;; # commands that need -version flag - ant|java|javac) __dynamic_detect-version ${command} ;; - scala|kotlin) __dynamic_detect-version ${command} ;; + ant|java|javac) __dynamic_detect-version "${command}" ;; + scala|kotlin) __dynamic_detect-version "${command}" ;; # commands that need version arg - hugo) __dynamic_detect-arg_version ${command} ;; + hugo) __dynamic_detect-arg_version "${command}" ;; ## Example of commands that need custom processing ## go needs version arg go) - version=$(go version 2>&1| egrep -o "$REGEX_SIMPLE_VERSION" | head -1) + version=$(go version 2>&1| grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1) status=$? ;; ## TODO cleanup, currently need to add extra space in regex, otherwise the time gets selected gulp) - version=$(gulp --version 2>&1| egrep -o " $REGEX_SIMPLE_VERSION" | head -1) + version=$(gulp --version 2>&1| grep -Eo " ${REGEX_SIMPLE_VERSION}" | head -1) status=$? ;; ## ab uses -V flag ab) - version=$(ab -V 2>&1 | egrep -o "$REGEX_SIMPLE_VERSION" | head -1) + version=$(ab -V 2>&1 | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1) status=$? ;; ## gor returns version but does not return normal status code, hence needs custom processing gor) - version=$(gor version 2>&1 | egrep -o "$REGEX_SIMPLE_VERSION" | head -1) + version=$(gor version 2>&1 | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1) if [ $? -eq 1 ]; then status=0; else status=1; fi ;; sbt) - version=$(sbt about 2>&1 | egrep -o "([[:digit:]]{1,4}\.){2}[[:digit:]]{1,4}" | head -1) - status=$? + version=$(sbt about 2>&1 | grep -Eo "([[:digit:]]{1,4}\.){2}[[:digit:]]{1,4}" | head -1) + status=$? ;; has) - version=$(has 2>&1 | egrep -o "$REGEX_SIMPLE_VERSION" | head -1) + version=$(has 2>&1 | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1) status=$? ;; *) ## Can allow dynamic checking here, i.e. checking commands that are not listed above if [[ "${HAS_ALLOW_UNSAFE}" == "y" ]]; then - __dynamic_detect--version ${command} + __dynamic_detect--version "${command}" ## fallback checking based on status!=127 (127 means command not found) ## TODO can check other type of supported version-checks if status was not 127 else @@ -143,27 +160,23 @@ __detect(){ ;; esac - if [ "$status" -eq "-1" ]; then ## When unsafe processing is not allowed, the -1 signifies - printf "${FAIL} ${command} not understood\n" + printf '%b %s not understood\n' "${FAIL}" "${command}" KO=$(($KO+1)) elif [ ${status} -eq 127 ]; then ## command not installed - printf "${FAIL} ${command}\n" + printf '%b %s\n' "${FAIL}" "${command}" KO=$(($KO+1)) elif [ ${status} -eq 0 ] || [ ${status} -eq 141 ]; then ## successfully executed - printf "${PASS} ${command} \e[1m\e[33;5;2m${version}\e[m\n" + printf "%b %s %b\n" "${PASS}" "${command}" "${txtbold}${txtyellow}${version}${txtreset}" OK=$(($OK+1)) else ## as long as its not 127, command is there, but we might not have been able to extract version - printf "${PASS} ${command}\n" + printf '%b %s\n' "${PASS}" "${command}" OK=$(($OK+1)) fi - -} - - +} #end __detect if [ -s "${RC_FILE}" ]; then HASRC="true" @@ -172,16 +185,14 @@ fi # if HASRC is not set AND no arguments passed to script if [[ -z "${HASRC}" ]] && [ "$#" -eq 0 ]; then # print help - BINARY_NAME="has" - VERSION="v1.4.0" - printf "${BINARY_NAME} ${VERSION}\n" - printf "USAGE: ${BINARY_NAME} ..\n" - printf "EXAMPLE: ${BINARY_NAME} git curl node\n\n" + printf '%s %s\n' "${BINARY_NAME}" "${VERSION}" + printf 'USAGE:\t %s ..\n' "${BINARY_NAME}" + printf 'EXAMPLE: %s git curl node\n' "${BINARY_NAME}" else # for each cli-arg for cmd in "$@"; do - __detect "$cmd" + __detect "${cmd}" done ## display found / total @@ -190,9 +201,9 @@ else ## if HASRC has been set if [[ -n "${HASRC}" ]]; then ## for all - for line in $(cat "${RC_FILE}" | egrep -v "^\s*(#|$)" ); do - __detect "$line" - done + while read -r line; do + __detect "${line}" + done <<<"$(grep -Ev "^\s*(#|$)" "${RC_FILE}" )" fi ## max status code that can be returned