From f8460dacbf0c169af9d8cccaeba1b7d455370dd3 Mon Sep 17 00:00:00 2001 From: Adam Farley Date: Tue, 17 Dec 2024 15:06:04 +0000 Subject: [PATCH] Switching -x checks to -r Signed-off-by: Adam Farley --- lib/functionLibrary.sh | 10 +++++----- lib/tests/functionLibraryTests.sh | 22 +++++++++++----------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/functionLibrary.sh b/lib/functionLibrary.sh index f18d64abf..4b8ee5829 100644 --- a/lib/functionLibrary.sh +++ b/lib/functionLibrary.sh @@ -61,7 +61,7 @@ function checkFileSha() { return 1 fi - if [[ ! `ls "${2}" &> /dev/null` ]]; then + if [[ ! -r "${2}" ]]; then info "The file we're trying to check does not exist: ${2}" return 1 fi @@ -187,10 +187,10 @@ function downloadFile() { info "Source exists." info "Checking if destination folder exists." - [ ! -x ${destination} ] && echo "Error: Destination folder could not be found." && return 1 + [ ! -r "${destination}" ] && echo "Error: Destination folder could not be found." && return 1 info "Destination folder exists. Checking if file is already present." - if [ -x "${destination}/${filename}" ]; then + if [ -r "${destination}/${filename}" ]; then info "Warning: File already exists." checkFileSha "${sha}" "${destination}/${filename}" if [[ $? != 0 ]]; then @@ -205,7 +205,7 @@ function downloadFile() { return 0 fi fi - if [ -x "${destination}/${source##*/}" ]; then + if [ -r "${destination}/${source##*/}" ]; then info "Warning: File already exists with the default file name: ${source##*/}" checkFileSha "${sha}" "${destination}/${source##*/}" if [[ $? != 0 ]]; then @@ -230,7 +230,7 @@ function downloadFile() { info "Found curl. Using curl to download the file." curl -s "${source}" -o "${destination}" fi - if [ ! -x "${destination}/${filename}" ]; then + if [ ! -r "${destination}/${filename}" ]; then mv "${destination}/${source##*/}" "${destination}/${filename}" fi diff --git a/lib/tests/functionLibraryTests.sh b/lib/tests/functionLibraryTests.sh index 5193eb105..7c13e44b9 100644 --- a/lib/tests/functionLibraryTests.sh +++ b/lib/tests/functionLibraryTests.sh @@ -16,8 +16,8 @@ scriptLocation=$0 scriptDir="${scriptLocation%/*}" -[[ ! -x "${scriptDir}" || ! "${scriptDir}" =~ .*tests$ ]] && scriptDir="." -[[ ! `ls "${scriptDir}/.." | grep functionLibrary.sh` ]] && echo "Error: Please launch this script with a full path, or from within the test directory." && exit 1 +[[ ! -r "${scriptDir}" || ! "${scriptDir}" =~ .*tests$ ]] && scriptDir="." +[[ ! -r "${scriptDir}/../functionLibrary.sh" ]] && echo "Error: Please launch this script with a full path, or from within the test directory." && exit 1 source "${scriptDir}/../functionLibrary.sh" @@ -87,46 +87,46 @@ function doesThisURLExistTests(){ function downloadFileTests() { workdir="${scriptDir}/tmp_test_work_dir" # Setup - [[ -x "${workdir}" ]] && echo "Error: Temporary test work directory exists and shouldn't: ${workdir}" && exit 1 + [[ -r "${workdir}" ]] && echo "Error: Temporary test work directory exists and shouldn't: ${workdir}" && exit 1 mkdir "${workdir}" - [[ ! -x "${workdir}" ]] && echo "Error: Temporary test work directory could not be created: ${workdir}" && exit 1 + [[ ! -r "${workdir}" ]] && echo "Error: Temporary test work directory could not be created: ${workdir}" && exit 1 # Does it pass when it should (no sha)? downloadFile -s "${sampleFileURL}/${sampleFileName}" -d "${workdir}" - [[ $? == 0 && -x "${workdir}/${sampleFileName}" ]] + [[ $? == 0 && -r "${workdir}/${sampleFileName}" ]] testResults "downloadFileTest 1" "$?" rm -rf "${workdir}/*" # Does it pass when it should (sha)? downloadFile -s "${sampleFileURL}/${sampleFileName}" -d "${workdir}" -sha "${sampleFileSha}" - [[ $? == 0 && -x "${workdir}/${sampleFileName}" ]] + [[ $? == 0 && -r "${workdir}/${sampleFileName}" ]] testResults "downloadFileTest 2" "$?" rm -rf "${workdir}/*" # Does it correctly rename the downloaded file? downloadFile -s "${sampleFileURL}/${sampleFileName}" -d "${workdir}" -sha "${sampleFileSha}" -f "newfilename" - [[ $? == 0 && -x "${workdir}/newfilename" ]] + [[ $? == 0 && -r "${workdir}/newfilename" ]] testResults "downloadFileTest 3" "$?" rm -rf "${workdir}/*" # Does it fail when it should (no sha, source does not exist)? downloadFile -s "${sampleFileURL}/thisFileDoesNotExist" -d "${workdir}" &> /dev/null - [[ $? != 0 && ! -x "${workdir}/${sampleFileName}" ]] + [[ $? != 0 && ! -r "${workdir}/${sampleFileName}" ]] testResults "downloadFileTest 4" "$?" # Does it fail when it should (with sha, source does not exist)? downloadFile -s "${sampleFileURL}/thisFileDoesNotExist" -d "${workdir}" -sha "${sampleFileSha}" &> /dev/null - [[ $? != 0 && ! -x "${workdir}/${sampleFileName}" ]] + [[ $? != 0 && ! -r "${workdir}/${sampleFileName}" ]] testResults "downloadFileTest 5" "$?" # Does it fail when it should (with invalid sha, source exists)? downloadFile -s "${sampleFileURL}/${sampleFileName}" -d "${workdir}" -sha "thisisaninvalidsha12345" -f "newfilename" &> /dev/null - [[ $? != 0 && ! -x "${workdir}/newfilename" ]] + [[ $? != 0 && ! -r "${workdir}/newfilename" ]] testResults "downloadFileTest 6" "$?" # Does it fail when it should (secure mode)? downloadFile -s "${sampleFileURL}/${sampleFileName}" -d "${workdir}" -secure "true" &> /dev/null - [[ $? != 0 && ! -x "${workdir}/newfilename" ]] + [[ $? != 0 && ! -r "${workdir}/newfilename" ]] testResults "downloadFileTest 7" "$?" # Clean up