diff --git a/README.md b/README.md index d9a5275..1c41e0f 100644 --- a/README.md +++ b/README.md @@ -512,6 +512,10 @@ Usage: Description: List all disabled records. This is an alias for `hosts list disabled`. + +Exit status: + 0 One or more disabled entries found. + 1 Invalid parameters or no disabled entries found. ``` ### `hosts edit` @@ -547,6 +551,10 @@ Usage: Description: List all enabled records. This is an alias for `hosts list enabled`. + +Exit status: + 0 One or more enabled entries found. + 1 Invalid parameters or no enabled entries found. ``` ### `hosts file` @@ -579,6 +587,10 @@ Description: List the existing IP / hostname pairs, optionally limited to a specified state. When provided with a seach string, all matching enabled records will be printed. + +Exit status: + 0 One or more matching entries found. + 1 Invalid parameters or entry not found. ``` ### `hosts remove` diff --git a/hosts b/hosts index a516d1f..83a3faa 100755 --- a/hosts +++ b/hosts @@ -1131,6 +1131,10 @@ Usage: Description: List all disabled records. This is an alias for \`hosts list disabled\`. + +Exit status: + 0 One or more disabled entries found. + 1 Invalid parameters or no disabled entries found. HEREDOC disabled() { list disabled @@ -1230,6 +1234,10 @@ Usage: Description: List all enabled records. This is an alias for \`hosts list enabled\`. + +Exit status: + 0 One or more enabled entries found. + 1 Invalid parameters or no enabled entries found. HEREDOC enabled() { list enabled @@ -1258,6 +1266,10 @@ Description: List the existing IP / hostname pairs, optionally limited to a specified state. When provided with a seach string, all matching enabled records will be printed. + +Exit status: + 0 One or more matching entries found. + 1 Invalid parameters or entry not found. HEREDOC list() { local _disabled_records @@ -1265,22 +1277,30 @@ list() { sed -n "s/^\\#disabled: \\(.*\\)$/\\1/p" "${HOSTS_PATH}" ) + # NOTE: use separate expressions since using `|` for the 'or' results in + # inconsistent behavior. + local _enabled_records + _enabled_records="$( + grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}" + )" + if [[ -n "${1:-}" ]] then if [[ "${1}" == "disabled" ]] then + [[ -z "${_disabled_records}" ]] && return 1 _print_entries "${_disabled_records}" elif [[ "${1}" == "enabled" ]] then - _print_entries "$(grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}")" + [[ -z "${_enabled_records}" ]] && return 1 + _print_entries "${_enabled_records}" else show "${1}" fi else - # NOTE: use separate expressions since using `|` for the 'or' results in - # inconsistent behavior. - local _enabled_records - _enabled_records="$(grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}")" + [[ -z "${_enabled_records}" ]] && + [[ -z "${_enabled_records}" ]] && + return 1 _print_entries "${_enabled_records:-}" diff --git a/test/disabled.bats b/test/disabled.bats index 5a93525..aff2e0b 100644 --- a/test/disabled.bats +++ b/test/disabled.bats @@ -34,6 +34,13 @@ load test_helper [[ "${lines[2]}" == "" ]] } +@test "\`disabled\` exits with status 1 when no matching entries found." { + run "${_HOSTS}" disabled + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ ${status} -eq 1 ]] +} + # help ######################################################################## @test "\`help disabled\` exits with status 0." { diff --git a/test/enabled.bats b/test/enabled.bats index 211b2fa..b6529b8 100644 --- a/test/enabled.bats +++ b/test/enabled.bats @@ -36,6 +36,18 @@ load test_helper [[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]] } +@test "\`enabled\` exits with status 1 when no matching entries found." { + { + run "${_HOSTS}" disable localhost + run "${_HOSTS}" disable broadcasthost + } + + run "${_HOSTS}" enabled + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ ${status} -eq 1 ]] +} + # help ######################################################################## @test "\`help enabled\` exits with status 0." { diff --git a/test/list.bats b/test/list.bats index b189d71..8da5b86 100644 --- a/test/list.bats +++ b/test/list.bats @@ -18,7 +18,7 @@ load test_helper [[ ${status} -eq 0 ]] } -@test "\`list\` prints lists of enabled and disabled records." { +@test "\`list\` prints lists of enabled and disabled entries." { { run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.net @@ -60,7 +60,7 @@ Disabled: [[ ${status} -eq 0 ]] } -@test "\`list enabled\` prints list of enabled records." { +@test "\`list enabled\` prints list of enabled entries." { { run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.net @@ -78,6 +78,18 @@ Disabled: [[ "${lines[4]}" =~ 127\.0\.0\.2[[:space:]]+example.com ]] } +@test "\`list enabled\` exits with status 1 when no matching entries found." { + { + run "${_HOSTS}" disable localhost + run "${_HOSTS}" disable broadcasthost + } + + run "${_HOSTS}" list enabled + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ ${status} -eq 1 ]] +} + # `hosts list disabled` ####################################################### @test "\`list disabled\` exits with status 0." { @@ -94,7 +106,7 @@ Disabled: [[ ${status} -eq 0 ]] } -@test "\`list disabled\` prints list of disabled records." { +@test "\`list disabled\` prints list of disabled entries." { { run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.net @@ -110,6 +122,13 @@ Disabled: [[ "${lines[2]}" == "" ]] } +@test "\`list disabled\` exits with status 1 when no matching entries found." { + run "${_HOSTS}" list disabled + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ ${status} -eq 1 ]] +} + # `hosts list ` ################################################ @test "\`list \` exits with status 0." { @@ -125,7 +144,7 @@ Disabled: [[ ${status} -eq 0 ]] } -@test "\`list \` prints list of matching records." { +@test "\`list \` prints list of matching entries." { { run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.net @@ -140,7 +159,7 @@ Disabled: [[ "${lines[2]}" == "" ]] } -@test "\`list \` prints records with matching comments." { +@test "\`list \` prints entries with matching comments." { { run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment" @@ -155,7 +174,7 @@ Disabled: } -@test "\`list \` prints disabled records with matching comments." { +@test "\`list \` prints disabled entries with matching comments." { { run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment" @@ -172,6 +191,13 @@ Disabled: [[ "${lines[4]}" == "" ]] } +@test "\`list \` exits with status 1 when no matching entries found." { + run "${_HOSTS}" list query-that-matches-no-entries + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ ${status} -eq 1 ]] +} + # help ######################################################################## @test "\`help list\` exits with status 0." { diff --git a/test/search.bats b/test/search.bats index e565853..96af481 100644 --- a/test/search.bats +++ b/test/search.bats @@ -29,18 +29,8 @@ load test_helper run "${_HOSTS}" search printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - _expected="\ -Usage: - hosts search - -Description: - Search entries for . - -Exit status: - 0 One or more matching entries found. - 1 Invalid parameters or entry not found." - _compare "'${_expected}'" "'${output}'" - [[ "${output}" == "${_expected}" ]] + [[ "${lines[0]}" == "Usage:" ]] + [[ "${lines[1]}" == " hosts search " ]] } # `hosts search enabled` ###################################################### @@ -77,6 +67,18 @@ Exit status: [[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]] } +@test "\`search enabled\` exits with status 1 when no matching entries found." { + { + run "${_HOSTS}" disable localhost + run "${_HOSTS}" disable broadcasthost + } + + run "${_HOSTS}" search enabled + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ ${status} -eq 1 ]] +} + # `hosts search disabled` ##################################################### @test "\`search disabled\` exits with status 0." { @@ -109,6 +111,13 @@ Exit status: [[ "${lines[2]}" == "" ]] } +@test "\`search disabled\` exits with status 1 when no matching entries found." { + run "${_HOSTS}" search disabled + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ ${status} -eq 1 ]] +} + # `hosts search ` ################################################ @test "\`search \` exits with status 0." { @@ -170,6 +179,13 @@ Exit status: [[ "${lines[4]}" == "" ]] } +@test "\`search \` exits with status 1 when no matching entries found." { + run "${_HOSTS}" search query-that-matches-no-entries + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ ${status} -eq 1 ]] +} + # help ######################################################################## @test "\`help search\` exits with status 0." {