Rev 4669: Implement a 'cat' command. in file:///home/vila/src/bzr/experimental/shell-like-tests/

Vincent Ladeuil v.ladeuil+lp at free.fr
Tue Sep 1 12:34:49 BST 2009


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

------------------------------------------------------------
revno: 4669
revision-id: v.ladeuil+lp at free.fr-20090901113449-7rym3i388se6st59
parent: v.ladeuil+lp at free.fr-20090901093342-ph59wrvdt85xrpbn
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: shell-like-tests
timestamp: Tue 2009-09-01 13:34:49 +0200
message:
  Implement a 'cat' command.
  
  * bzrlib/tests/test_script.py:
  (TestCat): Test the 'cat' command.
  
  * bzrlib/tests/script.py:
  (TestCaseWithScript): TestCase that can run shell-like
  scripts. Implement 'cat' to fill files.
-------------- next part --------------
=== modified file 'bzrlib/tests/script.py'
--- a/bzrlib/tests/script.py	2009-09-01 09:33:42 +0000
+++ b/bzrlib/tests/script.py	2009-09-01 11:34:49 +0000
@@ -14,8 +14,11 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
+from cStringIO import StringIO
 import shlex
 
+from bzrlib import tests
+
 
 def split(s):
     """Split a command line respecting quotes."""
@@ -97,3 +100,73 @@
     if cmd_cur is not None:
         commands.append((cmd_cur, input, output, error))
     return commands
+
+
+class TestCaseWithScript(tests.TestCaseWithTransport):
+
+    def setUp(self):
+        super(TestCaseWithScript, self).setUp()
+        self._vars = {}
+
+    def run_script(self, text):
+        for cmd, input, output, error in _script_to_commands(text):
+            self.run_command(cmd, input, output, error)
+
+    def run_command(self, cmd, input, output, error):
+        mname = 'do_' + cmd[0]
+        method = getattr(self, mname, None)
+        if method is None:
+            raise SyntaxError('Command not found "%s"' % (cmd[0],),
+                              None, 1, ' '.join(cmd))
+        if input is None:
+            str_input = ''
+        else:
+            str_input = ''.join(input)
+        actual_output, actual_error = method(str_input, cmd[1:])
+        if output is None:
+            output = ''
+        self.assertEquals(''.join(output), actual_output)
+        if error is None:
+            error = ''
+        self.assertEquals(''.join(error), actual_error)
+        return actual_output, actual_error
+
+    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]')
+        if in_name is not None:
+            infile = open(in_name, 'rb')
+            try:
+                input = infile.read()
+            finally:
+                infile.close()
+        out = StringIO(input)
+        output = out.getvalue()
+        if out_name is not None:
+            outfile = open(out_name, 'wb')
+            try:
+                outfile.write(output)
+            finally:
+                outfile.close()
+            output = ''
+        return output, ''
+
+

=== modified file 'bzrlib/tests/test_script.py'
--- a/bzrlib/tests/test_script.py	2009-09-01 09:33:42 +0000
+++ b/bzrlib/tests/test_script.py	2009-09-01 11:34:49 +0000
@@ -79,3 +79,29 @@
                           script._script_to_commands(story))
 
 
+class TestScriptExecution(script.TestCaseWithScript):
+
+    def test_unknown_command(self):
+        self.assertRaises(SyntaxError, self.run_script, 'foo')
+
+
+class TestCat(script.TestCaseWithScript):
+
+    def test_cat_usage(self):
+        self.assertRaises(SyntaxError, self.run_script, 'cat foo bar baz')
+
+    def test_cat_input_to_output(self):
+        out, err = self.run_command(['cat'], ['content\n'], ['content\n'], None)
+
+    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)
+
+    def test_cat_input_to_file(self):
+        out, err = self.run_command(['cat', '>file'], ['content\n'], None, None)
+        self.assertFileEqual('content\n', 'file')
+
+    def test_cat_file_to_file(self):
+        self.build_tree_contents([('file', 'content\n')])
+        out, err = self.run_command(['cat', 'file', '>file2'], None, None, None)
+        self.assertFileEqual('content\n', 'file2')



More information about the bazaar-commits mailing list