Bump to 1.14.1
[platform/upstream/augeas.git] / tests / test-augtool.sh
1 #!/bin/bash
2
3 TOP_DIR=$(cd $(dirname $0)/.. && pwd)
4 TOP_BUILDDIR="$abs_top_builddir"
5 [ -z "$TOP_BUILDDIR" ] && TOP_BUILDDIR="$TOP_DIR"
6 TOP_SRCDIR="$abs_top_srcdir"
7 [ -z "$TOP_SRCDIR" ] && TOP_SRCDIR="$TOP_DIR"
8
9 TEST_DIR="$TOP_SRCDIR/tests"
10
11 export PATH="$TOP_BUILDDIR/src:${PATH}"
12
13 export AUGEAS_ROOT="$TOP_BUILDDIR/build/test-augtool"
14 export AUGEAS_LENS_LIB="$TOP_SRCDIR/lenses"
15
16 GSED=sed
17 type gsed >/dev/null 2>&1 && GSED=gsed
18
19 fail() {
20     [ -z "$failed" ] && echo FAIL
21     failed=yes
22     echo "$@"
23     result=1
24 }
25
26 # Without args, run all tests
27 if [ $# -eq 0 ] ; then
28     args="$TEST_DIR/test-augtool/*.sh"
29 else
30     args="$@"
31 fi
32
33 result=0
34
35 for tst in $args; do
36     unset failed
37
38     printf "%-40s ... " $(basename $tst .sh)
39
40     # Read in test variables. The variables we understand are
41     # echo              - echo augtool commands if set to some value
42     # commands          - the commands to send to augtool
43     # lens              - the lens to use
44     # file              - the file that should be changed
45     # diff              - the expected diff
46     # refresh           - print diff in a form suitable for cut and paste
47     #                     into the test file if set to some value
48
49     unset echo commands lens file diff refresh
50     . $tst
51
52     # Setup test root from root/
53     [ -d "$AUGEAS_ROOT" ] && rm -rf "$AUGEAS_ROOT"
54     dest_dir="$AUGEAS_ROOT"$(dirname $file)
55     mkdir -p $dest_dir
56     cp -p "$TEST_DIR"/root/$file $dest_dir
57
58     [ -n "$echo" ] && echo="-e"
59
60     commands="set /augeas/load/Test/lens $lens
61 set /augeas/load/Test/incl $file
62 load
63 $commands
64 save
65 quit"
66     echo "$commands" | augtool $echo --nostdinc --noautoload -n || fail "augtool failed"
67
68     abs_file="$AUGEAS_ROOT$file"
69     if [ ! -f "${abs_file}.augnew" ]; then
70         fail "Expected file $file.augnew"
71     else
72         safe_augeas_root=$(printf "%s\n" "$AUGEAS_ROOT" | sed 's/[][\.*^$(){}?+|/]/\\&/g')
73         act=$(diff -u "$abs_file" "${abs_file}.augnew" \
74             | $GSED -r -e "s/^ $//;s!^(---|\+\+\+) ${safe_augeas_root}($file(\.augnew)?)(.*)\$!\1 \2!;s/\\t/\\\\t/g")
75
76         if [ "$act" != "$diff" ] ; then
77             fail "$act"
78         fi
79     fi
80     other_files=$(find "$AUGEAS_ROOT" -name \*.augnew | grep -v "$abs_file.augnew")
81     [ -n "$other_files" ] && fail "Unexpected file(s) $other_files"
82     [ -z "$failed" ] && echo OK
83 done
84
85 exit $result