Rev 4608: Merge shell-like rm implementation in file:///home/vila/src/bzr/experimental/conflict-manager/

Vincent Ladeuil v.ladeuil+lp at free.fr
Thu Sep 10 11:19:46 BST 2009


At file:///home/vila/src/bzr/experimental/conflict-manager/

------------------------------------------------------------
revno: 4608
revision-id: v.ladeuil+lp at free.fr-20090910101946-ekl989e9nt9i5mo1
parent: v.ladeuil+lp at free.fr-20090909140419-14581woemoz5lzka
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: shell-like-tests
timestamp: Thu 2009-09-10 12:19:46 +0200
message:
  Merge shell-like rm implementation
-------------- next part --------------
=== modified file 'bzrlib/tests/script.py'
--- a/bzrlib/tests/script.py	2009-09-09 14:04:19 +0000
+++ b/bzrlib/tests/script.py	2009-09-10 10:19:46 +0000
@@ -96,6 +96,7 @@
 """
 
 import doctest
+import errno
 import os
 import shlex
 from cStringIO import StringIO
@@ -351,6 +352,48 @@
         os.mkdir(d)
         return 0, None, None
 
+    def do_rm(self, input, args):
+        err = None
+
+        def error(msg, path):
+            return  "rm: cannot remove '%s': %s\n" % (path, msg)
+
+        force, recursive = False, False
+        opts = None
+        if args and args[0][0] == '-':
+            opts = args.pop(0)[1:]
+            if 'f' in opts:
+                force = True
+                opts = opts.replace('f', '', 1)
+            if 'r' in opts:
+                recursive = True
+                opts = opts.replace('r', '', 1)
+        if not args or opts:
+            raise SyntaxError('Usage: rm [-fr] path+')
+        for p in args:
+            self._ensure_in_jail(p)
+            # FIXME: Should we put that in osutils ?
+            try:
+                os.remove(p)
+            except OSError, e:
+                if e.errno == errno.EISDIR:
+                    if recursive:
+                        osutils.rmtree(p)
+                    else:
+                        err = error('Is a directory', p)
+                        break
+                elif e.errno == errno.ENOENT:
+                    if not force:
+                        err =  error('No such file or directory', p)
+                        break
+                else:
+                    raise
+        if err:
+            retcode = 1
+        else:
+            retcode = 0
+        return retcode, None, err
+
 
 class TestCaseWithMemoryTransportAndScript(tests.TestCaseWithMemoryTransport):
 

=== modified file 'bzrlib/tests/test_script.py'
--- a/bzrlib/tests/test_script.py	2009-09-09 14:04:19 +0000
+++ b/bzrlib/tests/test_script.py	2009-09-10 10:19:46 +0000
@@ -275,3 +275,45 @@
         self.assertEquals(None, err)
         self.assertFileEqual('hello\nhappy\n', 'file')
 
+
+class TestRm(script.TestCaseWithTransportAndScript):
+
+    def test_rm_usage(self):
+        self.assertRaises(SyntaxError, self.run_script, 'rm')
+        self.assertRaises(SyntaxError, self.run_script, 'rm -ff foo')
+
+    def test_rm_file(self):
+        self.run_script('echo content >file')
+        self.failUnlessExists('file')
+        self.run_script('rm file')
+        self.failIfExists('file')
+
+    def test_rm_file_force(self):
+        self.failIfExists('file')
+        self.run_script('rm -f file')
+        self.failIfExists('file')
+
+    def test_rm_files(self):
+        self.run_script("""
+echo content >file
+echo content >file2
+""")
+        self.failUnlessExists('file2')
+        self.run_script('rm file file2')
+        self.failIfExists('file2')
+
+    def test_rm_dir(self):
+        self.run_script('mkdir dir')
+        self.failUnlessExists('dir')
+        self.run_script("""
+rm dir
+2>rm: cannot remove 'dir': Is a directory
+""")
+        self.failUnlessExists('dir')
+
+    def test_rm_dir_recursive(self):
+        self.run_script("""
+mkdir dir
+rm -r dir
+""")
+        self.failIfExists('dir')



More information about the bazaar-commits mailing list