From 21bc9ed48bf80aa8de217699efe0be36daca1508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Molinero=20Fern=C3=A1ndez?= Date: Sun, 25 Oct 2020 21:28:32 +0100 Subject: [PATCH] Added some tests --- .gitlab-ci.yml | 4 +-- Makefile | 12 ++++++-- hblock | 4 +-- resources/tests/env.sh | 41 +++++++++++++++++++++++++++ resources/tests/sources.txt | 40 ++++++++++++++++++++++++++ resources/tests/test-000.out | 21 ++++++++++++++ resources/tests/test-000.sh | 32 +++++++++++++++++++++ resources/tests/test-001.out | 23 +++++++++++++++ resources/tests/test-001.sh | 32 +++++++++++++++++++++ resources/tests/test-002.out | 2 ++ resources/tests/test-002.sh | 34 ++++++++++++++++++++++ resources/tests/test-003.out | 2 ++ resources/tests/test-003.sh | 34 ++++++++++++++++++++++ resources/tests/test-004.out | 17 +++++++++++ resources/tests/test-004.sh | 55 ++++++++++++++++++++++++++++++++++++ resources/tests/test-005.out | 6 ++++ resources/tests/test-005.sh | 47 ++++++++++++++++++++++++++++++ resources/tests/test-006.out | 6 ++++ resources/tests/test-006.sh | 47 ++++++++++++++++++++++++++++++ resources/tests/test-007.out | 2 ++ resources/tests/test-007.sh | 32 +++++++++++++++++++++ resources/tests/test-008.out | 2 ++ resources/tests/test-008.sh | 32 +++++++++++++++++++++ resources/tests/test-009.out | 1 + resources/tests/test-009.sh | 32 +++++++++++++++++++++ 25 files changed, 554 insertions(+), 6 deletions(-) create mode 100644 resources/tests/env.sh create mode 100644 resources/tests/sources.txt create mode 100644 resources/tests/test-000.out create mode 100755 resources/tests/test-000.sh create mode 100644 resources/tests/test-001.out create mode 100755 resources/tests/test-001.sh create mode 100644 resources/tests/test-002.out create mode 100755 resources/tests/test-002.sh create mode 100644 resources/tests/test-003.out create mode 100755 resources/tests/test-003.sh create mode 100644 resources/tests/test-004.out create mode 100755 resources/tests/test-004.sh create mode 100644 resources/tests/test-005.out create mode 100755 resources/tests/test-005.sh create mode 100644 resources/tests/test-006.out create mode 100755 resources/tests/test-006.sh create mode 100644 resources/tests/test-007.out create mode 100755 resources/tests/test-007.sh create mode 100644 resources/tests/test-008.out create mode 100755 resources/tests/test-008.sh create mode 100644 resources/tests/test-009.out create mode 100755 resources/tests/test-009.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3fb3c03..ea8d770 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -130,8 +130,8 @@ build:busybox:latest: image: 'docker.io/busybox:latest' script: - | # Partial build, since Busybox does not have the necessary tools. - mkdir ./dist/ - HOSTNAME='' ./hblock -H builtin -F builtin -S builtin -A builtin -D builtin -O ./dist/hosts + find ./resources/tests/ -type f -name 'test-*.sh' | sort -n | xargs -n1 /bin/sh + mkdir ./dist/; HOSTNAME='' ./hblock -H builtin -F builtin -S builtin -A builtin -D builtin -O ./dist/hosts ./resources/alt-formats/domains.txt.sh ./dist/hosts ./dist/hosts_domains.txt ./hblock ./resources/stats/suffix.sh ./dist/hosts_domains.txt none > ./dist/most_abused_tlds.txt ./resources/templates/index.sh ./dist/ > ./dist/index.html diff --git a/Makefile b/Makefile index 2f107bd..fe90bc9 100755 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ VERSION := $(shell ./resources/version/version.sh get) .PHONY: all all: + $(MAKE) test $(MAKE) build stats $(MAKE) index @@ -88,8 +89,15 @@ man: ./hblock.1 ./hblock.1.md .PHONY: lint lint: - '$(SHELLCHECK)' ./hblock - find ./ -type f -name '*.sh' | xargs '$(SHELLCHECK)' + find ./ -type f '(' -name 'hblock' -or -name '*.sh' ')' | xargs '$(SHELLCHECK)' + +################################################## +## "test" target +################################################## +.PHONY: test + +test: + find ./resources/tests/ -type f -name 'test-*.sh' | sort -n | xargs -n1 /bin/sh ################################################## ## "install" target diff --git a/hblock b/hblock index 89890c9..f6da1b2 100755 --- a/hblock +++ b/hblock @@ -141,14 +141,14 @@ optArgBool() { else optArg='false'; fi } optDie() { - printf -- '%s\n' "${@-}" "Try '${0:?} --help' for more information" >&2 + printf -- '%s\n' "${@-}" "Try 'hblock --help' for more information" >&2 exit 2 } # Show help and quit. showHelp() { printf -- '%s\n' "$(sed -e 's/\\n/\n/g' <<-EOF - Usage: ${0:?} [OPTION]... + Usage: hblock [OPTION]... hBlock is a POSIX-compliant shell script that gets a list of domains that serve ads, tracking scripts and malware from multiple sources and creates a hosts diff --git a/resources/tests/env.sh b/resources/tests/env.sh new file mode 100644 index 0000000..24aa7b9 --- /dev/null +++ b/resources/tests/env.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +# Author: Héctor Molinero Fernández +# License: MIT, https://opensource.org/licenses/MIT +# Repository: https://github.com/hectorm/hblock + +set -eu +export LC_ALL='C' + +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)" + +export TEST_SHELL='/bin/sh' + +export HBLOCK_OUTPUT_FILE='/dev/null' + +export HBLOCK_HEADER_FILE='builtin' +export HBLOCK_HEADER='' + +export HBLOCK_FOOTER_FILE='builtin' +export HBLOCK_FOOTER='' + +export HBLOCK_SOURCES_FILE='builtin' +export HBLOCK_SOURCES="file://${SCRIPT_DIR:?}/sources.txt" + +export HBLOCK_ALLOWLIST_FILE='builtin' +export HBLOCK_ALLOWLIST='' + +export HBLOCK_DENYLIST_FILE='builtin' +export HBLOCK_DENYLIST='' + +export HBLOCK_REDIRECTION='0.0.0.0' +export HBLOCK_WRAP='1' +export HBLOCK_TEMPLATE='\2 \1' +export HBLOCK_COMMENT='#' + +export HBLOCK_LENIENT='false' +export HBLOCK_REGEX='false' +export HBLOCK_CONTINUE='false' + +export HBLOCK_QUIET='false' +export HBLOCK_COLOR='false' diff --git a/resources/tests/sources.txt b/resources/tests/sources.txt new file mode 100644 index 0000000..31cf645 --- /dev/null +++ b/resources/tests/sources.txt @@ -0,0 +1,40 @@ +# Single entries +single-entry-000.com +single-entry-000.com +single-entry-001.com +SINGLE-ENTRY-001.COM + single-entry-002.com + single-entry-003.com +single-entry-004 +single-entry-005.corp +single-entry-006.domain +single-entry-007.example +single-entry-008.home +single-entry-009.host +single-entry-010.invalid +single-entry-011.lan +single-entry-012.local +single-entry-013.localdomain +single-entry-014.localhost +single-entry-015.test +single-entry-016.com single-entry-016.com + +# Entries with prefix +0.0.0.0 entry-with-prefix-000.com +0.0.0.0 entry-with-prefix-001.com + 0.0.0.0 entry-with-prefix-002.com + 0.0.0.0 entry-with-prefix-003.com +127.0.0.1 entry-with-prefix-004.com +:: entry-with-prefix-005.com +::1 entry-with-prefix-006.com +255.255.255.255 entry-with-prefix-007.com +ffff:fff:ff:f::ffff:ffff:ffff entry-with-prefix-008.com +not-an-ip entry-with-prefix-009.com + +# Entries with comments +entry-with-comment-000.com# Dummy comment +entry-with-comment-001.com # Dummy comment +entry-with-comment-002.com # Dummy comment +entry-with-comment-003.com # Dummy comment +0.0.0.0 entry-with-comment-004.com # Dummy comment +#0.0.0.0 entry-with-comment-005.com diff --git a/resources/tests/test-000.out b/resources/tests/test-000.out new file mode 100644 index 0000000..b00603b --- /dev/null +++ b/resources/tests/test-000.out @@ -0,0 +1,21 @@ +# Generated with: https://github.com/hectorm/hblock +# Blocked domains: 16 + +# BEGIN BLOCKLIST +0.0.0.0 entry-with-comment-000.com +0.0.0.0 entry-with-comment-001.com +0.0.0.0 entry-with-comment-002.com +0.0.0.0 entry-with-comment-003.com +0.0.0.0 entry-with-comment-004.com +0.0.0.0 entry-with-prefix-000.com +0.0.0.0 entry-with-prefix-001.com +0.0.0.0 entry-with-prefix-002.com +0.0.0.0 entry-with-prefix-003.com +0.0.0.0 entry-with-prefix-004.com +0.0.0.0 entry-with-prefix-005.com +0.0.0.0 entry-with-prefix-006.com +0.0.0.0 single-entry-000.com +0.0.0.0 single-entry-001.com +0.0.0.0 single-entry-002.com +0.0.0.0 single-entry-003.com +# END BLOCKLIST diff --git a/resources/tests/test-000.sh b/resources/tests/test-000.sh new file mode 100755 index 0000000..ac6bb6a --- /dev/null +++ b/resources/tests/test-000.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +# Author: Héctor Molinero Fernández +# License: MIT, https://opensource.org/licenses/MIT +# Repository: https://github.com/hectorm/hblock + +set -eu +export LC_ALL='C' + +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)" + +main() { + hblock="${1:-hblock}" + + # shellcheck disable=SC1090 + . "${SCRIPT_DIR:?}"/env.sh + + expected="$(cat -- "${SCRIPT_DIR:?}"/test-000.out)" + obtained="$("${TEST_SHELL:?}" "${hblock:?}" -qO- --no-lenient 2>&1 ||:)" + + if [ "${obtained?}" = "${expected?}" ]; then + printf -- 'Test 000 - OK\n' + exit 0 + else + printf -- 'Test 000 - FAIL\n' >&2 + printf -- 'Expected:\n\n%s\n\n' "${expected?}" >&2 + printf -- 'Obtained:\n\n%s\n\n' "${obtained?}" >&2 + exit 1 + fi +} + +main "${@-}" diff --git a/resources/tests/test-001.out b/resources/tests/test-001.out new file mode 100644 index 0000000..8d3d01d --- /dev/null +++ b/resources/tests/test-001.out @@ -0,0 +1,23 @@ +# Generated with: https://github.com/hectorm/hblock +# Blocked domains: 18 + +# BEGIN BLOCKLIST +0.0.0.0 entry-with-comment-000.com +0.0.0.0 entry-with-comment-001.com +0.0.0.0 entry-with-comment-002.com +0.0.0.0 entry-with-comment-003.com +0.0.0.0 entry-with-comment-004.com +0.0.0.0 entry-with-prefix-000.com +0.0.0.0 entry-with-prefix-001.com +0.0.0.0 entry-with-prefix-002.com +0.0.0.0 entry-with-prefix-003.com +0.0.0.0 entry-with-prefix-004.com +0.0.0.0 entry-with-prefix-005.com +0.0.0.0 entry-with-prefix-006.com +0.0.0.0 entry-with-prefix-007.com +0.0.0.0 entry-with-prefix-008.com +0.0.0.0 single-entry-000.com +0.0.0.0 single-entry-001.com +0.0.0.0 single-entry-002.com +0.0.0.0 single-entry-003.com +# END BLOCKLIST diff --git a/resources/tests/test-001.sh b/resources/tests/test-001.sh new file mode 100755 index 0000000..db0e10b --- /dev/null +++ b/resources/tests/test-001.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +# Author: Héctor Molinero Fernández +# License: MIT, https://opensource.org/licenses/MIT +# Repository: https://github.com/hectorm/hblock + +set -eu +export LC_ALL='C' + +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)" + +main() { + hblock="${1:-hblock}" + + # shellcheck disable=SC1090 + . "${SCRIPT_DIR:?}"/env.sh + + expected="$(cat -- "${SCRIPT_DIR:?}"/test-001.out)" + obtained="$("${TEST_SHELL:?}" "${hblock:?}" -qO- --lenient 2>&1 ||:)" + + if [ "${obtained?}" = "${expected?}" ]; then + printf -- 'Test 001 - OK\n' + exit 0 + else + printf -- 'Test 001 - FAIL\n' >&2 + printf -- 'Expected:\n\n%s\n\n' "${expected?}" >&2 + printf -- 'Obtained:\n\n%s\n\n' "${obtained?}" >&2 + exit 1 + fi +} + +main "${@-}" diff --git a/resources/tests/test-002.out b/resources/tests/test-002.out new file mode 100644 index 0000000..b9da734 --- /dev/null +++ b/resources/tests/test-002.out @@ -0,0 +1,2 @@ +# Generated with: https://github.com/hectorm/hblock +# Blocked domains: 0 diff --git a/resources/tests/test-002.sh b/resources/tests/test-002.sh new file mode 100755 index 0000000..191ef71 --- /dev/null +++ b/resources/tests/test-002.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +# Author: Héctor Molinero Fernández +# License: MIT, https://opensource.org/licenses/MIT +# Repository: https://github.com/hectorm/hblock + +set -eu +export LC_ALL='C' + +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)" + +main() { + hblock="${1:-hblock}" + + # shellcheck disable=SC1090 + . "${SCRIPT_DIR:?}"/env.sh + + export HBLOCK_SOURCES='' + + expected="$(cat -- "${SCRIPT_DIR:?}"/test-002.out)" + obtained="$("${TEST_SHELL:?}" "${hblock:?}" -qO- 2>&1 ||:)" + + if [ "${obtained?}" = "${expected?}" ]; then + printf -- 'Test 002 - OK\n' + exit 0 + else + printf -- 'Test 002 - FAIL\n' >&2 + printf -- 'Expected:\n\n%s\n\n' "${expected?}" >&2 + printf -- 'Obtained:\n\n%s\n\n' "${obtained?}" >&2 + exit 1 + fi +} + +main "${@-}" diff --git a/resources/tests/test-003.out b/resources/tests/test-003.out new file mode 100644 index 0000000..d7c2e94 --- /dev/null +++ b/resources/tests/test-003.out @@ -0,0 +1,2 @@ +// Generated with: https://github.com/hectorm/hblock +// Blocked domains: 0 diff --git a/resources/tests/test-003.sh b/resources/tests/test-003.sh new file mode 100755 index 0000000..29546ae --- /dev/null +++ b/resources/tests/test-003.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +# Author: Héctor Molinero Fernández +# License: MIT, https://opensource.org/licenses/MIT +# Repository: https://github.com/hectorm/hblock + +set -eu +export LC_ALL='C' + +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)" + +main() { + hblock="${1:-hblock}" + + # shellcheck disable=SC1090 + . "${SCRIPT_DIR:?}"/env.sh + + export HBLOCK_SOURCES='' + + expected="$(cat -- "${SCRIPT_DIR:?}"/test-003.out)" + obtained="$("${TEST_SHELL:?}" "${hblock:?}" -qO- --comment='//' -- --comment='!' 2>&1 ||:)" + + if [ "${obtained?}" = "${expected?}" ]; then + printf -- 'Test 003 - OK\n' + exit 0 + else + printf -- 'Test 003 - FAIL\n' >&2 + printf -- 'Expected:\n\n%s\n\n' "${expected?}" >&2 + printf -- 'Obtained:\n\n%s\n\n' "${obtained?}" >&2 + exit 1 + fi +} + +main "${@-}" diff --git a/resources/tests/test-004.out b/resources/tests/test-004.out new file mode 100644 index 0000000..ef43663 --- /dev/null +++ b/resources/tests/test-004.out @@ -0,0 +1,17 @@ +# Generated with: https://github.com/hectorm/hblock +# Blocked domains: 2 + +# BEGIN HEADER +TEST HEADER - LINE 1 +TEST HEADER - LINE 2 +# END HEADER + +# BEGIN BLOCKLIST +0.0.0.0 example.com +0.0.0.0 example.net +# END BLOCKLIST + +# BEGIN FOOTER +TEST FOOTER - LINE 1 +TEST FOOTER - LINE 2 +# END FOOTER diff --git a/resources/tests/test-004.sh b/resources/tests/test-004.sh new file mode 100755 index 0000000..6ff1cd7 --- /dev/null +++ b/resources/tests/test-004.sh @@ -0,0 +1,55 @@ +#!/bin/sh + +# Author: Héctor Molinero Fernández +# License: MIT, https://opensource.org/licenses/MIT +# Repository: https://github.com/hectorm/hblock + +set -eu +export LC_ALL='C' + +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)" + +main() { + hblock="${1:-hblock}" + + # shellcheck disable=SC1090 + . "${SCRIPT_DIR:?}"/env.sh + + export HBLOCK_SOURCES='' + + # shellcheck disable=SC2155 + export HBLOCK_HEADER="$(cat <<-'EOF' + TEST HEADER - LINE 1 + TEST HEADER - LINE 2 + EOF + )" + + # shellcheck disable=SC2155 + export HBLOCK_FOOTER="$(cat <<-'EOF' + TEST FOOTER - LINE 1 + TEST FOOTER - LINE 2 + EOF + )" + + # shellcheck disable=SC2155 + export HBLOCK_DENYLIST="$(cat <<-'EOF' + example.com + example.net + EOF + )" + + expected="$(cat -- "${SCRIPT_DIR:?}"/test-004.out)" + obtained="$("${TEST_SHELL:?}" "${hblock:?}" -qO- 2>&1 ||:)" + + if [ "${obtained?}" = "${expected?}" ]; then + printf -- 'Test 004 - OK\n' + exit 0 + else + printf -- 'Test 004 - FAIL\n' >&2 + printf -- 'Expected:\n\n%s\n\n' "${expected?}" >&2 + printf -- 'Obtained:\n\n%s\n\n' "${obtained?}" >&2 + exit 1 + fi +} + +main "${@-}" diff --git a/resources/tests/test-005.out b/resources/tests/test-005.out new file mode 100644 index 0000000..6f35b1d --- /dev/null +++ b/resources/tests/test-005.out @@ -0,0 +1,6 @@ +# Generated with: https://github.com/hectorm/hblock +# Blocked domains: 1 + +# BEGIN BLOCKLIST +0.0.0.0 example.com +# END BLOCKLIST diff --git a/resources/tests/test-005.sh b/resources/tests/test-005.sh new file mode 100755 index 0000000..034dede --- /dev/null +++ b/resources/tests/test-005.sh @@ -0,0 +1,47 @@ +#!/bin/sh + +# Author: Héctor Molinero Fernández +# License: MIT, https://opensource.org/licenses/MIT +# Repository: https://github.com/hectorm/hblock + +set -eu +export LC_ALL='C' + +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)" + +main() { + hblock="${1:-hblock}" + + # shellcheck disable=SC1090 + . "${SCRIPT_DIR:?}"/env.sh + + export HBLOCK_SOURCES='' + + # shellcheck disable=SC2155 + export HBLOCK_DENYLIST="$(cat <<-'EOF' + example.com + example.net + EOF + )" + + # shellcheck disable=SC2155 + export HBLOCK_ALLOWLIST="$(cat <<-'EOF' + example.net + EOF + )" + + expected="$(cat -- "${SCRIPT_DIR:?}"/test-005.out)" + obtained="$("${TEST_SHELL:?}" "${hblock:?}" -qO- --no-regex 2>&1 ||:)" + + if [ "${obtained?}" = "${expected?}" ]; then + printf -- 'Test 005 - OK\n' + exit 0 + else + printf -- 'Test 005 - FAIL\n' >&2 + printf -- 'Expected:\n\n%s\n\n' "${expected?}" >&2 + printf -- 'Obtained:\n\n%s\n\n' "${obtained?}" >&2 + exit 1 + fi +} + +main "${@-}" diff --git a/resources/tests/test-006.out b/resources/tests/test-006.out new file mode 100644 index 0000000..34faf13 --- /dev/null +++ b/resources/tests/test-006.out @@ -0,0 +1,6 @@ +# Generated with: https://github.com/hectorm/hblock +# Blocked domains: 1 + +# BEGIN BLOCKLIST +0.0.0.0 example.net +# END BLOCKLIST diff --git a/resources/tests/test-006.sh b/resources/tests/test-006.sh new file mode 100755 index 0000000..340692b --- /dev/null +++ b/resources/tests/test-006.sh @@ -0,0 +1,47 @@ +#!/bin/sh + +# Author: Héctor Molinero Fernández +# License: MIT, https://opensource.org/licenses/MIT +# Repository: https://github.com/hectorm/hblock + +set -eu +export LC_ALL='C' + +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)" + +main() { + hblock="${1:-hblock}" + + # shellcheck disable=SC1090 + . "${SCRIPT_DIR:?}"/env.sh + + export HBLOCK_SOURCES='' + + # shellcheck disable=SC2155 + export HBLOCK_DENYLIST="$(cat <<-'EOF' + example.com + example.net + EOF + )" + + # shellcheck disable=SC2155 + export HBLOCK_ALLOWLIST="$(cat <<-'EOF' + .*\.com + EOF + )" + + expected="$(cat -- "${SCRIPT_DIR:?}"/test-006.out)" + obtained="$("${TEST_SHELL:?}" "${hblock:?}" -qO- --regex 2>&1 ||:)" + + if [ "${obtained?}" = "${expected?}" ]; then + printf -- 'Test 006 - OK\n' + exit 0 + else + printf -- 'Test 006 - FAIL\n' >&2 + printf -- 'Expected:\n\n%s\n\n' "${expected?}" >&2 + printf -- 'Obtained:\n\n%s\n\n' "${obtained?}" >&2 + exit 1 + fi +} + +main "${@-}" diff --git a/resources/tests/test-007.out b/resources/tests/test-007.out new file mode 100644 index 0000000..1855d88 --- /dev/null +++ b/resources/tests/test-007.out @@ -0,0 +1,2 @@ +Illegal option -I +Try 'hblock --help' for more information diff --git a/resources/tests/test-007.sh b/resources/tests/test-007.sh new file mode 100755 index 0000000..d209c4b --- /dev/null +++ b/resources/tests/test-007.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +# Author: Héctor Molinero Fernández +# License: MIT, https://opensource.org/licenses/MIT +# Repository: https://github.com/hectorm/hblock + +set -eu +export LC_ALL='C' + +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)" + +main() { + hblock="${1:-hblock}" + + # shellcheck disable=SC1090 + . "${SCRIPT_DIR:?}"/env.sh + + expected="$(cat -- "${SCRIPT_DIR:?}"/test-007.out)" + obtained="$("${TEST_SHELL:?}" "${hblock:?}" -O- -H/dev/null -F /dev/null --sources /dev/null --allowlist=/dev/null -rcqI 2>&1 ||:)" + + if [ "${obtained?}" = "${expected?}" ]; then + printf -- 'Test 007 - OK\n' + exit 0 + else + printf -- 'Test 007 - FAIL\n' >&2 + printf -- 'Expected:\n\n%s\n\n' "${expected?}" >&2 + printf -- 'Obtained:\n\n%s\n\n' "${obtained?}" >&2 + exit 1 + fi +} + +main "${@-}" diff --git a/resources/tests/test-008.out b/resources/tests/test-008.out new file mode 100644 index 0000000..b3f49e1 --- /dev/null +++ b/resources/tests/test-008.out @@ -0,0 +1,2 @@ +Illegal option --invalid +Try 'hblock --help' for more information diff --git a/resources/tests/test-008.sh b/resources/tests/test-008.sh new file mode 100755 index 0000000..acf2b05 --- /dev/null +++ b/resources/tests/test-008.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +# Author: Héctor Molinero Fernández +# License: MIT, https://opensource.org/licenses/MIT +# Repository: https://github.com/hectorm/hblock + +set -eu +export LC_ALL='C' + +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)" + +main() { + hblock="${1:-hblock}" + + # shellcheck disable=SC1090 + . "${SCRIPT_DIR:?}"/env.sh + + expected="$(cat -- "${SCRIPT_DIR:?}"/test-008.out)" + obtained="$("${TEST_SHELL:?}" "${hblock:?}" -qO- --continue --no-regex --lenient --invalid 2>&1 ||:)" + + if [ "${obtained?}" = "${expected?}" ]; then + printf -- 'Test 008 - OK\n' + exit 0 + else + printf -- 'Test 008 - FAIL\n' >&2 + printf -- 'Expected:\n\n%s\n\n' "${expected?}" >&2 + printf -- 'Obtained:\n\n%s\n\n' "${obtained?}" >&2 + exit 1 + fi +} + +main "${@-}" diff --git a/resources/tests/test-009.out b/resources/tests/test-009.out new file mode 100644 index 0000000..c0ffdaa --- /dev/null +++ b/resources/tests/test-009.out @@ -0,0 +1 @@ +[ERROR] No such file: /no/such/file diff --git a/resources/tests/test-009.sh b/resources/tests/test-009.sh new file mode 100755 index 0000000..355486a --- /dev/null +++ b/resources/tests/test-009.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +# Author: Héctor Molinero Fernández +# License: MIT, https://opensource.org/licenses/MIT +# Repository: https://github.com/hectorm/hblock + +set -eu +export LC_ALL='C' + +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)" + +main() { + hblock="${1:-hblock}" + + # shellcheck disable=SC1090 + . "${SCRIPT_DIR:?}"/env.sh + + expected="$(cat -- "${SCRIPT_DIR:?}"/test-009.out)" + obtained="$("${TEST_SHELL:?}" "${hblock:?}" -qO- --header=/no/such/file 2>&1 ||:)" + + if [ "${obtained?}" = "${expected?}" ]; then + printf -- 'Test 009 - OK\n' + exit 0 + else + printf -- 'Test 009 - FAIL\n' >&2 + printf -- 'Expected:\n\n%s\n\n' "${expected?}" >&2 + printf -- 'Obtained:\n\n%s\n\n' "${obtained?}" >&2 + exit 1 + fi +} + +main "${@-}"