Rev 4677: Support '...' in expected strings. in file:///home/vila/src/bzr/experimental/shell-like-tests/

Vincent Ladeuil v.ladeuil+lp at free.fr
Fri Sep 4 14:15:24 BST 2009


At file:///home/vila/src/bzr/experimental/shell-like-tests/

------------------------------------------------------------
revno: 4677
revision-id: v.ladeuil+lp at free.fr-20090904131523-t280hl1i8efz1ncb
parent: v.ladeuil+lp at free.fr-20090903154905-g9kudjm7noikwmyb
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: shell-like-tests
timestamp: Fri 2009-09-04 15:15:23 +0200
message:
  Support '...' in expected strings.
  
  * bzrlib/tests/test_script.py:
  (TestScriptExecution.test_ellipsis_output): '...' can be used to
  under specified expected outout/errors.
  
  * bzrlib/tests/script.py:
  (ScriptRunner.__init__, ScriptRunner._check_output): Use
  doctest.OutputChecker.
-------------- next part --------------
=== modified file 'bzrlib/tests/script.py'
--- a/bzrlib/tests/script.py	2009-09-03 15:49:05 +0000
+++ b/bzrlib/tests/script.py	2009-09-04 13:15:23 +0000
@@ -36,9 +36,11 @@
 When no output is specified, any ouput from the command is accepted
 and let the execution continue. 
 
+FIXME: not yet true
 If an error occurs and no expected error is specified, the execution stops.
 
-The matching is done on a full string comparison basis.
+The matching is done on a full string comparison basis unless '...' is used, in
+which case expected output/errors can be lees precise.
 
 Examples:
 
@@ -60,11 +62,19 @@
   bzr not-a-command
   2> bzr: ERROR: unknown command "not-a-command"
 
+You can use ellipsis (...) to replace any piece of text you don't want to be
+matched exactly.
+
+  bzr branch not-a-branch
+  2>bzr: ERROR: Not a branch...not-a-branch/".
+
+
 """
 
-from cStringIO import StringIO
+import doctest
 import os
 import shlex
+from cStringIO import StringIO
 
 from bzrlib import (
     osutils,
@@ -196,16 +206,29 @@
 
     def __init__(self, test_case):
         self.test_case = test_case
+        self.output_checker = doctest.OutputChecker()
+        self.check_options = doctest.ELLIPSIS
 
     def run_script(self, text):
         for cmd, input, output, error in _script_to_commands(text):
-            self.run_command(cmd, input, output, error)
+            out, err = self.run_command(cmd, input, output, error)
 
     def _check_output(self, expected, actual):
         if expected is None:
             # Specifying None means: any output is accepted
             return
-        self.test_case.assertEquals(expected, actual)
+        if actual is None:
+            self.test_case.fail('Unexpected: %s' % actual)
+        matching = self.output_checker.check_output(
+            expected, actual, self.check_options)
+        if not matching:
+            # Note that we can't use output_checker.output_difference() here
+            # because... the API is boken (expected must be a doctest specific
+            # object of whicha 'want' attribute will be our 'expected'
+            # parameter. So we just fallbacl to our good old assertEqualDiff
+            # since we know there are differences and the output should be
+            # decemtly readable.
+            self.test_case.assertEqualDiff(expected, actual)
 
     def run_command(self, cmd, input, output, error):
         mname = 'do_' + cmd[0]

=== modified file 'bzrlib/tests/test_script.py'
--- a/bzrlib/tests/test_script.py	2009-09-03 15:49:05 +0000
+++ b/bzrlib/tests/test_script.py	2009-09-04 13:15:23 +0000
@@ -97,6 +97,30 @@
         self.assertRaises(AssertionError, self.run_script, story)
 
 
+    def test_ellipsis_output(self):
+        story = """
+cat
+<first line
+<second line
+<last line
+>first line
+>...
+>last line
+"""
+        self.run_script(story)
+        story = """
+bzr not-a-command
+2>..."not-a-command"
+"""
+        self.run_script(story)
+
+        story = """
+bzr branch not-a-branch
+2>bzr: ERROR: Not a branch...not-a-branch/".
+"""
+        self.run_script(story)
+
+
 class TestCat(script.TestCaseWithTransportAndScript):
 
     def test_cat_usage(self):



More information about the bazaar-commits mailing list