From patchwork Tue Sep 6 13:10:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Schindelin X-Patchwork-Id: 12967473 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 30012ECAAA1 for ; Tue, 6 Sep 2022 13:11:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234239AbiIFNLL (ORCPT ); Tue, 6 Sep 2022 09:11:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234144AbiIFNLI (ORCPT ); Tue, 6 Sep 2022 09:11:08 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8791C6BCE9 for ; Tue, 6 Sep 2022 06:11:02 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id b17so2043716wrq.3 for ; Tue, 06 Sep 2022 06:11:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date; bh=lK13n0J9B0+HA/bv4Q2PLLry6hE1FNg3803j+DY/GdQ=; b=Y1VAktafrTOIz4vf6+f/XeTWUdrvib+Gn/mBBpr6Me6CZn7hooHWsUkUkQmeL23BTh NQjDpqV5zTMCaDhCSPuwvEqNsrTtXGXKvGQuwsVm5Ui2gU8uwikv3NbnLIqLzGGgpsUh wvlPdfI+O42DCoXhmRUSaSlfQeaDCLGZ5yNjhcNd1A097SMsjO9jdNpNm1vcG8MIwNTu thGDQ18rFI4YvgvcJa28CR78HWOz/MX2ykPTgqehngrEchBlh2F+DdrQzATkagHcHeRx kutySizL71UupB/hfTVTrk0vs8GEATW8d4czdmicmBfBIKbbBDzm5eTjRbKNgHa6MYIn t1CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date; bh=lK13n0J9B0+HA/bv4Q2PLLry6hE1FNg3803j+DY/GdQ=; b=c1+2OdSNXwKMlnPO9P7YL8Vqr9BratUdd0lGXBh2c2onYpHidy5/plI1OlzjphbTca 6LXkKWwirdo2I6kNELb+hed1+8T8tDRZMocFeIsGK5AQxuJ3D0vu9g3JYtw/cv/XeOhp LpcPL/qiL0q7bIxJOjlsRm3xKqlxPR3d1+ZOii4yXDpM7Tt1Vw06ioXpDXN3Vy72dkI4 kd4gRjqtnvl/nkXFAvS+GwRc9xU3AyJhexzNvXOee0lgDwa6U++jiDCSW/t6thTlWS/b http7xwCey3VdoLu1sxnKe+/KS6LMqUta/A75Yok75EdyL/DRh0SiDGVTuc2bZmQ5n28 zJGg== X-Gm-Message-State: ACgBeo2FQX24xT1LFy5d0p7ctDsb7wR+Vy/4P7Ufj4K4MkaPk3IdRZqg GqjpyaUucFO6b0k7iKY8fvilN76YH3Y= X-Google-Smtp-Source: AA6agR4WKb6ikcahsyqi+vooC+xSid9iHYpLI9QXLPrp4O3DzUVp9EdFy8j8ShiEKFcW5Uz/Hd5dag== X-Received: by 2002:adf:d1c7:0:b0:226:ee75:3e73 with SMTP id b7-20020adfd1c7000000b00226ee753e73mr16947665wrd.239.1662469860761; Tue, 06 Sep 2022 06:11:00 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id bh6-20020a05600c3d0600b003a845621c5bsm15257257wmb.34.2022.09.06.06.11.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Sep 2022 06:11:00 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Tue, 06 Sep 2022 13:10:57 +0000 Subject: [PATCH v2 1/2] t0021: use Windows-friendly `pwd` Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Johannes Schindelin , Johannes Schindelin Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Johannes Schindelin From: Johannes Schindelin In Git for Windows, when passing paths from shell scripts to regular Win32 executables, thanks to the MSYS2 runtime a somewhat magic path conversion happens that lets the shell script think that there is a file at `/git/Makefile` and the Win32 process it spawned thinks that the shell script said `C:/git-sdk-64/git/Makefile` instead. This conversion is documented in detail over here: https://www.msys2.org/docs/filesystem-paths/#automatic-unix-windows-path-conversion As all automatic conversions, there are gaps. For example, to avoid mistaking command-line options like `/LOG=log.txt` (which are quite common in the Windows world) from being mistaken for a Unix-style absolute path, the MSYS2 runtime specifically exempts arguments containing a `=` character from that conversion. We are about to change `test_cmp` to use a test helper, which is such a Win32 process. In combination, this would cause a failure in `t0021-conversion.sh` where we pass an absolute path containing an equal character to the `test_cmp` function. Seeing as the Unix tools like `cp` and `diff` that are used by Git's test suite in the Git for Windows SDK (thanks to the MSYS2 project) understand both Unix-style as well as Windows-style paths, we can stave off this problem by simply switching to Windows-style paths and side-stepping the need for any automatic path conversion. Note: The `PATH` variable is obviously special, as it is colon-separated in the MSYS2 Bash used by Git for Windows, and therefore _cannot_ contain absolute Windows-style paths, lest the colon after the drive letter is mistaken for a path separator. Therefore, we need to be careful to keep the Unix-style when modifying the `PATH` variable. Signed-off-by: Johannes Schindelin --- t/t0021-conversion.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/t0021-conversion.sh b/t/t0021-conversion.sh index bad37abad2c..15482fa78e3 100755 --- a/t/t0021-conversion.sh +++ b/t/t0021-conversion.sh @@ -8,8 +8,8 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME . ./test-lib.sh . "$TEST_DIRECTORY"/lib-terminal.sh -TEST_ROOT="$PWD" -PATH=$TEST_ROOT:$PATH +PATH=$PWD:$PATH +TEST_ROOT="$(pwd)" write_script <<\EOF "$TEST_ROOT/rot13.sh" tr \ From patchwork Tue Sep 6 13:10:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Schindelin X-Patchwork-Id: 12967474 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DDC1DECAAD5 for ; Tue, 6 Sep 2022 13:11:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234277AbiIFNLM (ORCPT ); Tue, 6 Sep 2022 09:11:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233689AbiIFNLJ (ORCPT ); Tue, 6 Sep 2022 09:11:09 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21F1332B9D for ; Tue, 6 Sep 2022 06:11:03 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id t14so8107278wrx.8 for ; Tue, 06 Sep 2022 06:11:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date; bh=xPA0XII9wOIePE3Ia78NfyaiVC0AALJ9ng2C7DBKJyw=; b=CM6EK5veknFAhZE1nFGnppiVzcDqxNey038DnsXxzv6KPs1U4tD1oV7a8Mg3qAMIZQ 814Dp2whSNaC2+FDvWzOGFQ1T7M7sp4DkAyJ5rGrW8C+6EKq5/0EdoNwwCXeZB7XpC1z 64LCx11KwBFQrT930vPjt9JfNjLx4nCSVqMLQMAuYMAjfqPioCqkP3ELmXhQZB8XcqKj pw7NZdZs8I8cHyG8CxgQuZidvzB0V7xBik1K0FnEZNfIkhbt1FC6wmDMKW6pzZAxbMn6 xoVjEC59CxGemRQu0f3iclHRQ9LAdCyqbBkjDi7hrzZL1Wy+Yb7BJc2H54qqBevhNo1O 2a4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date; bh=xPA0XII9wOIePE3Ia78NfyaiVC0AALJ9ng2C7DBKJyw=; b=6TcgEEvec66D7l4EXKGWJViD0ccVcl49SSiKTxPKRbyVWpI0V8Qp7Ty3KpDOWtyrKD PobC7rggtbU2JfbZ65Sut6q0UnpArgID93tb3kqQlmriwyF5aMtpO04dQFyUwHFDNILO ZqEKoqMYLzRHraGw3GrSamTfulS5BjeDEBdrsLgl1HPyE/wDHRpEyjv5LAqKB0BPSnzJ GAdGgu3sQhzvgeyG0bNQmtFGq/8Rg+mNmUOwG7ieOT+8Jr8g/Nt0tSgwPEZ09fth3CDD jHE+UYlmx8G3/L+8hk9h/fiVc4FVv8131oGmxGeliA3/iaf1RsIxnRJsNDp72q6Xq2tk xAiA== X-Gm-Message-State: ACgBeo1vYvgp9zTErTw1RNkiqg/7bS6X1GkBsRRj4zEw+jdqymSl3BlI sJq7S9P8NQ4iyjvcnEVlhB5MWysAwAQ= X-Google-Smtp-Source: AA6agR7Aj8pEMRGUDey6BJaQhn+UOuox9vkqOKlov2w4i0nsaeNmR3OSkU1QEV689zHtSNCRyCwD0Q== X-Received: by 2002:a5d:64aa:0:b0:226:de65:810c with SMTP id m10-20020a5d64aa000000b00226de65810cmr23372414wrp.536.1662469861470; Tue, 06 Sep 2022 06:11:01 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d13-20020a05600c3acd00b003a643ac2b08sm13997637wms.8.2022.09.06.06.11.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Sep 2022 06:11:01 -0700 (PDT) Message-Id: <1f5366f137967cbec30041b40eedd86ce5f6e953.1662469859.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 06 Sep 2022 13:10:58 +0000 Subject: [PATCH v2 2/2] tests: replace mingw_test_cmp with a helper in C Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Johannes Schindelin , Johannes Schindelin Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Johannes Schindelin From: Johannes Schindelin This helper is more performant than running the `mingw_test_cmp` code 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 --- Makefile | 1 + t/helper/test-text-cmp.c | 78 ++++++++++++++++++++++++++++++++++++++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/test-lib-functions.sh | 68 +---------------------------------- t/test-lib.sh | 2 +- 6 files changed, 83 insertions(+), 68 deletions(-) create mode 100644 t/helper/test-text-cmp.c diff --git a/Makefile b/Makefile index 1624471badc..73db55bba0f 100644 --- a/Makefile +++ b/Makefile @@ -786,6 +786,7 @@ TEST_BUILTINS_OBJS += test-string-list.o TEST_BUILTINS_OBJS += test-submodule-config.o TEST_BUILTINS_OBJS += test-submodule-nested-repo-config.o TEST_BUILTINS_OBJS += test-subprocess.o +TEST_BUILTINS_OBJS += test-text-cmp.o TEST_BUILTINS_OBJS += test-trace2.o TEST_BUILTINS_OBJS += test-urlmatch-normalization.o TEST_BUILTINS_OBJS += test-userdiff.o diff --git a/t/helper/test-text-cmp.c b/t/helper/test-text-cmp.c new file mode 100644 index 00000000000..7c26d925086 --- /dev/null +++ b/t/helper/test-text-cmp.c @@ -0,0 +1,78 @@ +#include "test-tool.h" +#include "git-compat-util.h" +#include "strbuf.h" +#include "gettext.h" +#include "parse-options.h" +#include "run-command.h" + +#ifdef WIN32 +#define NO_SUCH_DIR "\\\\.\\GLOBALROOT\\invalid" +#else +#define NO_SUCH_DIR "/dev/null" +#endif + +static int run_diff(const char *path1, const char *path2) +{ + const char *argv[] = { + "diff", "--no-index", "--", NULL, NULL, NULL + }; + const char *env[] = { + "GIT_PAGER=cat", + "GIT_DIR=" NO_SUCH_DIR, + "HOME=" NO_SUCH_DIR, + NULL + }; + + argv[3] = path1; + argv[4] = path2; + return run_command_v_opt_cd_env(argv, + RUN_COMMAND_NO_STDIN | RUN_GIT_CMD, + NULL, env); +} + +int cmd__text_cmp(int argc, const char **argv) +{ + FILE *f0, *f1; + struct strbuf b0 = STRBUF_INIT, b1 = STRBUF_INIT; + + if (argc != 3) + die("Require exactly 2 arguments, got %d", argc); + + if (!strcmp(argv[1], "-") && !strcmp(argv[2], "-")) + die("only one parameter can refer to `stdin` but not both"); + + if (!(f0 = !strcmp(argv[1], "-") ? stdin : fopen(argv[1], "r"))) + return error_errno("could not open '%s'", argv[1]); + if (!(f1 = !strcmp(argv[2], "-") ? stdin : fopen(argv[2], "r"))) { + fclose(f0); + return error_errno("could not open '%s'", argv[2]); + } + + for (;;) { + int r0 = strbuf_getline(&b0, f0); + int r1 = strbuf_getline(&b1, f1); + + if (r0 == EOF) { + fclose(f0); + fclose(f1); + strbuf_release(&b0); + strbuf_release(&b1); + if (r1 == EOF) + return 0; +cmp_failed: + if (!strcmp(argv[1], "-") || !strcmp(argv[2], "-")) + warning("cannot show diff because `stdin` was already consumed"); + else if (!run_diff(argv[1], argv[2])) + die("Huh? 'diff --no-index %s %s' succeeded", + argv[1], argv[2]); + return 1; + } + if (r1 == EOF || strbuf_cmp(&b0, &b1)) { + fclose(f0); + fclose(f1); + strbuf_release(&b0); + strbuf_release(&b1); + goto cmp_failed; + } + } +} diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c index 318fdbab0c3..c6654ebc48b 100644 --- a/t/helper/test-tool.c +++ b/t/helper/test-tool.c @@ -81,6 +81,7 @@ static struct test_cmd cmds[] = { { "submodule-config", cmd__submodule_config }, { "submodule-nested-repo-config", cmd__submodule_nested_repo_config }, { "subprocess", cmd__subprocess }, + { "text-cmp", cmd__text_cmp }, { "trace2", cmd__trace2 }, { "userdiff", cmd__userdiff }, { "urlmatch-normalization", cmd__urlmatch_normalization }, diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h index bb799271631..2acfd2bcabc 100644 --- a/t/helper/test-tool.h +++ b/t/helper/test-tool.h @@ -71,6 +71,7 @@ int cmd__string_list(int argc, const char **argv); int cmd__submodule_config(int argc, const char **argv); int cmd__submodule_nested_repo_config(int argc, const char **argv); int cmd__subprocess(int argc, const char **argv); +int cmd__text_cmp(int argc, const char **argv); int cmd__trace2(int argc, const char **argv); int cmd__userdiff(int argc, const char **argv); int cmd__urlmatch_normalization(int argc, const char **argv); diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index 8c44856eaec..28eddbc8e36 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -1240,7 +1240,7 @@ test_expect_code () { test_cmp () { test "$#" -ne 2 && BUG "2 param" - eval "$GIT_TEST_CMP" '"$@"' + GIT_ALLOC_LIMIT=0 eval "$GIT_TEST_CMP" '"$@"' } # Check that the given config key has the expected value. @@ -1541,72 +1541,6 @@ test_skip_or_die () { error "$2" } -# The following mingw_* functions obey POSIX shell syntax, but are actually -# bash scripts, and are meant to be used only with bash on Windows. - -# A test_cmp function that treats LF and CRLF equal and avoids to fork -# diff when possible. -mingw_test_cmp () { - # Read text into shell variables and compare them. If the results - # are different, use regular diff to report the difference. - local test_cmp_a= test_cmp_b= - - # When text came from stdin (one argument is '-') we must feed it - # to diff. - local stdin_for_diff= - - # Since it is difficult to detect the difference between an - # empty input file and a failure to read the files, we go straight - # to diff if one of the inputs is empty. - if test -s "$1" && test -s "$2" - then - # regular case: both files non-empty - mingw_read_file_strip_cr_ test_cmp_a <"$1" - mingw_read_file_strip_cr_ test_cmp_b <"$2" - elif test -s "$1" && test "$2" = - - then - # read 2nd file from stdin - mingw_read_file_strip_cr_ test_cmp_a <"$1" - mingw_read_file_strip_cr_ test_cmp_b - stdin_for_diff='<<<"$test_cmp_b"' - elif test "$1" = - && test -s "$2" - then - # read 1st file from stdin - mingw_read_file_strip_cr_ test_cmp_a - mingw_read_file_strip_cr_ test_cmp_b <"$2" - stdin_for_diff='<<<"$test_cmp_a"' - fi - test -n "$test_cmp_a" && - test -n "$test_cmp_b" && - test "$test_cmp_a" = "$test_cmp_b" || - eval "diff -u \"\$@\" $stdin_for_diff" -} - -# $1 is the name of the shell variable to fill in -mingw_read_file_strip_cr_ () { - # Read line-wise using LF as the line separator - # and use IFS to strip CR. - local line - while : - do - if IFS=$'\r' read -r -d $'\n' line - then - # good - line=$line$'\n' - else - # we get here at EOF, but also if the last line - # was not terminated by LF; in the latter case, - # some text was read - if test -z "$line" - then - # EOF, really - break - fi - fi - eval "$1=\$$1\$line" - done -} - # Like "env FOO=BAR some-program", but run inside a subshell, which means # it also works for shell functions (though those functions cannot impact # the environment outside of the test_env invocation). diff --git a/t/test-lib.sh b/t/test-lib.sh index 7726d1da88a..0be25ecbd59 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -1546,7 +1546,7 @@ case $uname_s in test_set_prereq SED_STRIPS_CR test_set_prereq GREP_STRIPS_CR test_set_prereq WINDOWS - GIT_TEST_CMP=mingw_test_cmp + GIT_TEST_CMP="test-tool text-cmp" ;; *CYGWIN*) test_set_prereq POSIXPERM