-
Notifications
You must be signed in to change notification settings - Fork 36
/
test.sh
executable file
·230 lines (197 loc) · 7.45 KB
/
test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/bash -e
#
# For these tests meld is mocked out and replaced by a bash function. Each
# test case is made up of a pair of functions test_xxx and xxx_handler.
# test_xxx typically will invoke git-meld and xxx_handler will be *invoked by*
# git-meld. The success of the test depends on the exit code of the handler.
#
# This means that:
# - test_xxx sets up some conditions and invokes git-meld with some arguments
# - xxx_handler checks that git-meld would invoke meld correctly (i.e. with
# the correct arguments and filesystem contents
#
this_script=$(readlink -f $BASH_SOURCE)
git_meld=$(dirname "$this_script")/git-meld.pl
handler=$1
# * Add a c < *branch-2* |
# |\
# | * Add b
# | |
# | * Modify c < master |
# |\
# | * Add b (modified) < branch-1 |
# |
# * Local changes checked in to index but not committed
# |
# * Local uncommitted changes, not checked in to index
function setup {
export repo_dir=$(mktemp -d -t "git-meld XXXXXX")
cd "$repo_dir"
git init
git config user.name 'Tiny Testah'
git config user.email '[email protected]'
# master
echo "Some file" > a
echo "And another" > c
git add a c
git commit -m "Add a c"
git tag root
echo "Another file" > b
git add b
git commit -m "Added another file"
echo "Modify c" > c
git commit -am "Modify c"
# branch-1
git checkout -b branch-1 root
echo "b with some different content" > b
git add b
git commit -a -m "Add b (modified)"
# branch-2 (HEAD)
git checkout -b branch-2 root
echo "An indexed file" > wtf
git add wtf
echo "And some changes to it" > wtf
git config treediff.tool test_checker
git config treediff.test_checker.cmd "$this_script"
}
function teardown {
rm -Rf "$repo_dir"
}
function die {
echo $1
exit 1
}
function assert_file_contents_equal_to {
tmp=$(mktemp -t expected-contents.XXXXXX)
echo $2 > "$tmp"
cmp "$1" "$tmp" || diff "$1" "$tmp" || die "Assertion failed: $1 contains '$2'"
}
###### test 1
function test_compare_index_to_working_dir {
"$git_meld"
}
function compare_index_to_working_dir_handler {
assert_file_contents_equal_to $tree_a/wtf "An indexed file"
assert_file_contents_equal_to $tree_b/wtf "And some changes to it"
[ "$(readlink -f "$tree_b/wtf")" == "$(readlink -f "$repo_dir/wtf")" ] \
|| die "When comparing against the working tree symbolic links rather than copies should be created"
}
###### test 2
function test_compare_HEAD_to_working_dir {
"$git_meld" HEAD
}
function compare_HEAD_to_working_dir_handler {
[ ! -e "$tree_a/wtf" ] || die "$tree_a/wtf is not in HEAD, so shouldn't be here"
assert_file_contents_equal_to "$tree_b/wtf" "And some changes to it"
[ "$(readlink -f "$tree_b/wtf")" == "$(readlink -f "$repo_dir/wtf")" ] \
|| die "When comparing against the working tree symbolic links rather than copies should be created"
}
###### test 3
function test_compare_HEAD_to_index {
"$git_meld" --cached
}
function compare_HEAD_to_index_handler {
[ ! -e $tree_a/wtf ] || die "$tree_a/wtf is not in HEAD, so shouldn't be here"
assert_file_contents_equal_to $tree_b/wtf "An indexed file"
}
###### test 4
function test_compare_branch1_to_branch2 {
"$git_meld" branch-1 branch-2
}
function compare_branch1_to_branch2_handler {
assert_file_contents_equal_to $tree_a/b "b with some different content"
[ -z "$(ls -A $tree_b)" ] || die "$tree_b should be empty"
}
##### test 5
# Regression test for bug reported by Adam Dingle and fixed in commit
# 181b1f2adcbd53eea488ef027baf9942bfe3620f
function test_that_git_meld_works_with_branches_with_slashes_in_their_names {
git branch branch/with/slashes branch-1
"$git_meld" branch/with/slashes branch-2
}
function that_git_meld_works_with_branches_with_slashes_in_their_names_handler {
assert_file_contents_equal_to $tree_a/b "b with some different content"
[ -z "$(ls -A $tree_b)" ] || die "$tree_b should be empty"
}
##### test 6
function test_that_git_meld_works_when_not_invoked_from_root_of_repo {
mkdir moo
cd moo
"$git_meld" branch-1 branch-2
}
function that_git_meld_works_when_not_invoked_from_root_of_repo_handler {
assert_file_contents_equal_to "$tree_a/b" "b with some different content"
[ -z "$(ls -A $tree_b)" ] || die "$tree_b should be empty"
}
##### test 7
function test_that_using_elipsis_between_two_revisions_uses_their_merge_base {
# Should behave the same as:
# git meld $(git merge-base branch-1 master) master
# which is the same as
# git meld root master
"$git_meld" branch-1...master
}
function that_using_elipsis_between_two_revisions_uses_their_merge_base_handler {
[ -z $(ls --hide c $tree_a) ] || die "tree_a should be the same as root"
[ -z $(ls --hide b --hide c $tree_b) ] || die "tree_a should be the same as root"
assert_file_contents_equal_to "$tree_a/c" 'And another'
assert_file_contents_equal_to "$tree_b/b" 'Another file'
assert_file_contents_equal_to "$tree_b/c" 'Modify c'
}
##### test 8
# Regression test for issue #4 - git meld master... fails
function test_that_elipsis_with_no_second_revision_uses_merge_base_HEAD {
# Should behave the same as git meld root branch-1
git checkout branch-1 &>/dev/null
"$git_meld" master...
}
function that_elipsis_with_no_second_revision_uses_merge_base_HEAD_handler {
# The git diff man page says about the elipsis form of invocation:
# > You can omit any one of <commit>, which has the same effect as using
# > HEAD instead.
#
# Note: if either commit are omitted HEAD is used *and not* the working
# directory as you might otherwise expect.
[ -z $(ls $tree_a) ] || die "There are no changes between root and here"
[ "$(ls $tree_b)" == "b" ] || die "$tree_b should only contain b"
assert_file_contents_equal_to "$tree_b/b" 'b with some different content'
}
##### test 9
# Regression test for issue #17
function test_that_git_meld_works_with_spaces_in_dirnames {
# setup function changed. Temporary directory name now contains one white
# space: mktemp -d -t "git-meld XXXXXX". So this function only consists in
# run git meld on it to test if it works.
cd "$repo_dir"
"$git_meld"
}
function that_git_meld_works_with_spaces_in_dirnames_handler {
# Similar to test 1
assert_file_contents_equal_to $tree_a/wtf "An indexed file"
assert_file_contents_equal_to $tree_b/wtf "And some changes to it"
[ "$(readlink -f "$tree_b/wtf")" == "$(readlink -f "$repo_dir/wtf")" ] \
|| die "When comparing against the working tree symbolic links rather than copies should be created"
}
# If the variable $test_handler is not set this script should run through all
# the tests. The tests involve instructing git-meld to invoke this script
# rather than meld itself with the $test_handler variable set so we can check if
# the appropriate environment would have been set up for meld.
if [ -z "$test_handler" ]; then
[ "$1" == '-v' ] && verbosefile=/dev/stdout || verbosefile=/dev/null
## foreach test
declare -F | cut -d' ' -f 3 | grep test_ | sed s/test_// |
{
while read line
do
echo "Running test test_$line..." >"$verbosefile" 2>&1
setup >"$verbosefile" 2>&1
export test_handler=${line}_handler
test_$line && echo "SUCCESS test_$line" || echo "FAILURE test_$line"
teardown >"$verbosefile" 2>&1
done
}
else
tree_a=$1
tree_b=$2
"$test_handler"
fi