# t300 --- rlog on invalid RCS file
#
# Copyright (C) 2010 Thien-Thi Nguyen
#
# 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 <http://www.gnu.org/licenses/>.

##
# We expect rlog to barf on an invalid RCS file.
#
# This test checks that rlog exits failurefully, but DOES NOT
# check the error message.  Invalid RCS files are formed first
# by direct construction, then by introducing bad syntax into
# otherwise well-formed RCS files, then by truncating otherwise
# well-formed RCS files.
#
# TODO: Validate error message.
##

. $srcdir/common
split_std_out_err no

echo '                                       -*-org-*-'

barfstars='*'

barf ()
{
    # $1 -- title, in the form of ADJECTIVE-PHRASE
    # $2 -- shell command to create $v
    echo "$barfstars (barf) $1"
    must "$2"
    $hey sed 's/^/|/' $v
    rlog $v && problem "rlog did not barf on invalid ($1) RCS file"
}

barf 'zero size' \
    'touch $v'

barf 'blatant syntax error' \
    'echo random junk > $v'

zinvasion ()
{
    # $1 -- RCS file
    # $2 -- (optional) list of line numbers
    #       that are part of an @TEXT@ block
    echo "* (zinvasion) $1"
    commav=$1
    atatlist="$2"
    lines=$(wc -l "$commav" | sed 's/ .*//')
    line=0
    z=z

    test "$atatlist" && atatlist=" $atatlist "
    while test ! $line = $lines && line=$(expr 1 + $line) ; do

        test "$atatlist" &&
        case "$atatlist" in
            (*" $line "*) z='z@z' ;;
            (*) z=z ;;
        esac

        barf 'typo at beginning of line '$line \
            'sed '$line's/^/'$z'/ '$commav' > $v'

        # Only do substutition (and testing) for non-empty lines.
        if [ "$(sed -n ${line}p $commav)" ] ; then

            barf 'typo at first char of line '$line \
                'sed '$line's/^./'$z'/ '$commav' > $v'

            barf 'typo at last char of line '$line \
                'sed '$line's/.$/'$z'/ '$commav' > $v'
        fi

        barf 'typo at end of line '$line \
            'sed '$line's/$/'$z'/ '$commav' > $v'
    done
}

barfstars='**'
zinvasion $(bundled_commav empty-minimal)
zinvasion $(bundled_commav empty)
zinvasion $(bundled_commav one) '19 20 22 23'

jaws ()
{
    # $1 -- RCS file
    eval $(wc -l "$1" | sed 's/^/lines=/;s/ / commav=/')
    line=1

    echo "* (jaws) $commav"
    while test ! $line = $lines && line=$(expr 1 + $line) ; do
        barf 'truncated at line '$line \
            'sed '$line',\$d '$commav' > $v'
    done
}

jaws $(bundled_commav empty-minimal)
jaws $(bundled_commav empty)
jaws $(bundled_commav one)

exit 0

# t300 ends here
