~bzr-pqm/bzr/bzr.dev

4988.10.3 by John Arbash Meinel
Merge bzr.dev 5007, resolve conflict, update NEWS
1
# Copyright (C) 2009, 2010 Canonical Ltd
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
16
17
import os
4996.1.1 by Robert Collins
Set the file mtime when exporting to a directory to prevent triggering make rebuilds unnecessarily.
18
import time
4241.9.5 by Vincent Ladeuil
Fix unicode related OSX failures.
19
20
from bzrlib import (
4576.1.1 by Alexander Belchenko
Fixed export to existing empty directory.
21
    errors,
4241.9.5 by Vincent Ladeuil
Fix unicode related OSX failures.
22
    export,
23
    tests,
24
    )
25
26
27
class TestExport(tests.TestCaseWithTransport):
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
28
29
    def test_dir_export_missing_file(self):
30
        self.build_tree(['a/', 'a/b', 'a/c'])
31
        wt = self.make_branch_and_tree('.')
32
        wt.add(['a', 'a/b', 'a/c'])
33
        os.unlink('a/c')
4241.9.5 by Vincent Ladeuil
Fix unicode related OSX failures.
34
        export.export(wt, 'target', format="dir")
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
35
        self.failUnlessExists('target/a/b')
36
        self.failIfExists('target/a/c')
4241.9.5 by Vincent Ladeuil
Fix unicode related OSX failures.
37
4562.1.1 by Jelmer Vernooij
Support exporting symlinks when exporting from a working tree.
38
    def test_dir_export_symlink(self):
4562.1.2 by Jelmer Vernooij
Use self.requireFeature(tests.SymlinkFeature) per John's review.
39
        self.requireFeature(tests.SymlinkFeature)
4562.1.1 by Jelmer Vernooij
Support exporting symlinks when exporting from a working tree.
40
        wt = self.make_branch_and_tree('.')
41
        os.symlink('source', 'link')
42
        wt.add(['link'])
43
        export.export(wt, 'target', format="dir")
44
        self.failUnlessExists('target/link')
4576.1.1 by Alexander Belchenko
Fixed export to existing empty directory.
45
46
    def test_dir_export_to_existing_empty_dir_success(self):
47
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
48
        wt = self.make_branch_and_tree('source')
49
        wt.add(['a', 'b', 'b/c'])
50
        wt.commit('1')
51
        self.build_tree(['target/'])
52
        export.export(wt, 'target', format="dir")
53
        self.failUnlessExists('target/a')
54
        self.failUnlessExists('target/b')
55
        self.failUnlessExists('target/b/c')
56
57
    def test_dir_export_to_existing_nonempty_dir_fail(self):
58
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
59
        wt = self.make_branch_and_tree('source')
60
        wt.add(['a', 'b', 'b/c'])
61
        wt.commit('1')
62
        self.build_tree(['target/', 'target/foo'])
63
        self.assertRaises(errors.BzrError, export.export, wt, 'target', format="dir")
4988.10.1 by michal
- bug #511987 fixed, export of single file
64
65
    def test_dir_export_existing_single_file(self):
66
        self.build_tree(['dir1/', 'dir1/dir2/', 'dir1/first', 'dir1/dir2/second'])
67
        wtree = self.make_branch_and_tree('dir1')
68
        wtree.add(['dir2', 'first', 'dir2/second'])
69
        wtree.commit('1')
70
        export.export(wtree, 'target1', format='dir', subdir='first')
71
        self.failUnlessExists('target1/first')
72
        export.export(wtree, 'target2', format='dir', subdir='dir2/second')
73
        self.failUnlessExists('target2/second')
74
        
4996.1.1 by Robert Collins
Set the file mtime when exporting to a directory to prevent triggering make rebuilds unnecessarily.
75
    def test_dir_export_files_same_timestamp(self):
76
        builder = self.make_branch_builder('source')
77
        builder.start_series()
78
        builder.build_snapshot(None, None, [
79
            ('add', ('', 'root-id', 'directory', '')),
80
            ('add', ('a', 'a-id', 'file', 'content\n'))])
81
        builder.build_snapshot(None, None, [
82
            ('add', ('b', 'b-id', 'file', 'content\n'))])
83
        builder.finish_series()
84
        b = builder.get_branch()
85
        b.lock_read()
86
        self.addCleanup(b.unlock)
87
        tree = b.basis_tree()
88
        orig_iter_files_bytes = tree.iter_files_bytes
89
        # Make iter_files_bytes slower, so we provoke mtime skew
90
        def iter_files_bytes(to_fetch):
91
            for thing in orig_iter_files_bytes(to_fetch):
92
                yield thing
93
                time.sleep(1)
94
        tree.iter_files_bytes = iter_files_bytes
95
        export.export(tree, 'target', format='dir')
96
        t = self.get_transport('target')
97
        st_a = t.stat('a')
98
        st_b = t.stat('b')
99
        # All files must be given the same mtime.
100
        self.assertEqual(st_a.st_mtime, st_b.st_mtime)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
101
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
102
    def test_dir_export_files_per_file_timestamps(self):
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
103
        builder = self.make_branch_builder('source')
104
        builder.start_series()
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
105
        # Earliest allowable date on FAT32 filesystems is 1980-01-01
106
        a_time = time.mktime((1999, 12, 12, 0, 0, 0, 0, 0, 0))
107
        b_time = time.mktime((1980, 01, 01, 0, 0, 0, 0, 0, 0))
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
108
        builder.build_snapshot(None, None, [
109
            ('add', ('', 'root-id', 'directory', '')),
110
            ('add', ('a', 'a-id', 'file', 'content\n'))],
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
111
            timestamp=a_time)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
112
        builder.build_snapshot(None, None, [
113
            ('add', ('b', 'b-id', 'file', 'content\n'))],
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
114
            timestamp=b_time)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
115
        builder.finish_series()
116
        b = builder.get_branch()
117
        b.lock_read()
118
        self.addCleanup(b.unlock)
119
        tree = b.basis_tree()
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
120
        export.export(tree, 'target', format='dir', per_file_timestamps=True)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
121
        t = self.get_transport('target')
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
122
        self.assertEqual(a_time, t.stat('a').st_mtime)
123
        self.assertEqual(b_time, t.stat('b').st_mtime)