Skip to content

Commit 2e2619f

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 acfba9a commit 2e2619f

File tree

4 files changed

+72
-68
lines changed

4 files changed

+72
-68
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ TEST_PROGRAMS_NEED_X += test-example-decorate
665665
TEST_PROGRAMS_NEED_X += test-fake-ssh
666666
TEST_PROGRAMS_NEED_X += test-genrandom
667667
TEST_PROGRAMS_NEED_X += test-hashmap
668+
TEST_PROGRAMS_NEED_X += test-helper
668669
TEST_PROGRAMS_NEED_X += test-index-version
669670
TEST_PROGRAMS_NEED_X += test-lazy-init-name-hash
670671
TEST_PROGRAMS_NEED_X += test-line-buffer

t/helper/test-helper.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "git-compat-util.h"
2+
#include "strbuf.h"
3+
#include "gettext.h"
4+
#include "parse-options.h"
5+
6+
static const char * const test_helper_usage[] = {
7+
N_("test-helper [<options>]"),
8+
NULL
9+
};
10+
11+
static int cmp(int argc, const char **argv)
12+
{
13+
FILE *f0, *f1;
14+
struct strbuf b0 = STRBUF_INIT, b1 = STRBUF_INIT;
15+
16+
if (argc != 3)
17+
die("Require exactly 2 arguments, got %d", argc);
18+
19+
if (!(f0 = !strcmp(argv[1], "-") ? stdin : fopen(argv[1], "r")))
20+
return error_errno("could not open '%s'", argv[1]);
21+
if (!(f1 = !strcmp(argv[2], "-") ? stdin : fopen(argv[2], "r"))) {
22+
fclose(f0);
23+
return error_errno("could not open '%s'", argv[2]);
24+
}
25+
26+
for (;;) {
27+
int r0 = strbuf_getline(&b0, f0);
28+
int r1 = strbuf_getline(&b1, f1);
29+
30+
if (r0 == EOF) {
31+
fclose(f0);
32+
fclose(f1);
33+
strbuf_release(&b0);
34+
strbuf_release(&b1);
35+
if (r1 == EOF)
36+
return 0;
37+
return 1;
38+
}
39+
if (r1 == EOF || strbuf_cmp(&b0, &b1)) {
40+
fclose(f0);
41+
fclose(f1);
42+
strbuf_release(&b0);
43+
strbuf_release(&b1);
44+
return 1;
45+
}
46+
}
47+
}
48+
49+
int cmd_main(int argc, const char **argv)
50+
{
51+
enum mode {
52+
CMP = 1
53+
} command = 0;
54+
struct option options[] = {
55+
OPT_CMDMODE(0, "cmp", &command,
56+
N_("compare files (ignoring LF vs CR/LF)"), CMP),
57+
OPT_END()
58+
};
59+
60+
argc = parse_options(argc, argv, NULL, options,
61+
test_helper_usage,
62+
PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);
63+
64+
if (command == CMP)
65+
return !!cmp(argc, argv);
66+
67+
die("unhandled mode");
68+
}
69+

t/test-lib-functions.sh

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ test_expect_code () {
706706
# - not all diff versions understand "-u"
707707

708708
test_cmp() {
709-
$GIT_TEST_CMP "$@"
709+
GIT_ALLOC_LIMIT=0 $GIT_TEST_CMP "$@"
710710
}
711711

712712
# test_cmp_bin - helper to compare binary files
@@ -957,72 +957,6 @@ test_skip_or_die () {
957957
esac
958958
}
959959

960-
# The following mingw_* functions obey POSIX shell syntax, but are actually
961-
# bash scripts, and are meant to be used only with bash on Windows.
962-
963-
# A test_cmp function that treats LF and CRLF equal and avoids to fork
964-
# diff when possible.
965-
mingw_test_cmp () {
966-
# Read text into shell variables and compare them. If the results
967-
# are different, use regular diff to report the difference.
968-
local test_cmp_a= test_cmp_b=
969-
970-
# When text came from stdin (one argument is '-') we must feed it
971-
# to diff.
972-
local stdin_for_diff=
973-
974-
# Since it is difficult to detect the difference between an
975-
# empty input file and a failure to read the files, we go straight
976-
# to diff if one of the inputs is empty.
977-
if test -s "$1" && test -s "$2"
978-
then
979-
# regular case: both files non-empty
980-
mingw_read_file_strip_cr_ test_cmp_a <"$1"
981-
mingw_read_file_strip_cr_ test_cmp_b <"$2"
982-
elif test -s "$1" && test "$2" = -
983-
then
984-
# read 2nd file from stdin
985-
mingw_read_file_strip_cr_ test_cmp_a <"$1"
986-
mingw_read_file_strip_cr_ test_cmp_b
987-
stdin_for_diff='<<<"$test_cmp_b"'
988-
elif test "$1" = - && test -s "$2"
989-
then
990-
# read 1st file from stdin
991-
mingw_read_file_strip_cr_ test_cmp_a
992-
mingw_read_file_strip_cr_ test_cmp_b <"$2"
993-
stdin_for_diff='<<<"$test_cmp_a"'
994-
fi
995-
test -n "$test_cmp_a" &&
996-
test -n "$test_cmp_b" &&
997-
test "$test_cmp_a" = "$test_cmp_b" ||
998-
eval "diff -u \"\$@\" $stdin_for_diff"
999-
}
1000-
1001-
# $1 is the name of the shell variable to fill in
1002-
mingw_read_file_strip_cr_ () {
1003-
# Read line-wise using LF as the line separator
1004-
# and use IFS to strip CR.
1005-
local line
1006-
while :
1007-
do
1008-
if IFS=$'\r' read -r -d $'\n' line
1009-
then
1010-
# good
1011-
line=$line$'\n'
1012-
else
1013-
# we get here at EOF, but also if the last line
1014-
# was not terminated by LF; in the latter case,
1015-
# some text was read
1016-
if test -z "$line"
1017-
then
1018-
# EOF, really
1019-
break
1020-
fi
1021-
fi
1022-
eval "$1=\$$1\$line"
1023-
done
1024-
}
1025-
1026960
# Like "env FOO=BAR some-program", but run inside a subshell, which means
1027961
# it also works for shell functions (though those functions cannot impact
1028962
# 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
@@ -1053,7 +1053,7 @@ case $uname_s in
10531053
test_set_prereq NATIVE_CRLF
10541054
test_set_prereq SED_STRIPS_CR
10551055
test_set_prereq GREP_STRIPS_CR
1056-
GIT_TEST_CMP=mingw_test_cmp
1056+
GIT_TEST_CMP="test-helper --cmp"
10571057
;;
10581058
*CYGWIN*)
10591059
test_set_prereq POSIXPERM

0 commit comments

Comments
 (0)