[PATCH 1 of 3] run-tests: alternative way of handling \r on Windows

Mads Kiilerich mads at kiilerich.com
Sun Oct 14 19:45:26 CDT 2012


# HG changeset patch
# User Mads Kiilerich <mads at kiilerich.com>
# Date 1350261192 -7200
# Node ID b49d886bbc64ed0b4bd02f4df60db9175b3f5e80
# Parent  9837cafc25b10c51659b65b5971622eab0bc9197
run-tests: alternative way of handling \r on Windows

After f71d60da58fb all \r was stripped from output on Windows, and the places
where a \r explicitly was expected it was accepted that it was missing. Ugly
hack.

Instead we now accept that an extra \r might appear at the end of lines on
Windows. That is more to the point and less ugly.

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -494,8 +494,10 @@
 
 def rematch(el, l):
     try:
-        # ensure that the regex matches to the end of the string
-        return re.match(el + r'\Z', l)
+        # use \Z to ensure that the regex matches to the end of the string
+        if os.name == 'nt':
+            return re.match(el + r'\r?\n\Z', l)
+        return re.match(el + r'\n\Z', l)
     except re.error:
         # el is an invalid regex
         return False
@@ -525,12 +527,12 @@
     if el == l: # perfect match (fast)
         return True
     if (el and
-        (el.endswith(" (re)\n") and rematch(el[:-6] + '\n', l) or
-         el.endswith(" (glob)\n") and globmatch(el[:-8] + '\n', l) or
+        (el.endswith(" (re)\n") and rematch(el[:-6], l) or
+         el.endswith(" (glob)\n") and globmatch(el[:-8], l) or
          el.endswith(" (esc)\n") and
              (el[:-7].decode('string-escape') + '\n' == l or
-              el[:-7].decode('string-escape').replace('\r', '') +
-                  '\n' == l and os.name == 'nt'))):
+              os.name == 'nt' and
+              el[:-7].decode('string-escape') + '\n' == l))):
         return True
     return False
 
@@ -885,7 +887,6 @@
         (r':%s\b' % (options.port + 2), ':$HGPORT2'),
         ]
     if os.name == 'nt':
-        replacements.append((r'\r\n', '\n'))
         replacements.append(
             (''.join(c.isalpha() and '[%s%s]' % (c.lower(), c.upper()) or
                      c in '/\\' and r'[/\\]' or
diff --git a/tests/test-run-tests.t b/tests/test-run-tests.t
--- a/tests/test-run-tests.t
+++ b/tests/test-run-tests.t
@@ -52,6 +52,16 @@
   $ echo 'foo (re)'
   foo (re)
 
+Windows: \r\n is handled like \n and can be escaped:
+
+#if windows
+  $ printf 'crlf\r\ncr\r\tcrlf\r\ncrcrlf\r\r\n'
+  crlf
+  cr\r (no-eol) (esc)
+  \tcrlf (esc)
+  crcrlf\r (esc)
+#endif
+
 testing hghave
 
   $ "$TESTDIR/hghave" true


More information about the Mercurial-devel mailing list