Rev 4682: Implement 'rm' command. in file:///home/vila/src/bzr/experimental/shell-like-tests/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Thu Sep 10 11:16:17 BST 2009
At file:///home/vila/src/bzr/experimental/shell-like-tests/
------------------------------------------------------------
revno: 4682
revision-id: v.ladeuil+lp at free.fr-20090910101617-o36u03has8sqbnpt
parent: v.ladeuil+lp at free.fr-20090909135607-bfmm0gl738nx534g
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: shell-like-tests
timestamp: Thu 2009-09-10 12:16:17 +0200
message:
Implement 'rm' command.
* bzrlib/tests/test_script.py:
(TestRM): Test expcted behavior.
* bzrlib/tests/script.py:
(ScriptRunner.do_rm): Implements 'rm' with proper retcodes and no
"expected" exceptions.
-------------- next part --------------
=== modified file 'bzrlib/tests/script.py'
--- a/bzrlib/tests/script.py 2009-09-09 13:56:07 +0000
+++ b/bzrlib/tests/script.py 2009-09-10 10:16:17 +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 13:56:07 +0000
+++ b/bzrlib/tests/test_script.py 2009-09-10 10:16:17 +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