implements #15

support for .hasrc file in current working directory, from where
has command is fired. these commands are merged with cli args.
This commit is contained in:
Kunal Dabir 2018-11-16 16:01:07 +05:30
parent b60c239bc4
commit 1702c991ce
4 changed files with 61 additions and 6 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*~
.DS_Store
.idea
tmp-for-test/

2
.hasrc Normal file
View File

@ -0,0 +1,2 @@
make
curl

View File

@ -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")" ]]
}

27
has
View File

@ -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} <command-names>..\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