Skip to content

Commit

Permalink
Switching -x checks to -r
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Farley <[email protected]>
  • Loading branch information
adamfarley committed Dec 17, 2024
1 parent 3a742dc commit f8460da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
10 changes: 5 additions & 5 deletions lib/functionLibrary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
22 changes: 11 additions & 11 deletions lib/tests/functionLibraryTests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f8460da

Please sign in to comment.