Rev 4666: Start some shell-like capability to write tests. in file:///home/vila/src/bzr/experimental/shell-like-tests/

Vincent Ladeuil v.ladeuil+lp at free.fr
Tue Sep 1 09:24:45 BST 2009


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

------------------------------------------------------------
revno: 4666
revision-id: v.ladeuil+lp at free.fr-20090901082444-b73u7vngxtrwurfd
parent: pqm at pqm.ubuntu.com-20090831002739-vd6487doda1b2k9h
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: shell-like-tests
timestamp: Tue 2009-09-01 10:24:44 +0200
message:
  Start some shell-like capability to write tests.
  
  * bzrlib/tests/test_script.py: 
  Associated tests.
  
  * bzrlib/tests/script.py: 
  Start implementing shell-like test scripts.
-------------- next part --------------
=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2009-08-28 21:05:31 +0000
+++ b/bzrlib/tests/__init__.py	2009-09-01 08:24:44 +0000
@@ -3618,6 +3618,7 @@
         'bzrlib.tests.test_rio',
         'bzrlib.tests.test_rules',
         'bzrlib.tests.test_sampler',
+        'bzrlib.tests.test_script',
         'bzrlib.tests.test_selftest',
         'bzrlib.tests.test_serializer',
         'bzrlib.tests.test_setup',

=== added file 'bzrlib/tests/script.py'
--- a/bzrlib/tests/script.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/script.py	2009-09-01 08:24:44 +0000
@@ -0,0 +1,80 @@
+# Copyright (C) 2009 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+import shlex
+
+
+def _script_to_commands(script, file_name=None):
+    """Turn a script into a list of commands with their associated IOs.
+
+    Each command appears on a line by itself. It can be associated with an
+    input that will feed it and an expected output.
+    Comments starts with '#' until the end of line.
+    Empty lines are ignored.
+    Input and output are full lines terminated by a '\n'.
+    Input lines start with '<'.
+    Output lines start with '>' 
+    """
+    commands = []
+    cmd_cur = None
+    cmd_line = 1
+    lineno = 0
+    input = None
+    output = None
+    for line in script.split('\n'):
+        lineno += 1
+        # Keep a copy for error reporting
+        orig = line
+        comment =  line.find('#')
+        if comment >= 0:
+            # Delete comments
+            line = line[0:comment]
+            line = line.rstrip()
+        if line == '':
+            # Ignore empty lines
+            continue
+        if line.startswith('<'):
+            if input is None:
+                if cmd_cur is None:
+                    raise SyntaxError('No command for that input',
+                                      (file_name, lineno, 1, orig))
+                input = []
+            input.append(line[1:] + '\n')
+            continue
+        elif line.startswith('>'):
+            if output is None:
+                if cmd_cur is None:
+                    raise SyntaxError('No command for that output',
+                                      (file_name, lineno, 1, orig))
+                output = []
+            output.append(line[1:] + '\n')
+            continue
+        else:
+            # Time to output the current command
+            if cmd_cur is not None:
+                commands.append((cmd_cur, input, output))
+            # And start a new one
+            cmd_cur = shlex.split(line)
+            cmd_line = lineno
+            input = None
+            output = None
+    # Add the last seen command
+    if cmd_cur is not None:
+        commands.append((cmd_cur, input, output))
+    return commands
+
+
+

=== added file 'bzrlib/tests/test_script.py'
--- a/bzrlib/tests/test_script.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/test_script.py	2009-09-01 08:24:44 +0000
@@ -0,0 +1,61 @@
+# Copyright (C) 2009 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+from bzrlib import (
+    tests,
+    )
+from bzrlib.tests import script
+
+
+class TestUserTest(tests.TestCase):
+
+    def test_comment_is_ignored(self):
+        self.assertEquals([], script._script_to_commands('#comment\n'))
+
+    def test_empty_line_is_ignored(self):
+        self.assertEquals([], script._script_to_commands('\n'))
+
+    def test_simple_command(self):
+        self.assertEquals([(['cd', 'trunk'], None, None)],
+                           script._script_to_commands('cd trunk'))
+
+    def test_command_with_input(self):
+        self.assertEquals([(['cat', '>file'], ['content\n'], None)],
+                           script._script_to_commands('cat >file\n<content\n'))
+
+    def test_command_with_output(self):
+        story = """
+bzr add
+>adding file
+"""
+        self.assertEquals([(['bzr', 'add'], None, ['adding file\n'])],
+                          script._script_to_commands(story))
+
+    def test_input_without_command(self):
+        self.assertRaises(SyntaxError, script._script_to_commands, '<input')
+
+    def test_output_without_command(self):
+        self.assertRaises(SyntaxError, script._script_to_commands, '>input')
+
+    # FIXME: not passing yet.
+    def xtest_command_with_backquotes(self):
+        story = """
+foo = `bzr file-id toto`
+"""
+        self.assertEquals([(['foo', '=', '`bzr file-id toto`'], None, None)],
+                          script._script_to_commands(story))
+
+



More information about the bazaar-commits mailing list