Skip to content

Commit 23b4adc

Browse files
committed
test-tool: 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 cde2063 commit 23b4adc

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ TEST_BUILTINS_OBJS += test-hash-speed.o
812812
TEST_BUILTINS_OBJS += test-hash.o
813813
TEST_BUILTINS_OBJS += test-hashmap.o
814814
TEST_BUILTINS_OBJS += test-hexdump.o
815+
TEST_BUILTINS_OBJS += test-iconv.o
815816
TEST_BUILTINS_OBJS += test-index-version.o
816817
TEST_BUILTINS_OBJS += test-json-writer.o
817818
TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o

t/helper/test-iconv.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "test-tool.h"
2+
#include "git-compat-util.h"
3+
#include "strbuf.h"
4+
#include "gettext.h"
5+
#include "parse-options.h"
6+
#include "utf8.h"
7+
8+
int cmd__iconv(int argc, const char **argv)
9+
{
10+
struct strbuf buf = STRBUF_INIT;
11+
char *from = NULL, *to = NULL, *p;
12+
size_t len;
13+
int ret = 0;
14+
const char * const iconv_usage[] = {
15+
N_("test-helper --iconv [<options>]"),
16+
NULL
17+
};
18+
struct option options[] = {
19+
OPT_STRING('f', "from-code", &from, "encoding", "from"),
20+
OPT_STRING('t', "to-code", &to, "encoding", "to"),
21+
OPT_END()
22+
};
23+
24+
argc = parse_options(argc, argv, NULL, options,
25+
iconv_usage, 0);
26+
27+
if (argc > 1 || !from || !to)
28+
usage_with_options(iconv_usage, options);
29+
30+
if (!argc) {
31+
if (strbuf_read(&buf, 0, 2048) < 0)
32+
die_errno("Could not read from stdin");
33+
} else if (strbuf_read_file(&buf, argv[0], 2048) < 0)
34+
die_errno("Could not read from '%s'", argv[0]);
35+
36+
p = reencode_string_len(buf.buf, buf.len, to, from, &len);
37+
if (!p)
38+
die_errno("Could not reencode");
39+
if (write(1, p, len) < 0)
40+
ret = !!error_errno("Could not write %"PRIuMAX" bytes",
41+
(uintmax_t)len);
42+
43+
strbuf_release(&buf);
44+
free(p);
45+
46+
return ret;
47+
}

t/helper/test-tool.c

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static struct test_cmd cmds[] = {
3838
{ "hashmap", cmd__hashmap },
3939
{ "hash-speed", cmd__hash_speed },
4040
{ "hexdump", cmd__hexdump },
41+
{ "iconv", cmd__iconv },
4142
{ "index-version", cmd__index_version },
4243
{ "json-writer", cmd__json_writer },
4344
{ "lazy-init-name-hash", cmd__lazy_init_name_hash },

t/helper/test-tool.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ int cmd__getcwd(int argc, const char **argv);
3232
int cmd__hashmap(int argc, const char **argv);
3333
int cmd__hash_speed(int argc, const char **argv);
3434
int cmd__hexdump(int argc, const char **argv);
35+
int cmd__iconv(int argc, const char **argv);
3536
int cmd__index_version(int argc, const char **argv);
3637
int cmd__json_writer(int argc, const char **argv);
3738
int cmd__lazy_init_name_hash(int argc, const char **argv);

0 commit comments

Comments
 (0)