Rev 4673: Implement 'echo' command. in file:///home/vila/src/bzr/experimental/shell-like-tests/

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


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

------------------------------------------------------------
revno: 4673
revision-id: v.ladeuil+lp at free.fr-20090901154552-bixxt5tircyhds3a
parent: v.ladeuil+lp at free.fr-20090901144416-eooxqi89bxsxli60
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: shell-like-tests
timestamp: Tue 2009-09-01 17:45:52 +0200
message:
  Implement 'echo' command.
  
  * bzrlib/tests/test_script.py:
  (TestCat.test_cat_input_to_file): Test appending.
  (TestEcho): Test 'echo' command.
  
  * bzrlib/tests/script.py:
  (_scan_redirection_options): Factor out redirections handling.
  (TestCaseWithScript._read_input): Factor out input handling.
  (TestCaseWithScript._write_output): Factor out output handling.
  (TestCaseWithScript.do_cat): Simplified.
  (TestCaseWithScript.do_echo): New 'echo' command, more natural
  than 'cat'.
-------------- next part --------------
=== modified file 'bzrlib/tests/script.py'
--- a/bzrlib/tests/script.py	2009-09-01 14:44:16 +0000
+++ b/bzrlib/tests/script.py	2009-09-01 15:45:52 +0000
@@ -116,6 +116,34 @@
     return commands
 
 
+def _scan_redirection_options(args):
+    """Recognize and process input and output redirections.
+
+    :param args: The command line arguments
+
+    :return: A tuple containing: 
+        - The file name redirected from or None
+        - The file name redirected to or None
+        - The mode to open the output file or None
+        - The reamining arguments
+    """
+    remaining = []
+    in_name = None
+    out_name, out_mode = None, None
+    for arg in  args:
+        if arg.startswith('<'):
+            in_name = arg[1:]
+        elif arg.startswith('>>'):
+            out_name = arg[2:]
+            out_mode = 'ab+'
+        elif arg.startswith('>'):
+            out_name = arg[1:]
+            out_mode = 'wb+'
+        else:
+            remaining.append(arg)
+    return in_name, out_name, out_mode, remaining
+
+
 class TestCaseWithScript(tests.TestCaseWithTransport):
 
     def setUp(self):
@@ -148,47 +176,59 @@
         self._check_output(error, actual_error)
         return actual_output, actual_error
 
-    def do_bzr(self, input, args):
-        out, err = self._run_bzr_core(args, retcode=None, encoding=None,
-                                      stdin=input, working_dir=None)
-        return out, err
-
-    def do_cat(self, input, args):
-        in_name = None
-        out_name = None
-        syntax_ok = False
-        if not args:
-            in_name = None
-            out_name = None
-            syntax_ok = True
-        elif len(args) == 1:
-            in_name = args[0]
-            if in_name.startswith('>'):
-                out_name = in_name[1:]
-                in_name = None
-            else:
-                out_name = None
-            syntax_ok = True
-        elif len(args) == 2:
-            in_name, out_name = args[0], args[1][1:]
-            syntax_ok = args[1].startswith('>')
-        if not syntax_ok:
-            raise SyntaxError('Usage: cat [file1] [>file2]')
+    def _read_input(self, input, in_name):
         if in_name is not None:
             infile = open(in_name, 'rb')
             try:
+                # Command redirection takes precedence over provided input
                 input = infile.read()
             finally:
                 infile.close()
-        out = StringIO(input)
-        output = out.getvalue()
+        return input
+
+    def _write_output(self, output, out_name, out_mode):
         if out_name is not None:
-            outfile = open(out_name, 'wb')
+            outfile = open(out_name, out_mode)
             try:
                 outfile.write(output)
             finally:
                 outfile.close()
             output = None
+        return output
+
+    def do_bzr(self, input, args):
+        out, err = self._run_bzr_core(args, retcode=None, encoding=None,
+                                      stdin=input, working_dir=None)
+        return out, err
+
+    def do_cat(self, input, args):
+        (in_name, out_name, out_mode, args) = _scan_redirection_options(args)
+        if len(args) > 1:
+            raise SyntaxError('Usage: cat [file1]')
+        if args:
+            if in_name is not None:
+                raise SyntaxError('Specify a file OR use redirection')
+            in_name = args[0]
+        input = self._read_input(input, in_name)
+        # Basically cat copy input to output
+        output = input
+        # Handle output redirections
+        output = self._write_output(output, out_name, out_mode)
+        return output, None
+
+    def do_echo(self, input, args):
+        (in_name, out_name, out_mode, args) = _scan_redirection_options(args)
+        if input and args:
+                raise SyntaxError('Specify parameters OR use redirection')
+        if args:
+            input = ''.join(args)
+        input = self._read_input(input, in_name)
+        # Always append a \n'
+        input += '\n'
+        # Process output
+        output = input
+        # Handle output redirections
+        output = self._write_output(output, out_name, out_mode)
         return output, None
 
     def _ensure_in_jail(self, path):

=== modified file 'bzrlib/tests/test_script.py'
--- a/bzrlib/tests/test_script.py	2009-09-01 14:44:16 +0000
+++ b/bzrlib/tests/test_script.py	2009-09-01 15:45:52 +0000
@@ -101,17 +101,28 @@
 
     def test_cat_usage(self):
         self.assertRaises(SyntaxError, self.run_script, 'cat foo bar baz')
+        self.assertRaises(SyntaxError, self.run_script, 'cat foo <bar')
 
     def test_cat_input_to_output(self):
         out, err = self.run_command(['cat'], 'content\n', 'content\n', None)
+        self.assertEquals('content\n', out)
+        self.assertEquals(None, err)
 
     def test_cat_file_to_output(self):
         self.build_tree_contents([('file', 'content\n')])
         out, err = self.run_command(['cat', 'file'], None, 'content\n', None)
+        self.assertEquals('content\n', out)
+        self.assertEquals(None, err)
 
     def test_cat_input_to_file(self):
         out, err = self.run_command(['cat', '>file'], 'content\n', None, None)
         self.assertFileEqual('content\n', 'file')
+        self.assertEquals(None, out)
+        self.assertEquals(None, err)
+        out, err = self.run_command(['cat', '>>file'], 'more\n', None, None)
+        self.assertFileEqual('content\nmore\n', 'file')
+        self.assertEquals(None, out)
+        self.assertEquals(None, err)
 
     def test_cat_file_to_file(self):
         self.build_tree_contents([('file', 'content\n')])
@@ -167,3 +178,41 @@
     def test_bzr_smoke(self):
         self.run_script('bzr init branch')
         self.failUnlessExists('branch')
+
+
+class TestEcho(script.TestCaseWithScript):
+
+    def test_echo_usage(self):
+        story = """
+echo foo
+<bar
+"""
+        self.assertRaises(SyntaxError, self.run_script, story)
+
+    def test_echo_to_output(self):
+        out, err = self.run_command(['echo'], None, '\n', None)
+        self.assertEquals('\n', out)
+        self.assertEquals(None, err)
+
+    def test_echo_some_to_output(self):
+        out, err = self.run_command(['echo', 'hello'], None, 'hello\n', None)
+        self.assertEquals('hello\n', out)
+        self.assertEquals(None, err)
+
+    def test_echo_more_output(self):
+        out, err = self.run_command(['echo', 'hello', 'happy', 'world'],
+                                    None, 'hellohappyworld\n', None)
+        self.assertEquals('hellohappyworld\n', out)
+        self.assertEquals(None, err)
+
+    def test_echo_appended(self):
+        out, err = self.run_command(['echo', 'hello', '>file'],
+                                    None, None, None)
+        self.assertEquals(None, out)
+        self.assertEquals(None, err)
+        self.assertFileEqual('hello\n', 'file')
+        out, err = self.run_command(['echo', 'happy', '>>file'],
+                                    None, None, None)
+        self.assertEquals(None, out)
+        self.assertEquals(None, err)
+        self.assertFileEqual('hello\nhappy\n', 'file')



More information about the bazaar-commits mailing list