* [PATCH] t/test_lib: avoid naked bash arrays in file_lineno @ 2020-05-07 5:51 Carlo Marcelo Arenas Belón 2020-05-07 14:53 ` Junio C Hamano ` (2 more replies) 0 siblings, 3 replies; 8+ messages in thread From: Carlo Marcelo Arenas Belón @ 2020-05-07 5:51 UTC (permalink / raw) To: git Cc: congdanhqx, gitster, Carlo Marcelo Arenas Belón, Johannes Schindelin 662f9cf154 (tests: when run in Bash, annotate test failures with file name/line number, 2020-04-11), introduces a way to report the location (file:lineno) of a failed test case by traversing the bash callstack. The implementation requires bash and is therefore protected by a guard but NetBSD sh will still have to parse the function and therefore will result in: ** t0000-basic.sh *** ./test-lib.sh: 681: Syntax error: Bad substitution Enclose the bash specific code inside an eval to avoid parsing errors and while at it, simplify the logic so that instead of traversing the callstack just pop the two topmost entries that are required. Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com> --- t/test-lib.sh | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 1b221951a8..60b8e70678 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -677,14 +677,13 @@ die () { file_lineno () { test -z "$GIT_TEST_FRAMEWORK_SELFTEST" && test -n "$BASH" || return 0 - local i - for i in ${!BASH_SOURCE[*]} - do - case $i,"${BASH_SOURCE[$i]##*/}" in - 0,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:$LINENO: ${1+$1: }"; return;; - *,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:${BASH_LINENO[$(($i-1))]}: ${1+$1: }"; return;; - esac - done + eval ' + local n=$(echo ${#BASH_SOURCE[*]}) + if test $n -ge 2 + then + echo "${BASH_SOURCE[$n - 1]}:${BASH_LINENO[$n - 2]}: $1: " + fi + ' } GIT_EXIT_OK= -- 2.26.2.686.gfaf46a9ccd ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] t/test_lib: avoid naked bash arrays in file_lineno 2020-05-07 5:51 [PATCH] t/test_lib: avoid naked bash arrays in file_lineno Carlo Marcelo Arenas Belón @ 2020-05-07 14:53 ` Junio C Hamano 2020-05-07 17:33 ` Carlo Arenas 2020-05-07 17:57 ` Carlo Marcelo Arenas Belón 2020-05-07 19:52 ` Johannes Schindelin 2 siblings, 1 reply; 8+ messages in thread From: Junio C Hamano @ 2020-05-07 14:53 UTC (permalink / raw) To: Carlo Marcelo Arenas Belón; +Cc: git, congdanhqx, Johannes Schindelin Carlo Marcelo Arenas Belón <carenas@gmail.com> writes: > 662f9cf154 (tests: when run in Bash, annotate test failures with file > name/line number, 2020-04-11), introduces a way to report the location > (file:lineno) of a failed test case by traversing the bash callstack. > > The implementation requires bash and is therefore protected by a guard > but NetBSD sh will still have to parse the function and therefore will > result in: > > ** t0000-basic.sh *** > ./test-lib.sh: 681: Syntax error: Bad substitution > > Enclose the bash specific code inside an eval to avoid parsing errors > and while at it, simplify the logic so that instead of traversing the > callstack just pop the two topmost entries that are required. > > Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Is the rewritten bash-snippet in the evaled text what Dscho suggested us to use, or is it totally yours? I know Dscho helped to shoot down some "simpler" reimplementations you came up with, pointing out how they were broken, but it is unclear how we ended up with this version. I wish you didn't do the "while at it" rewrite in the same patch. If it were only "put bash-only stuff in an evaled string", it would have been a lot more comfortable applying it and merging quickly down, as it would be clear that we won't be breaking bash codepath and we'd be helping other shells. With the "while at it", you made it quite unclear. > Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com> > --- > t/test-lib.sh | 15 +++++++-------- > 1 file changed, 7 insertions(+), 8 deletions(-) > > diff --git a/t/test-lib.sh b/t/test-lib.sh > index 1b221951a8..60b8e70678 100644 > --- a/t/test-lib.sh > +++ b/t/test-lib.sh > @@ -677,14 +677,13 @@ die () { > > file_lineno () { > test -z "$GIT_TEST_FRAMEWORK_SELFTEST" && test -n "$BASH" || return 0 > - local i > - for i in ${!BASH_SOURCE[*]} > - do > - case $i,"${BASH_SOURCE[$i]##*/}" in > - 0,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:$LINENO: ${1+$1: }"; return;; > - *,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:${BASH_LINENO[$(($i-1))]}: ${1+$1: }"; return;; > - esac > - done > + eval ' > + local n=$(echo ${#BASH_SOURCE[*]}) > + if test $n -ge 2 > + then > + echo "${BASH_SOURCE[$n - 1]}:${BASH_LINENO[$n - 2]}: $1: " > + fi > + ' > } > > GIT_EXIT_OK= ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] t/test_lib: avoid naked bash arrays in file_lineno 2020-05-07 14:53 ` Junio C Hamano @ 2020-05-07 17:33 ` Carlo Arenas 0 siblings, 0 replies; 8+ messages in thread From: Carlo Arenas @ 2020-05-07 17:33 UTC (permalink / raw) To: Junio C Hamano; +Cc: git, congdanhqx, Johannes Schindelin On Thu, May 7, 2020 at 7:53 AM Junio C Hamano <gitster@pobox.com> wrote: > > Carlo Marcelo Arenas Belón <carenas@gmail.com> writes: > > > 662f9cf154 (tests: when run in Bash, annotate test failures with file > > name/line number, 2020-04-11), introduces a way to report the location > > (file:lineno) of a failed test case by traversing the bash callstack. > > > > The implementation requires bash and is therefore protected by a guard > > but NetBSD sh will still have to parse the function and therefore will > > result in: > > > > ** t0000-basic.sh *** > > ./test-lib.sh: 681: Syntax error: Bad substitution > > > > Enclose the bash specific code inside an eval to avoid parsing errors > > and while at it, simplify the logic so that instead of traversing the > > callstack just pop the two topmost entries that are required. > > > > Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> > > Is the rewritten bash-snippet in the evaled text what Dscho > suggested us to use, or is it totally yours? It is all mine, but Dscho's detailed description of the implementation it is replacing allowed me to validate its correctness (under the documented use cases) my hope was that Dscho's and Danh would quickly agree as it is simpler and faster, as well as providing some more description of its operation in the commit message for future reviewers. > I know Dscho helped to > shoot down some "simpler" reimplementations you came up with, > pointing out how they were broken, but it is unclear how we ended up > with this version. I made the wrong assumptions while focusing on translating the code to posix sh and ended up with a broken version. Another reason why I did the "while at it" was to avoid someone else having the same problem by simplifying it and making sure that uncommon syntax (like ${1+$1..}, or the use of $LINENO which would never apply to our code) weren't needed. > I wish you didn't do the "while at it" rewrite in the same patch. > If it were only "put bash-only stuff in an evaled string", it would > have been a lot more comfortable applying it and merging quickly > down, as it would be clear that we won't be breaking bash codepath > and we'd be helping other shells. With the "while at it", you made > it quite unclear. fair enough, and considering that "fixing other shell" has higher priority will send a v2 to do so. will do any "simplification" on a follow up and try to monitor these issues early Carlo ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH] t/test_lib: avoid naked bash arrays in file_lineno 2020-05-07 5:51 [PATCH] t/test_lib: avoid naked bash arrays in file_lineno Carlo Marcelo Arenas Belón 2020-05-07 14:53 ` Junio C Hamano @ 2020-05-07 17:57 ` Carlo Marcelo Arenas Belón 2020-05-08 23:53 ` Johannes Schindelin 2020-05-07 19:52 ` Johannes Schindelin 2 siblings, 1 reply; 8+ messages in thread From: Carlo Marcelo Arenas Belón @ 2020-05-07 17:57 UTC (permalink / raw) To: git; +Cc: gitster, Carlo Marcelo Arenas Belón 662f9cf154 (tests: when run in Bash, annotate test failures with file name/line number, 2020-04-11), introduces a way to report the location (file:lineno) of a failed test case by traversing the bash callstack. The implementation requires bash and uses shell arrays and is therefore protected by a guard but NetBSD sh will still have to parse the function and therefore will result in: ** t0000-basic.sh *** ./test-lib.sh: 681: Syntax error: Bad substitution Enclose the bash specific code inside an eval to avoid parsing errors in the same way than 5826b7b595 (test-lib: check Bash version for '-x' without using shell arrays, 2019-01-03) Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com> --- t/test-lib.sh | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 1b221951a8..baf94546da 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -677,14 +677,16 @@ die () { file_lineno () { test -z "$GIT_TEST_FRAMEWORK_SELFTEST" && test -n "$BASH" || return 0 - local i - for i in ${!BASH_SOURCE[*]} - do - case $i,"${BASH_SOURCE[$i]##*/}" in - 0,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:$LINENO: ${1+$1: }"; return;; - *,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:${BASH_LINENO[$(($i-1))]}: ${1+$1: }"; return;; - esac - done + eval ' + local i + for i in ${!BASH_SOURCE[*]} + do + case $i,"${BASH_SOURCE[$i]##*/}" in + 0,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:$LINENO: ${1+$1: }"; return;; + *,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:${BASH_LINENO[$(($i-1))]}: ${1+$1: }"; return;; + esac + done + ' } GIT_EXIT_OK= -- 2.26.2.717.g5cccb0e1a8 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] t/test_lib: avoid naked bash arrays in file_lineno 2020-05-07 17:57 ` Carlo Marcelo Arenas Belón @ 2020-05-08 23:53 ` Johannes Schindelin 0 siblings, 0 replies; 8+ messages in thread From: Johannes Schindelin @ 2020-05-08 23:53 UTC (permalink / raw) To: Carlo Marcelo Arenas Belón; +Cc: git, gitster [-- Attachment #1: Type: text/plain, Size: 2251 bytes --] Hi Carlo, this is v2, right? The subject says `[PATCH]`, not `[PATCH v2]`, so I am just trying to make sure. It's things like this why I never use `git send-email` manually anymore. Yes, sure, GitGitGadget helps others, too, but its initial primary goal was to help me not botch my patch submissions. On Thu, 7 May 2020, Carlo Marcelo Arenas Belón wrote: > 662f9cf154 (tests: when run in Bash, annotate test failures with file > name/line number, 2020-04-11), introduces a way to report the location > (file:lineno) of a failed test case by traversing the bash callstack. > > The implementation requires bash and uses shell arrays and is therefore > protected by a guard but NetBSD sh will still have to parse the function > and therefore will result in: > > ** t0000-basic.sh *** > ./test-lib.sh: 681: Syntax error: Bad substitution > > Enclose the bash specific code inside an eval to avoid parsing errors in > the same way than 5826b7b595 (test-lib: check Bash version for '-x' > without using shell arrays, 2019-01-03) > > Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com> I still provided help, I hope? > --- > t/test-lib.sh | 18 ++++++++++-------- > 1 file changed, 10 insertions(+), 8 deletions(-) > > diff --git a/t/test-lib.sh b/t/test-lib.sh > index 1b221951a8..baf94546da 100644 > --- a/t/test-lib.sh > +++ b/t/test-lib.sh > @@ -677,14 +677,16 @@ die () { > > file_lineno () { > test -z "$GIT_TEST_FRAMEWORK_SELFTEST" && test -n "$BASH" || return 0 > - local i > - for i in ${!BASH_SOURCE[*]} > - do > - case $i,"${BASH_SOURCE[$i]##*/}" in > - 0,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:$LINENO: ${1+$1: }"; return;; > - *,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:${BASH_LINENO[$(($i-1))]}: ${1+$1: }"; return;; > - esac > - done > + eval ' > + local i > + for i in ${!BASH_SOURCE[*]} > + do > + case $i,"${BASH_SOURCE[$i]##*/}" in > + 0,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:$LINENO: ${1+$1: }"; return;; > + *,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:${BASH_LINENO[$(($i-1))]}: ${1+$1: }"; return;; > + esac > + done > + ' This one looks correct to me. Ciao, Dscho > } > > GIT_EXIT_OK= > -- > 2.26.2.717.g5cccb0e1a8 > > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] t/test_lib: avoid naked bash arrays in file_lineno 2020-05-07 5:51 [PATCH] t/test_lib: avoid naked bash arrays in file_lineno Carlo Marcelo Arenas Belón 2020-05-07 14:53 ` Junio C Hamano 2020-05-07 17:57 ` Carlo Marcelo Arenas Belón @ 2020-05-07 19:52 ` Johannes Schindelin 2020-05-08 0:58 ` Carlo Marcelo Arenas Belón 2 siblings, 1 reply; 8+ messages in thread From: Johannes Schindelin @ 2020-05-07 19:52 UTC (permalink / raw) To: Carlo Marcelo Arenas Belón; +Cc: git, congdanhqx, gitster [-- Attachment #1: Type: text/plain, Size: 2891 bytes --] Hi Carlo, On Wed, 6 May 2020, Carlo Marcelo Arenas Belón wrote: > 662f9cf154 (tests: when run in Bash, annotate test failures with file > name/line number, 2020-04-11), introduces a way to report the location > (file:lineno) of a failed test case by traversing the bash callstack. > > The implementation requires bash and is therefore protected by a guard > but NetBSD sh will still have to parse the function and therefore will > result in: > > ** t0000-basic.sh *** > ./test-lib.sh: 681: Syntax error: Bad substitution > > Enclose the bash specific code inside an eval to avoid parsing errors > and while at it, simplify the logic so that instead of traversing the > callstack just pop the two topmost entries that are required. I would be okay with that, but that's not what the patch does: > Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> > Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com> > --- > t/test-lib.sh | 15 +++++++-------- > 1 file changed, 7 insertions(+), 8 deletions(-) > > diff --git a/t/test-lib.sh b/t/test-lib.sh > index 1b221951a8..60b8e70678 100644 > --- a/t/test-lib.sh > +++ b/t/test-lib.sh > @@ -677,14 +677,13 @@ die () { > > file_lineno () { > test -z "$GIT_TEST_FRAMEWORK_SELFTEST" && test -n "$BASH" || return 0 > - local i > - for i in ${!BASH_SOURCE[*]} > - do > - case $i,"${BASH_SOURCE[$i]##*/}" in > - 0,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:$LINENO: ${1+$1: }"; return;; > - *,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:${BASH_LINENO[$(($i-1))]}: ${1+$1: }"; return;; > - esac > - done Note how this `for` loop stops as soon as it finds a `t/[0-9]*.sh` in the backtrace? > + eval ' > + local n=$(echo ${#BASH_SOURCE[*]}) > + if test $n -ge 2 > + then > + echo "${BASH_SOURCE[$n - 1]}:${BASH_LINENO[$n - 2]}: $1: " > + fi This loop is completely lost here. Now, you could argue that your version is better because it avoids the loop and always shows the ultimate caller in the backtrace. However, I would then immediately point you to `t/t0027-auto-crlf.sh` and ask whether it is all that useful to the `commit_chk_wrnNNO` call rather than one of the five `test_expect_success` calls contained in that function. Besides, you simply replaces `${1+$1: }` by `$1: `. Again, you could argue that your version is better because there is now only one caller, and it always passes `error` as first parameter. I would not be _too_ happy about losing the logic that allows calling `file_lineno` in other circumstances (I found it valuable for debugging), but _in the least_ you need to talk about this change in the commit message. Just changing the behavior without even describing, let alone justifying, it in the commit message is a bad idea. Ciao, Dscho > + ' > } > > GIT_EXIT_OK= > -- > 2.26.2.686.gfaf46a9ccd > > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] t/test_lib: avoid naked bash arrays in file_lineno 2020-05-07 19:52 ` Johannes Schindelin @ 2020-05-08 0:58 ` Carlo Marcelo Arenas Belón 2020-05-08 1:17 ` Junio C Hamano 0 siblings, 1 reply; 8+ messages in thread From: Carlo Marcelo Arenas Belón @ 2020-05-08 0:58 UTC (permalink / raw) To: Johannes Schindelin; +Cc: git, congdanhqx, gitster On Thu, May 07, 2020 at 09:52:12PM +0200, Johannes Schindelin wrote: > On Wed, 6 May 2020, Carlo Marcelo Arenas Belón wrote: > > > > Enclose the bash specific code inside an eval to avoid parsing errors > > and while at it, simplify the logic so that instead of traversing the > > callstack just pop the two topmost entries that are required. > > I would be okay with that, but that's not what the patch does: FWIW that was the intention, but luckily Junio quickly predicted it was most likely buggy and so has been since made obsolete by: https://lore.kernel.org/git/20200507175706.19986-1-carenas@gmail.com/ > > + eval ' > > + local n=$(echo ${#BASH_SOURCE[*]}) > > + if test $n -ge 2 > > + then > > + echo "${BASH_SOURCE[$n - 1]}:${BASH_LINENO[$n - 2]}: $1: " > > + fi > > This loop is completely lost here. > > Now, you could argue that your version is better because it avoids the > loop and always shows the ultimate caller in the backtrace. > > However, I would then immediately point you to `t/t0027-auto-crlf.sh` and > ask whether it is all that useful to the `commit_chk_wrnNNO` call rather > than one of the five `test_expect_success` calls contained in that > function. as I said originally, I thought it would had been even better to be specific up to the line number of the instruction that failed, after all we already have the whole function code for context in the message. > Besides, you simply replaces `${1+$1: }` by `$1: `. I couldn't figure out why that was needed, but while I am just slow, this one is actually genius!; you are using a shell parameter expansion (which is even POSIX sh compatible) to do string concatenation! conditionally, noneless and not even using a silly if (like I would have done) or a bash += concatenation operator. > Again, you could argue that your version is better because there is now > only one caller, and it always passes `error` as first parameter. > > I would not be _too_ happy about losing the logic that allows calling > `file_lineno` in other circumstances (I found it valuable for debugging), > but _in the least_ you need to talk about this change in the commit > message. Just changing the behavior without even describing, let alone > justifying, it in the commit message is a bad idea. fair enough; but in my defense, I couldn't had been made aware this was being used also for debugging, or called without the parameter or even called in a way where it will report its own LINENO (which you clearly explained before as being the wrong value to report and that lead to my confused first attempt at porting it) thanks again, for your insightful feedback, and guess then there is no more point on trying to simplify it further, and if you could ACK my other proposal, hopefully neither a need to find workarounds to running tests on NetBSD for the current master. if we decide later into improving the accuracy of the report, might be worth documenting some of what I learned in the commit message, for the future reviewers, who hopefully will be less thick headed. Carlo PS. I know I botched the v2 git send-email, so you didn't get a CC, or even have the v2 in the subject, hope it would be still good enough but let me know otherwise. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] t/test_lib: avoid naked bash arrays in file_lineno 2020-05-08 0:58 ` Carlo Marcelo Arenas Belón @ 2020-05-08 1:17 ` Junio C Hamano 0 siblings, 0 replies; 8+ messages in thread From: Junio C Hamano @ 2020-05-08 1:17 UTC (permalink / raw) To: Carlo Marcelo Arenas Belón; +Cc: Johannes Schindelin, git, congdanhqx Carlo Marcelo Arenas Belón <carenas@gmail.com> writes: > On Thu, May 07, 2020 at 09:52:12PM +0200, Johannes Schindelin wrote: >> On Wed, 6 May 2020, Carlo Marcelo Arenas Belón wrote: >> > >> > Enclose the bash specific code inside an eval to avoid parsing errors >> > and while at it, simplify the logic so that instead of traversing the >> > callstack just pop the two topmost entries that are required. >> >> I would be okay with that, but that's not what the patch does: > > FWIW that was the intention, but luckily Junio quickly predicted it was > most likely buggy and so has been since made obsolete by: > > https://lore.kernel.org/git/20200507175706.19986-1-carenas@gmail.com/ Heh, don't give me too much credit. I just noticed that they cannot be implementing the same thing, but I couldn't tell if the new behaviour was something you two agreed to be better, and asked for a clarification. In any case, the "just protect with eval '' block to avoid hurting other shells" version should be the first step. Improving it further is a separate topic. Thanks. ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2020-05-08 23:54 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2020-05-07 5:51 [PATCH] t/test_lib: avoid naked bash arrays in file_lineno Carlo Marcelo Arenas Belón 2020-05-07 14:53 ` Junio C Hamano 2020-05-07 17:33 ` Carlo Arenas 2020-05-07 17:57 ` Carlo Marcelo Arenas Belón 2020-05-08 23:53 ` Johannes Schindelin 2020-05-07 19:52 ` Johannes Schindelin 2020-05-08 0:58 ` Carlo Marcelo Arenas Belón 2020-05-08 1:17 ` Junio C Hamano
Code repositories for project(s) associated with this public inbox https://80x24.org/mirrors/git.git This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).