Skip to content

Commit 2199b51

Browse files
committed
tests: replace mingw_test_cmp with a helper in C
This helper is slightly more performant than the script with MSYS2's Bash. And a lot more readable. To accommodate t1050, which wants to compare files weighing in with 3MB (falling outside of t1050's malloc limit of 1.5MB), we simply lift the allocation limit by setting the environment variable GIT_ALLOC_LIMIT to zero when calling the helper. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 9089d75 commit 2199b51

File tree

6 files changed

+78
-74
lines changed

6 files changed

+78
-74
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
693693
TEST_BUILTINS_OBJS += test-advise.o
694694
TEST_BUILTINS_OBJS += test-bloom.o
695695
TEST_BUILTINS_OBJS += test-chmtime.o
696+
TEST_BUILTINS_OBJS += test-cmp.o
696697
TEST_BUILTINS_OBJS += test-config.o
697698
TEST_BUILTINS_OBJS += test-ctype.o
698699
TEST_BUILTINS_OBJS += test-date.o

t/helper/test-cmp.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 "run-command.h"
7+
8+
#ifdef WIN32
9+
#define NO_SUCH_DIR "\\\\.\\GLOBALROOT\\invalid"
10+
#else
11+
#define NO_SUCH_DIR "/dev/null"
12+
#endif
13+
14+
static int run_diff(const char *path1, const char *path2)
15+
{
16+
const char *argv[] = {
17+
"diff", "--no-index", NULL, NULL, NULL
18+
};
19+
const char *env[] = {
20+
"GIT_PAGER=cat",
21+
"GIT_DIR=" NO_SUCH_DIR,
22+
"HOME=" NO_SUCH_DIR,
23+
NULL
24+
};
25+
26+
argv[2] = path1;
27+
argv[3] = path2;
28+
return run_command_v_opt_cd_env(argv,
29+
RUN_COMMAND_NO_STDIN | RUN_GIT_CMD,
30+
NULL, env);
31+
}
32+
33+
int cmd__cmp(int argc, const char **argv)
34+
{
35+
FILE *f0, *f1;
36+
struct strbuf b0 = STRBUF_INIT, b1 = STRBUF_INIT;
37+
38+
if (argc != 3)
39+
die("Require exactly 2 arguments, got %d", argc);
40+
41+
if (!(f0 = !strcmp(argv[1], "-") ? stdin : fopen(argv[1], "r")))
42+
return error_errno("could not open '%s'", argv[1]);
43+
if (!(f1 = !strcmp(argv[2], "-") ? stdin : fopen(argv[2], "r"))) {
44+
fclose(f0);
45+
return error_errno("could not open '%s'", argv[2]);
46+
}
47+
48+
for (;;) {
49+
int r0 = strbuf_getline(&b0, f0);
50+
int r1 = strbuf_getline(&b1, f1);
51+
52+
if (r0 == EOF) {
53+
fclose(f0);
54+
fclose(f1);
55+
strbuf_release(&b0);
56+
strbuf_release(&b1);
57+
if (r1 == EOF)
58+
return 0;
59+
cmp_failed:
60+
if (!run_diff(argv[1], argv[2]))
61+
die("Huh? 'diff --no-index %s %s' succeeded",
62+
argv[1], argv[2]);
63+
return 1;
64+
}
65+
if (r1 == EOF || strbuf_cmp(&b0, &b1)) {
66+
fclose(f0);
67+
fclose(f1);
68+
strbuf_release(&b0);
69+
strbuf_release(&b1);
70+
goto cmp_failed;
71+
}
72+
}
73+
}

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ static struct test_cmd cmds[] = {
1717
{ "advise", cmd__advise_if_enabled },
1818
{ "bloom", cmd__bloom },
1919
{ "chmtime", cmd__chmtime },
20+
{ "cmp", cmd__cmp },
2021
{ "config", cmd__config },
2122
{ "ctype", cmd__ctype },
2223
{ "date", cmd__date },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
int cmd__advise_if_enabled(int argc, const char **argv);
88
int cmd__bloom(int argc, const char **argv);
99
int cmd__chmtime(int argc, const char **argv);
10+
int cmd__cmp(int argc, const char **argv);
1011
int cmd__config(int argc, const char **argv);
1112
int cmd__ctype(int argc, const char **argv);
1213
int cmd__date(int argc, const char **argv);

t/test-lib-functions.sh

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -952,13 +952,7 @@ test_expect_code () {
952952
# - not all diff versions understand "-u"
953953

954954
test_cmp() {
955-
test $# -eq 2 || BUG "test_cmp requires two arguments"
956-
if ! eval "$GIT_TEST_CMP" '"$@"'
957-
then
958-
test "x$1" = x- || test -e "$1" || BUG "test_cmp '$1' missing"
959-
test "x$2" = x- || test -e "$2" || BUG "test_cmp '$2' missing"
960-
return 1
961-
fi
955+
GIT_ALLOC_LIMIT=0 eval "$GIT_TEST_CMP" '"$@"'
962956
}
963957

964958
# Check that the given config key has the expected value.
@@ -1289,72 +1283,6 @@ test_skip_or_die () {
12891283
error "$2"
12901284
}
12911285

1292-
# The following mingw_* functions obey POSIX shell syntax, but are actually
1293-
# bash scripts, and are meant to be used only with bash on Windows.
1294-
1295-
# A test_cmp function that treats LF and CRLF equal and avoids to fork
1296-
# diff when possible.
1297-
mingw_test_cmp () {
1298-
# Read text into shell variables and compare them. If the results
1299-
# are different, use regular diff to report the difference.
1300-
local test_cmp_a= test_cmp_b=
1301-
1302-
# When text came from stdin (one argument is '-') we must feed it
1303-
# to diff.
1304-
local stdin_for_diff=
1305-
1306-
# Since it is difficult to detect the difference between an
1307-
# empty input file and a failure to read the files, we go straight
1308-
# to diff if one of the inputs is empty.
1309-
if test -s "$1" && test -s "$2"
1310-
then
1311-
# regular case: both files non-empty
1312-
mingw_read_file_strip_cr_ test_cmp_a <"$1"
1313-
mingw_read_file_strip_cr_ test_cmp_b <"$2"
1314-
elif test -s "$1" && test "$2" = -
1315-
then
1316-
# read 2nd file from stdin
1317-
mingw_read_file_strip_cr_ test_cmp_a <"$1"
1318-
mingw_read_file_strip_cr_ test_cmp_b
1319-
stdin_for_diff='<<<"$test_cmp_b"'
1320-
elif test "$1" = - && test -s "$2"
1321-
then
1322-
# read 1st file from stdin
1323-
mingw_read_file_strip_cr_ test_cmp_a
1324-
mingw_read_file_strip_cr_ test_cmp_b <"$2"
1325-
stdin_for_diff='<<<"$test_cmp_a"'
1326-
fi
1327-
test -n "$test_cmp_a" &&
1328-
test -n "$test_cmp_b" &&
1329-
test "$test_cmp_a" = "$test_cmp_b" ||
1330-
eval "diff -u \"\$@\" $stdin_for_diff"
1331-
}
1332-
1333-
# $1 is the name of the shell variable to fill in
1334-
mingw_read_file_strip_cr_ () {
1335-
# Read line-wise using LF as the line separator
1336-
# and use IFS to strip CR.
1337-
local line
1338-
while :
1339-
do
1340-
if IFS=$'\r' read -r -d $'\n' line
1341-
then
1342-
# good
1343-
line=$line$'\n'
1344-
else
1345-
# we get here at EOF, but also if the last line
1346-
# was not terminated by LF; in the latter case,
1347-
# some text was read
1348-
if test -z "$line"
1349-
then
1350-
# EOF, really
1351-
break
1352-
fi
1353-
fi
1354-
eval "$1=\$$1\$line"
1355-
done
1356-
}
1357-
13581286
# Like "env FOO=BAR some-program", but run inside a subshell, which means
13591287
# it also works for shell functions (though those functions cannot impact
13601288
# the environment outside of the test_env invocation).

t/test-lib.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ case $uname_s in
14821482
test_set_prereq NATIVE_CRLF
14831483
test_set_prereq SED_STRIPS_CR
14841484
test_set_prereq GREP_STRIPS_CR
1485-
GIT_TEST_CMP=mingw_test_cmp
1485+
GIT_TEST_CMP="test-tool cmp"
14861486
;;
14871487
*CYGWIN*)
14881488
test_set_prereq POSIXPERM

0 commit comments

Comments
 (0)