Skip to content

Commit

Permalink
Merge pull request #19 from cytopia/release-0.12
Browse files Browse the repository at this point in the history
Release 0.12
  • Loading branch information
cytopia authored Nov 10, 2018
2 parents 0cf2bb1 + 402df29 commit 5e4672d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ help:
@printf "%s\n" "make help Show help"

test:
./linkcheck -r 10 -t 30 tests/urls_valid_4.txt
./linkcheck -r 10 -t 30 tests/urls_valid_4.txt | grep -c '[ok]'
./tests/test.sh

lint:
shellcheck --shell=bash linkcheck
19 changes: 13 additions & 6 deletions linkcheck
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ FOLLOW_REDIRECT=""
###
URL_REGEX="http(s)?:\\/\\/[-+%=?&():,._/#0-9a-zA-Z]+"

MY_VERSION="v0.11"
MY_VERSION="v0.12"


###
Expand Down Expand Up @@ -239,6 +239,13 @@ probe_urls() {
local status_codes="${4}"
local insecure_ssl="${5}"
local follow_redirect="${6}"

local clr_test="\033[0;33m" # Yellow
local clr_fail="\033[0;31m" # Red
local clr_err="\033[0;31m" # Red
local clr_ok="\033[0;32m" # Green
local clr_rst="\033[m" # Reset to normal

local host=
local ret_code=0

Expand All @@ -264,7 +271,7 @@ probe_urls() {
opts="${opts} ${follow_redirect}"
#echo "curl ${opts} ${url}"

printf "\\e[0;33m[TEST]\\e[0m %s ..." "${url}"
printf "${clr_test}[TEST]${clr_rst} %s ..." "${url}"

# Get header from URL
eval "$(eval "curl ${opts} \"${url}\"" 2> >(setval errval) > >(setval header); <<<$? setval retval)";
Expand All @@ -273,7 +280,7 @@ probe_urls() {
# shellcheck disable=SC2154
if [ "${retval}" != "0" ]; then
# shellcheck disable=SC2154
printf "\\r\\e[0;31m[FAIL]\\e[0m %s %s\\n" "${url}" "${errval}"
printf "\r${clr_fail}[FAIL]${clr_rst} %s %s\n" "${url}" "${errval}"
ret_code=1

# Curl request succeeded
Expand All @@ -284,13 +291,13 @@ probe_urls() {

if ! echo "${stat}" | tail -1 | grep -qE "${status_codes}"; then
# Fix error line for multiline (in case of redirects via -l option)
line="$( echo "${line}" | paste -sd "," | sed 's/,/ -> /g' | head -1 | xargs )"
printf "\\r\\e[0;31m[ERR]\\e[0m %s \\e[0;31m[%s]\\e[0m\\n" "${url}" "${line}"
line="$( echo "${line}" | paste -sd "," | sed 's/,/ -> /g' | head -1 | tr -d '\n' | tr -d '\r' | sed 's/\s*$//g' )"
printf "\r${clr_err}[ERR]${clr_rst} %s ${clr_err}%s${clr_rst}\n" "${url}" "(${line})"
ret_code=1
else
# Fix status code for multiline (in case of redirects via -l option)
stat="$( echo "${stat}" | paste -sd "," | sed 's/,/ -> /g' )"
printf "\\r\\e[0;32m[OK]\\e[0m %s \\e[0;32m[%s]\\e[0m\\n" "${url}" "${stat}"
printf "\r${clr_ok}[OK]${clr_rst} %s ${clr_ok}%s${clr_rst}\n" "${url}" "(${stat})"
fi
fi
done
Expand Down
68 changes: 68 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env bash

# Be strict
set -e
set -u
set -o pipefail

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"


###
### Find exactly 4 unique URLs
###
"${DIR}/../linkcheck" -r 10 -t 30 "${DIR}/urls_valid_4.txt"
echo

if [ "$( "${DIR}/../linkcheck" -r 10 -t 30 "${DIR}/urls_valid_4.txt" | grep -c "[OK]" )" -eq "4" ]; then
echo "[OK]"
echo
else
>&2 echo "FAILED: Not exactly 4 succeeded URLs found."
exit 1
fi


###
### Find exactly 4 redirecting URLs and fail
###
"${DIR}/../linkcheck" -r 10 -t 30 "${DIR}/urls_redirect_4.txt" || true
echo

if [ "$( "${DIR}/../linkcheck" -r 10 -t 30 "${DIR}/urls_redirect_4.txt" | grep -c "[ERR]" )" -eq "4" ]; then
echo "[OK]"
echo
else
>&2 echo "FAILED: Not exactly 4 redirecting URLs found."
exit 1
fi


###
### Find exactly 4 redirecting URLs and allow
###
"${DIR}/../linkcheck" -r 10 -t 30 -c '200,301,302' "${DIR}/urls_redirect_4.txt" || true
echo

if [ "$( "${DIR}/../linkcheck" -r 10 -t 30 -c '200,301,302' "${DIR}/urls_redirect_4.txt" | grep -c "[OK]" )" -eq "4" ]; then
echo "[OK]"
echo
else
>&2 echo "FAILED: Not exactly 4 redirecting URLs found."
exit 1
fi


###
### Find exactly 4 redirecting URLs and follow them
###
"${DIR}/../linkcheck" -l -r 10 -t 30 "${DIR}/urls_redirect_4.txt"
echo

if [ "$( "${DIR}/../linkcheck" -l -r 10 -t 30 "${DIR}/urls_redirect_4.txt" | grep -c "[OK]" )" -eq "4" ]; then
echo "[OK]"
echo
else
>&2 echo "FAILED: Not exactly 4 redirecting and successfully followed URLs found."
exit 1
fi
5 changes: 5 additions & 0 deletions tests/urls_redirect_4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Redirect URLS
https://google.com
https://yahoo.com
https://bing.com
https://www.duckduckgo.com

0 comments on commit 5e4672d

Please sign in to comment.