~bzr-pqm/bzr/bzr.dev

1 by mbp at sourcefrog
import from baz patch-364
1
#! /usr/bin/env python
2
# -*- coding: UTF-8 -*-
3
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
19
20
# XXX: We might prefer these to be in a text file rather than Python
21
# source, but that only works in doctest from Python 2.4 and later,
22
# which is not present in Warty.
23
24
r"""
25
Bazaar-NG test cases
26
********************
27
28
These are run by ``bzr.doctest``.
29
52 by mbp at sourcefrog
fixup doctest for new module structure
30
>>> import bzrlib, os
100 by mbp at sourcefrog
- add test case for ignore files
31
>>> from bzrlib import ScratchBranch
33 by mbp at sourcefrog
fix up doctest for code rearrangement
32
>>> bzrlib.commands.cmd_rocks()
1 by mbp at sourcefrog
import from baz patch-364
33
it sure does!
34
35
Hey, nice place to begin.
36
37
The basic object is a Branch.  We have a special helper class
38
ScratchBranch that automatically makes a directory and cleans itself
39
up, but is in other respects identical.
40
41
ScratchBranches are initially empty:
42
33 by mbp at sourcefrog
fix up doctest for code rearrangement
43
>>> b = bzrlib.ScratchBranch()
1 by mbp at sourcefrog
import from baz patch-364
44
>>> b.show_status()
45
46
New files in that directory are, it is initially unknown:
47
48
>>> file(b.base + '/hello.c', 'wt').write('int main() {}')
49
>>> b.show_status()
50
?       hello.c
51
52
That's not quite true; some files (like editor backups) are ignored by
53
default:
54
55
>>> file(b.base + '/hello.c~', 'wt').write('int main() {}')
56
>>> b.show_status()
57
?       hello.c
58
>>> list(b.unknowns())
59
['hello.c']
60
61
The ``add`` command marks a file to be added in the next revision:
62
63
>>> b.add('hello.c')
64
>>> b.show_status()
65
A       hello.c
66
67
You can also add files that otherwise would be ignored.  The ignore
68
patterns only apply to files that would be otherwise unknown, so they
69
have no effect once it's added.
70
71
>>> b.add('hello.c~')
72
>>> b.show_status()
73
A       hello.c
74
A       hello.c~
75
76
It is an error to add a file that isn't present in the working copy:
77
78
  >>> b.add('nothere')
79
  Traceback (most recent call last):
80
  ...
81
  BzrError: ('cannot add: not a regular file or directory: nothere', [])
82
83
If we add a file and then change our mind, we can either revert it or
84
remove the file.  If we revert, we are left with the working copy (in
85
either I or ? state).  If we remove, the working copy is gone.  Let's
86
do that to the backup, presumably added accidentally.
87
88
  >>> b.remove('hello.c~')
89
  >>> b.show_status()
90
  A       hello.c
91
92
Now to commit, creating a new revision.  (Fake the date and name for
93
reproducibility.)
94
95
  >>> b.commit('start hello world', timestamp=0, committer='foo@nowhere')
96
  >>> b.show_status()
97
  >>> b.show_status(show_all=True)
98
  .       hello.c
99
  I       hello.c~
100
101
102
We can look back at history
103
104
  >>> r = b.get_revision(b.lookup_revision(1))
105
  >>> r.message
106
  'start hello world'
12 by mbp at sourcefrog
new --timezone option for bzr log
107
  >>> b.write_log(show_timezone='utc')
1 by mbp at sourcefrog
import from baz patch-364
108
  ----------------------------------------
109
  revno: 1
110
  committer: foo@nowhere
12 by mbp at sourcefrog
new --timezone option for bzr log
111
  timestamp: Thu 1970-01-01 00:00:00 +0000
1 by mbp at sourcefrog
import from baz patch-364
112
  message:
113
    start hello world
114
115
(The other fields will be a bit unpredictable, depending on who ran
116
this test and when.)
117
118
As of 2005-02-21, we can also add subdirectories to the revision!
119
120
  >>> os.mkdir(b.base + "/lib")
121
  >>> b.show_status()
122
  ?       lib/
123
  >>> b.add('lib')
124
  >>> b.show_status()
125
  A       lib/
126
  >>> b.commit('add subdir')
127
  >>> b.show_status()
128
  >>> b.show_status(show_all=True)
129
  .       hello.c
130
  I       hello.c~
131
  .       lib/
132
133
and we can also add files within subdirectories:
134
135
  >>> file(b.base + '/lib/hello', 'w').write('hello!\n')
136
  >>> b.show_status()
137
  ?       lib/hello
138
  
139
  
140
Tests for adding subdirectories, etc.
141
142
    >>> b = bzrlib.branch.ScratchBranch()
67 by mbp at sourcefrog
use abspath() for the function that makes an absolute
143
    >>> os.mkdir(b.abspath('d1'))
144
    >>> os.mkdir(b.abspath('d2'))
145
    >>> os.mkdir(b.abspath('d2/d3'))
1 by mbp at sourcefrog
import from baz patch-364
146
    >>> list(b.working_tree().unknowns())
147
    ['d1', 'd2']
148
149
Create some files, but they're not seen as unknown yet:
150
67 by mbp at sourcefrog
use abspath() for the function that makes an absolute
151
    >>> file(b.abspath('d1/f1'), 'w').close()
152
    >>> file(b.abspath('d2/f2'), 'w').close()
153
    >>> file(b.abspath('d2/f3'), 'w').close()
1 by mbp at sourcefrog
import from baz patch-364
154
    >>> [v[0] for v in b.inventory.directories()]
155
    ['']
156
    >>> list(b.working_tree().unknowns())
157
    ['d1', 'd2']
158
159
Adding a directory, and we see the file underneath:
160
    
161
    >>> b.add('d1')
162
    >>> [v[0] for v in b.inventory.directories()]
163
    ['', 'd1']
164
    >>> list(b.working_tree().unknowns())
165
    ['d1/f1', 'd2']
166
    >>> # d2 comes first because it's in the top directory
167
168
    >>> b.add('d2')
169
    >>> b.commit('add some stuff')
170
    >>> list(b.working_tree().unknowns())
171
    ['d1/f1', 'd2/d3', 'd2/f2', 'd2/f3']
172
56 by mbp at sourcefrog
more add tests
173
    >>> b.add('d1/f1')
174
    >>> list(b.working_tree().unknowns())
175
    ['d2/d3', 'd2/f2', 'd2/f3']
176
100 by mbp at sourcefrog
- add test case for ignore files
177
Tests for ignored files and patterns:
178
179
    >>> b = ScratchBranch(dirs=['src', 'doc'],
180
    ...                   files=['configure.in', 'configure',
181
    ...                          'doc/configure', 'foo.c',
182
    ...                          'foo'])
183
    >>> list(b.unknowns())
184
    ['configure', 'configure.in', 'doc', 'foo', 'foo.c', 'src']
185
    >>> b.add(['doc', 'foo.c', 'src', 'configure.in'])
186
    >>> list(b.unknowns())
187
    ['configure', 'doc/configure', 'foo']
188
    >>> f = file(b.abspath('.bzrignore'), 'w')
189
    >>> f.write('./configure\n'
190
    ...         './foo\n')
191
    >>> f.close()
192
    >>> b.add('.bzrignore')
193
    >>> list(b.unknowns())
194
    ['configure', 'doc/configure', 'foo']
1 by mbp at sourcefrog
import from baz patch-364
195
"""