On Thu, Jun 29, 2017 at 9:50 AM, Assaf Gordon <assafgor...@gmail.com> wrote:
> Hello Bruno and all,
>
>> On Jun 29, 2017, at 12:26, Bruno Haible <br...@clisp.org> wrote:
>>
...
> Luckily, many GNU test scripts already use "$prog" perl variable
> for the program's name in error message, so perhaps it would be possible
> to accomodate qemu without code changes.
>
> I've encountered the same for my program (datamash).
> and as an ugly hack added an undocumented option to print the program's name
> to set '$prog' accordingly:
> https://git.savannah.gnu.org/cgit/datamash.git/tree/tests/datamash-tests.pl#n37
>   ## Cross-Compiling portability hack:
>   ##  under qemu/binfmt, argv[0] (which is used to report errors) will contain
>   ##  the full path of the binary, if the binary is on the $PATH.
>   ##  So we try to detect what is the actual returned value of the program
>   ##  in case of an error.
>   my $prog = `$prog_bin ---print-progname`;
>   $prog = $prog_bin unless $prog;
>
>
> But this hack can be avoided, if we just run 'grep' with invalid
> arguments, triggering an error, then extracting the program name from STDERR.
>
> E.g. for 
> https://git.savannah.gnu.org/cgit/grep.git/tree/tests/filename-lineno.pl
> change the following on line 26 from:
>     my $prog = 'grep';
> to
>     my $prog = 'grep';
>     my $full_prog_name = `$prog --invalid-option-for-testing 2>&1 | head -n1 
> | cut -f1 -d:`;
>     $prog = $full_prog_name if $full_prog_name;
>
> This is untested code, but should work more-or-less.
> Once "$prog" is updated, all the tests should pass.

Thanks to both of you.
Does this patch solve the problem?
From d86483b0c4be5298c96ccaaa62670a04b018af42 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyer...@fb.com>
Date: Thu, 29 Jun 2017 18:06:11 -0700
Subject: [PATCH] tests: avoid false failure when run in qemu user mode

* tests/filename-lineno.pl: Derive the program name that grep
will use in diagnostics, based on a suggestion from Assaf Gordon.
Reported by Bruno Haible in http://bugs.gnu.org/27532
---
 tests/filename-lineno.pl | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tests/filename-lineno.pl b/tests/filename-lineno.pl
index 8eead57..6cc86b7 100755
--- a/tests/filename-lineno.pl
+++ b/tests/filename-lineno.pl
@@ -24,6 +24,9 @@ use strict;
 (my $program_name = $0) =~ s|.*/||;

 my $prog = 'grep';
+my $full_prog_name = `$prog --no-such-option 2>&1`;
+$full_prog_name =~ s/:.*//s;
+$prog = $full_prog_name if $full_prog_name;

 # Turn off localization of executable's output.
 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
-- 
2.13.0

Reply via email to