Improve `add` formatting.

This commit is contained in:
William Melody 2020-04-08 17:27:11 -07:00
parent 902c996b3b
commit 5d9f876b13
8 changed files with 77 additions and 67 deletions

22
hosts
View File

@ -775,6 +775,7 @@ add() {
local _ip="${1:-}" local _ip="${1:-}"
local _hostname="${2:-}" local _hostname="${2:-}"
local _comment="${*:3}" local _comment="${*:3}"
local _tabs=$'\t'
if [[ -z "${_ip:-}" ]] if [[ -z "${_ip:-}" ]]
then then
@ -786,32 +787,41 @@ add() {
help add help add
exit 1 exit 1
elif grep \ elif grep \
-e "^${_ip}\\t${_hostname}$" \ -e "^${_ip}[[:space:]]\+${_hostname}$" \
-e "^${_ip}\\t${_hostname}\\t.*$" "${HOSTS_PATH}" -e "^${_ip}[[:space:]]\+${_hostname}\\t.*$" "${HOSTS_PATH}"
then then
_die printf \ _die printf \
"Duplicate address/host combination, %s unchanged.\\n" \ "Duplicate address/host combination, %s unchanged.\\n" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
else else
if [[ "${#_ip}" -lt 8 ]]
then
_tabs=$'\t\t'
fi
if [[ -n "${_comment:-}" ]] if [[ -n "${_comment:-}" ]]
then then
local _formatted_comment local _formatted_comment
_formatted_comment=$(_join " " "${_comment[@]}") _formatted_comment=$(_join " " "${_comment[@]}")
printf "%s\\t%s\\t# %s\\n" \ printf "%s%s%s\\t# %s\\n" \
"${_ip}" \ "${_ip}" \
"${_tabs}" \
"${_hostname}" \ "${_hostname}" \
"${_formatted_comment}" >> "${HOSTS_PATH}" "${_formatted_comment}" >> "${HOSTS_PATH}"
printf "Added:\\n%s\\t%s\\t# %s\\n" \ printf "Added:\\n%s%s%s\\t# %s\\n" \
"${_ip}" \ "${_ip}" \
"${_tabs}" \
"${_hostname}" \ "${_hostname}" \
"${_formatted_comment}" "${_formatted_comment}"
else else
printf "%s\\t%s\\n" \ printf "%s%s%s\\n" \
"${_ip}" \ "${_ip}" \
"${_tabs}" \
"${_hostname}" >> "${HOSTS_PATH}" "${_hostname}" >> "${HOSTS_PATH}"
printf "Added:\\n%s\\t%s\\n" \ printf "Added:\\n%s%s%s\\n" \
"${_ip}" \ "${_ip}" \
"${_tabs}" \
"${_hostname}" "${_hostname}"
fi fi
fi fi

View File

@ -73,7 +73,7 @@ load test_helper
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
_compare '0.0.0.0 example.com' "$(sed -n '11p' "${HOSTS_PATH}")" _compare '0.0.0.0 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
} }
@test "\`add <ip> <hostname>\` prints feedback." { @test "\`add <ip> <hostname>\` prints feedback." {
@ -81,7 +81,7 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Added:" ]] [[ "${lines[0]}" == "Added:" ]]
[[ "${lines[1]}" == "0.0.0.0 example.com" ]] [[ "${lines[1]}" == "0.0.0.0 example.com" ]]
} }
@test "\`add <ip> <hostname>\` doesn't add duplicate entry." { @test "\`add <ip> <hostname>\` doesn't add duplicate entry." {
@ -95,10 +95,10 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
_compare '0.0.0.0 example.com' "$(sed -n '11p' "${HOSTS_PATH}")" _compare '0.0.0.0 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
[[ "$(cat "${HOSTS_PATH}")" == "${_modified}" ]] [[ "$(cat "${HOSTS_PATH}")" == "${_modified}" ]]
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
} }
# `hosts add <ip> <hostname> [comment]` ####################################### # `hosts add <ip> <hostname> [comment]` #######################################
@ -118,7 +118,7 @@ load test_helper
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == \ [[ "$(sed -n '11p' "${HOSTS_PATH}")" == \
"0.0.0.0 example.com # Example multi-word comment." ]] "0.0.0.0 example.com # Example multi-word comment." ]]
} }
@test "\`add <ip> <hostname> [comment]\` doesn't add duplicate entry." { @test "\`add <ip> <hostname> [comment]\` doesn't add duplicate entry." {
@ -135,9 +135,9 @@ load test_helper
_compare '0.0.0.0 example.com' "$(sed -n '11p' "${HOSTS_PATH}")" _compare '0.0.0.0 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
[[ "$(cat "${HOSTS_PATH}")" == "${_modified}" ]] [[ "$(cat "${HOSTS_PATH}")" == "${_modified}" ]]
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
[[ "$(sed -n '11p' "${HOSTS_PATH}")" != \ [[ "$(sed -n '11p' "${HOSTS_PATH}")" != \
"0.0.0.0 example.com # Example multi-word comment." ]] "0.0.0.0 example.com # Example multi-word comment." ]]
} }
@test "\`add <ip> <hostname> [comment]\` doesn't add duplicate commented entry." { @test "\`add <ip> <hostname> [comment]\` doesn't add duplicate commented entry." {
@ -151,11 +151,11 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
_compare '0.0.0.0 example.com' "$(sed -n '11p' "${HOSTS_PATH}")" _compare '0.0.0.0 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
[[ "$(cat "${HOSTS_PATH}")" == "${_modified}" ]] [[ "$(cat "${HOSTS_PATH}")" == "${_modified}" ]]
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == \ [[ "$(sed -n '11p' "${HOSTS_PATH}")" == \
"0.0.0.0 example.com # Example multi-word comment." ]] "0.0.0.0 example.com # Example multi-word comment." ]]
} }
@test "\`add <ip> <hostname> [comment]\` prints feedback." { @test "\`add <ip> <hostname> [comment]\` prints feedback." {
@ -163,7 +163,7 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Added:" ]] [[ "${lines[0]}" == "Added:" ]]
[[ "${lines[1]}" == "0.0.0.0 example.com # Example multi-word comment." ]] [[ "${lines[1]}" == "0.0.0.0 example.com # Example multi-word comment." ]]
} }
# help ######################################################################## # help ########################################################################

View File

@ -87,7 +87,7 @@ No backups found. Create a new backup:
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
printf "\${lines[1]}: '%s'\\n" "${lines[1]}" printf "\${lines[1]}: '%s'\\n" "${lines[1]}"
[[ ${status} -eq 1 ]] [[ ${status} -eq 1 ]]
[[ "${lines[1]}" == '< 0.0.0.0 example.com' ]] [[ "${lines[1]}" =~ \<\ 0.0.0.0[[:space:]]+example.com ]]
} }
@test "\`backups compare\` with missing backup exits with status 1" { @test "\`backups compare\` with missing backup exits with status 1" {

View File

@ -46,7 +46,7 @@ load test_helper
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
_compare '127.0.0.1 example.com' "$(sed -n '11p' "${HOSTS_PATH}")" _compare '127.0.0.1 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "127.0.0.1 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example.com ]]
} }
@test "\`block <hostname>\` prints feedback." { @test "\`block <hostname>\` prints feedback." {
@ -54,11 +54,11 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Added:" ]] [[ "${lines[0]}" == "Added:" ]]
[[ "${lines[1]}" == "127.0.0.1 example.com" ]] [[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]]
[[ "${lines[2]}" == "Added:" ]] [[ "${lines[2]}" == "Added:" ]]
[[ "${lines[3]}" == "fe80::1%lo0 example.com" ]] [[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]example.com ]]
[[ "${lines[4]}" == "Added:" ]] [[ "${lines[4]}" == "Added:" ]]
[[ "${lines[5]}" == "::1 example.com" ]] [[ "${lines[5]}" =~ \:\:1[[:space:]]+example.com ]]
} }
# `hosts block <hostname> <hostname2>` ######################################## # `hosts block <hostname> <hostname2>` ########################################
@ -80,8 +80,8 @@ load test_helper
_compare '127.0.0.1 example.com' "$(sed -n '11p' "${HOSTS_PATH}")" _compare '127.0.0.1 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
_compare '127.0.0.1 example2.com' "$(sed -n '11p' "${HOSTS_PATH}")" _compare '127.0.0.1 example2.com' "$(sed -n '11p' "${HOSTS_PATH}")"
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "127.0.0.1 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example.com ]]
[[ "$(sed -n '14p' "${HOSTS_PATH}")" == "127.0.0.1 example2.com" ]] [[ "$(sed -n '14p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example2.com ]]
} }
@test "\`block <hostname> <hostname2>\` prints feedback." { @test "\`block <hostname> <hostname2>\` prints feedback." {
@ -89,17 +89,17 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Added:" ]] [[ "${lines[0]}" == "Added:" ]]
[[ "${lines[1]}" == "127.0.0.1 example.com" ]] [[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]]
[[ "${lines[2]}" == "Added:" ]] [[ "${lines[2]}" == "Added:" ]]
[[ "${lines[3]}" == "fe80::1%lo0 example.com" ]] [[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+example.com ]]
[[ "${lines[4]}" == "Added:" ]] [[ "${lines[4]}" == "Added:" ]]
[[ "${lines[5]}" == "::1 example.com" ]] [[ "${lines[5]}" =~ \:\:1[[:space:]]+example.com ]]
[[ "${lines[6]}" == "Added:" ]] [[ "${lines[6]}" == "Added:" ]]
[[ "${lines[7]}" == "127.0.0.1 example2.com" ]] [[ "${lines[7]}" =~ 127.0.0.1[[:space:]]+example2.com ]]
[[ "${lines[8]}" == "Added:" ]] [[ "${lines[8]}" == "Added:" ]]
[[ "${lines[9]}" == "fe80::1%lo0 example2.com" ]] [[ "${lines[9]}" =~ fe80\:\:1\%lo0[[:space:]]+example2.com ]]
[[ "${lines[10]}" == "Added:" ]] [[ "${lines[10]}" == "Added:" ]]
[[ "${lines[11]}" == "::1 example2.com" ]] [[ "${lines[11]}" =~ \:\:1[[:space:]]+example2.com ]]
} }
# help ######################################################################## # help ########################################################################

View File

@ -53,8 +53,8 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled\:\ 0.0.0.0[[:space:]]+example.com ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example.net ]]
} }
@test "\`disable <ip>\` disables all matches." { @test "\`disable <ip>\` disables all matches." {
@ -68,8 +68,8 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled\:\ 0.0.0.0[[:space:]]+example.com ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ \#disabled\:\ 0.0.0.0[[:space:]]+example.net ]]
} }
@test "\`disable <ip>\` prints feedback." { @test "\`disable <ip>\` prints feedback." {
@ -82,7 +82,7 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Disabling:" ]] [[ "${lines[0]}" == "Disabling:" ]]
[[ "${lines[1]}" == "0.0.0.0 example.com" ]] [[ "${lines[1]}" =~ 0.0.0.0[[:space:]]+example.com ]]
} }
# `hosts disable <hostname>` ################################################## # `hosts disable <hostname>` ##################################################
@ -110,8 +110,8 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled\:\ 0.0.0.0[[:space:]]+example.com ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example.net ]]
} }
@test "\`disable <hostname>\` disables all matches." { @test "\`disable <hostname>\` disables all matches." {
@ -125,8 +125,8 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled\:\ 0.0.0.0[[:space:]]+example.com ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 127.0.0.1 example.com" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example.com ]]
} }
@test "\`disable <hostname>\` prints feedback." { @test "\`disable <hostname>\` prints feedback." {
@ -139,7 +139,7 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Disabling:" ]] [[ "${lines[0]}" == "Disabling:" ]]
[[ "${lines[1]}" == "0.0.0.0 example.com" ]] [[ "${lines[1]}" =~ 0.0.0.0[[:space:]]+example.com ]]
} }
# help ######################################################################## # help ########################################################################

View File

@ -58,9 +58,9 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled:\ 0.0.0.0[[:space:]]+example.com ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ \#disabled:\ 0.0.0.0[[:space:]]+example.net ]]
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "127.0.0.2 example.com" ]] [[ "$(sed -n '13p' "${HOSTS_PATH}")" =~ 127.0.0.2[[:space:]]+example.com ]]
} }
@test "\`enable <ip>\` enables all matches." { @test "\`enable <ip>\` enables all matches." {
@ -89,9 +89,9 @@ load test_helper
"'#disabled: 127.0.0.2 example.com'" \ "'#disabled: 127.0.0.2 example.com'" \
"'$(sed -n '13p' "${HOSTS_PATH}")'" "'$(sed -n '13p' "${HOSTS_PATH}")'"
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.com ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "0.0.0.0 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.net ]]
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "#disabled: 127.0.0.2 example.com" ]] [[ "$(sed -n '13p' "${HOSTS_PATH}")" =~ \#disabled:\ 127.0.0.2[[:space:]]+example.com ]]
} }
@test "\`enable <ip>\` prints feedback." { @test "\`enable <ip>\` prints feedback." {
@ -106,7 +106,7 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Enabling:" ]] [[ "${lines[0]}" == "Enabling:" ]]
[[ "${lines[1]}" == "127.0.0.2 example.com" ]] [[ "${lines[1]}" =~ 127.0.0.2[[:space:]]+example.com ]]
} }
# `hosts enable <hostname>` ################################################### # `hosts enable <hostname>` ###################################################
@ -138,9 +138,9 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled:\ 0.0.0.0[[:space:]]+example.com ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "0.0.0.0 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.net ]]
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "127.0.0.2 example.com" ]] [[ "$(sed -n '13p' "${HOSTS_PATH}")" =~ 127.0.0.2[[:space:]]+example.com ]]
} }
@test "\`enable <hostname>\` enables all matches." { @test "\`enable <hostname>\` enables all matches." {
@ -169,9 +169,9 @@ load test_helper
"'127.0.0.2 example.com'" \ "'127.0.0.2 example.com'" \
"'$(sed -n '13p' "${HOSTS_PATH}")'" "'$(sed -n '13p' "${HOSTS_PATH}")'"
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.com ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ \#disabled:\ 0.0.0.0[[:space:]]+example.net ]]
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "127.0.0.2 example.com" ]] [[ "$(sed -n '13p' "${HOSTS_PATH}")" =~ 127.0.0.2[[:space:]]+example.com ]]
} }
@test "\`enable <hostname>\` prints feedback." { @test "\`enable <hostname>\` prints feedback." {
@ -186,7 +186,7 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Enabling:" ]] [[ "${lines[0]}" == "Enabling:" ]]
[[ "${lines[1]}" == "0.0.0.0 example.net" ]] [[ "${lines[1]}" =~ 0.0.0.0[[:space:]]+example.net ]]
} }
# help ######################################################################## # help ########################################################################

View File

@ -83,8 +83,8 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.com ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "0.0.0.0 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.net ]]
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]] [[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]]
} }
@ -129,7 +129,7 @@ fe80::1%lo0 localhost
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Removed:" ]] [[ "${lines[0]}" == "Removed:" ]]
[[ "${lines[1]}" == "127.0.0.2 example.com" ]] [[ "${lines[1]}" =~ 127.0.0.2[[:space:]]+example.com ]]
} }
# `hosts remove <hostname> --force` ########################################### # `hosts remove <hostname> --force` ###########################################
@ -159,7 +159,7 @@ fe80::1%lo0 localhost
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.net" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.net ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "" ]]
} }
@ -187,8 +187,8 @@ fe80::1%lo0 localhost
255.255.255.255 broadcasthost 255.255.255.255 broadcasthost
::1 localhost ::1 localhost
fe80::1%lo0 localhost fe80::1%lo0 localhost
0.0.0.0 example.net 0.0.0.0 example.net
#disabled: 0.0.0.0 example.dev" #disabled: 0.0.0.0 example.dev"
_compare "'${_expected}'" "'$(cat "${HOSTS_PATH}")'" _compare "'${_expected}'" "'$(cat "${HOSTS_PATH}")'"
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
[[ "$(cat "${HOSTS_PATH}")" == "${_expected}" ]] [[ "$(cat "${HOSTS_PATH}")" == "${_expected}" ]]
@ -206,7 +206,7 @@ fe80::1%lo0 localhost
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_expected="\ _expected="\
Removed: Removed:
0.0.0.0 example.com 0.0.0.0 example.com
127.0.0.2 example.com" 127.0.0.2 example.com"
[[ "${output}" == "${_expected}" ]] [[ "${output}" == "${_expected}" ]]
} }

View File

@ -79,7 +79,7 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]]
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]] [[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]]
} }
@ -106,7 +106,7 @@ load test_helper
255.255.255.255 broadcasthost 255.255.255.255 broadcasthost
::1 localhost ::1 localhost
fe80::1%lo0 localhost fe80::1%lo0 localhost
0.0.0.0 example.com 0.0.0.0 example.com
127.0.0.1 example.net" 127.0.0.1 example.net"
_compare "'${_expected}'" "'$(cat "${HOSTS_PATH}")'" _compare "'${_expected}'" "'$(cat "${HOSTS_PATH}")'"
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
@ -129,7 +129,7 @@ Removed:
Removed: Removed:
fe80::1%lo0 example.com fe80::1%lo0 example.com
Removed: Removed:
::1 example.com" ::1 example.com"
[[ "${output}" == "${_expected}" ]] [[ "${output}" == "${_expected}" ]]
} }
@ -162,7 +162,7 @@ Removed:
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]]
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]] [[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]]
} }
@ -190,7 +190,7 @@ Removed:
255.255.255.255 broadcasthost 255.255.255.255 broadcasthost
::1 localhost ::1 localhost
fe80::1%lo0 localhost fe80::1%lo0 localhost
0.0.0.0 example.com 0.0.0.0 example.com
127.0.0.1 example.net" 127.0.0.1 example.net"
_compare "'${_expected}'" "'$(cat "${HOSTS_PATH}")'" _compare "'${_expected}'" "'$(cat "${HOSTS_PATH}")'"
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
@ -214,13 +214,13 @@ Removed:
Removed: Removed:
fe80::1%lo0 example.com fe80::1%lo0 example.com
Removed: Removed:
::1 example.com ::1 example.com
Removed: Removed:
127.0.0.1 example2.com 127.0.0.1 example2.com
Removed: Removed:
fe80::1%lo0 example2.com fe80::1%lo0 example2.com
Removed: Removed:
::1 example2.com" ::1 example2.com"
_compare "'${output}'" "'${_expected}'" _compare "'${output}'" "'${_expected}'"
diff <(echo "${output}" ) <(echo "${_expected}") diff <(echo "${output}" ) <(echo "${_expected}")
[[ "${output}" == "${_expected}" ]] [[ "${output}" == "${_expected}" ]]