Skip to content

Commit

Permalink
Silence wget/curl output if stdout is not a TTY (closes #397).
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Feb 3, 2025
1 parent 024a060 commit abca35d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
21 changes: 16 additions & 5 deletions share/ruby-install/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,28 @@ function download()
local url="$1"
local dest="$2"

if [[ -z "$downloader" ]]; then
error "Could not find wget or curl"
return 1
fi

local quiet

if [[ ! -t 1 ]]; then
quiet=1
fi

[[ -d "$dest" ]] && dest="$dest/${url##*/}"
[[ -f "$dest" ]] && return

mkdir -p "${dest%/*}" || return $?

case "$downloader" in
wget) run wget -c -O "$dest.part" "$url" || return $? ;;
curl) run curl -f -L -C - -o "$dest.part" "$url" || return $? ;;
"")
error "Could not find wget or curl"
return 1
wget)
run wget ${quiet:+-q} -c -O "$dest.part" "$url" || return $?
;;
curl)
run curl ${quiet:+-s -S} -f -L -C - -o "$dest.part" "$url" || return $?
;;
esac

Expand Down
18 changes: 18 additions & 0 deletions test/util-tests/download_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ function test_download_using_wget()
assertTrue "did not download the file" '[[ -f "$test_dest" ]]'
}

function test_download_using_wget_when_stdout_is_not_a_tty()
{
command -v wget >/dev/null || return 0

local output="$(downloader="wget" download "$test_url" "$test_dest")"

assertEquals "did not silence wget output" "" "$output"
}

function test_download_using_curl()
{
command -v curl >/dev/null || return 0
Expand All @@ -46,6 +55,15 @@ function test_download_using_curl()
assertTrue "did not download the file" '[[ -f "$test_dest" ]]'
}

function test_download_using_curl_when_stdout_is_not_a_tty()
{
command -v curl >/dev/null || return 0

local output="$(downloader="curl" download "$test_url" "$test_dest")"

assertEquals "did not silence curl output" "" "$output"
}

function tearDown()
{
rm -rf "$test_dir"
Expand Down

0 comments on commit abca35d

Please sign in to comment.