Skip to content

Commit 64f0109

Browse files
avargitster
authored andcommitted
test-lib-functions: use test-tool for [de]packetize()
The shell+perl "[de]packetize()" helper functions were added in 4414a15 (t/lib-git-daemon: add network-protocol helpers, 2018-01-24), and around the same time we added the "pkt-line" helper in 74e7002 (test-pkt-line: introduce a packet-line test helper, 2018-03-14). For some reason it seems we've mostly used the shell+perl version instead of the helper since then. There were discussions around 88124ab (test-lib-functions: make packetize() more efficient, 2020-03-27) and cacae43 (test-lib-functions: simplify packetize() stdin code, 2020-03-29) to improve them and make them more efficient. There was one good reason to do so, we needed an equivalent of "test-tool pkt-line pack", but that command wasn't capable of handling input with "\n" (a feature) or "\0" (just because it happens to be printf-based under the hood). Let's add a "pkt-line-raw" helper for that, and expose is at a packetize_raw() to go with the existing packetize() on the shell level, this gives us the smallest amount of change to the tests themselves. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ebf3c04 commit 64f0109

File tree

5 files changed

+24
-34
lines changed

5 files changed

+24
-34
lines changed

t/helper/test-pkt-line.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ static void pack(int argc, const char **argv)
2626
}
2727
}
2828

29+
static void pack_raw_stdin(void)
30+
{
31+
struct strbuf sb = STRBUF_INIT;
32+
33+
if (strbuf_read(&sb, 0, 0) < 0)
34+
die_errno("failed to read from stdin");
35+
packet_write(1, sb.buf, sb.len);
36+
strbuf_release(&sb);
37+
}
38+
2939
static void unpack(void)
3040
{
3141
struct packet_reader reader;
@@ -110,6 +120,8 @@ int cmd__pkt_line(int argc, const char **argv)
110120

111121
if (!strcmp(argv[1], "pack"))
112122
pack(argc - 2, argv + 2);
123+
else if (!strcmp(argv[1], "pack-raw-stdin"))
124+
pack_raw_stdin();
113125
else if (!strcmp(argv[1], "unpack"))
114126
unpack();
115127
else if (!strcmp(argv[1], "unpack-sideband"))

t/t5411/once-0010-report-status-v1.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ test_expect_success "proc-receive: report status v1" '
2828
if test -z "$GIT_DEFAULT_HASH" || test "$GIT_DEFAULT_HASH" = "sha1"
2929
then
3030
printf "%s %s refs/heads/main\0report-status\n" \
31-
$A $B | packetize
31+
$A $B | packetize_raw
3232
else
3333
printf "%s %s refs/heads/main\0report-status object-format=$GIT_DEFAULT_HASH\n" \
34-
$A $B | packetize
34+
$A $B | packetize_raw
3535
fi &&
3636
printf "%s %s refs/for/main/topic1\n" \
3737
$ZERO_OID $A | packetize &&

t/t5562-http-backend-content-length.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ test_expect_success 'setup' '
6363
hash_next=$(git commit-tree -p HEAD -m next HEAD^{tree}) &&
6464
{
6565
printf "%s %s refs/heads/newbranch\\0report-status object-format=%s\\n" \
66-
"$ZERO_OID" "$hash_next" "$(test_oid algo)" | packetize &&
66+
"$ZERO_OID" "$hash_next" "$(test_oid algo)" | packetize_raw
6767
printf 0000 &&
6868
echo "$hash_next" | git pack-objects --stdout
6969
} >push_body &&

t/t5570-git-daemon.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ test_expect_success 'hostname cannot break out of directory' '
194194

195195
test_expect_success FAKENC 'hostname interpolation works after LF-stripping' '
196196
{
197-
printf "git-upload-pack /interp.git\n\0host=localhost" | packetize
197+
printf "git-upload-pack /interp.git\n\0host=localhost" | packetize_raw
198198
printf "0000"
199199
} >input &&
200200
fake_nc "$GIT_DAEMON_HOST_PORT" <input >output &&

t/test-lib-functions.sh

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,46 +1437,24 @@ nongit () {
14371437
)
14381438
} 7>&2 2>&4
14391439

1440-
# convert function arguments or stdin (if not arguments given) to pktline
1441-
# representation. If multiple arguments are given, they are separated by
1442-
# whitespace and put in a single packet. Note that data containing NULs must be
1443-
# given on stdin, and that empty input becomes an empty packet, not a flush
1444-
# packet (for that you can just print 0000 yourself).
1440+
# These functions are historical wrappers around "test-tool pkt-line"
1441+
# for older tests. Use "test-tool pkt-line" itself in new tests.
14451442
packetize () {
14461443
if test $# -gt 0
14471444
then
14481445
packet="$*"
14491446
printf '%04x%s' "$((4 + ${#packet}))" "$packet"
14501447
else
1451-
perl -e '
1452-
my $packet = do { local $/; <STDIN> };
1453-
printf "%04x%s", 4 + length($packet), $packet;
1454-
'
1448+
test-tool pkt-line pack
14551449
fi
14561450
}
14571451

1458-
# Parse the input as a series of pktlines, writing the result to stdout.
1459-
# Sideband markers are removed automatically, and the output is routed to
1460-
# stderr if appropriate.
1461-
#
1462-
# NUL bytes are converted to "\\0" for ease of parsing with text tools.
1452+
packetize_raw () {
1453+
test-tool pkt-line pack-raw-stdin
1454+
}
1455+
14631456
depacketize () {
1464-
perl -e '
1465-
while (read(STDIN, $len, 4) == 4) {
1466-
if ($len eq "0000") {
1467-
print "FLUSH\n";
1468-
} else {
1469-
read(STDIN, $buf, hex($len) - 4);
1470-
$buf =~ s/\0/\\0/g;
1471-
if ($buf =~ s/^[\x2\x3]//) {
1472-
print STDERR $buf;
1473-
} else {
1474-
$buf =~ s/^\x1//;
1475-
print $buf;
1476-
}
1477-
}
1478-
}
1479-
'
1457+
test-tool pkt-line unpack
14801458
}
14811459

14821460
# Converts base-16 data into base-8. The output is given as a sequence of

0 commit comments

Comments
 (0)