From 4330a73c3c567420c915507a0815838b0462b5f1 Mon Sep 17 00:00:00 2001 From: William Melody Date: Tue, 26 Jan 2016 19:35:04 -0800 Subject: [PATCH] Add remove.bats. --- test/remove.bats | 199 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 test/remove.bats diff --git a/test/remove.bats b/test/remove.bats new file mode 100644 index 0000000..7ef0103 --- /dev/null +++ b/test/remove.bats @@ -0,0 +1,199 @@ +#!/usr/bin/env bats + +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 ]] +} + +@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" + [[ "$(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" + [[ "${lines[0]}" == "Usage:" ]] + [[ "${lines[1]}" == " hosts remove ( | | ) [--force]" ]] +} + +# `hosts remove --force` ################################################# + +@test "\`remove --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" remove 127.0.0.2 --force + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + [[ $status -eq 0 ]] +} + +@test "\`remove --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 + } + _original="$(cat "${HOSTS_PATH}")" + + 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" ]] + [[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]] +} + +@test "\`remove \` 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 + } + _original="$(cat "${HOSTS_PATH}")" + + run "$_HOSTS" remove 0.0.0.0 --force + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + _expected="\ +## +# Host Database +# +# localhost is used to configure the loopback interface +# when the system is booting. Do not change this entry. +## +127.0.0.1 localhost +255.255.255.255 broadcasthost +::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" ]] +} + +@test "\`remove \` 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" 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" ]] +} + +# `hosts remove --force` ########################################### + +@test "\`remove --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" remove example.com --force + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + [[ $status -eq 0 ]] +} + +@test "\`remove --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 + } + _original="$(cat "${HOSTS_PATH}")" + + 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}")" == "" ]] +} + +@test "\`remove \` 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 + } + _original="$(cat "${HOSTS_PATH}")" + + run "$_HOSTS" remove example.com --force + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + _expected="\ +## +# Host Database +# +# localhost is used to configure the loopback interface +# when the system is booting. Do not change this entry. +## +127.0.0.1 localhost +255.255.255.255 broadcasthost +::1 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" ]] +} + +@test "\`remove \` 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" 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" ]] +} + +# help ######################################################################## + +@test "\`help remove\` exits with status 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" + [[ "${lines[0]}" == "Usage:" ]] + [[ "${lines[1]}" == " hosts remove ( | | ) [--force]" ]] +}