Skip to content

Commit 74e7002

Browse files
bmwillgitster
authored andcommitted
test-pkt-line: introduce a packet-line test helper
Introduce a packet-line test helper which can either pack or unpack an input stream into packet-lines and writes out the result to stdout. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 8f6982b commit 74e7002

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ TEST_PROGRAMS_NEED_X += test-mktemp
666666
TEST_PROGRAMS_NEED_X += test-online-cpus
667667
TEST_PROGRAMS_NEED_X += test-parse-options
668668
TEST_PROGRAMS_NEED_X += test-path-utils
669+
TEST_PROGRAMS_NEED_X += test-pkt-line
669670
TEST_PROGRAMS_NEED_X += test-prio-queue
670671
TEST_PROGRAMS_NEED_X += test-read-cache
671672
TEST_PROGRAMS_NEED_X += test-write-cache

t/helper/test-pkt-line.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "pkt-line.h"
2+
3+
static void pack_line(const char *line)
4+
{
5+
if (!strcmp(line, "0000") || !strcmp(line, "0000\n"))
6+
packet_flush(1);
7+
else if (!strcmp(line, "0001") || !strcmp(line, "0001\n"))
8+
packet_delim(1);
9+
else
10+
packet_write_fmt(1, "%s", line);
11+
}
12+
13+
static void pack(int argc, const char **argv)
14+
{
15+
if (argc) { /* read from argv */
16+
int i;
17+
for (i = 0; i < argc; i++)
18+
pack_line(argv[i]);
19+
} else { /* read from stdin */
20+
char line[LARGE_PACKET_MAX];
21+
while (fgets(line, sizeof(line), stdin)) {
22+
pack_line(line);
23+
}
24+
}
25+
}
26+
27+
static void unpack(void)
28+
{
29+
struct packet_reader reader;
30+
packet_reader_init(&reader, 0, NULL, 0,
31+
PACKET_READ_GENTLE_ON_EOF |
32+
PACKET_READ_CHOMP_NEWLINE);
33+
34+
while (packet_reader_read(&reader) != PACKET_READ_EOF) {
35+
switch (reader.status) {
36+
case PACKET_READ_EOF:
37+
break;
38+
case PACKET_READ_NORMAL:
39+
printf("%s\n", reader.line);
40+
break;
41+
case PACKET_READ_FLUSH:
42+
printf("0000\n");
43+
break;
44+
case PACKET_READ_DELIM:
45+
printf("0001\n");
46+
break;
47+
}
48+
}
49+
}
50+
51+
int cmd_main(int argc, const char **argv)
52+
{
53+
if (argc < 2)
54+
die("too few arguments");
55+
56+
if (!strcmp(argv[1], "pack"))
57+
pack(argc - 2, argv + 2);
58+
else if (!strcmp(argv[1], "unpack"))
59+
unpack();
60+
else
61+
die("invalid argument '%s'", argv[1]);
62+
63+
return 0;
64+
}

0 commit comments

Comments
 (0)