~bzr-pqm/bzr/bzr.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Copyright (C) 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Test that all WorkingTree's implement get_file_mtime."""

import os

from bzrlib import errors
from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree


class TestGetFileMTime(TestCaseWithWorkingTree):
    """Test WorkingTree.get_file_mtime.

    These are more involved because we need to handle files which have been
    renamed, etc.
    """

    def make_basic_tree(self):
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/one'])
        tree.add(['one'], ['one-id'])
        return tree

    def test_get_file_mtime(self):
        tree = self.make_basic_tree()

        st = os.lstat('tree/one')
        tree.lock_read()
        try:
            mtime_file_id = tree.get_file_mtime(file_id='one-id')
            self.assertIsInstance(mtime_file_id, (float, int))
            self.assertAlmostEqual(st.st_mtime, mtime_file_id)
            mtime_path = tree.get_file_mtime(file_id='one-id', path='one')
            self.assertAlmostEqual(mtime_file_id, mtime_path)
        finally:
            tree.unlock()

    def test_after_commit(self):
        """Committing shouldn't change the mtime."""
        tree = self.make_basic_tree()

        st = os.lstat('tree/one')
        tree.commit('one', rev_id='rev-1')

        tree.lock_read()
        try:
            mtime = tree.get_file_mtime(file_id='one-id')
            self.assertAlmostEqual(st.st_mtime, mtime)

            mtime = tree.get_file_mtime(file_id='one-id', path='one')
            self.assertAlmostEqual(st.st_mtime, mtime)
        finally:
            tree.unlock()

    def test_get_renamed_time(self):
        """We should handle renamed files."""
        tree = self.make_basic_tree()

        tree.rename_one('one', 'two')
        st = os.lstat('tree/two')

        tree.lock_read()
        try:
            mtime = tree.get_file_mtime(file_id='one-id')
            self.assertAlmostEqual(st.st_mtime, mtime)
            mtime = tree.get_file_mtime(file_id='one-id', path='two')
            self.assertAlmostEqual(st.st_mtime, mtime)
        finally:
            tree.unlock()

    def test_get_renamed_in_subdir_time(self):
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/d/', 'tree/d/a'])
        tree.add(['d', 'd/a'], ['d-id', 'a-id'])
        tree.commit('1', rev_id='rev-1')

        tree.rename_one('d', 'e')

        st = os.lstat('tree/e/a')
        tree.lock_read()
        try:
            mtime = tree.get_file_mtime(file_id='a-id')
            self.assertAlmostEqual(st.st_mtime, mtime)
            mtime = tree.get_file_mtime(file_id='a-id', path='e/a')
            self.assertAlmostEqual(st.st_mtime, mtime)
        finally:
            tree.unlock()

    def test_missing(self):
        tree = self.make_basic_tree()

        os.remove('tree/one')
        tree.lock_read()
        try:
            self.assertRaises(errors.FileTimestampUnavailable,
                tree.get_file_mtime, file_id='one-id')
        finally:
            tree.unlock()