From a329839346a3c2c9591985b70841858dd575846f Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 4 Jun 2013 22:02:21 -0600 Subject: add a system to skip tests that require networking or sudo. --- test/lib-conf-test.sh | 9 +++++---- test/librechroot-test.sh | 13 ++++++++++--- test/libremakepkg-test.sh | 9 ++++++--- test/test-common.sh | 29 +++++++++++++++++++++++++++++ test/testenv | 14 ++++++++++++++ 5 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 test/test-common.sh (limited to 'test') diff --git a/test/lib-conf-test.sh b/test/lib-conf-test.sh index 0ee0f62..86c11c6 100644 --- a/test/lib-conf-test.sh +++ b/test/lib-conf-test.sh @@ -2,16 +2,16 @@ describe lib/conf.sh +. ./test-common.sh + before() { - tmpdir=$(mktemp -d --tmpdir test-conf.sh.XXXXXXXXXXXX) - stat=0 + _before test-conf.sh } after() { - rm -rf -- "$tmpdir" "$XDG_CACHE_HOME" "$XDG_CONFIG_HOME" + _after } - it_sets_makepkg_vars_in_custom_file() { unset PKGDEST touch "$tmpdir/makepkg.conf" @@ -22,6 +22,7 @@ it_sets_makepkg_vars_in_custom_file() { } it_figures_out_HOME_when_root() { + require sudo || return 0 cd "$tmpdir" echo '. $(librelib conf.sh); echo "$LIBREHOME"' > test.sh diff --git a/test/librechroot-test.sh b/test/librechroot-test.sh index 8c9d690..7096e37 100644 --- a/test/librechroot-test.sh +++ b/test/librechroot-test.sh @@ -2,19 +2,22 @@ describe librechroot +. ./test-common.sh + before() { - tmpdir=$(mktemp -d --tmpdir test-librechroot.XXXXXXXXXXXX) + _before librechroot mkdir -p "$XDG_CONFIG_HOME"/libretools echo "CHROOTDIR='$tmpdir'" > "$XDG_CONFIG_HOME"/libretools/chroot.conf echo "CHROOT='default'" >> "$XDG_CONFIG_HOME"/libretools/chroot.conf - stat=0 } after() { - rm -rf -- "$tmpdir" "$XDG_CONFIG_HOME" + _after } + it_creates_repo_for_new_chroots() { + require network sudo || return 0 libremessages msg 'Creating a chroot, may take a few minutes' &>/dev/tty sudo -EH librechroot run test -r /repo/repo.db } @@ -43,7 +46,9 @@ it_displays_help_and_fails_with_0_args() { [[ "$(sed 1q $tmpdir/stderr)" =~ Usage:.* ]] } +# requires sudo so we know it's not failing because it needs root it_fails_for_unknown_commands() { + require sudo || return 0 sudo -EH librechroot phony >$tmpdir/stdout 2>$tmpdir/stderr || stat=$? [[ $stat != 0 ]] @@ -51,7 +56,9 @@ it_fails_for_unknown_commands() { [[ -n "$(cat $tmpdir/stderr)" ]] } +# requires sudo so we know it's not failing because it needs root it_fails_for_unknown_flags() { + require sudo || return 0 sudo -EH librechroot -q >$tmpdir/stdout 2>$tmpdir/stderr || stat=$? [[ $stat != 0 ]] diff --git a/test/libremakepkg-test.sh b/test/libremakepkg-test.sh index 9e54fb6..94249a9 100644 --- a/test/libremakepkg-test.sh +++ b/test/libremakepkg-test.sh @@ -2,19 +2,22 @@ describe libremakepkg +. ./test-common.sh + before() { - tmpdir=$(mktemp -d --tmpdir test-libremakepkg.XXXXXXXXXXXX) + _before libremakepkg + mkdir -p "$XDG_CONFIG_HOME"/libretools echo "CHROOTDIR='$tmpdir/chrootdir'" > "$XDG_CONFIG_HOME"/libretools/chroot.conf echo "CHROOT='default'" >> "$XDG_CONFIG_HOME"/libretools/chroot.conf - stat=0 } after() { - rm -rf -- "$tmpdir" "$XDG_CONFIG_HOME" + _after } it_builds_a_trivial_package() { + require network sudo || return 0 cp libremakepkg.d/PKGBUILD-hello "$tmpdir/PKGBUILD" cd "$tmpdir" diff --git a/test/test-common.sh b/test/test-common.sh new file mode 100644 index 0000000..e837498 --- /dev/null +++ b/test/test-common.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +_before() { + tmpdir="$(mktemp -d --tmpdir "test-$1.XXXXXXXXXXXX")" + stat=0 +} + +_after() { + if [[ $SUDO ]]; then + sudo rm -rf -- "$tmpdir" "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME" + else + rm -rf -- "$tmpdir" "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME" + fi +} + +require() { + local missing=() + if libremessages in_array "network" "$@" && ! [[ $NETWORK ]]; then + missing+=('networking') + fi + if libremessages in_array "sudo" "$@" && ! [[ $SUDO ]]; then + missing+=('sudo') + fi + if (( ${#missing[@]} )); then + libremessages warning "Next test requires %s; Skipping (passing)..." "$(echo "${missing[@]}"|sed 's/ /, /g')" &>/dev/tty + return 1 + fi + return 0; +} diff --git a/test/testenv b/test/testenv index 0c8c4a7..0448e33 100755 --- a/test/testenv +++ b/test/testenv @@ -1,5 +1,19 @@ #!/bin/bash +NETWORK=true +SUDO=true +while [[ $# -gt 0 ]]; do + case "$1" in + --no-network) shift; unset NETWORK;; + --network) shift; NETWORK=true;; + --no-sudo) shift; unset SUDO;; + --sudo) shift; SUDO=true;; + --) shift; break;; + *) break;; + esac +done +export NETWORK SUDO + export TMPDIR="$(mktemp --tmpdir -d libretools-test.XXXXXXXXXX)" trap "rm -rf '$TMPDIR'" EXIT # Set up the install to work with -- cgit v1.2.3-54-g00ecf