diff --git a/test/add.bats b/test/add.bats index 1f73bcd..506a8e9 100644 --- a/test/add.bats +++ b/test/add.bats @@ -84,6 +84,23 @@ load test_helper [[ "${lines[1]}" == "0.0.0.0 example.com" ]] } +@test "\`add \` doesn't add duplicate entry." { + _original="$(cat "${HOSTS_PATH}")" + { + run "${_HOSTS}" add 0.0.0.0 example.com + } + _modified="$(cat "${HOSTS_PATH}")" + + 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}" ]] + [[ "$(cat "${HOSTS_PATH}")" == "${_modified}" ]] + [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]] +} + # `hosts add [comment]` ####################################### @test "\`add [comment]\` exits with status 0." { @@ -104,6 +121,43 @@ load test_helper "0.0.0.0 example.com # Example multi-word comment." ]] } +@test "\`add [comment]\` doesn't add duplicate entry." { + _original="$(cat "${HOSTS_PATH}")" + { + run "${_HOSTS}" add 0.0.0.0 example.com + } + _modified="$(cat "${HOSTS_PATH}")" + + run "${_HOSTS}" add 0.0.0.0 example.com 'Example multi-word comment.' + 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}" ]] + [[ "$(cat "${HOSTS_PATH}")" == "${_modified}" ]] + [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]] + [[ "$(sed -n '11p' "${HOSTS_PATH}")" != \ + "0.0.0.0 example.com # Example multi-word comment." ]] +} + +@test "\`add [comment]\` doesn't add duplicate commented entry." { + _original="$(cat "${HOSTS_PATH}")" + { + run "${_HOSTS}" add 0.0.0.0 example.com 'Example multi-word comment.' + } + _modified="$(cat "${HOSTS_PATH}")" + + run "${_HOSTS}" add 0.0.0.0 example.com 'Example multi-word comment.' + 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}" ]] + [[ "$(cat "${HOSTS_PATH}")" == "${_modified}" ]] + [[ "$(sed -n '11p' "${HOSTS_PATH}")" == \ + "0.0.0.0 example.com # Example multi-word comment." ]] +} + @test "\`add [comment]\` prints feedback." { run "${_HOSTS}" add 0.0.0.0 example.com 'Example multi-word comment.' printf "\${status}: %s\\n" "${status}"