Use braces in all variable references.

Braces are only required in certain cases, but the cognitive overhead in
keeping track of which cases require braces can be reduced by simply
always using them.

Example: `${NAME}`

Retain more widely-used braces `$NAME` convention in documentation.
This commit is contained in:
William Melody 2016-02-23 18:14:21 -08:00
parent 9782f78f22
commit 187222614a
13 changed files with 584 additions and 576 deletions

256
hosts
View File

@ -77,7 +77,7 @@ _debug() {
then then
# Prefix debug message with "bug (U+1F41B)" # Prefix debug message with "bug (U+1F41B)"
printf "🐛 " printf "🐛 "
"$@" "${@}"
printf "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n" printf "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n"
fi fi
} }
@ -90,7 +90,7 @@ _debug() {
# #
# This is a shortcut for the _debug() function that simply echos the message. # This is a shortcut for the _debug() function that simply echos the message.
debug() { debug() {
_debug echo "$@" _debug echo "${@}"
} }
############################################################################### ###############################################################################
@ -108,7 +108,7 @@ debug() {
_die() { _die() {
# Prefix die message with "cross mark (U+274C)", often displayed as a red x. # Prefix die message with "cross mark (U+274C)", often displayed as a red x.
printf "❌ " printf "❌ "
"$@" 1>&2 "${@}" 1>&2
exit 1 exit 1
} }
# die() # die()
@ -120,7 +120,7 @@ _die() {
# #
# This is a shortcut for the _die() function that simply echos the message. # This is a shortcut for the _die() function that simply echos the message.
die() { die() {
_die echo "$@" _die echo "${@}"
} }
############################################################################### ###############################################################################
@ -128,7 +128,7 @@ die() {
############################################################################### ###############################################################################
# Get raw options for any commands that expect them. # Get raw options for any commands that expect them.
_RAW_OPTIONS="$*" _RAW_OPTIONS="${*}"
# Steps: # Steps:
# #
@ -166,9 +166,9 @@ optstring=h
# argument to the option, such as wget -qO-) # argument to the option, such as wget -qO-)
unset options unset options
# while the number of arguments is greater than 0 # while the number of arguments is greater than 0
while (($#)) while ((${#}))
do do
case $1 in case ${1} in
# if option is of type -ab # if option is of type -ab
-[!-]?*) -[!-]?*)
# loop over each character starting with the second # loop over each character starting with the second
@ -177,11 +177,11 @@ do
# extract 1 character from position 'i' # extract 1 character from position 'i'
c=${1:i:1} c=${1:i:1}
# add current char to options # add current char to options
options+=("-$c") options+=("-${c}")
# if option takes a required argument, and it's not the last char # if option takes a required argument, and it's not the last char
# make the rest of the string its argument # make the rest of the string its argument
if [[ $optstring = *"$c:"* && ${1:i+1} ]] if [[ ${optstring} = *"${c}:"* && ${1:i+1} ]]
then then
options+=("${1:i+1}") options+=("${1:i+1}")
break break
@ -196,12 +196,12 @@ do
--) --)
options+=(--endopts) options+=(--endopts)
shift shift
options+=("$@") options+=("${@}")
break break
;; ;;
# otherwise, nothing special # otherwise, nothing special
*) *)
options+=("$1") options+=("${1}")
;; ;;
esac esac
@ -219,17 +219,17 @@ unset options
# command. This is essentially the same as the program arguments, minus those # command. This is essentially the same as the program arguments, minus those
# that have been filtered out in the program option parsing loop. This array # that have been filtered out in the program option parsing loop. This array
# is initialized with $0, which is the program's name. # is initialized with $0, which is the program's name.
_COMMAND_ARGV=("$0") _COMMAND_ARGV=("${0}")
# Initialize $_CMD and `$_USE_DEBUG`, which can continue to be blank depending # Initialize $_CMD and `$_USE_DEBUG`, which can continue to be blank depending
# on what the program needs. # on what the program needs.
_CMD="" _CMD=""
_USE_DEBUG=0 _USE_DEBUG=0
while [ $# -gt 0 ] while [ ${#} -gt 0 ]
do do
opt="$1" opt="${1}"
shift shift
case "$opt" in case "${opt}" in
-h|--help) -h|--help)
_CMD="help" _CMD="help"
;; ;;
@ -242,11 +242,11 @@ do
*) *)
# The first non-option argument is assumed to be the command name. # The first non-option argument is assumed to be the command name.
# All subsequent arguments are added to $_COMMAND_ARGV. # All subsequent arguments are added to $_COMMAND_ARGV.
if [[ -n $_CMD ]] if [[ -n ${_CMD} ]]
then then
_COMMAND_ARGV+=("$opt") _COMMAND_ARGV+=("${opt}")
else else
_CMD="$opt" _CMD="${opt}"
fi fi
;; ;;
esac esac
@ -263,10 +263,18 @@ done
_COMMAND_PARAMETERS=(${_COMMAND_ARGV[*]}) _COMMAND_PARAMETERS=(${_COMMAND_ARGV[*]})
unset _COMMAND_PARAMETERS[0] unset _COMMAND_PARAMETERS[0]
_debug printf "\$_CMD: %s\n" "$_CMD" _debug printf \
_debug printf "\$_RAW_OPTIONS (one per line):\n%s\n" "$_RAW_OPTIONS" "\${_CMD}: %s\n" \
_debug printf "\$_COMMAND_ARGV: %s\n" "${_COMMAND_ARGV[*]}" "${_CMD}"
_debug printf "\$_COMMAND_PARAMETERS: %s\n" "${_COMMAND_PARAMETERS[*]:-}" _debug printf \
"\${_RAW_OPTIONS} (one per line):\n%s\n" \
"${_RAW_OPTIONS}"
_debug printf \
"\${_COMMAND_ARGV[*]}: %s\n" \
"${_COMMAND_ARGV[*]}"
_debug printf \
"\${_COMMAND_PARAMETERS[*]:-}: %s\n" \
"${_COMMAND_PARAMETERS[*]:-}"
############################################################################### ###############################################################################
# Environment # Environment
@ -275,9 +283,9 @@ _debug printf "\$_COMMAND_PARAMETERS: %s\n" "${_COMMAND_PARAMETERS[*]:-}"
# $_ME # $_ME
# #
# Set to the program's basename. # Set to the program's basename.
_ME=$(basename "$0") _ME=$(basename "${0}")
_debug printf "\$_ME: %s\n" "$_ME" _debug printf "\${_ME}: %s\n" "${_ME}"
############################################################################### ###############################################################################
# Load Commands # Load Commands
@ -308,25 +316,25 @@ _load_commands() {
# Each element has the format `declare -f function_name`, so set the name # Each element has the format `declare -f function_name`, so set the name
# to only the 'function_name' part of the string. # to only the 'function_name' part of the string.
local function_name local function_name
function_name=$(printf "%s" "$c" | awk '{ print $3 }') function_name=$(printf "%s" "${c}" | awk '{ print $3 }')
_debug printf "_load_commands() \$function_name: %s\n" "$function_name" _debug printf "_load_commands() \${function_name}: %s\n" "${function_name}"
# Add the function name to the $_DEFINED_COMMANDS array unless it starts # Add the function name to the $_DEFINED_COMMANDS array unless it starts
# with an underscore or is one of the desc(), debug(), or die() functions, # with an underscore or is one of the desc(), debug(), or die() functions,
# since these are treated as having 'private' visibility. # since these are treated as having 'private' visibility.
if ! ( [[ "$function_name" =~ ^_(.*) ]] || \ if ! ( [[ "${function_name}" =~ ^_(.*) ]] || \
[[ "$function_name" == "desc" ]] || \ [[ "${function_name}" == "desc" ]] || \
[[ "$function_name" == "debug" ]] || \ [[ "${function_name}" == "debug" ]] || \
[[ "$function_name" == "die" ]] [[ "${function_name}" == "die" ]]
) )
then then
_DEFINED_COMMANDS+=("$function_name") _DEFINED_COMMANDS+=("${function_name}")
fi fi
done done
_debug printf \ _debug printf \
"commands() \$_DEFINED_COMMANDS:\n%s\n" \ "commands() \${_DEFINED_COMMANDS[*]:-}:\n%s\n" \
"${_DEFINED_COMMANDS[*]:-}" "${_DEFINED_COMMANDS[*]:-}"
} }
@ -344,24 +352,24 @@ _load_commands() {
# NOTE: must be called at end of program after all commands have been defined. # NOTE: must be called at end of program after all commands have been defined.
_main() { _main() {
_debug printf "main(): entering...\n" _debug printf "main(): entering...\n"
_debug printf "main() \$_CMD (upon entering): %s\n" "$_CMD" _debug printf "main() \${_CMD} (upon entering): %s\n" "${_CMD}"
# If $_CMD is blank, then set to `$DEFAULT_COMMAND` # If $_CMD is blank, then set to `$DEFAULT_COMMAND`
if [[ -z $_CMD ]] if [[ -z ${_CMD} ]]
then then
_CMD="$DEFAULT_COMMAND" _CMD="${DEFAULT_COMMAND}"
fi fi
# Load all of the commands. # Load all of the commands.
_load_commands _load_commands
# If the command is defined, run it, otherwise return an error. # If the command is defined, run it, otherwise return an error.
if _contains "$_CMD" "${_DEFINED_COMMANDS[*]:-}" if _contains "${_CMD}" "${_DEFINED_COMMANDS[*]:-}"
then then
# Pass all comment arguments to the program except for the first ($0). # Pass all comment arguments to the program except for the first ($0).
$_CMD "${_COMMAND_PARAMETERS[@]:-}" ${_CMD} "${_COMMAND_PARAMETERS[@]:-}"
else else
_die printf "Unknown command: %s\n" "$_CMD" _die printf "Unknown command: %s\n" "${_CMD}"
fi fi
} }
@ -377,7 +385,7 @@ _main() {
# Takes a potential function name as an argument and returns whether a function # Takes a potential function name as an argument and returns whether a function
# exists with that name. # exists with that name.
_function_exists() { _function_exists() {
[ "$(type -t "$1")" == 'function' ] [ "$(type -t "${1}")" == 'function' ]
} }
# _command_exists() # _command_exists()
@ -391,7 +399,7 @@ _function_exists() {
# For information on why `hash` is used here, see: # For information on why `hash` is used here, see:
# http://stackoverflow.com/a/677212 # http://stackoverflow.com/a/677212
_command_exists() { _command_exists() {
hash "$1" 2>/dev/null hash "${1}" 2>/dev/null
} }
# _contains() # _contains()
@ -404,10 +412,10 @@ _contains() {
local test_list=(${*:2}) local test_list=(${*:2})
for _test_element in "${test_list[@]:-}" for _test_element in "${test_list[@]:-}"
do do
_debug printf "_contains() \$_test_element: %s\n" "$_test_element" _debug printf "_contains() \${_test_element}: %s\n" "${_test_element}"
if [[ "$_test_element" == "$1" ]] if [[ "${_test_element}" == "${1}" ]]
then then
_debug printf "_contains() match: %s\n" "$1" _debug printf "_contains() match: %s\n" "${1}"
return 0 return 0
fi fi
done done
@ -427,7 +435,7 @@ _join() {
local target_array local target_array
local dirty local dirty
local clean local clean
separator="$1" separator="${1}"
target_array=(${@:2}) target_array=(${@:2})
dirty="$(printf "${separator}%s" "${target_array[@]}")" dirty="$(printf "${separator}%s" "${target_array[@]}")"
clean="${dirty:${#separator}}" clean="${dirty:${#separator}}"
@ -445,7 +453,7 @@ _join() {
# This is a shortcut for simple cases where a command wants to check for the # This is a shortcut for simple cases where a command wants to check for the
# presence of options quickly without parsing the options again. # presence of options quickly without parsing the options again.
_command_argv_includes() { _command_argv_includes() {
_contains "$1" "${_COMMAND_ARGV[*]}" _contains "${1}" "${_COMMAND_ARGV[*]}"
} }
# _blank() # _blank()
@ -512,16 +520,16 @@ sudo !!\n"
# escaping backslashes, which is more common. # escaping backslashes, which is more common.
desc() { desc() {
set +e set +e
[[ -z $1 ]] && _die printf "desc: No command name specified.\n" [[ -z ${1} ]] && _die printf "desc: No command name specified.\n"
if [[ -n ${2:-} ]] if [[ -n ${2:-} ]]
then then
read -d '' "_desc_$1" <<EOM read -d '' "_desc_${1}" <<EOM
$2 ${2}
EOM EOM
_debug printf "desc() set with argument: _desc_%s\n" "$1" _debug printf "desc() set with argument: _desc_%s\n" "${1}"
else else
read -d '' "_desc_$1" read -d '' "_desc_${1}"
_debug printf "desc() set with pipe: _desc_%s\n" "$1" _debug printf "desc() set with pipe: _desc_%s\n" "${1}"
fi fi
set -e set -e
} }
@ -534,12 +542,12 @@ EOM
# Prints the description for a given command, provided the description has been # Prints the description for a given command, provided the description has been
# set using the desc() function. # set using the desc() function.
_print_desc() { _print_desc() {
local var="_desc_$1" local var="_desc_${1}"
if [[ -n ${!var:-} ]] if [[ -n ${!var:-} ]]
then then
printf "%s\n" "${!var}" printf "%s\n" "${!var}"
else else
printf "No additional information for \`%s\`\n" "$1" printf "No additional information for \`%s\`\n" "${1}"
fi fi
} }
@ -551,25 +559,25 @@ _print_desc() {
desc "version" <<EOM desc "version" <<EOM
Usage: Usage:
$_ME (version | --version) ${_ME} (version | --version)
Description: Description:
Display the current program version. Display the current program version.
To save you the trouble, the current version is $_VERSION To save you the trouble, the current version is ${_VERSION}
EOM EOM
version() { version() {
printf "%s\n" "$_VERSION" printf "%s\n" "${_VERSION}"
} }
# Help ######################################################################## # Help ########################################################################
desc "help" <<EOM desc "help" <<EOM
Usage: Usage:
$_ME help [<command>] ${_ME} help [<command>]
Description: Description:
Display help information for $_ME or a specified command. Display help information for ${_ME} or a specified command.
EOM EOM
help() { help() {
if [[ ${#_COMMAND_ARGV[@]} = 1 ]] if [[ ${#_COMMAND_ARGV[@]} = 1 ]]
@ -583,34 +591,34 @@ help() {
A program for managing host file entries. A program for managing host file entries.
Version: $_VERSION Version: ${_VERSION}
Usage: Usage:
$_ME ${_ME}
$_ME add <ip> <hostname> [<comment>] ${_ME} add <ip> <hostname> [<comment>]
$_ME disable (<ip> | <hostname> | <search string>) ${_ME} disable (<ip> | <hostname> | <search string>)
$_ME disabled ${_ME} disabled
$_ME edit ${_ME} edit
$_ME enable (<ip> | <hostname> | <search string>) ${_ME} enable (<ip> | <hostname> | <search string>)
$_ME enabled ${_ME} enabled
$_ME file ${_ME} file
$_ME list [enabled | disabled | <search string>] ${_ME} list [enabled | disabled | <search string>]
$_ME show (<ip> | <hostname> | <search string>) ${_ME} show (<ip> | <hostname> | <search string>)
$_ME remove (<ip> | <hostname> | <search string>) [--force] ${_ME} remove (<ip> | <hostname> | <search string>) [--force]
$_ME -h | --help ${_ME} -h | --help
$_ME --version ${_ME} --version
Options: Options:
-h --help Display this help information. -h --help Display this help information.
--version Display version information. --version Display version information.
Help: Help:
$_ME help [<command>] ${_ME} help [<command>]
$(commands) $(commands)
EOM EOM
else else
_print_desc "$1" _print_desc "${1}"
fi fi
} }
@ -618,7 +626,7 @@ EOM
desc "commands" <<EOM desc "commands" <<EOM
Usage: Usage:
$_ME commands [--raw] ${_ME} commands [--raw]
Options: Options:
--raw Display the command list without formatting. --raw Display the command list without formatting.
@ -667,15 +675,15 @@ commands() {
desc "add" <<EOM desc "add" <<EOM
Usage: Usage:
$_ME add <ip> <hostname> [<comment>] ${_ME} add <ip> <hostname> [<comment>]
Description: Description:
Add a given IP address and hostname pair, along with an optional comment. Add a given IP address and hostname pair, along with an optional comment.
EOM EOM
add() { add() {
_debug printf "add() \$1: %s\n" "${1:-}" _debug printf "add() \${1}: %s\n" "${1:-}"
_debug printf "add() \$2: %s\n" "${2:-}" _debug printf "add() \${2}: %s\n" "${2:-}"
_debug printf "add() \$3: %s\n" "${3:-}" _debug printf "add() \${3}: %s\n" "${3:-}"
_verify_write_permissions _verify_write_permissions
local ip=${1:-} local ip=${1:-}
@ -683,12 +691,12 @@ add() {
local comment=${*:3} local comment=${*:3}
if [[ -z ${ip} ]] if [[ -z ${ip} ]]
then then
$_ME help add ${_ME} help add
exit 1 exit 1
elif [[ -z ${hostname} ]] elif [[ -z ${hostname} ]]
then then
printf "Please include a hostname\n" printf "Please include a hostname\n"
$_ME help add ${_ME} help add
exit 1 exit 1
elif grep \ elif grep \
-e "^${ip}\t${hostname}$" \ -e "^${ip}\t${hostname}$" \
@ -725,7 +733,7 @@ add() {
desc "disable" <<EOM desc "disable" <<EOM
Usage: Usage:
$_ME disable (<ip> | <hostname> | <search string>) ${_ME} disable (<ip> | <hostname> | <search string>)
Description: Description:
Disable one or more records based on a given ip address, hostname, or Disable one or more records based on a given ip address, hostname, or
@ -733,13 +741,13 @@ Description:
EOM EOM
disable() { disable() {
_verify_write_permissions _verify_write_permissions
local search_string=$1 local search_string="${1}"
if [[ -z "${search_string}" ]] if [[ -z "${search_string}" ]]
then then
$_ME help disable ${_ME} help disable
exit 1 exit 1
else else
_debug printf "disable() \$search_string: %s\n" "$search_string" _debug printf "disable() \${search_string}: %s\n" "${search_string}"
target_regex_ip="^\(${search_string}[${_TAB_SPACE_}]..*\)$" target_regex_ip="^\(${search_string}[${_TAB_SPACE_}]..*\)$"
target_regex_commented_hostname="^\([^#]..*[${_TAB_SPACE_}]${search_string}[${_TAB_SPACE_}]..*\)$" target_regex_commented_hostname="^\([^#]..*[${_TAB_SPACE_}]${search_string}[${_TAB_SPACE_}]..*\)$"
@ -757,7 +765,7 @@ disable() {
-e "s/${target_regex_hostname}/\1/p" \ -e "s/${target_regex_hostname}/\1/p" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
) )
_debug printf "disable() \$targets: %s\n" "$targets" _debug printf "disable() \${targets}: %s\n" "${targets}"
if [[ -z "${targets}" ]] if [[ -z "${targets}" ]]
then then
_die printf "Not found: %s\n" "${search_string}" _die printf "Not found: %s\n" "${search_string}"
@ -779,31 +787,31 @@ disable() {
desc "disabled" <<EOM desc "disabled" <<EOM
Usage: Usage:
$_ME disabled ${_ME} disabled
Description: Description:
List all disabled records. This is an alias for \`hosts list disabled\`. List all disabled records. This is an alias for \`hosts list disabled\`.
EOM EOM
disabled() { disabled() {
$_ME list disabled ${_ME} list disabled
} }
# ------------------------------------------------------------------------ edit # ------------------------------------------------------------------------ edit
desc "edit" <<EOM desc "edit" <<EOM
Usage: Usage:
$_ME edit ${_ME} edit
Description: Description:
Open the ${HOSTS_PATH} file in your \$EDITOR. Open the ${HOSTS_PATH} file in your \$EDITOR.
EOM EOM
edit() { edit() {
_verify_write_permissions _verify_write_permissions
if [[ -z "$EDITOR" ]] if [[ -z "${EDITOR}" ]]
then then
_die printf "\$EDITOR not set.\n" _die printf "\$EDITOR not set.\n"
else else
"$EDITOR" "${HOSTS_PATH}" "${EDITOR}" "${HOSTS_PATH}"
fi fi
} }
@ -811,7 +819,7 @@ edit() {
desc "enable" <<EOM desc "enable" <<EOM
Usage: Usage:
$_ME enable (<ip> | <hostname> | <search string>) ${_ME} enable (<ip> | <hostname> | <search string>)
Description: Description:
Enable one or more disabled records based on a given ip address, hostname, Enable one or more disabled records based on a given ip address, hostname,
@ -819,13 +827,13 @@ Description:
EOM EOM
enable() { enable() {
_verify_write_permissions _verify_write_permissions
local search_string=$1 local search_string="${1}"
if [[ -z "${search_string}" ]] if [[ -z "${search_string}" ]]
then then
$_ME help enable ${_ME} help enable
exit 1 exit 1
else else
_debug printf "enable() \$search_string: %s\n" "$search_string" _debug printf "enable() \${search_string}: %s\n" "${search_string}"
target_regex_ip="^\#disabled: \(${search_string}[${_TAB_SPACE_}]..*\)$" target_regex_ip="^\#disabled: \(${search_string}[${_TAB_SPACE_}]..*\)$"
target_regex_commented_hostname="^\#disabled: \(..*[${_TAB_SPACE_}]${search_string}[${_TAB_SPACE_}]..*\)$" target_regex_commented_hostname="^\#disabled: \(..*[${_TAB_SPACE_}]${search_string}[${_TAB_SPACE_}]..*\)$"
@ -843,7 +851,7 @@ enable() {
-e "s/${target_regex_hostname}/\1/p" \ -e "s/${target_regex_hostname}/\1/p" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
) )
_debug printf "enable() \$targets: %s\n" "$targets" _debug printf "enable() \${targets}: %s\n" "${targets}"
if [[ -z "${targets}" ]] if [[ -z "${targets}" ]]
then then
_die printf "Not found: %s\n" "${search_string}" _die printf "Not found: %s\n" "${search_string}"
@ -865,20 +873,20 @@ enable() {
desc "enabled" <<EOM desc "enabled" <<EOM
Usage: Usage:
$_ME enabled ${_ME} enabled
Description: Description:
List all enabled records. This is an alias for \`hosts list enabled\`. List all enabled records. This is an alias for \`hosts list enabled\`.
EOM EOM
enabled() { enabled() {
$_ME list enabled ${_ME} list enabled
} }
# ------------------------------------------------------------------------ file # ------------------------------------------------------------------------ file
desc "file" <<EOM desc "file" <<EOM
Usage: Usage:
$_ME file ${_ME} file
Description: Description:
Print the entire contents of the ${HOSTS_PATH} file. Print the entire contents of the ${HOSTS_PATH} file.
@ -891,7 +899,7 @@ file() {
desc "list" <<EOM desc "list" <<EOM
Usage: Usage:
$_ME list [enabled | disabled | <search string>] ${_ME} list [enabled | disabled | <search string>]
Description: Description:
List the existing IP / hostname pairs, optionally limited to a specified List the existing IP / hostname pairs, optionally limited to a specified
@ -905,16 +913,16 @@ list() {
sed -n "s/^\#disabled: \(.*\)$/\1/p" "${HOSTS_PATH}" sed -n "s/^\#disabled: \(.*\)$/\1/p" "${HOSTS_PATH}"
) )
if [[ -n "$1" ]] if [[ -n "${1}" ]]
then then
if [[ "$1" == "disabled" ]] if [[ "${1}" == "disabled" ]]
then then
printf "%s\n" "${disabled_records}" printf "%s\n" "${disabled_records}"
elif [[ "$1" == "enabled" ]] elif [[ "${1}" == "enabled" ]]
then then
grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}" grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}"
else else
$_ME show "$1" ${_ME} show "${1}"
fi fi
else else
# NOTE: use separate expressions since using a | for the or results in # NOTE: use separate expressions since using a | for the or results in
@ -931,8 +939,8 @@ list() {
desc "remove" <<EOM desc "remove" <<EOM
Usage: Usage:
$_ME remove (<ip> | <hostname> | <search string>) [--force] ${_ME} remove (<ip> | <hostname> | <search string>) [--force]
$_ME remove <ip> <hostname> ${_ME} remove <ip> <hostname>
Options: Options:
--force Skip the confirmation prompt. --force Skip the confirmation prompt.
@ -951,17 +959,17 @@ remove() {
local search_hostname="" local search_hostname=""
local search_string="" local search_string=""
_debug printf "remove() \$1: %s\n" "${1:-}" _debug printf "remove() \${1}: %s\n" "${1:-}"
_debug printf "remove() \$2: %s\n" "${2:-}" _debug printf "remove() \${2}: %s\n" "${2:-}"
for arg in "${_COMMAND_ARGV[@]:-}" for arg in "${_COMMAND_ARGV[@]:-}"
do do
case $arg in case ${arg} in
--force) --force)
force_skip_prompt=1 force_skip_prompt=1
;; ;;
*) *)
arguments+=($arg) arguments+=(${arg})
;; ;;
esac esac
done done
@ -972,17 +980,17 @@ remove() {
if [[ -z "${arguments[1]:-}" ]] if [[ -z "${arguments[1]:-}" ]]
then then
$_ME help remove ${_ME} help remove
exit 1 exit 1
elif [[ -n "${arguments[2]:-}" ]] elif [[ -n "${arguments[2]:-}" ]]
then then
search_ip="${arguments[1]}" search_ip="${arguments[1]}"
search_hostname="${arguments[2]}" search_hostname="${arguments[2]}"
is_search_pair=1 is_search_pair=1
_debug printf "remove() \$is_search_pair: %s\n" "$is_search_pair" _debug printf "remove() \${is_search_pair}: %s\n" "${is_search_pair}"
else else
search_string="${arguments[1]:-}" search_string="${arguments[1]:-}"
_debug printf "remove() \$search_string: %s\n" "$search_string" _debug printf "remove() \${search_string}: %s\n" "${search_string}"
fi fi
# Regular Expression Notes # Regular Expression Notes
@ -1026,11 +1034,11 @@ remove() {
if ! ((force_skip_prompt)) if ! ((force_skip_prompt))
then then
printf "Removing the following records:\n%s\n" "$target_records" printf "Removing the following records:\n%s\n" "${target_records}"
while true while true
do do
read -p "Are you sure you want to proceed? [y/N] " yn read -p "Are you sure you want to proceed? [y/N] " yn
case $yn in case ${yn} in
[Yy]* ) [Yy]* )
break break
;; ;;
@ -1066,34 +1074,34 @@ remove() {
desc "show" <<EOM desc "show" <<EOM
Usage: Usage:
$_ME show (<ip> | <hostname> | <search string>) ${_ME} show (<ip> | <hostname> | <search string>)
Description: Description:
Print entries matching a given IP address, hostname, or search string. Print entries matching a given IP address, hostname, or search string.
EOM EOM
show() { show() {
if [[ -n "$1" ]] if [[ -n "${1}" ]]
then then
# Run `sed` before `grep` to avoid conflict that supress `sed` output. # Run `sed` before `grep` to avoid conflict that supress `sed` output.
local disabled_records local disabled_records
disabled_records=$( disabled_records=$(
sed -n "s/^\#\(disabled: .*$1.*\)$/\1/p" "${HOSTS_PATH}" sed -n "s/^\#\(disabled: .*${1}.*\)$/\1/p" "${HOSTS_PATH}"
) )
local enabled_records local enabled_records
enabled_records=$( enabled_records=$(
grep "^[^#]*$1" "${HOSTS_PATH}" grep "^[^#]*${1}" "${HOSTS_PATH}"
) )
# Output disabled records secondly for better organization. # Output disabled records secondly for better organization.
if [[ -n "$enabled_records" ]] if [[ -n "${enabled_records}" ]]
then then
printf "%s\n" "$enabled_records" printf "%s\n" "${enabled_records}"
fi fi
if [[ -n "$disabled_records" ]] if [[ -n "${disabled_records}" ]]
then then
printf "%s\n" "$disabled_records" printf "%s\n" "${disabled_records}"
fi fi
else else
$_ME help show ${_ME} help show
exit 1 exit 1
fi fi
} }

View File

@ -5,25 +5,25 @@ load test_helper
# `hosts add` ################################################################# # `hosts add` #################################################################
@test "\`add\` with no arguments exits with status 1." { @test "\`add\` with no arguments exits with status 1." {
run "$_HOSTS" add run "${_HOSTS}" add
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 1 ]] [[ ${status} -eq 1 ]]
} }
@test "\`add\` with no argument does not change the hosts file." { @test "\`add\` with no argument does not change the hosts file." {
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" add run "${_HOSTS}" add
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
} }
@test "\`add\` with no arguments prints help information." { @test "\`add\` with no arguments prints help information." {
run "$_HOSTS" add run "${_HOSTS}" add
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts add <ip> <hostname> [<comment>]" ]] [[ "${lines[1]}" == " hosts add <ip> <hostname> [<comment>]" ]]
} }
@ -31,25 +31,25 @@ load test_helper
# `hosts add <ip>` ############################################################ # `hosts add <ip>` ############################################################
@test "\`add <ip>\` exits with status 1." { @test "\`add <ip>\` exits with status 1." {
run "$_HOSTS" add 0.0.0.0 run "${_HOSTS}" add 0.0.0.0
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 1 ]] [[ ${status} -eq 1 ]]
} }
@test "\`add <ip>\` does not change the hosts file." { @test "\`add <ip>\` does not change the hosts file." {
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" add 0.0.0.0 run "${_HOSTS}" add 0.0.0.0
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
} }
@test "\`add <ip>\` prints help information." { @test "\`add <ip>\` prints help information." {
run "$_HOSTS" add 0.0.0.0 run "${_HOSTS}" add 0.0.0.0
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Please include a hostname" ]] [[ "${lines[0]}" == "Please include a hostname" ]]
[[ "${lines[1]}" == "Usage:" ]] [[ "${lines[1]}" == "Usage:" ]]
[[ "${lines[2]}" == " hosts add <ip> <hostname> [<comment>]" ]] [[ "${lines[2]}" == " hosts add <ip> <hostname> [<comment>]" ]]
@ -58,18 +58,18 @@ load test_helper
# `hosts add <ip> <hostname>` ################################################# # `hosts add <ip> <hostname>` #################################################
@test "\`add <ip> <hostname>\` exits with status 0." { @test "\`add <ip> <hostname>\` exits with status 0." {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`add <ip> <hostname>\` adds the entry to the hosts file." { @test "\`add <ip> <hostname>\` adds the entry to the hosts file." {
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
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}" ]]
@ -77,9 +77,9 @@ load test_helper
} }
@test "\`add <ip> <hostname>\` prints feedback." { @test "\`add <ip> <hostname>\` prints feedback." {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
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" ]]
} }
@ -87,26 +87,26 @@ load test_helper
# `hosts add <ip> <hostname> [comment]` ####################################### # `hosts add <ip> <hostname> [comment]` #######################################
@test "\`add <ip> <hostname> [comment]\` exits with status 0." { @test "\`add <ip> <hostname> [comment]\` exits with status 0." {
run "$_HOSTS" add 0.0.0.0 example.com 'Comment.' run "${_HOSTS}" add 0.0.0.0 example.com 'Comment.'
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`add <ip> <hostname> [comment]\` adds the entry to the hosts file." { @test "\`add <ip> <hostname> [comment]\` adds the entry to the hosts file." {
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" add 0.0.0.0 example.com 'Comment.' run "${_HOSTS}" add 0.0.0.0 example.com 'Comment.'
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com # Comment." ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com # Comment." ]]
} }
@test "\`add <ip> <hostname> [comment]\` prints feedback." { @test "\`add <ip> <hostname> [comment]\` prints feedback." {
run "$_HOSTS" add 0.0.0.0 example.com 'Comment.' run "${_HOSTS}" add 0.0.0.0 example.com 'Comment.'
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 # Comment." ]] [[ "${lines[1]}" == "0.0.0.0 example.com # Comment." ]]
} }
@ -114,14 +114,14 @@ load test_helper
# help ######################################################################## # help ########################################################################
@test "\`help add\` exits with status 0." { @test "\`help add\` exits with status 0." {
run "$_HOSTS" help add run "${_HOSTS}" help add
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`help add\` prints help information." { @test "\`help add\` prints help information." {
run "$_HOSTS" help add run "${_HOSTS}" help add
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts add <ip> <hostname> [<comment>]" ]] [[ "${lines[1]}" == " hosts add <ip> <hostname> [<comment>]" ]]
} }

View File

@ -5,25 +5,25 @@ load test_helper
# `hosts disable` ############################################################# # `hosts disable` #############################################################
@test "\`disable\` with no arguments exits with status 1." { @test "\`disable\` with no arguments exits with status 1." {
run "$_HOSTS" disable run "${_HOSTS}" disable
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 1 ]] [[ ${status} -eq 1 ]]
} }
@test "\`disable\` with no argument does not change the hosts file." { @test "\`disable\` with no argument does not change the hosts file." {
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" disable run "${_HOSTS}" disable
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
} }
@test "\`disable\` with no arguments prints help information." { @test "\`disable\` with no arguments prints help information." {
run "$_HOSTS" disable run "${_HOSTS}" disable
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts disable (<ip> | <hostname> | <search string>)" ]] [[ "${lines[1]}" == " hosts disable (<ip> | <hostname> | <search string>)" ]]
} }
@ -32,26 +32,26 @@ load test_helper
@test "\`disable <ip>\` exits with status 0." { @test "\`disable <ip>\` exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 127.0.0.1 example.net run "${_HOSTS}" add 127.0.0.1 example.net
} }
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`disable <ip>\` updates the hosts file." { @test "\`disable <ip>\` updates the hosts file." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 127.0.0.1 example.net run "${_HOSTS}" add 127.0.0.1 example.net
} }
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
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 example.com" ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]]
@ -59,14 +59,14 @@ load test_helper
@test "\`disable <ip>\` disables all matches." { @test "\`disable <ip>\` disables all matches." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
} }
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
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 example.com" ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.net" ]]
@ -74,13 +74,13 @@ load test_helper
@test "\`disable <ip>\` prints feedback." { @test "\`disable <ip>\` prints feedback." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 127.0.0.1 example.net run "${_HOSTS}" add 127.0.0.1 example.net
} }
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
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 example.com" ]]
} }
@ -89,26 +89,26 @@ load test_helper
@test "\`disable <hostname>\` exits with status 0." { @test "\`disable <hostname>\` exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 127.0.0.1 example.net run "${_HOSTS}" add 127.0.0.1 example.net
} }
run "$_HOSTS" disable example.com run "${_HOSTS}" disable example.com
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`disable <hostname>\` updates the hosts file." { @test "\`disable <hostname>\` updates the hosts file." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 127.0.0.1 example.net run "${_HOSTS}" add 127.0.0.1 example.net
} }
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" disable example.com run "${_HOSTS}" disable example.com
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 example.com" ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]]
@ -116,14 +116,14 @@ load test_helper
@test "\`disable <hostname>\` disables all matches." { @test "\`disable <hostname>\` disables all matches." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 127.0.0.1 example.com run "${_HOSTS}" add 127.0.0.1 example.com
} }
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" disable example.com run "${_HOSTS}" disable example.com
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 example.com" ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 127.0.0.1 example.com" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 127.0.0.1 example.com" ]]
@ -131,13 +131,13 @@ load test_helper
@test "\`disable <hostname>\` prints feedback." { @test "\`disable <hostname>\` prints feedback." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 127.0.0.1 example.net run "${_HOSTS}" add 127.0.0.1 example.net
} }
run "$_HOSTS" disable example.com run "${_HOSTS}" disable example.com
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 example.com" ]]
} }
@ -145,14 +145,14 @@ load test_helper
# help ######################################################################## # help ########################################################################
@test "\`help disable\` exits with status 0." { @test "\`help disable\` exits with status 0." {
run "$_HOSTS" help disable run "${_HOSTS}" help disable
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`help disable\` prints help information." { @test "\`help disable\` prints help information." {
run "$_HOSTS" help disable run "${_HOSTS}" help disable
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts disable (<ip> | <hostname> | <search string>)" ]] [[ "${lines[1]}" == " hosts disable (<ip> | <hostname> | <search string>)" ]]
} }

View File

@ -6,29 +6,29 @@ load test_helper
@test "\`disabled\` with no arguments exits with status 0." { @test "\`disabled\` with no arguments exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.1 example.com run "${_HOSTS}" add 127.0.0.1 example.com
run "$_HOSTS" disable example.com run "${_HOSTS}" disable example.com
} }
run "$_HOSTS" disabled run "${_HOSTS}" disabled
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`disabled\` with no arguments prints list of disabled records." { @test "\`disabled\` with no arguments prints list of disabled records." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.1 example.com run "${_HOSTS}" add 127.0.0.1 example.com
run "$_HOSTS" disable example.com run "${_HOSTS}" disable example.com
} }
run "$_HOSTS" disabled run "${_HOSTS}" disabled
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.com" ]] [[ "${lines[0]}" == "0.0.0.0 example.com" ]]
[[ "${lines[1]}" == "127.0.0.1 example.com" ]] [[ "${lines[1]}" == "127.0.0.1 example.com" ]]
[[ "${lines[2]}" == "" ]] [[ "${lines[2]}" == "" ]]
@ -37,14 +37,14 @@ load test_helper
# help ######################################################################## # help ########################################################################
@test "\`help disabled\` exits with status 0." { @test "\`help disabled\` exits with status 0." {
run "$_HOSTS" help disabled run "${_HOSTS}" help disabled
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`help disabled\` prints help information." { @test "\`help disabled\` prints help information." {
run "$_HOSTS" help disabled run "${_HOSTS}" help disabled
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts disabled" ]] [[ "${lines[1]}" == " hosts disabled" ]]
} }

View File

@ -5,25 +5,25 @@ load test_helper
# `hosts enable` ############################################################## # `hosts enable` ##############################################################
@test "\`enable\` with no arguments exits with status 1." { @test "\`enable\` with no arguments exits with status 1." {
run "$_HOSTS" enable run "${_HOSTS}" enable
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 1 ]] [[ ${status} -eq 1 ]]
} }
@test "\`enable\` with no argument does not change the hosts file." { @test "\`enable\` with no argument does not change the hosts file." {
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" enable run "${_HOSTS}" enable
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
} }
@test "\`enable\` with no arguments prints help information." { @test "\`enable\` with no arguments prints help information." {
run "$_HOSTS" enable run "${_HOSTS}" enable
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts enable (<ip> | <hostname> | <search string>)" ]] [[ "${lines[1]}" == " hosts enable (<ip> | <hostname> | <search string>)" ]]
} }
@ -32,31 +32,31 @@ load test_helper
@test "\`enable <ip>\` exits with status 0." { @test "\`enable <ip>\` exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 127.0.0.2 run "${_HOSTS}" disable 127.0.0.2
} }
run "$_HOSTS" enable 127.0.0.2 run "${_HOSTS}" enable 127.0.0.2
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`enable <ip>\` updates the hosts file." { @test "\`enable <ip>\` updates the hosts file." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
run "$_HOSTS" disable 127.0.0.2 run "${_HOSTS}" disable 127.0.0.2
} }
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" enable 127.0.0.2 run "${_HOSTS}" enable 127.0.0.2
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 example.com" ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.net" ]]
@ -65,17 +65,17 @@ load test_helper
@test "\`enable <ip>\` enables all matches." { @test "\`enable <ip>\` enables all matches." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
run "$_HOSTS" disable 127.0.0.2 run "${_HOSTS}" disable 127.0.0.2
} }
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" enable 0.0.0.0 run "${_HOSTS}" enable 0.0.0.0
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
_compare \ _compare \
"${_original}" \ "${_original}" \
"$(cat "${HOSTS_PATH}")" "$(cat "${HOSTS_PATH}")"
@ -96,15 +96,15 @@ load test_helper
@test "\`disable <ip>\` prints feedback." { @test "\`disable <ip>\` prints feedback." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 127.0.0.2 run "${_HOSTS}" disable 127.0.0.2
} }
run "$_HOSTS" enable 127.0.0.2 run "${_HOSTS}" enable 127.0.0.2
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 example.com" ]]
} }
@ -113,30 +113,30 @@ load test_helper
@test "\`enable <hostname>\` exits with status 0." { @test "\`enable <hostname>\` exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
} }
run "$_HOSTS" enable example.net run "${_HOSTS}" enable example.net
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`enable <hostname>\` updates the hosts file." { @test "\`enable <hostname>\` updates the hosts file." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
} }
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" enable example.net run "${_HOSTS}" enable example.net
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 example.com" ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "0.0.0.0 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "0.0.0.0 example.net" ]]
@ -145,17 +145,17 @@ load test_helper
@test "\`enable <hostname>\` enables all matches." { @test "\`enable <hostname>\` enables all matches." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
run "$_HOSTS" disable 127.0.0.2 run "${_HOSTS}" disable 127.0.0.2
} }
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" enable example.com run "${_HOSTS}" enable example.com
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
_compare \ _compare \
"${_original}" \ "${_original}" \
"$(cat "${HOSTS_PATH}")" "$(cat "${HOSTS_PATH}")"
@ -176,15 +176,15 @@ load test_helper
@test "\`disable <hostname>\` prints feedback." { @test "\`disable <hostname>\` prints feedback." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
} }
run "$_HOSTS" enable example.net run "${_HOSTS}" enable example.net
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 example.net" ]]
} }
@ -192,14 +192,14 @@ load test_helper
# help ######################################################################## # help ########################################################################
@test "\`help enable\` exits with status 0." { @test "\`help enable\` exits with status 0." {
run "$_HOSTS" help enable run "${_HOSTS}" help enable
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`help enable\` prints help information." { @test "\`help enable\` prints help information." {
run "$_HOSTS" help enable run "${_HOSTS}" help enable
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts enable (<ip> | <hostname> | <search string>)" ]] [[ "${lines[1]}" == " hosts enable (<ip> | <hostname> | <search string>)" ]]
} }

View File

@ -6,29 +6,29 @@ load test_helper
@test "\`enabled\` with no arguments exits with status 0." { @test "\`enabled\` with no arguments exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
} }
run "$_HOSTS" enabled run "${_HOSTS}" enabled
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`enabled\` with no arguments prints list of enabled records." { @test "\`enabled\` with no arguments prints list of enabled records." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
} }
run "$_HOSTS" enabled run "${_HOSTS}" enabled
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "127.0.0.1 localhost" ]] [[ "${lines[0]}" == "127.0.0.1 localhost" ]]
[[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]] [[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]]
[[ "${lines[2]}" == "::1 localhost" ]] [[ "${lines[2]}" == "::1 localhost" ]]
@ -39,14 +39,14 @@ load test_helper
# help ######################################################################## # help ########################################################################
@test "\`help enabled\` exits with status 0." { @test "\`help enabled\` exits with status 0." {
run "$_HOSTS" help enabled run "${_HOSTS}" help enabled
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`help enabled\` prints help information." { @test "\`help enabled\` prints help information." {
run "$_HOSTS" help enabled run "${_HOSTS}" help enabled
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts enabled" ]] [[ "${lines[1]}" == " hosts enabled" ]]
} }

View File

@ -3,26 +3,26 @@
load test_helper load test_helper
@test "\`file\` exits with status 0." { @test "\`file\` exits with status 0." {
run "$_HOSTS" file run "${_HOSTS}" file
[ "$status" -eq 0 ] [ "${status}" -eq 0 ]
} }
@test "\`file\` prints \$HOSTS_PATH contents." { @test "\`file\` prints \$HOSTS_PATH contents." {
run "$_HOSTS" file run "${_HOSTS}" file
[[ "$output" == "$(cat $HOSTS_PATH)" ]] [[ "${output}" == "$(cat ${HOSTS_PATH})" ]]
} }
# help ######################################################################## # help ########################################################################
@test "\`help file\` exits with status 0." { @test "\`help file\` exits with status 0." {
run "$_HOSTS" help file run "${_HOSTS}" help file
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`help file\` prints help information." { @test "\`help file\` prints help information." {
run "$_HOSTS" help file run "${_HOSTS}" help file
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts file" ]] [[ "${lines[1]}" == " hosts file" ]]
} }

View File

@ -14,27 +14,27 @@ HEREDOC
export _HELP_HEADER export _HELP_HEADER
@test "\`help\` with no arguments exits with status 0." { @test "\`help\` with no arguments exits with status 0." {
run "$_HOSTS" help run "${_HOSTS}" help
[ "$status" -eq 0 ] [ "${status}" -eq 0 ]
} }
@test "\`help\` with no arguments prints default help." { @test "\`help\` with no arguments prints default help." {
run "$_HOSTS" help run "${_HOSTS}" help
[[ $(IFS=$'\n'; echo "${lines[*]:0:5}") == "$_HELP_HEADER" ]] [[ $(IFS=$'\n'; echo "${lines[*]:0:5}") == "${_HELP_HEADER}" ]]
} }
@test "\`hosts -h\` prints default help." { @test "\`hosts -h\` prints default help." {
run "$_HOSTS" -h run "${_HOSTS}" -h
[[ $(IFS=$'\n'; echo "${lines[*]:0:5}") == "$_HELP_HEADER" ]] [[ $(IFS=$'\n'; echo "${lines[*]:0:5}") == "${_HELP_HEADER}" ]]
} }
@test "\`hosts --help\` prints default help." { @test "\`hosts --help\` prints default help." {
run "$_HOSTS" --help run "${_HOSTS}" --help
[[ $(IFS=$'\n'; echo "${lines[*]:0:5}") == "$_HELP_HEADER" ]] [[ $(IFS=$'\n'; echo "${lines[*]:0:5}") == "${_HELP_HEADER}" ]]
} }
@test "\`hosts help help\` prints \`help\` subcommand usage." { @test "\`hosts help help\` prints \`help\` subcommand usage." {
run "$_HOSTS" help help run "${_HOSTS}" help help
_expected="$( _expected="$(
cat <<HEREDOC cat <<HEREDOC
Usage: Usage:
@ -44,5 +44,5 @@ Description:
Display help information for hosts or a specified command. Display help information for hosts or a specified command.
HEREDOC HEREDOC
)" )"
[[ "$output" == "$_expected" ]] [[ "${output}" == "${_expected}" ]]
} }

View File

@ -3,12 +3,12 @@
load test_helper load test_helper
@test "\`hosts\` with no arguments exits with status 0." { @test "\`hosts\` with no arguments exits with status 0." {
run "$_HOSTS" run "${_HOSTS}"
[ "$status" -eq 0 ] [ "${status}" -eq 0 ]
} }
@test "\`hosts\` with no arguments prints enabled rules." { @test "\`hosts\` with no arguments prints enabled rules." {
run "$_HOSTS" run "${_HOSTS}"
[[ "${#lines[@]}" -eq 4 ]] [[ "${#lines[@]}" -eq 4 ]]
[[ "${lines[0]}" == "127.0.0.1 localhost" ]] [[ "${lines[0]}" == "127.0.0.1 localhost" ]]
[[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]] [[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]]

View File

@ -6,29 +6,29 @@ load test_helper
@test "\`list\` exits with status 0." { @test "\`list\` exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
} }
run "$_HOSTS" list run "${_HOSTS}" list
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`list\` prints lists of enabled and disabled records." { @test "\`list\` prints lists of enabled and disabled records." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
} }
run "$_HOSTS" list run "${_HOSTS}" list
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
_expected="\ _expected="\
127.0.0.1 localhost 127.0.0.1 localhost
255.255.255.255 broadcasthost 255.255.255.255 broadcasthost
@ -39,37 +39,37 @@ fe80::1%lo0 localhost
Disabled: Disabled:
0.0.0.0 example.com 0.0.0.0 example.com
0.0.0.0 example.net" 0.0.0.0 example.net"
_compare "'$_expected'" "'$output'" _compare "'${_expected}'" "'${output}'"
[[ "$output" == "$_expected" ]] [[ "${output}" == "${_expected}" ]]
} }
# `hosts list enabled` ######################################################## # `hosts list enabled` ########################################################
@test "\`list enabled\` exits with status 0." { @test "\`list enabled\` exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
} }
run "$_HOSTS" list enabled run "${_HOSTS}" list enabled
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`list enabled\` prints list of enabled records." { @test "\`list enabled\` prints list of enabled records." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
} }
run "$_HOSTS" list enabled run "${_HOSTS}" list enabled
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "127.0.0.1 localhost" ]] [[ "${lines[0]}" == "127.0.0.1 localhost" ]]
[[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]] [[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]]
[[ "${lines[2]}" == "::1 localhost" ]] [[ "${lines[2]}" == "::1 localhost" ]]
@ -81,29 +81,29 @@ Disabled:
@test "\`list disabled\` exits with status 0." { @test "\`list disabled\` exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.1 example.com run "${_HOSTS}" add 127.0.0.1 example.com
run "$_HOSTS" disable example.com run "${_HOSTS}" disable example.com
} }
run "$_HOSTS" disabled run "${_HOSTS}" disabled
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`list disabled\` prints list of disabled records." { @test "\`list disabled\` prints list of disabled records." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.1 example.com run "${_HOSTS}" add 127.0.0.1 example.com
run "$_HOSTS" disable example.com run "${_HOSTS}" disable example.com
} }
run "$_HOSTS" list disabled run "${_HOSTS}" list disabled
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.com" ]] [[ "${lines[0]}" == "0.0.0.0 example.com" ]]
[[ "${lines[1]}" == "127.0.0.1 example.com" ]] [[ "${lines[1]}" == "127.0.0.1 example.com" ]]
[[ "${lines[2]}" == "" ]] [[ "${lines[2]}" == "" ]]
@ -113,27 +113,27 @@ Disabled:
@test "\`list <search string>\` exits with status 0." { @test "\`list <search string>\` exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.1 example.com run "${_HOSTS}" add 127.0.0.1 example.com
} }
run "$_HOSTS" list example.com run "${_HOSTS}" list example.com
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`list <search string>\` prints list of matching records." { @test "\`list <search string>\` prints list of matching records." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.1 example.com run "${_HOSTS}" add 127.0.0.1 example.com
} }
run "$_HOSTS" list example.com run "${_HOSTS}" list example.com
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.com" ]] [[ "${lines[0]}" == "0.0.0.0 example.com" ]]
[[ "${lines[1]}" == "127.0.0.1 example.com" ]] [[ "${lines[1]}" == "127.0.0.1 example.com" ]]
[[ "${lines[2]}" == "" ]] [[ "${lines[2]}" == "" ]]
@ -142,14 +142,14 @@ Disabled:
# help ######################################################################## # help ########################################################################
@test "\`help list\` exits with status 0." { @test "\`help list\` exits with status 0." {
run "$_HOSTS" help list run "${_HOSTS}" help list
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`help list\` prints help information." { @test "\`help list\` prints help information." {
run "$_HOSTS" help list run "${_HOSTS}" help list
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts list [enabled | disabled | <search string>]" ]] [[ "${lines[1]}" == " hosts list [enabled | disabled | <search string>]" ]]
} }

View File

@ -5,25 +5,25 @@ load test_helper
# `hosts remove` ################################################################# # `hosts remove` #################################################################
@test "\`remove\` with no arguments exits with status 1." { @test "\`remove\` with no arguments exits with status 1." {
run "$_HOSTS" remove run "${_HOSTS}" remove
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 1 ]] [[ ${status} -eq 1 ]]
} }
@test "\`remove\` with no argument does not change the hosts file." { @test "\`remove\` with no argument does not change the hosts file." {
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" remove run "${_HOSTS}" remove
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]] [[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
} }
@test "\`remove\` with no arguments prints help information." { @test "\`remove\` with no arguments prints help information." {
run "$_HOSTS" remove run "${_HOSTS}" remove
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts remove (<ip> | <hostname> | <search string>) [--force]" ]] [[ "${lines[1]}" == " hosts remove (<ip> | <hostname> | <search string>) [--force]" ]]
} }
@ -32,56 +32,56 @@ load test_helper
@test "\`remove <invalid> --force\` exits with status 1." { @test "\`remove <invalid> --force\` exits with status 1." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
} }
run "$_HOSTS" remove 127.0.0.3 --force run "${_HOSTS}" remove 127.0.0.3 --force
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 1 ]] [[ ${status} -eq 1 ]]
} }
@test "\`remove <invalid> --force\` prints error message." { @test "\`remove <invalid> --force\` prints error message." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
} }
run "$_HOSTS" remove 127.0.0.3 --force run "${_HOSTS}" remove 127.0.0.3 --force
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $output == "No matching records found." ]] [[ ${output} == "No matching records found." ]]
} }
# `hosts remove <ip> --force` ################################################# # `hosts remove <ip> --force` #################################################
@test "\`remove <ip> --force\` exits with status 0." { @test "\`remove <ip> --force\` exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
} }
run "$_HOSTS" remove 127.0.0.2 --force run "${_HOSTS}" remove 127.0.0.2 --force
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`remove <ip> --force\` updates the hosts file." { @test "\`remove <ip> --force\` updates the hosts file." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
} }
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" remove 127.0.0.2 --force run "${_HOSTS}" remove 127.0.0.2 --force
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}")" == "0.0.0.0 example.net" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "0.0.0.0 example.net" ]]
@ -90,17 +90,17 @@ load test_helper
@test "\`remove <ip>\` removes all matches." { @test "\`remove <ip>\` removes all matches." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 0.0.0.0 example.dev run "${_HOSTS}" add 0.0.0.0 example.dev
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable example.dev run "${_HOSTS}" disable example.dev
} }
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" remove 0.0.0.0 --force run "${_HOSTS}" remove 0.0.0.0 --force
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
_expected="\ _expected="\
## ##
# Host Database # Host Database
@ -113,21 +113,21 @@ load test_helper
::1 localhost ::1 localhost
fe80::1%lo0 localhost fe80::1%lo0 localhost
127.0.0.2 example.com" 127.0.0.2 example.com"
_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}" ]]
} }
@test "\`remove <ip>\` prints feedback." { @test "\`remove <ip>\` prints feedback." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
} }
run "$_HOSTS" remove 127.0.0.2 --force run "${_HOSTS}" remove 127.0.0.2 --force
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 example.com" ]]
} }
@ -136,28 +136,28 @@ fe80::1%lo0 localhost
@test "\`remove <hostname> --force\` exits with status 0." { @test "\`remove <hostname> --force\` exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
} }
run "$_HOSTS" remove example.com --force run "${_HOSTS}" remove example.com --force
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`remove <hostname> --force\` updates the hosts file." { @test "\`remove <hostname> --force\` updates the hosts file." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
} }
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" remove example.com --force run "${_HOSTS}" remove example.com --force
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 example.net" ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "" ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "" ]]
@ -165,17 +165,17 @@ fe80::1%lo0 localhost
@test "\`remove <hostname>\` removes all matches." { @test "\`remove <hostname>\` removes all matches." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 0.0.0.0 example.dev run "${_HOSTS}" add 0.0.0.0 example.dev
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable example.dev run "${_HOSTS}" disable example.dev
} }
_original="$(cat "${HOSTS_PATH}")" _original="$(cat "${HOSTS_PATH}")"
run "$_HOSTS" remove example.com --force run "${_HOSTS}" remove example.com --force
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
_expected="\ _expected="\
## ##
# Host Database # Host Database
@ -189,39 +189,39 @@ fe80::1%lo0 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}" ]]
} }
@test "\`remove <hostname>\` prints feedback." { @test "\`remove <hostname>\` prints feedback." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
} }
run "$_HOSTS" remove example.com --force run "${_HOSTS}" remove example.com --force
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
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}" ]]
} }
# help ######################################################################## # help ########################################################################
@test "\`help remove\` exits with status 0." { @test "\`help remove\` exits with status 0." {
run "$_HOSTS" help remove run "${_HOSTS}" help remove
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`help remove\` prints help information." { @test "\`help remove\` prints help information." {
run "$_HOSTS" help remove run "${_HOSTS}" help remove
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts remove (<ip> | <hostname> | <search string>) [--force]" ]] [[ "${lines[1]}" == " hosts remove (<ip> | <hostname> | <search string>) [--force]" ]]
} }

View File

@ -5,16 +5,16 @@ load test_helper
# `hosts show` ############################################################## # `hosts show` ##############################################################
@test "\`show\` with no arguments exits with status 1." { @test "\`show\` with no arguments exits with status 1." {
run "$_HOSTS" show run "${_HOSTS}" show
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 1 ]] [[ ${status} -eq 1 ]]
} }
@test "\`show\` with no arguments prints help information." { @test "\`show\` with no arguments prints help information." {
run "$_HOSTS" show run "${_HOSTS}" show
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts show (<ip> | <hostname> | <search string>)" ]] [[ "${lines[1]}" == " hosts show (<ip> | <hostname> | <search string>)" ]]
} }
@ -23,29 +23,29 @@ load test_helper
@test "\`show <ip>\` exits with status 0." { @test "\`show <ip>\` exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable example.com run "${_HOSTS}" disable example.com
} }
run "$_HOSTS" show 0.0.0.0 run "${_HOSTS}" show 0.0.0.0
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`enable <ip>\` shows all matches." { @test "\`enable <ip>\` shows all matches." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable example.com run "${_HOSTS}" disable example.com
} }
run "$_HOSTS" show 0.0.0.0 run "${_HOSTS}" show 0.0.0.0
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.net" ]] [[ "${lines[0]}" == "0.0.0.0 example.net" ]]
[[ "${lines[1]}" == "disabled: 0.0.0.0 example.com" ]] [[ "${lines[1]}" == "disabled: 0.0.0.0 example.com" ]]
@ -55,29 +55,29 @@ load test_helper
@test "\`show <hostname>\` exits with status 0." { @test "\`show <hostname>\` exits with status 0." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
} }
run "$_HOSTS" show example.com run "${_HOSTS}" show example.com
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`enable <hostname>\` shows all matches." { @test "\`enable <hostname>\` shows all matches." {
{ {
run "$_HOSTS" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "$_HOSTS" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
run "$_HOSTS" add 127.0.0.2 example.com run "${_HOSTS}" add 127.0.0.2 example.com
run "$_HOSTS" disable 0.0.0.0 run "${_HOSTS}" disable 0.0.0.0
} }
run "$_HOSTS" show example.com run "${_HOSTS}" show example.com
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "127.0.0.2 example.com" ]] [[ "${lines[0]}" == "127.0.0.2 example.com" ]]
[[ "${lines[1]}" == "disabled: 0.0.0.0 example.com" ]] [[ "${lines[1]}" == "disabled: 0.0.0.0 example.com" ]]
@ -86,14 +86,14 @@ load test_helper
# help ######################################################################## # help ########################################################################
@test "\`help show\` exits with status 0." { @test "\`help show\` exits with status 0." {
run "$_HOSTS" help show run "${_HOSTS}" help show
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`help show\` prints help information." { @test "\`help show\` prints help information." {
run "$_HOSTS" help show run "${_HOSTS}" help show
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts show (<ip> | <hostname> | <search string>)" ]] [[ "${lines[1]}" == " hosts show (<ip> | <hostname> | <search string>)" ]]
} }

View File

@ -3,38 +3,38 @@
load test_helper load test_helper
@test "\`hosts version\` returns with 0 status." { @test "\`hosts version\` returns with 0 status." {
run "$_HOSTS" version run "${_HOSTS}" version
[[ "$status" -eq 0 ]] [[ "${status}" -eq 0 ]]
} }
@test "\`hosts version\` prints a version number." { @test "\`hosts version\` prints a version number." {
run "$_HOSTS" version run "${_HOSTS}" version
printf "'%s'" "$output" printf "'%s'" "${output}"
echo "$output" | grep -q '\d\+\.\d\+\.\d\+' echo "${output}" | grep -q '\d\+\.\d\+\.\d\+'
} }
@test "\`hosts --version\` returns with 0 status." { @test "\`hosts --version\` returns with 0 status." {
run "$_HOSTS" --version run "${_HOSTS}" --version
[[ "$status" -eq 0 ]] [[ "${status}" -eq 0 ]]
} }
@test "\`hosts --version\` prints a version number." { @test "\`hosts --version\` prints a version number." {
run "$_HOSTS" --version run "${_HOSTS}" --version
printf "'%s'" "$output" printf "'%s'" "${output}"
echo "$output" | grep -q '\d\+\.\d\+\.\d\+' echo "${output}" | grep -q '\d\+\.\d\+\.\d\+'
} }
# help ######################################################################## # help ########################################################################
@test "\`help version\` exits with status 0." { @test "\`help version\` exits with status 0." {
run "$_HOSTS" help version run "${_HOSTS}" help version
[[ $status -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`help version\` prints help information." { @test "\`help version\` prints help information." {
run "$_HOSTS" help version run "${_HOSTS}" help version
printf "\$status: %s\n" "$status" printf "\${status}: %s\n" "${status}"
printf "\$output: '%s'\n" "$output" printf "\${output}: '%s'\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts (version | --version)" ]] [[ "${lines[1]}" == " hosts (version | --version)" ]]
} }