diff options
-rw-r--r-- | setup-apkrepos.in | 17 | ||||
-rwxr-xr-x | tests/setup_apkrepos_test | 10 |
2 files changed, 18 insertions, 9 deletions
diff --git a/setup-apkrepos.in b/setup-apkrepos.in index bb60132..5c6d49b 100644 --- a/setup-apkrepos.in +++ b/setup-apkrepos.in @@ -16,6 +16,11 @@ get_hostname_from_url() { echo ${n%%/*} } +get_mirror_count() { + set -- $MIRRORS + echo $# +} + ask_setup_method() { cat <<-__EOF__ r) Add random from the above list @@ -23,12 +28,12 @@ ask_setup_method() { e) Edit ${ROOT}etc/apk/repositories with text editor __EOF__ - ask "Enter mirror number (1-$mirror_count) or URL to add (or r/f/e/done)" "$1" + ask "Enter mirror number (1-$(get_mirror_count)) or URL to add (or r/f/e/done)" "$1" } add_random_mirror() { local i=0 - local random_mirror_index="$(( $RANDOM % $mirror_count ))" + local random_mirror_index="$(( $RANDOM % $(get_mirror_count) ))" printf %s "Picking random mirror... " for mirror in $MIRRORS; do @@ -71,10 +76,9 @@ add_fastest_mirror() { add_mirror "$fastest" } -# show mirrors and store how many in global mirror_count +# show mirrors show_mirror_list() { local mirror i=0 - mirror_count=0 [ -z "$MIRRORS" ] && return echo "" echo "Available mirrors:" @@ -83,12 +87,11 @@ show_mirror_list() { echo "$i) $(get_hostname_from_url $mirror)" done echo "" - mirror_count=$i } add_from_list() { local mirror_index="$1" - if [ $mirror_index -lt 1 ] || [ $mirror_index -gt $mirror_count ]; then + if [ $mirror_index -lt 1 ] || [ $mirror_index -gt $(get_mirror_count) ]; then return 1 fi set $MIRRORS @@ -196,12 +199,10 @@ if $add_first; then fi if $add_random; then - show_mirror_list > /dev/null add_random_mirror && changed=true fi if $add_fastest; then - show_mirror_list > /dev/null add_fastest_mirror && changed=true fi diff --git a/tests/setup_apkrepos_test b/tests/setup_apkrepos_test index bf57b4f..bb152c0 100755 --- a/tests/setup_apkrepos_test +++ b/tests/setup_apkrepos_test @@ -3,7 +3,8 @@ . $(atf_get_srcdir)/test_env.sh init_tests \ setup_apkrepos_usage \ - setup_apkrepos_https + setup_apkrepos_https \ + setup_apkrepos_random setup_apkrepos_usage_body() { test_usage setup-apkrepos @@ -20,3 +21,10 @@ setup_apkrepos_https_body() { grep -q 'https://example.com' etc/apk/repositories || atf_fail "example.com was not added to /etc/apk/repositories" } +setup_apkrepos_random_body() { + init_env + export WGETCONTENT="$(seq 0 9 | awk '{print "https://a" $0 ".example.com"}')" + atf_check -s exit:0 \ + -o match:"Added mirror a[0-9].example.com" \ + setup-apkrepos -r +} |