diff --git a/.gitignore b/.gitignore index 0da0427..e427588 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *~ .DS_Store .idea +tmp-for-test/ diff --git a/.hasrc b/.hasrc new file mode 100644 index 0000000..993eaa4 --- /dev/null +++ b/.hasrc @@ -0,0 +1,2 @@ +make +curl diff --git a/.hastest.bats b/.hastest.bats index bca11e6..d483261 100644 --- a/.hastest.bats +++ b/.hastest.bats @@ -1,5 +1,17 @@ #!/usr/bin/env bats +setup() { + mkdir -p ./tmp-for-test + cp -f has ./tmp-for-test/ + cd tmp-for-test +} + +teardown() { + cd .. + rm -rf ./tmp-for-test +} + + @test "works with single command check" { run bash has git @@ -34,3 +46,28 @@ [[ "$status" -eq 126 ]] } + + +@test "loads commands from .hasrc file" { + printf "bash\nmake\n" >> .hasrc + + run bash has + + [[ "$status" -eq 0 ]] + + [[ "$(echo "${output}" | grep "✔" | grep "bash")" ]] + [[ "$(echo "${output}" | grep "✔" | grep "make")" ]] +} + +@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")" ]] +} diff --git a/has b/has index 2fb28fc..ca2cbae 100755 --- a/has +++ b/has @@ -16,8 +16,11 @@ KO=0 ## Regex to extract simple version - extracts numeric sem-ver style versions 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 @@ -30,7 +33,7 @@ __dynamic_detect--version(){ __dynamic_detect $1 "--version" } -## commands that use `-version` flag +# commands that use `-version` flag __dynamic_detect-version(){ __dynamic_detect $1 "-version" } @@ -162,24 +165,36 @@ __detect(){ } + +if [ -s "${RC_FILE}" ]; then + HASRC="true" +fi + # if no arguments passed to script -if [ "$#" -eq 0 ]; then +if [[ "${HASRC}" -ne "true" ]] && [ "$#" -eq 0 ]; then # print help BINARY_NAME="has" VERSION="v1.3.0" printf "${BINARY_NAME} ${VERSION}\n" printf "USAGE: ${BINARY_NAME} ..\n" - printf "EXAMPLE: ${BINARY_NAME} git curl node\n" + printf "EXAMPLE: ${BINARY_NAME} git curl node\n\n" else - # for each arg + # for each cli-arg for cmd in "$@"; do - __detect $cmd + __detect "$cmd" done ## display found / total # echo ${OK} / $(($OK+$KO)) + if [[ "${HASRC}" == "true" ]]; then + ## for all + for line in $(cat ".hasrc" | egrep -v "^\s*(#|$)" ); do + __detect "$line" + done + fi + ## max status code that can be returned MAX_STATUS_CODE=126