~abentley/bzrtools/bzrtools.dev

621 by Aaron Bentley
Implement link_tree function
1
# Copyright (C) 2008 Aaron Bentley
2
#
3
#    This program is free software; you can redistribute it and/or modify
4
#    it under the terms of the GNU General Public License as published by
5
#    the Free Software Foundation; either version 2 of the License, or
6
#    (at your option) any later version.
7
#
8
#    This program is distributed in the hope that it will be useful,
9
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
10
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
#    GNU General Public License for more details.
12
#
13
#    You should have received a copy of the GNU General Public License
14
#    along with this program; if not, write to the Free Software
15
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
import os
18
19
from bzrlib.transform import TreeTransform
776.1.1 by Jelmer Vernooij
Cope with HardlinkFeature having moved to bzrlib.tests.features in bzr.dev.
20
from bzrlib.tests import TestCaseWithTransport
21
22
try:
23
    from bzrlib.tests.features import HardlinkFeature
24
except ImportError: # bzr < 2.5
25
    from bzrlib.tests import HardlinkFeature
621 by Aaron Bentley
Implement link_tree function
26
730.1.5 by Aaron Bentley
Disable version check for commands run in the test suite.
27
from bzrlib.plugins.bzrtools import command
621 by Aaron Bentley
Implement link_tree function
28
from bzrlib.plugins.bzrtools.link_tree import link_tree
29
622 by Aaron Bentley
Add link-tree command
30
31
class TestLinkTreeBase(object):
621 by Aaron Bentley
Implement link_tree function
32
636 by Aaron Bentley
Simplify hardlink feature use
33
    _test_needs_features = [HardlinkFeature]
34
621 by Aaron Bentley
Implement link_tree function
35
    def setUp(self):
36
        TestCaseWithTransport.setUp(self)
37
        self.parent_tree = self.make_branch_and_tree('parent')
38
        self.parent_tree.lock_write()
39
        self.addCleanup(self.parent_tree.unlock)
40
        self.build_tree_contents([('parent/foo', 'bar')])
41
        self.parent_tree.add('foo', 'foo-id')
42
        self.parent_tree.commit('added foo')
43
        child_bzrdir = self.parent_tree.bzrdir.sprout('child')
44
        self.child_tree = child_bzrdir.open_workingtree()
45
46
    def hardlinked(self):
47
        parent_stat = os.lstat(self.parent_tree.abspath('foo'))
48
        child_stat = os.lstat(self.child_tree.abspath('foo'))
49
        return parent_stat.st_ino == child_stat.st_ino
50
622 by Aaron Bentley
Add link-tree command
51
52
class TestLinkTree(TestLinkTreeBase, TestCaseWithTransport):
53
621 by Aaron Bentley
Implement link_tree function
54
    def test_link_fails_if_modified(self):
55
        """If the file to be linked has modified text, don't link."""
56
        self.build_tree_contents([('child/foo', 'baz')])
57
        link_tree(self.child_tree, self.parent_tree)
58
        self.assertFalse(self.hardlinked())
59
60
    def test_link_fails_if_execute_bit_changed(self):
61
        """If the file to be linked has modified execute bit, don't link."""
62
        tt = TreeTransform(self.child_tree)
63
        try:
64
            trans_id = tt.trans_id_tree_file_id('foo-id')
65
            tt.set_executability(True, trans_id)
66
            tt.apply()
67
        finally:
68
            tt.finalize()
69
        link_tree(self.child_tree, self.parent_tree)
70
        self.assertFalse(self.hardlinked())
71
72
    def test_link_succeeds_if_unmodified(self):
73
        """If the file to be linked is unmodified, link"""
74
        link_tree(self.child_tree, self.parent_tree)
75
        self.assertTrue(self.hardlinked())
622 by Aaron Bentley
Add link-tree command
76
77
78
class TestLinkTreeCommand(TestLinkTreeBase, TestCaseWithTransport):
79
80
    def test_link_tree(self):
81
        """Ensure the command works as intended"""
730.1.5 by Aaron Bentley
Disable version check for commands run in the test suite.
82
        command._testing = True
83
        try:
84
            os.chdir('child')
85
            self.parent_tree.unlock()
86
            self.run_bzr('link-tree ../parent')
87
            self.assertTrue(self.hardlinked())
88
            # want teh addCleanup to work properly
89
            self.parent_tree.lock_write()
90
        finally:
91
            command._testing = False