2017-03-01 21:43:10 +00:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
|
|
|
|
load test_helper
|
|
|
|
|
|
|
|
# `hosts block` #################################################################
|
|
|
|
|
|
|
|
@test "\`block\` with no arguments exits with status 1." {
|
|
|
|
run "${_HOSTS}" block
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ ${status} -eq 1 ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`block\` with no argument does not change the hosts file." {
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
|
|
|
run "${_HOSTS}" block
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`block\` with no arguments prints help information." {
|
|
|
|
run "${_HOSTS}" block
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ "${lines[0]}" == "Usage:" ]]
|
2020-03-15 20:14:26 +00:00
|
|
|
[[ "${lines[1]}" == " hosts block <hostname>..." ]]
|
2017-03-01 21:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# `hosts block <hostname>` #################################################
|
|
|
|
|
|
|
|
@test "\`block <hostname>\` exits with status 0." {
|
|
|
|
run "${_HOSTS}" block example.com
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ ${status} -eq 0 ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`block <hostname>\` adds entries to the hosts file." {
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
|
|
|
run "${_HOSTS}" block example.com
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
|
|
|
_compare '127.0.0.1 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
|
|
|
|
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example.com ]]
|
2017-03-01 21:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`block <hostname>\` prints feedback." {
|
|
|
|
run "${_HOSTS}" block example.com
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ "${lines[0]}" == "Added:" ]]
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]]
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ "${lines[2]}" == "Added:" ]]
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]example.com ]]
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ "${lines[4]}" == "Added:" ]]
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "${lines[5]}" =~ \:\:1[[:space:]]+example.com ]]
|
2017-03-01 21:43:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 20:14:26 +00:00
|
|
|
# `hosts block <hostname> <hostname2>` ########################################
|
|
|
|
|
|
|
|
@test "\`block <hostname> <hostname2>\` exits with status 0." {
|
|
|
|
run "${_HOSTS}" block example.com example2.com
|
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
|
|
|
[[ ${status} -eq 0 ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`block <hostname> <hostname2>\` adds entries to the hosts file." {
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
|
|
|
run "${_HOSTS}" block example.com example2.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}")"
|
|
|
|
_compare '127.0.0.1 example2.com' "$(sed -n '11p' "${HOSTS_PATH}")"
|
|
|
|
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example.com ]]
|
|
|
|
[[ "$(sed -n '14p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example2.com ]]
|
2020-03-15 20:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`block <hostname> <hostname2>\` prints feedback." {
|
|
|
|
run "${_HOSTS}" block example.com example2.com
|
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
|
|
|
[[ "${lines[0]}" == "Added:" ]]
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]]
|
2020-03-15 20:14:26 +00:00
|
|
|
[[ "${lines[2]}" == "Added:" ]]
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+example.com ]]
|
2020-03-15 20:14:26 +00:00
|
|
|
[[ "${lines[4]}" == "Added:" ]]
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "${lines[5]}" =~ \:\:1[[:space:]]+example.com ]]
|
2020-03-15 20:14:26 +00:00
|
|
|
[[ "${lines[6]}" == "Added:" ]]
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "${lines[7]}" =~ 127.0.0.1[[:space:]]+example2.com ]]
|
2020-03-15 20:14:26 +00:00
|
|
|
[[ "${lines[8]}" == "Added:" ]]
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "${lines[9]}" =~ fe80\:\:1\%lo0[[:space:]]+example2.com ]]
|
2020-03-15 20:14:26 +00:00
|
|
|
[[ "${lines[10]}" == "Added:" ]]
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "${lines[11]}" =~ \:\:1[[:space:]]+example2.com ]]
|
2020-03-15 20:14:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 21:43:10 +00:00
|
|
|
# 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
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ "${lines[0]}" == "Usage:" ]]
|
2020-03-15 20:14:26 +00:00
|
|
|
[[ "${lines[1]}" == " hosts block <hostname>..." ]]
|
2017-03-01 21:43:10 +00:00
|
|
|
}
|