1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-12-28 04:05:00 +00:00

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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