Skip to content

Commit

Permalink
Add generic find_utility function for find_doxygen
Browse files Browse the repository at this point in the history
  • Loading branch information
JonatanAntoni authored Sep 1, 2022
1 parent 478ac85 commit dc9608d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
34 changes: 33 additions & 1 deletion lib/utilities
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@

function report_utility {
echo "$1 is $2"
if [[ -n "$3" && $3 != 0 ]]; then
echo "Error: No $1 utility found" >&2
echo "Action: Add $1 to your path" >&2
echo " "
exit $3
else
echo "$1 is $2"
fi
}

function find_utility {
local UTILS=($(type -a "$1" 2>/dev/null | cut -d" " -f3))
local retval=$?
if [[ $retval != 0 ]]; then
return $retval
fi
if [[ -n "$2" && -n "$3" ]]; then
for executable in ${UTILS[@]}; do
local VERSION=$(${executable} $2)
if [[ "$VERSION" == "$3" ]]; then
realpath -q "$executable"
return $?
fi
done
return 1
else
realpath -q "$UTILS"
return $?
fi
}

function get_os_type {
Expand Down Expand Up @@ -172,6 +200,10 @@ function find_linkchecker {
exit 1
}

function find_doxygen {
UTILITY_DOXYGEN=$(find_utility doxygen "-v | cut -d' ' -f1" "$1")
report_utility "doxygen" "$UTILITY_DOXYGEN" $?
}

function archive_7zip {
rm -f "$2"
Expand Down
61 changes: 61 additions & 0 deletions test/tests_utilities.sh
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,65 @@ EOF
"$(cat results.csv)"
}

test_find_utility() {
mkdir util-1.0
mkdir util-2.0

cat > util-1.0/util <<EOF
#!/bin/sh
echo "1.0.0"
EOF

cat > util-2.0/util <<EOF
#!/bin/sh
echo "2.0.3"
EOF

chmod +x util-1.0/util
chmod +x util-2.0/util

PATH="$(pwd)/util-1.0:$(pwd)/util-2.0:$PATH"

UTIL=$(find_utility "util")
assertEquals 0 $?
assertEquals "$(realpath $(pwd))/util-1.0/util" "$UTIL"

UTIL=$(find_utility "util" "-v" "1.0.0")
assertEquals 0 $?
assertEquals "$(realpath $(pwd))/util-1.0/util" "$UTIL"

UTIL=$(find_utility "util" "-v" "2.0.3")
assertEquals 0 $?
assertEquals "$(realpath $(pwd))/util-2.0/util" "$UTIL"
}

test_find_utility_version_na() {
mkdir util-1.0
mkdir util-2.0

cat > util-1.0/util <<EOF
#!/bin/sh
echo "1.0.0"
EOF

cat > util-2.0/util <<EOF
#!/bin/sh
echo "2.0.3"
EOF

chmod +x util-1.0/util
chmod +x util-2.0/util

PATH="$(pwd)/util-1.0:$(pwd)/util-2.0:$PATH"

UTIL=$(find_utility "util" "-v" "3.0.0")
assertNotEquals "find_utility did not fail" 0 $?
}

test_find_utility_na() {
UTIL=$(find_utility "utilX")
assertNotEquals "find_utility did not fail" 0 $?
}


. "$(dirname "$0")/shunit2/shunit2"

0 comments on commit dc9608d

Please sign in to comment.