From f9070781f16b9302838ec99468ce4e3d7f2cd5e9 Mon Sep 17 00:00:00 2001 From: Assaf Gordon Date: Sat, 16 Jul 2016 20:57:50 -0400 Subject: [PATCH] tests: test --follow-symlinks option * configure.ac: (TEST_SYMLINKS): New AM_CONDITIONAL. * testsuite/follow-symlinks.sh: New tests. * testsuite/Makefile.am: (T): Conditionally add new test (and existing test 'follow-symlink-stdin.sh') based on TEST_SYMLINKS. --- configure.ac | 2 + testsuite/Makefile.am | 6 ++- testsuite/follow-symlinks.sh | 74 ++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 testsuite/follow-symlinks.sh diff --git a/configure.ac b/configure.ac index 96840a2..231f8f8 100644 --- a/configure.ac +++ b/configure.ac @@ -97,6 +97,8 @@ if test "$ac_cv_func_lstat:$ac_cv_func_readlink" = yes:yes; then AC_DEFINE([ENABLE_FOLLOW_SYMLINKS], , [Follow symlinks when processing in place]) fi +AM_CONDITIONAL([TEST_SYMLINKS], + [test "$ac_cv_func_lstat:$ac_cv_func_readlink" = yes:yes]) AC_ARG_ENABLE(i18n, [ --disable-i18n disable internationalization (default=enabled)], , diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am index 0da2dc9..880c7fb 100644 --- a/testsuite/Makefile.am +++ b/testsuite/Makefile.am @@ -14,7 +14,6 @@ T = \ compile-tests.sh \ convert-number.sh \ execute-tests.sh \ - follow-symlinks-stdin.sh \ help-version.sh \ in-place-hyphen.sh \ in-place-suffix-backup.sh \ @@ -41,6 +40,11 @@ T = \ temp-file-cleanup.sh \ unbuffered.sh +if TEST_SYMLINKS +T += follow-symlinks.sh \ + follow-symlinks-stdin.sh +endif + TESTS = $(check_PROGRAMS) $(SEDTESTS) $(T) SEDTESTS = diff --git a/testsuite/follow-symlinks.sh b/testsuite/follow-symlinks.sh new file mode 100644 index 0000000..8de7e67 --- /dev/null +++ b/testsuite/follow-symlinks.sh @@ -0,0 +1,74 @@ +#!/bin/sh +# Test --follow-symlinks option + +# Copyright (C) 2016 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +. "${srcdir=.}/init.sh"; path_prepend_ ../sed +print_ver_ sed + +fail=0 + +# +# 'F' with/out follow-symlinks +# +echo dummy > a || framework_failure_ +ln -s a la1 || framework_failure_ +ln -s la1 la2 || framework_failure_ + +echo a > exp-a || framework_failure_ +echo la1 > exp-la1 || framework_failure_ + +# Sanity-check: the real file +sed 'F;Q' a > out-a || fail=1 +compare_ exp-a out-a || fail=1 + +# Without follow-symlinks +sed -n 'F' la1 > out-la1 || fail=1 +compare_ exp-la1 out-la1 || fail=1 + +# With follow-symlinks +sed -n --follow-symlinks 'F' la1 > out-la1-flw || fail=1 +compare_ exp-a out-la1-flw || fail=1 + +# With follow-symlinks and two levels of indirections +sed -n --follow-symlinks 'F' la2 > out-la2-flw || fail=1 +compare_ exp-a out-la2-flw || fail=1 + +# Two symlinks input +# (implementation note: utils.c:follow_symlinks() uses a static buffer +# which will be non-empty on the second invocation) +printf "%s\n" a a > exp-two-symlinks || framework_failure_ +sed --follow-symlinks -n 'F' la1 la2 > out-two-symlinks || fail=1 +compare_ exp-two-symlinks out-two-symlinks || fail=1 + +# non-existing input with --follow-symlink +# implementation note: lstat() will be called before open(), thus 'cannot stat'. +cat <<\EOF >exp-stat || framework_failure_ +sed: cannot stat badfile: +EOF +returns_ 4 sed --follow-symlinks 'F' badfile >/dev/null 2>err-stat || fail=1 + +# trim the filename/errno message (using sed itself...) +sed -i 's/badfile:.*$/badfile:/' err-stat || framework_failure_ +compare_ exp-stat err-stat || fail=1 + + +# symlinks with absolute path +ln -s "$PWD/a" la-abs || framework_failure_ +echo "$PWD/a" > exp-la-abs || framework_failure_ +sed -n --follow-symlinks 'F' la-abs > out-la-abs || fail=1 +compare_ exp-la-abs out-la-abs || fail=1 + +Exit $fail