1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-12-29 12:32:44 +00:00
hosts/test/disable.bats
William Melody 80edd464b6 Use explicit escaping for "\\n" newlines.
Backslash is literal, so explicitly escape it rather than rely on
fallback behavior.

ShellCheck SC1117
https://github.com/koalaman/shellcheck/wiki/SC1117
2018-04-15 14:55:26 -07:00

159 lines
4.8 KiB
Bash

#!/usr/bin/env bats
load test_helper
# `hosts disable` #############################################################
@test "\`disable\` with no arguments exits with status 1." {
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." {
_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}" disable
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts disable (<ip> | <hostname> | <search string>)" ]]
}
# `hosts disable <ip>` ########################################################
@test "\`disable <ip>\` 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 <ip>\` 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 <ip>\` 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 <ip>\` 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 <hostname>` ##################################################
@test "\`disable <hostname>\` 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 <hostname>\` 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 <hostname>\` 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 <hostname>\` 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" ]]
}
# help ########################################################################
@test "\`help disable\` exits with status 0." {
run "${_HOSTS}" help disable
[[ ${status} -eq 0 ]]
}
@test "\`help disable\` prints help information." {
run "${_HOSTS}" help disable
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts disable (<ip> | <hostname> | <search string>)" ]]
}