[PATCH 1 of 1] tests: asciify output of tsttest
Mads Kiilerich
mads at kiilerich.com
Sun Sep 19 07:55:02 CDT 2010
Yuya Nishihara wrote, On 09/19/2010 08:53 AM:
> # HG changeset patch
> # User Yuya Nishihara<yuya at tcha.org>
> # Date 1284876231 -32400
> # Node ID 617a551466acb0fc5f04ef8973d779b4f8939785
> # Parent d643ae555a4de42ca2798a43a48a7e7c2c729d0a
> tests: asciify output of tsttest
>
> It encodes non-ascii and most control characters of outputs to python-style
> \-escapes. It aims to avoid trouble with outputs for non-ascii, color
> and progress tests.
>
> This patch is based on Mads Kiilerich's.
Thanks for picking it up. I haven't had the time to finish it - but I
always have time to give comments ;-)
> diff --git a/tests/run-tests.py b/tests/run-tests.py
> --- a/tests/run-tests.py
> +++ b/tests/run-tests.py
> @@ -460,6 +460,11 @@ def battest(test, options):
> vlog("# Running", cmd)
> return run(cmd, options)
>
> +def asciify(line):
> + """Escape non-ascii and most control characters"""
> + return re.sub(r'[^\x20-\x7f\n\t]',
Regexp compilation is a relatively expensive process, so I think that a
general rule of thumb is that regexps used in loops always should be
compiled outside the loop.
Your measurements indicates that it can't make a big difference, but
also that it might make a little difference.
> + lambda m: m.group(0).encode('string-escape'), line)
My intent was to make the encoding as un-intrusive as possible and thus
never escape ' or ".
I didn't know that encoding. It seems like that encoding is defined as
eval("'" + x.encode('string-escape') + "'") == x
and it will thus always escape '.
An alternative is to use repr(x)[1:-1] which only will escape ' if the
string also contains ".
It probably doesn't matter.
BUT if you use string-escape then there is probably no reason to use
regexps first and it is probably faster to always just string-escape.
And then there is no need to define an asciify function at all.
> def tsttest(test, options):
> t = open(test)
> out = []
> @@ -501,6 +506,8 @@ def tsttest(test, options):
> finally:
> os.remove(name)
>
> + output = [asciify(l) for l in output]
Hmm. An extra iteration through the list and changing the "type" of
output from "list of raw output lines" to "list of asciified output
lines". I would prefer to asciify while we loop through the list.
I think that with this patch in place tests/printrepr.py should go away.
I'm not sure if it should be done in the same patch or in another.
/Mads
More information about the Mercurial-devel
mailing list