Rev 4670: Implement 'cd' and 'mkdir'. in file:///home/vila/src/bzr/experimental/shell-like-tests/

Vincent Ladeuil v.ladeuil+lp at free.fr
Tue Sep 1 13:27:28 BST 2009


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

------------------------------------------------------------
revno: 4670
revision-id: v.ladeuil+lp at free.fr-20090901122728-o33dw2k63tz1k0my
parent: v.ladeuil+lp at free.fr-20090901113449-7rym3i388se6st59
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: shell-like-tests
timestamp: Tue 2009-09-01 14:27:28 +0200
message:
  Implement 'cd' and 'mkdir'.
  
  * bzrlib/tests/test_script.py:
  (TestCd, TestMkdir): Test the 'cd' and 'mkdir' commands.
  
  * bzrlib/tests/script.py:
  (TestCaseWithScript._check_output): None and '' are equal as far
  as we're concerned.
  (TestCaseWithScript.do_cd, TestCaseWithScript.do_mkdir):
  Implements cd and mkdir.
-------------- next part --------------
=== modified file 'bzrlib/tests/script.py'
--- a/bzrlib/tests/script.py	2009-09-01 11:34:49 +0000
+++ b/bzrlib/tests/script.py	2009-09-01 12:27:28 +0000
@@ -15,9 +15,13 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 from cStringIO import StringIO
+import os
 import shlex
 
-from bzrlib import tests
+from bzrlib import (
+    osutils,
+    tests,
+    )
 
 
 def split(s):
@@ -112,6 +116,15 @@
         for cmd, input, output, error in _script_to_commands(text):
             self.run_command(cmd, input, output, error)
 
+    def _check_output(self, expected, actual):
+        if expected is None:
+            str_expected = ''
+        else:
+            str_expected = ''.join(expected)
+        if actual is None:
+            actual = ''
+        self.assertEqualDiff(str_expected, actual)
+
     def run_command(self, cmd, input, output, error):
         mname = 'do_' + cmd[0]
         method = getattr(self, mname, None)
@@ -123,12 +136,9 @@
         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)
+
+        self._check_output(output, actual_output)
+        self._check_output(error, actual_error)
         return actual_output, actual_error
 
     def do_cat(self, input, args):
@@ -166,7 +176,28 @@
                 outfile.write(output)
             finally:
                 outfile.close()
-            output = ''
-        return output, ''
-
-
+            output = None
+        return output, None
+
+    def _ensure_in_jail(self, path):
+        if not osutils.is_inside(self.test_dir, osutils.normalizepath(path)):
+                raise ValueError('%s is not inside %s' % (path, self.test_dir))
+
+    def do_cd(self, input, args):
+        if len(args) > 1:
+            raise SyntaxError('Usage: cd [dir]')
+        if len(args) == 1:
+            d = args[0]
+            self._ensure_in_jail(d)
+        else:
+            d = self.test_dir
+        os.chdir(d)
+        return None, None
+
+    def do_mkdir(self, input, args):
+        if not args or len(args) != 1:
+            raise SyntaxError('Usage: mkdir dir')
+        d = args[0]
+        self._ensure_in_jail(d)
+        os.mkdir(d)
+        return None, None

=== modified file 'bzrlib/tests/test_script.py'
--- a/bzrlib/tests/test_script.py	2009-09-01 11:34:49 +0000
+++ b/bzrlib/tests/test_script.py	2009-09-01 12:27:28 +0000
@@ -14,7 +14,9 @@
 # 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 (
+    osutils,
     tests,
     )
 from bzrlib.tests import script
@@ -105,3 +107,46 @@
         self.build_tree_contents([('file', 'content\n')])
         out, err = self.run_command(['cat', 'file', '>file2'], None, None, None)
         self.assertFileEqual('content\n', 'file2')
+
+
+class TestMkdir(script.TestCaseWithScript):
+
+    def test_mkdir_usage(self):
+        self.assertRaises(SyntaxError, self.run_script, 'mkdir')
+        self.assertRaises(SyntaxError, self.run_script, 'mkdir foo bar')
+
+    def test_mkdir_jailed(self):
+        self.assertRaises(ValueError, self.run_script, 'mkdir /out-of-jail')
+        self.assertRaises(ValueError, self.run_script, 'mkdir ../out-of-jail')
+
+    def test_mkdir_in_jail(self):
+        self.run_script("""
+mkdir dir
+cd dir
+mkdir ../dir2
+cd ..
+""")
+        self.failUnlessExists('dir')
+        self.failUnlessExists('dir2')
+
+
+class TestCd(script.TestCaseWithScript):
+
+    def test_cd_usage(self):
+        self.assertRaises(SyntaxError, self.run_script, 'cd foo bar')
+
+    def test_cd_out_of_jail(self):
+        self.assertRaises(ValueError, self.run_script, 'cd /out-of-jail')
+        self.assertRaises(ValueError, self.run_script, 'cd ..')
+
+    def test_cd_dir_and_back_home(self):
+        self.assertEquals(self.test_dir, osutils.getcwd())
+        self.run_script("""
+mkdir dir
+cd dir
+""")
+        self.assertEquals(osutils.pathjoin(self.test_dir, 'dir'),
+                          osutils.getcwd())
+
+        self.run_script('cd')
+        self.assertEquals(self.test_dir, osutils.getcwd())



More information about the bazaar-commits mailing list