/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/script.py

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.

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
"""
97
97
 
98
98
import doctest
 
99
import errno
99
100
import os
100
101
import shlex
101
102
from cStringIO import StringIO
351
352
        os.mkdir(d)
352
353
        return 0, None, None
353
354
 
 
355
    def do_rm(self, input, args):
 
356
        err = None
 
357
 
 
358
        def error(msg, path):
 
359
            return  "rm: cannot remove '%s': %s\n" % (path, msg)
 
360
 
 
361
        force, recursive = False, False
 
362
        opts = None
 
363
        if args and args[0][0] == '-':
 
364
            opts = args.pop(0)[1:]
 
365
            if 'f' in opts:
 
366
                force = True
 
367
                opts = opts.replace('f', '', 1)
 
368
            if 'r' in opts:
 
369
                recursive = True
 
370
                opts = opts.replace('r', '', 1)
 
371
        if not args or opts:
 
372
            raise SyntaxError('Usage: rm [-fr] path+')
 
373
        for p in args:
 
374
            self._ensure_in_jail(p)
 
375
            # FIXME: Should we put that in osutils ?
 
376
            try:
 
377
                os.remove(p)
 
378
            except OSError, e:
 
379
                if e.errno == errno.EISDIR:
 
380
                    if recursive:
 
381
                        osutils.rmtree(p)
 
382
                    else:
 
383
                        err = error('Is a directory', p)
 
384
                        break
 
385
                elif e.errno == errno.ENOENT:
 
386
                    if not force:
 
387
                        err =  error('No such file or directory', p)
 
388
                        break
 
389
                else:
 
390
                    raise
 
391
        if err:
 
392
            retcode = 1
 
393
        else:
 
394
            retcode = 0
 
395
        return retcode, None, err
 
396
 
354
397
 
355
398
class TestCaseWithMemoryTransportAndScript(tests.TestCaseWithMemoryTransport):
356
399