Rev 4667: Handle simple, double and back quotes. in file:///home/vila/src/bzr/experimental/shell-like-tests/

Vincent Ladeuil v.ladeuil+lp at free.fr
Tue Sep 1 10:21:04 BST 2009


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

------------------------------------------------------------
revno: 4667
revision-id: v.ladeuil+lp at free.fr-20090901092104-ol3np0cs3xyyytgw
parent: v.ladeuil+lp at free.fr-20090901082444-b73u7vngxtrwurfd
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: shell-like-tests
timestamp: Tue 2009-09-01 11:21:04 +0200
message:
  Handle simple, double and back quotes.
  
  * bzrlib/tests/test_script.py:
  (TestScriptSyntax): Add quote tests.
  
  * bzrlib/tests/script.py:
  (split): Encapsulate the split to better control which quotes are
  kept in the parameters.
-------------- next part --------------
=== modified file 'bzrlib/tests/script.py'
--- a/bzrlib/tests/script.py	2009-09-01 08:24:44 +0000
+++ b/bzrlib/tests/script.py	2009-09-01 09:21:04 +0000
@@ -17,7 +17,22 @@
 import shlex
 
 
-def _script_to_commands(script, file_name=None):
+def split(s):
+    """Split a command line respecting quotes."""
+    scanner = shlex.shlex(s)
+    scanner.quotes = '\'"`'
+    scanner.whitespace_split = True
+    for t in list(scanner):
+        # Strip the simple and double quotes since we don't care about them.
+        # We leave the backquotes in place though since they have a different
+        # semantic.
+        if t[0] in  ('"', "'") and t[0] == t[-1]:
+            yield t[1:-1]
+        else:
+            yield t
+
+
+def _script_to_commands(text, 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
@@ -34,7 +49,7 @@
     lineno = 0
     input = None
     output = None
-    for line in script.split('\n'):
+    for line in text.split('\n'):
         lineno += 1
         # Keep a copy for error reporting
         orig = line
@@ -67,7 +82,7 @@
             if cmd_cur is not None:
                 commands.append((cmd_cur, input, output))
             # And start a new one
-            cmd_cur = shlex.split(line)
+            cmd_cur = list(split(line))
             cmd_line = lineno
             input = None
             output = None

=== modified file 'bzrlib/tests/test_script.py'
--- a/bzrlib/tests/test_script.py	2009-09-01 08:24:44 +0000
+++ b/bzrlib/tests/test_script.py	2009-09-01 09:21:04 +0000
@@ -20,7 +20,7 @@
 from bzrlib.tests import script
 
 
-class TestUserTest(tests.TestCase):
+class TestScriptSyntax(tests.TestCase):
 
     def test_comment_is_ignored(self):
         self.assertEquals([], script._script_to_commands('#comment\n'))
@@ -32,6 +32,16 @@
         self.assertEquals([(['cd', 'trunk'], None, None)],
                            script._script_to_commands('cd trunk'))
 
+    def test_command_with_single_quoted_param(self):
+        story = """bzr commit -m 'two words'"""
+        self.assertEquals([(['bzr', 'commit', '-m', 'two words'], None, None)],
+                           script._script_to_commands(story))
+
+    def test_command_with_double_quoted_param(self):
+        story = """bzr commit -m "two words" """
+        self.assertEquals([(['bzr', 'commit', '-m', 'two words'], None, None)],
+                           script._script_to_commands(story))
+
     def test_command_with_input(self):
         self.assertEquals([(['cat', '>file'], ['content\n'], None)],
                            script._script_to_commands('cat >file\n<content\n'))
@@ -50,8 +60,7 @@
     def test_output_without_command(self):
         self.assertRaises(SyntaxError, script._script_to_commands, '>input')
 
-    # FIXME: not passing yet.
-    def xtest_command_with_backquotes(self):
+    def test_command_with_backquotes(self):
         story = """
 foo = `bzr file-id toto`
 """



More information about the bazaar-commits mailing list