From 789a0797c244e809e93269b317641f198f9c3b20 Mon Sep 17 00:00:00 2001 From: William Melody Date: Tue, 14 Aug 2018 11:14:55 -0700 Subject: [PATCH] Improve search behavior. Search comments in addition to IPs and hostnames. Call grep twice, excluding commented lines first before searching. --- hosts | 2 +- test/list.bats | 14 ++++++++++++++ test/search.bats | 14 ++++++++++++++ test/show.bats | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/hosts b/hosts index 9e7bf09..b10444b 100755 --- a/hosts +++ b/hosts @@ -1173,7 +1173,7 @@ show() { local _enabled_records _enabled_records=$( - grep "^[^#]*${1}" "${HOSTS_PATH}" + grep --invert-match "^#" "${HOSTS_PATH}" | grep "${1}" ) # Output disabled records secondly for better organization. diff --git a/test/list.bats b/test/list.bats index 1785051..b7dd577 100644 --- a/test/list.bats +++ b/test/list.bats @@ -139,6 +139,20 @@ Disabled: [[ "${lines[2]}" == "" ]] } +@test "\`search \` prints records with matching comments." { + { + run "${_HOSTS}" add 0.0.0.0 example.com + run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment" + run "${_HOSTS}" add 127.0.0.1 example.com + } + + run "${_HOSTS}" list "Comment" + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]] + [[ "${lines[2]}" == "" ]] +} + # help ######################################################################## @test "\`help list\` exits with status 0." { diff --git a/test/search.bats b/test/search.bats index d320cb8..e821907 100644 --- a/test/search.bats +++ b/test/search.bats @@ -135,6 +135,20 @@ Description: [[ "${lines[2]}" == "" ]] } +@test "\`search \` prints records with matching comments." { + { + run "${_HOSTS}" add 0.0.0.0 example.com + run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment" + run "${_HOSTS}" add 127.0.0.1 example.com + } + + run "${_HOSTS}" search "Comment" + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]] + [[ "${lines[2]}" == "" ]] +} + # help ######################################################################## @test "\`help search\` exits with status 0." { diff --git a/test/show.bats b/test/show.bats index 15bc2bd..4b8fd54 100644 --- a/test/show.bats +++ b/test/show.bats @@ -35,7 +35,7 @@ load test_helper [[ ${status} -eq 0 ]] } -@test "\`enable \` shows all matches." { +@test "\`show \` shows all matches." { { run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.net @@ -67,7 +67,7 @@ load test_helper [[ ${status} -eq 0 ]] } -@test "\`enable \` shows all matches." { +@test "\`show \` shows all matches." { { run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.net @@ -83,6 +83,50 @@ load test_helper [[ "${lines[1]}" == "disabled: 0.0.0.0 example.com" ]] } +# `hosts show ` ################################################ + +@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.1 example.com + } + + run "${_HOSTS}" show example.com + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ ${status} -eq 0 ]] +} + +@test "\`list \` 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}" show 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]}" == "" ]] +} + +@test "\`search \` prints records with matching comments." { + { + run "${_HOSTS}" add 0.0.0.0 example.com + run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment" + run "${_HOSTS}" add 127.0.0.1 example.com + } + + run "${_HOSTS}" show "Comment" + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]] + [[ "${lines[2]}" == "" ]] +} + # help ######################################################################## @test "\`help show\` exits with status 0." {