Skip to content

Commit 79b0edc

Browse files
bk2204gitster
authored andcommitted
t4015: abstract away SHA-1-specific constants
Adjust the test so that it computes variables for object IDs instead of using hard-coded hashes. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 840624f commit 79b0edc

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed

t/t4015-diff-whitespace.sh

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test_expect_success "Ray Lehtiniemi's example" '
1616
} while (0);
1717
EOF
1818
git update-index --add x &&
19+
before=$(git rev-parse --short $(git hash-object x)) &&
1920
2021
cat <<-\EOF >x &&
2122
do
@@ -24,10 +25,11 @@ test_expect_success "Ray Lehtiniemi's example" '
2425
}
2526
while (0);
2627
EOF
28+
after=$(git rev-parse --short $(git hash-object x)) &&
2729
28-
cat <<-\EOF >expect &&
30+
cat <<-EOF >expect &&
2931
diff --git a/x b/x
30-
index adf3937..6edc172 100644
32+
index $before..$after 100644
3133
--- a/x
3234
+++ b/x
3335
@@ -1,3 +1,5 @@
@@ -61,6 +63,7 @@ test_expect_success 'another test, without options' '
6163
EOF
6264
6365
git update-index x &&
66+
before=$(git rev-parse --short $(git hash-object x)) &&
6467
6568
tr "_" " " <<-\EOF >x &&
6669
_ whitespace at beginning
@@ -70,10 +73,11 @@ test_expect_success 'another test, without options' '
7073
unchanged line
7174
CR at end
7275
EOF
76+
after=$(git rev-parse --short $(git hash-object x)) &&
7377
74-
tr "Q_" "\015 " <<-\EOF >expect &&
78+
tr "Q_" "\015 " <<-EOF >expect &&
7579
diff --git a/x b/x
76-
index d99af23..22d9f73 100644
80+
index $before..$after 100644
7781
--- a/x
7882
+++ b/x
7983
@@ -1,6 +1,6 @@
@@ -108,9 +112,9 @@ test_expect_success 'another test, without options' '
108112
git diff -w --ignore-cr-at-eol >out &&
109113
test_must_be_empty out &&
110114
111-
tr "Q_" "\015 " <<-\EOF >expect &&
115+
tr "Q_" "\015 " <<-EOF >expect &&
112116
diff --git a/x b/x
113-
index d99af23..22d9f73 100644
117+
index $before..$after 100644
114118
--- a/x
115119
+++ b/x
116120
@@ -1,6 +1,6 @@
@@ -132,9 +136,9 @@ test_expect_success 'another test, without options' '
132136
git diff -b --ignore-cr-at-eol >out &&
133137
test_cmp expect out &&
134138
135-
tr "Q_" "\015 " <<-\EOF >expect &&
139+
tr "Q_" "\015 " <<-EOF >expect &&
136140
diff --git a/x b/x
137-
index d99af23..22d9f73 100644
141+
index $before..$after 100644
138142
--- a/x
139143
+++ b/x
140144
@@ -1,6 +1,6 @@
@@ -154,9 +158,9 @@ test_expect_success 'another test, without options' '
154158
git diff --ignore-space-at-eol --ignore-cr-at-eol >out &&
155159
test_cmp expect out &&
156160
157-
tr "Q_" "\015 " <<-\EOF >expect &&
161+
tr "Q_" "\015 " <<-EOF >expect &&
158162
diff --git a/x b/x
159-
index_d99af23..22d9f73 100644
163+
index_$before..$after 100644
160164
--- a/x
161165
+++ b/x
162166
@@ -1,6 +1,6 @@
@@ -786,23 +790,25 @@ test_expect_success 'whitespace-only changes not reported' '
786790
test_must_be_empty actual
787791
'
788792

789-
cat <<EOF >expect
790-
diff --git a/x b/z
791-
similarity index NUM%
792-
rename from x
793-
rename to z
794-
index 380c32a..a97b785 100644
795-
EOF
796793
test_expect_success 'whitespace-only changes reported across renames' '
797794
git reset --hard &&
798795
for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
799796
git add x &&
797+
before=$(git rev-parse --short $(git hash-object x)) &&
800798
git commit -m "base" &&
801799
sed -e "5s/^/ /" x >z &&
802800
git rm x &&
803801
git add z &&
802+
after=$(git rev-parse --short $(git hash-object z)) &&
804803
git diff -w -M --cached |
805804
sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
805+
cat <<-EOF >expect &&
806+
diff --git a/x b/z
807+
similarity index NUM%
808+
rename from x
809+
rename to z
810+
index $before..$after 100644
811+
EOF
806812
test_cmp expect actual
807813
'
808814

@@ -858,13 +864,15 @@ test_expect_success 'diff that introduces a line with only tabs' '
858864
git config core.whitespace blank-at-eol &&
859865
git reset --hard &&
860866
echo "test" >x &&
867+
before=$(git rev-parse --short $(git hash-object x)) &&
861868
git commit -m "initial" x &&
862869
echo "{NTN}" | tr "NT" "\n\t" >>x &&
870+
after=$(git rev-parse --short $(git hash-object x)) &&
863871
git diff --color | test_decode_color >current &&
864872
865-
cat >expected <<-\EOF &&
873+
cat >expected <<-EOF &&
866874
<BOLD>diff --git a/x b/x<RESET>
867-
<BOLD>index 9daeafb..2874b91 100644<RESET>
875+
<BOLD>index $before..$after 100644<RESET>
868876
<BOLD>--- a/x<RESET>
869877
<BOLD>+++ b/x<RESET>
870878
<CYAN>@@ -1 +1,4 @@<RESET>
@@ -883,19 +891,21 @@ test_expect_success 'diff that introduces and removes ws breakages' '
883891
echo "0. blank-at-eol " &&
884892
echo "1. blank-at-eol "
885893
} >x &&
894+
before=$(git rev-parse --short $(git hash-object x)) &&
886895
git commit -a --allow-empty -m preimage &&
887896
{
888897
echo "0. blank-at-eol " &&
889898
echo "1. still-blank-at-eol " &&
890899
echo "2. and a new line "
891900
} >x &&
901+
after=$(git rev-parse --short $(git hash-object x)) &&
892902
893903
git diff --color |
894904
test_decode_color >current &&
895905
896-
cat >expected <<-\EOF &&
906+
cat >expected <<-EOF &&
897907
<BOLD>diff --git a/x b/x<RESET>
898-
<BOLD>index d0233a2..700886e 100644<RESET>
908+
<BOLD>index $before..$after 100644<RESET>
899909
<BOLD>--- a/x<RESET>
900910
<BOLD>+++ b/x<RESET>
901911
<CYAN>@@ -1,2 +1,3 @@<RESET>
@@ -915,16 +925,18 @@ test_expect_success 'ws-error-highlight test setup' '
915925
echo "0. blank-at-eol " &&
916926
echo "1. blank-at-eol "
917927
} >x &&
928+
before=$(git rev-parse --short $(git hash-object x)) &&
918929
git commit -a --allow-empty -m preimage &&
919930
{
920931
echo "0. blank-at-eol " &&
921932
echo "1. still-blank-at-eol " &&
922933
echo "2. and a new line "
923934
} >x &&
935+
after=$(git rev-parse --short $(git hash-object x)) &&
924936
925-
cat >expect.default-old <<-\EOF &&
937+
cat >expect.default-old <<-EOF &&
926938
<BOLD>diff --git a/x b/x<RESET>
927-
<BOLD>index d0233a2..700886e 100644<RESET>
939+
<BOLD>index $before..$after 100644<RESET>
928940
<BOLD>--- a/x<RESET>
929941
<BOLD>+++ b/x<RESET>
930942
<CYAN>@@ -1,2 +1,3 @@<RESET>
@@ -934,9 +946,9 @@ test_expect_success 'ws-error-highlight test setup' '
934946
<GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
935947
EOF
936948
937-
cat >expect.all <<-\EOF &&
949+
cat >expect.all <<-EOF &&
938950
<BOLD>diff --git a/x b/x<RESET>
939-
<BOLD>index d0233a2..700886e 100644<RESET>
951+
<BOLD>index $before..$after 100644<RESET>
940952
<BOLD>--- a/x<RESET>
941953
<BOLD>+++ b/x<RESET>
942954
<CYAN>@@ -1,2 +1,3 @@<RESET>
@@ -946,9 +958,9 @@ test_expect_success 'ws-error-highlight test setup' '
946958
<GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
947959
EOF
948960
949-
cat >expect.none <<-\EOF
961+
cat >expect.none <<-EOF
950962
<BOLD>diff --git a/x b/x<RESET>
951-
<BOLD>index d0233a2..700886e 100644<RESET>
963+
<BOLD>index $before..$after 100644<RESET>
952964
<BOLD>--- a/x<RESET>
953965
<BOLD>+++ b/x<RESET>
954966
<CYAN>@@ -1,2 +1,3 @@<RESET>
@@ -1022,14 +1034,15 @@ test_expect_success 'detect moved code, complete file' '
10221034
EOF
10231035
git add test.c &&
10241036
git commit -m "add main function" &&
1037+
file=$(git rev-parse --short HEAD:test.c) &&
10251038
git mv test.c main.c &&
10261039
test_config color.diff.oldMoved "normal red" &&
10271040
test_config color.diff.newMoved "normal green" &&
10281041
git diff HEAD --color-moved=zebra --color --no-renames | test_decode_color >actual &&
1029-
cat >expected <<-\EOF &&
1042+
cat >expected <<-EOF &&
10301043
<BOLD>diff --git a/main.c b/main.c<RESET>
10311044
<BOLD>new file mode 100644<RESET>
1032-
<BOLD>index 0000000..a986c57<RESET>
1045+
<BOLD>index 0000000..$file<RESET>
10331046
<BOLD>--- /dev/null<RESET>
10341047
<BOLD>+++ b/main.c<RESET>
10351048
<CYAN>@@ -0,0 +1,5 @@<RESET>
@@ -1040,7 +1053,7 @@ test_expect_success 'detect moved code, complete file' '
10401053
<BGREEN>+<RESET><BGREEN>}<RESET>
10411054
<BOLD>diff --git a/test.c b/test.c<RESET>
10421055
<BOLD>deleted file mode 100644<RESET>
1043-
<BOLD>index a986c57..0000000<RESET>
1056+
<BOLD>index $file..0000000<RESET>
10441057
<BOLD>--- a/test.c<RESET>
10451058
<BOLD>+++ /dev/null<RESET>
10461059
<CYAN>@@ -1,5 +0,0 @@<RESET>
@@ -1094,6 +1107,8 @@ test_expect_success 'detect malicious moved code, inside file' '
10941107
EOF
10951108
git add main.c test.c &&
10961109
git commit -m "add main and test file" &&
1110+
before_main=$(git rev-parse --short HEAD:main.c) &&
1111+
before_test=$(git rev-parse --short HEAD:test.c) &&
10971112
cat <<-\EOF >main.c &&
10981113
#include<stdio.h>
10991114
int stuff()
@@ -1126,10 +1141,12 @@ test_expect_success 'detect malicious moved code, inside file' '
11261141
bar();
11271142
}
11281143
EOF
1144+
after_main=$(git rev-parse --short $(git hash-object main.c)) &&
1145+
after_test=$(git rev-parse --short $(git hash-object test.c)) &&
11291146
git diff HEAD --no-renames --color-moved=zebra --color | test_decode_color >actual &&
1130-
cat <<-\EOF >expected &&
1147+
cat <<-EOF >expected &&
11311148
<BOLD>diff --git a/main.c b/main.c<RESET>
1132-
<BOLD>index 27a619c..7cf9336 100644<RESET>
1149+
<BOLD>index $before_main..$after_main 100644<RESET>
11331150
<BOLD>--- a/main.c<RESET>
11341151
<BOLD>+++ b/main.c<RESET>
11351152
<CYAN>@@ -5,13 +5,6 @@<RESET> <RESET>printf("Hello ");<RESET>
@@ -1147,7 +1164,7 @@ test_expect_success 'detect malicious moved code, inside file' '
11471164
{<RESET>
11481165
foo();<RESET>
11491166
<BOLD>diff --git a/test.c b/test.c<RESET>
1150-
<BOLD>index 1dc1d85..2bedec9 100644<RESET>
1167+
<BOLD>index $before_test..$after_test 100644<RESET>
11511168
<BOLD>--- a/test.c<RESET>
11521169
<BOLD>+++ b/test.c<RESET>
11531170
<CYAN>@@ -4,6 +4,13 @@<RESET> <RESET>int bar()<RESET>
@@ -1176,9 +1193,9 @@ test_expect_success 'plain moved code, inside file' '
11761193
test_config color.diff.newMovedAlternative "yellow" &&
11771194
# needs previous test as setup
11781195
git diff HEAD --no-renames --color-moved=plain --color | test_decode_color >actual &&
1179-
cat <<-\EOF >expected &&
1196+
cat <<-EOF >expected &&
11801197
<BOLD>diff --git a/main.c b/main.c<RESET>
1181-
<BOLD>index 27a619c..7cf9336 100644<RESET>
1198+
<BOLD>index $before_main..$after_main 100644<RESET>
11821199
<BOLD>--- a/main.c<RESET>
11831200
<BOLD>+++ b/main.c<RESET>
11841201
<CYAN>@@ -5,13 +5,6 @@<RESET> <RESET>printf("Hello ");<RESET>
@@ -1196,7 +1213,7 @@ test_expect_success 'plain moved code, inside file' '
11961213
{<RESET>
11971214
foo();<RESET>
11981215
<BOLD>diff --git a/test.c b/test.c<RESET>
1199-
<BOLD>index 1dc1d85..2bedec9 100644<RESET>
1216+
<BOLD>index $before_test..$after_test 100644<RESET>
12001217
<BOLD>--- a/test.c<RESET>
12011218
<BOLD>+++ b/test.c<RESET>
12021219
<CYAN>@@ -4,6 +4,13 @@<RESET> <RESET>int bar()<RESET>

0 commit comments

Comments
 (0)