From 41a63ddf494ef0fb242d9899161ce141818d8b71 Mon Sep 17 00:00:00 2001 From: William Melody Date: Tue, 26 Jan 2016 18:26:49 -0800 Subject: [PATCH] Add disable.bats. --- test/disable.bats | 154 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 test/disable.bats diff --git a/test/disable.bats b/test/disable.bats new file mode 100644 index 0000000..c7caddb --- /dev/null +++ b/test/disable.bats @@ -0,0 +1,154 @@ +#!/usr/bin/env bats + +load test_helper + +# `hosts disable` ############################################################# + +@test "\`disable\` with no arguments exits with status 1." { + { + run "$_HOSTS" add 0.0.0.0 example.com + } + + run "$_HOSTS" disable + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + [[ $status -eq 1 ]] +} + +@test "\`disable\` with no argument does not change the hosts file." { + { + run "$_HOSTS" add 0.0.0.0 example.com + } + _original="$(cat "${HOSTS_PATH}")" + + run "$_HOSTS" disable + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + [[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]] +} + +@test "\`disable\` with no arguments prints help information." { + { + run "$_HOSTS" add 0.0.0.0 example.com + } + + run "$_HOSTS" disable + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + [[ "${lines[0]}" == "Usage:" ]] + [[ "${lines[1]}" == " hosts disable ( | | )" ]] +} + +# `hosts disable ` ######################################################## + +@test "\`disable \` exits with status 0." { + { + run "$_HOSTS" add 0.0.0.0 example.com + run "$_HOSTS" add 127.0.0.1 example.net + } + + run "$_HOSTS" disable 0.0.0.0 + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + [[ $status -eq 0 ]] +} + +@test "\`disable \` updates the hosts file." { + { + run "$_HOSTS" add 0.0.0.0 example.com + run "$_HOSTS" add 127.0.0.1 example.net + } + _original="$(cat "${HOSTS_PATH}")" + + run "$_HOSTS" disable 0.0.0.0 + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + _compare "${_original}" "$(cat "${HOSTS_PATH}")" + [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]] + [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]] +} + +@test "\`disable \` disables all matches." { + { + run "$_HOSTS" add 0.0.0.0 example.com + run "$_HOSTS" add 0.0.0.0 example.net + } + _original="$(cat "${HOSTS_PATH}")" + + run "$_HOSTS" disable 0.0.0.0 + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + _compare "${_original}" "$(cat "${HOSTS_PATH}")" + [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]] + [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.net" ]] +} + +@test "\`disable \` prints feedback." { + { + run "$_HOSTS" add 0.0.0.0 example.com + run "$_HOSTS" add 127.0.0.1 example.net + } + + run "$_HOSTS" disable 0.0.0.0 + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + [[ "${lines[0]}" == "Disabling:" ]] + [[ "${lines[1]}" == "0.0.0.0 example.com" ]] +} + +# `hosts disable ` ################################################## + +@test "\`disable \` exits with status 0." { + { + run "$_HOSTS" add 0.0.0.0 example.com + run "$_HOSTS" add 127.0.0.1 example.net + } + + run "$_HOSTS" disable example.com + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + [[ $status -eq 0 ]] +} + +@test "\`disable \` updates the hosts file." { + { + run "$_HOSTS" add 0.0.0.0 example.com + run "$_HOSTS" add 127.0.0.1 example.net + } + _original="$(cat "${HOSTS_PATH}")" + + run "$_HOSTS" disable example.com + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + _compare "${_original}" "$(cat "${HOSTS_PATH}")" + [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]] + [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]] +} + +@test "\`disable \` disables all matches." { + { + run "$_HOSTS" add 0.0.0.0 example.com + run "$_HOSTS" add 127.0.0.1 example.com + } + _original="$(cat "${HOSTS_PATH}")" + + run "$_HOSTS" disable example.com + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + _compare "${_original}" "$(cat "${HOSTS_PATH}")" + [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]] + [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 127.0.0.1 example.com" ]] +} + +@test "\`disable \` prints feedback." { + { + run "$_HOSTS" add 0.0.0.0 example.com + run "$_HOSTS" add 127.0.0.1 example.net + } + + run "$_HOSTS" disable example.com + printf "\$status: %s\n" "$status" + printf "\$output: '%s'\n" "$output" + [[ "${lines[0]}" == "Disabling:" ]] + [[ "${lines[1]}" == "0.0.0.0 example.com" ]] +}