~abentley/bzrtools/bzrtools.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from shutil import rmtree

from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
from bzrlib.delta import compare_trees
from bzrlib.workingtree import WorkingTree

from errors import NotCheckout, UncommittedCheckout


def zap(path):
    try:
        wt = WorkingTree.open(path)
    except (NoWorkingTree, NotBranchError):
        raise NotCheckout(path)
    tree_base = wt.bzrdir.transport.base
    branch_base = wt.branch.bzrdir.transport.base
    if tree_base == branch_base:
        raise NotCheckout(path)
    delta = compare_trees(wt.basis_tree(), wt, want_unchanged=False)
    if delta.has_changed():
        raise UncommittedCheckout()
    rmtree(path)


def test_suite():
    import os
    from unittest import makeSuite
    
    from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
    from bzrlib.branch import BranchReferenceFormat
    from bzrlib.tests import TestCaseInTempDir

    class TestZap(TestCaseInTempDir):

        def make_checkout(self):
            wt = BzrDir.create_standalone_workingtree('source')
            os.mkdir('checkout')
            checkout = BzrDirMetaFormat1().initialize('checkout')
            BranchReferenceFormat().initialize(checkout, wt.branch)
            return checkout.create_workingtree()

        def test_is_checkout(self):
            self.assertRaises(NotCheckout, zap, '.')
            wt = BzrDir.create_standalone_workingtree('.')
            self.assertRaises(NotCheckout, zap, '.')

        def test_zap_works(self):
            self.make_checkout()
            self.assertIs(True, os.path.exists('checkout'))
            zap('checkout')
            self.assertIs(False, os.path.exists('checkout'))

        def test_checks_modified(self):
            checkout = self.make_checkout()
            os.mkdir('checkout/foo')
            checkout.add('foo')
            self.assertRaises(UncommittedCheckout, zap, 'checkout')
            checkout.commit('commit changes to branch')
            zap('checkout')

    return makeSuite(TestZap)