regress: cmd and shouldfail shouldn't print escapes
[guilt.git] / regression / scaffold
blob6605034b86dcec19bbe9465957d87f7274ce8531
1 if [ -z "$REG_DIR" ]; then
2         echo "ERROR: You cannot run tests by hand" >&2
3         exit 2
4 fi
6 set -e
8 export PATH=$PWD/..:$PATH
10 export TZ="UTC"
11 export TOUCH_DATE="200701010000.00"
12 export GIT_AUTHOR_DATE="2007-01-01 00:00:00 UTC"
13 export GIT_COMMITTER_DATE="2007-01-01 00:00:00 UTC"
14 export GIT_AUTHOR_NAME="Author Name"
15 export GIT_AUTHOR_EMAIL="author@email"
16 export GIT_COMMITTER_NAME="Commiter Name"
17 export GIT_COMMITTER_EMAIL="commiter@email"
19 # usage: die <msg>
20 function die
22         echo "Error: $@" >&2
23         exit 1
26 UNAME_S=`uname -s`
28 if [ -r "$REG_DIR/../os.$UNAME_S" ]; then
29         . "$REG_DIR/../os.$UNAME_S"
30 else
31         die "Unsupported operating system: $UNAME_S"
34 function replace_path
36         sed -e "s,$PWD,PATH,g" \
37             -e "s,$REG_DIR/\.\./,,g" \
38             -e "s,^Usage: guilt-,Usage: guilt ,g" \
39             -e "s,\.\.\. initial, initial,g" \
40             -e "s,^Already on\( branch\)\? [\"']\([^\"']*\)[\"']$,Already on \"\2\",g" \
41             -e "s,^Switched to branch [\"']\([^\"']*\)[\"'],Switched to branch \"\1\",g" \
42             -e "\\,^Deleted branch guilt/.* (was .*).$,d"
45 function filter_dd
47         sed -e 's:^\([0-9]*\) bytes (\([0-9]*\.[0-9]* .B\)) copied.*$:\1 bytes (\2) copied:'
50 # usage: cmd <cmd>..
51 function cmd
53         printf "%% %s\n" "$*"
54         if ! (
55                 exec 3>&1
56                 rv=`(("$@" 2>&1; echo $? >&4) | replace_path >&3 ) 4>&1`
57                 exit $rv
58         ) ; then
59                 echo "% FAIL: The above command should succeed but failed."
60                 exit 1
61         fi
64 # usage: shouldfail <cmd>..
65 function shouldfail
67         printf "%% %s\n" "$*"
68         if (
69                 exec 3>&1
70                 rv=`(("$@" 2>&1; echo $? >&4) | replace_path >&3 ) 4>&1`
71                 exit $rv
72         ) ; then
73                 echo "% FAIL: The above command should fail but succeeded."
74                 exit 1
75         fi
78 # usage: list_files
79 function list_files
81         (
82                 find .git/refs/patches -type f -exec dump_ref {} \; 2> /dev/null || true
83                 find .git/refs/patches -type d -exec dump_reg {} \; 2> /dev/null || true
84                 find .git/patches -exec dump_reg {} \;
85         ) | sort
88 function setup_git_repo
90         # set up the repo so we have something interesting to run guilt on
91         echo "abc" > def
92         git add def
93         git commit -s -m "initial" 2> /dev/null > /dev/null
95         # The HEAD should be a specific hash.  Abort the test if it is not.
96         # If we kept on executing, the test would just fail in mysterious
97         # ways.
98         rev=`git rev-parse HEAD`
99         [ "d4850419ccc1146c7169f500725ce504b9774ed0" != "$rev" ] && \
100                 die "setup_git_repo HEAD check failed (HEAD: $rev)"
102         # Explicitly set config that the tests rely on.
103         git config log.date default
104         git config log.decorate no
105         git config guilt.diffstat false
106         git config guilt.reusebranch false
109 function setup_guilt_repo
111         mkdir -p .git/patches/master
113         # patch to modify a file
114         cp "$REG_DIR/data/modify.patch" .git/patches/master/modify
116         # patch to add a new file
117         cp "$REG_DIR/data/add.patch" .git/patches/master/add
119         # patch to remove an existing file
120         cp "$REG_DIR/data/remove.patch" .git/patches/master/remove
122         # patch to change a mode
123         cp "$REG_DIR/data/mode.patch" .git/patches/master/mode
125         # the series file of all the things
126         cp "$REG_DIR/data/series" .git/patches/master/series
128         # the status file
129         touch .git/patches/master/status
131         # the status file
132         touch .git/patches/master/status
134         touch -a -m -t "$TOUCH_DATE" .git/patches/master/modify
135         touch -a -m -t "$TOUCH_DATE" .git/patches/master/add
136         touch -a -m -t "$TOUCH_DATE" .git/patches/master/remove
137         touch -a -m -t "$TOUCH_DATE" .git/patches/master/mode
140 function reset_git_repo
142         git checkout master
144         git reset --hard d4850419ccc1146c7169f500725ce504b9774ed0
146         rm -rf .git/patches/
149 function setup_repo
151         setup_git_repo
152         setup_guilt_repo