1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-06-01 22:10:51 +00:00
hosts/test/block.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

78 lines
2.4 KiB
Bash

#!/usr/bin/env bats
load test_helper
# `hosts block` #################################################################
@test "\`block\` with no arguments exits with status 1." {
run "${_HOSTS}" block
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]]
}
@test "\`block\` with no argument does not change the hosts file." {
_original="$(cat "${HOSTS_PATH}")"
run "${_HOSTS}" block
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
}
@test "\`block\` with no arguments prints help information." {
run "${_HOSTS}" block
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts block <hostname>" ]]
}
# `hosts block <hostname>` #################################################
@test "\`block <hostname>\` exits with status 0." {
run "${_HOSTS}" block example.com
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 0 ]]
}
@test "\`block <hostname>\` adds entries to the hosts file." {
_original="$(cat "${HOSTS_PATH}")"
run "${_HOSTS}" block example.com
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
_compare '127.0.0.1 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "127.0.0.1 example.com" ]]
}
@test "\`block <hostname>\` prints feedback." {
run "${_HOSTS}" block example.com
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Added:" ]]
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
[[ "${lines[2]}" == "Added:" ]]
[[ "${lines[3]}" == "fe80::1%lo0 example.com" ]]
[[ "${lines[4]}" == "Added:" ]]
[[ "${lines[5]}" == "::1 example.com" ]]
}
# help ########################################################################
@test "\`help block\` exits with status 0." {
run "${_HOSTS}" help block
[[ ${status} -eq 0 ]]
}
@test "\`help block\` prints help information." {
run "${_HOSTS}" help block
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts block <hostname>" ]]
}