From 6b72e51651e1aba026a76463ace1cf7313c1b3c1 Mon Sep 17 00:00:00 2001 From: William Melody Date: Tue, 28 May 2019 18:18:29 -0700 Subject: [PATCH] Add tests for duplicate entry guard. Deplicate entries are avoided by a guard in the `add` subcommand. Add tests to ensure that duplicate entries are not created. --- test/add.bats | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) 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}"