Skip to content

Commit bfa7d01

Browse files
j6tgitster
authored andcommitted
t4018: an infrastructure to test hunk headers
Add an infrastructure that simplifies adding new tests of the hunk header regular expressions. To add new tests, a file with the syntax to test can be dropped in the directory t4018. The README file explains how a test file must contain; the README itself tests the default behavior. Signed-off-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent abf8f98 commit bfa7d01

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

t/t4018-diff-funcname.sh

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,25 @@ test_expect_funcname () {
100100
grep "^@@.*@@ $1" diff
101101
}
102102

103-
for p in ada bibtex cpp csharp fortran html java matlab objc pascal perl php python ruby tex
103+
diffpatterns="
104+
ada
105+
bibtex
106+
cpp
107+
csharp
108+
fortran
109+
html
110+
java
111+
matlab
112+
objc
113+
pascal
114+
perl
115+
php
116+
python
117+
ruby
118+
tex
119+
"
120+
121+
for p in $diffpatterns
104122
do
105123
test_expect_success "builtin $p pattern compiles" '
106124
echo "*.java diff=$p" >.gitattributes &&
@@ -118,11 +136,6 @@ do
118136
'
119137
done
120138

121-
test_expect_success 'default behaviour' '
122-
rm -f .gitattributes &&
123-
test_expect_funcname "public class Beer\$"
124-
'
125-
126139
test_expect_success 'set up .gitattributes declaring drivers to test' '
127140
cat >.gitattributes <<-\EOF
128141
*.java diff=java
@@ -182,4 +195,39 @@ test_expect_success 'alternation in pattern' '
182195
test_expect_funcname "public static void main("
183196
'
184197

198+
test_expect_success 'setup hunk header tests' '
199+
for i in $diffpatterns
200+
do
201+
echo "$i-* diff=$i"
202+
done > .gitattributes &&
203+
204+
# add all test files to the index
205+
(
206+
cd "$TEST_DIRECTORY"/t4018 &&
207+
git --git-dir="$TRASH_DIRECTORY/.git" add .
208+
) &&
209+
210+
# place modified files in the worktree
211+
for i in $(git ls-files)
212+
do
213+
sed -e "s/ChangeMe/IWasChanged/" <"$TEST_DIRECTORY/t4018/$i" >"$i" || return 1
214+
done
215+
'
216+
217+
# check each individual file
218+
for i in $(git ls-files)
219+
do
220+
if grep broken "$i" >/dev/null 2>&1
221+
then
222+
result=failure
223+
else
224+
result=success
225+
fi
226+
test_expect_$result "hunk header: $i" "
227+
test_when_finished 'cat actual' && # for debugging only
228+
git diff -U1 $i >actual &&
229+
grep '@@ .* @@.*RIGHT' actual
230+
"
231+
done
232+
185233
test_done

t/t4018/README

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
How to write RIGHT test cases
2+
=============================
3+
4+
Insert the word "ChangeMe" (exactly this form) at a distance of
5+
at least two lines from the line that must appear in the hunk header.
6+
7+
The text that must appear in the hunk header must contain the word
8+
"right", but in all upper-case, like in the title above.
9+
10+
To mark a test case that highlights a malfunction, insert the word
11+
BROKEN in all lower-case somewhere in the file.
12+
13+
This text is a bit twisted and out of order, but it is itself a
14+
test case for the default hunk header pattern. Know what you are doing
15+
if you change it.
16+
17+
BTW, this tests that the head line goes to the hunk header, not the line
18+
of equal signs.

0 commit comments

Comments
 (0)