~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to zap.py

  • Committer: Michael Ellerman
  • Date: 2006-06-16 03:48:48 UTC
  • mfrom: (0.4.1 shelf)
  • mto: (0.3.4 shelf-dev)
  • mto: This revision was merged to the branch mainline in revision 396.
  • Revision ID: michael@ellerman.id.au-20060616034848-25d1c5bb8a043237
Use bzrlib.patches, thanks to Aaron.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from shutil import rmtree
2
 
 
3
 
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
4
 
from bzrlib.delta import compare_trees
5
 
from bzrlib.workingtree import WorkingTree
6
 
 
7
 
from errors import NotCheckout, UncommittedCheckout
8
 
 
9
 
 
10
 
def zap(path, remove_branch=False):
11
 
    try:
12
 
        wt = WorkingTree.open(path)
13
 
    except (NoWorkingTree, NotBranchError):
14
 
        raise NotCheckout(path)
15
 
    tree_base = wt.bzrdir.transport.base
16
 
    branch = wt.branch
17
 
    branch_base = branch.bzrdir.transport.base
18
 
    if tree_base == branch_base:
19
 
        raise NotCheckout(path)
20
 
    delta = compare_trees(wt.basis_tree(), wt, want_unchanged=False)
21
 
    if delta.has_changed():
22
 
        raise UncommittedCheckout()
23
 
    rmtree(path)
24
 
    if remove_branch:
25
 
        t = branch.bzrdir.transport
26
 
        while t.base != branch_base:
27
 
            t = t.clone('..')
28
 
        t = t.clone('..')
29
 
        t.delete_tree('')
30
 
 
31
 
 
32
 
def test_suite():
33
 
    import os
34
 
    from unittest import makeSuite
35
 
    
36
 
    from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
37
 
    from bzrlib.branch import BranchReferenceFormat
38
 
    from bzrlib.tests import TestCaseInTempDir
39
 
 
40
 
    class TestZap(TestCaseInTempDir):
41
 
 
42
 
        def make_checkout(self):
43
 
            wt = BzrDir.create_standalone_workingtree('source')
44
 
            os.mkdir('checkout')
45
 
            checkout = BzrDirMetaFormat1().initialize('checkout')
46
 
            BranchReferenceFormat().initialize(checkout, wt.branch)
47
 
            return checkout.create_workingtree()
48
 
 
49
 
        def test_is_checkout(self):
50
 
            self.assertRaises(NotCheckout, zap, '.')
51
 
            wt = BzrDir.create_standalone_workingtree('.')
52
 
            self.assertRaises(NotCheckout, zap, '.')
53
 
 
54
 
        def test_zap_works(self):
55
 
            self.make_checkout()
56
 
            self.assertIs(True, os.path.exists('checkout'))
57
 
            zap('checkout')
58
 
            self.assertIs(False, os.path.exists('checkout'))
59
 
            self.assertIs(True, os.path.exists('source'))
60
 
 
61
 
        def test_zap_branch(self):
62
 
            self.make_checkout()
63
 
            base = WorkingTree.open('checkout').branch.base
64
 
            self.assertIs(True, os.path.exists('checkout'))
65
 
            zap('checkout', remove_branch=True)
66
 
            self.assertIs(False, os.path.exists('checkout'))
67
 
            self.assertIs(False, os.path.exists('source'))
68
 
 
69
 
        def test_checks_modified(self):
70
 
            checkout = self.make_checkout()
71
 
            os.mkdir('checkout/foo')
72
 
            checkout.add('foo')
73
 
            self.assertRaises(UncommittedCheckout, zap, 'checkout')
74
 
            checkout.commit('commit changes to branch')
75
 
            zap('checkout')
76
 
 
77
 
    return makeSuite(TestZap)