Skip to content

Commit 0adc7cd

Browse files
committed
test-helper: learn to act as a drop-in replacement for iconv
It is convenient to assume that everybody who wants to build & test Git has access to a working `iconv` executable (after all, we already pretty much require libiconv) However, that limits esoteric test scenarios such as Git for Windows', where an end user installation has to ship with `iconv` for the sole purpose of being testable. That payload serves no other purpose. So let's just have a test helper (to be able to test Git, the test helpers have to be available, after all) to act as `iconv` replacement. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 2e2619f commit 0adc7cd

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

t/helper/test-helper.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "strbuf.h"
33
#include "gettext.h"
44
#include "parse-options.h"
5+
#include "utf8.h"
56

67
static const char * const test_helper_usage[] = {
78
N_("test-helper [<options>]"),
@@ -46,14 +47,55 @@ static int cmp(int argc, const char **argv)
4647
}
4748
}
4849

50+
static int iconv_(int argc, const char **argv)
51+
{
52+
struct strbuf buf = STRBUF_INIT;
53+
char *from = NULL, *to = NULL, *p;
54+
int len, ret = 0;
55+
const char * const iconv_usage[] = {
56+
N_("test-helper --iconv [<options>]"),
57+
NULL
58+
};
59+
struct option options[] = {
60+
OPT_STRING('f', "from-code", &from, "encoding", "from"),
61+
OPT_STRING('t', "to-code", &to, "encoding", "to"),
62+
OPT_END()
63+
};
64+
65+
argc = parse_options(argc, argv, NULL, options,
66+
iconv_usage, 0);
67+
68+
if (argc > 1 || !from || !to)
69+
usage_with_options(iconv_usage, options);
70+
71+
if (!argc) {
72+
if (strbuf_read(&buf, 0, 2048) < 0)
73+
die_errno("Could not read from stdin");
74+
} else if (strbuf_read_file(&buf, argv[0], 2048) < 0)
75+
die_errno("Could not read from '%s'", argv[0]);
76+
77+
p = reencode_string_len(buf.buf, buf.len, to, from, &len);
78+
if (!p)
79+
die_errno("Could not reencode");
80+
if (write(1, p, len) < 0)
81+
ret = !!error_errno("Could not write %d bytes", len);
82+
83+
strbuf_release(&buf);
84+
free(p);
85+
86+
return ret;
87+
}
88+
4989
int cmd_main(int argc, const char **argv)
5090
{
5191
enum mode {
52-
CMP = 1
92+
CMP = 1, ICONV
5393
} command = 0;
5494
struct option options[] = {
5595
OPT_CMDMODE(0, "cmp", &command,
5696
N_("compare files (ignoring LF vs CR/LF)"), CMP),
97+
OPT_CMDMODE(0, "iconv", &command,
98+
N_("act as drop-in replacement for `iconv`"), ICONV),
5799
OPT_END()
58100
};
59101

@@ -63,6 +105,8 @@ int cmd_main(int argc, const char **argv)
63105

64106
if (command == CMP)
65107
return !!cmp(argc, argv);
108+
if (command == ICONV)
109+
return !!iconv_(argc, argv);
66110

67111
die("unhandled mode");
68112
}

0 commit comments

Comments
 (0)