113
114
self.assertListRaises(NoSuchFile, t.get_multi, ['a', 'b', 'c'])
114
115
self.assertListRaises(NoSuchFile, t.get_multi, iter(['a', 'b', 'c']))
117
def test_get_bytes(self):
118
t = self.get_transport()
120
files = ['a', 'b', 'e', 'g']
121
contents = ['contents of a\n',
126
self.build_tree(files, transport=t, line_endings='binary')
127
self.check_transport_contents('contents of a\n', t, 'a')
129
for content, fname in zip(contents, files):
130
self.assertEqual(content, t.get_bytes(fname))
132
self.assertRaises(NoSuchFile, t.get_bytes, 'c')
116
134
def test_put(self):
117
135
t = self.get_transport()
119
137
if t.is_readonly():
120
self.assertRaises(TransportNotPossible,
121
t.put, 'a', 'some text for a\n')
124
t.put('a', StringIO('some text for a\n'))
125
self.failUnless(t.has('a'))
126
self.check_transport_contents('some text for a\n', t, 'a')
127
# Make sure 'has' is updated
128
self.assertEqual(list(t.has_multi(['a', 'b', 'c', 'd', 'e'])),
129
[True, False, False, False, False])
130
# Put also replaces contents
131
self.assertEqual(t.put_multi([('a', StringIO('new\ncontents for\na\n')),
132
('d', StringIO('contents\nfor d\n'))]),
134
self.assertEqual(list(t.has_multi(['a', 'b', 'c', 'd', 'e'])),
135
[True, False, False, True, False])
140
self.applyDeprecated(zero_eleven, t.put, 'a', 'string\ncontents\n')
141
self.check_transport_contents('string\ncontents\n', t, 'a')
143
self.applyDeprecated(zero_eleven,
144
t.put, 'b', StringIO('file-like\ncontents\n'))
145
self.check_transport_contents('file-like\ncontents\n', t, 'b')
147
def test_put_bytes(self):
148
t = self.get_transport()
151
self.assertRaises(TransportNotPossible,
152
t.put_bytes, 'a', 'some text for a\n')
155
t.put_bytes('a', 'some text for a\n')
156
self.failUnless(t.has('a'))
157
self.check_transport_contents('some text for a\n', t, 'a')
159
# The contents should be overwritten
160
t.put_bytes('a', 'new text for a\n')
161
self.check_transport_contents('new text for a\n', t, 'a')
163
self.assertRaises(NoSuchFile,
164
t.put_bytes, 'path/doesnt/exist/c', 'contents')
166
def test_put_bytes_non_atomic(self):
167
t = self.get_transport()
170
self.assertRaises(TransportNotPossible,
171
t.put_bytes_non_atomic, 'a', 'some text for a\n')
174
self.failIf(t.has('a'))
175
t.put_bytes_non_atomic('a', 'some text for a\n')
176
self.failUnless(t.has('a'))
177
self.check_transport_contents('some text for a\n', t, 'a')
178
# Put also replaces contents
179
t.put_bytes_non_atomic('a', 'new\ncontents for\na\n')
180
self.check_transport_contents('new\ncontents for\na\n', t, 'a')
182
# Make sure we can create another file
183
t.put_bytes_non_atomic('d', 'contents for\nd\n')
184
# And overwrite 'a' with empty contents
185
t.put_bytes_non_atomic('a', '')
186
self.check_transport_contents('contents for\nd\n', t, 'd')
187
self.check_transport_contents('', t, 'a')
189
self.assertRaises(NoSuchFile, t.put_bytes_non_atomic, 'no/such/path',
191
# Now test the create_parent flag
192
self.assertRaises(NoSuchFile, t.put_bytes_non_atomic, 'dir/a',
194
self.failIf(t.has('dir/a'))
195
t.put_bytes_non_atomic('dir/a', 'contents for dir/a\n',
196
create_parent_dir=True)
197
self.check_transport_contents('contents for dir/a\n', t, 'dir/a')
199
# But we still get NoSuchFile if we can't make the parent dir
200
self.assertRaises(NoSuchFile, t.put_bytes_non_atomic, 'not/there/a',
202
create_parent_dir=True)
204
def test_put_bytes_permissions(self):
205
t = self.get_transport()
209
if not t._can_roundtrip_unix_modebits():
210
# Can't roundtrip, so no need to run this test
212
t.put_bytes('mode644', 'test text\n', mode=0644)
213
self.assertTransportMode(t, 'mode644', 0644)
214
t.put_bytes('mode666', 'test text\n', mode=0666)
215
self.assertTransportMode(t, 'mode666', 0666)
216
t.put_bytes('mode600', 'test text\n', mode=0600)
217
self.assertTransportMode(t, 'mode600', 0600)
218
# Yes, you can put_bytes a file such that it becomes readonly
219
t.put_bytes('mode400', 'test text\n', mode=0400)
220
self.assertTransportMode(t, 'mode400', 0400)
222
# The default permissions should be based on the current umask
223
umask = osutils.get_umask()
224
t.put_bytes('nomode', 'test text\n', mode=None)
225
self.assertTransportMode(t, 'nomode', 0666 & ~umask)
227
def test_put_bytes_non_atomic_permissions(self):
228
t = self.get_transport()
232
if not t._can_roundtrip_unix_modebits():
233
# Can't roundtrip, so no need to run this test
235
t.put_bytes_non_atomic('mode644', 'test text\n', mode=0644)
236
self.assertTransportMode(t, 'mode644', 0644)
237
t.put_bytes_non_atomic('mode666', 'test text\n', mode=0666)
238
self.assertTransportMode(t, 'mode666', 0666)
239
t.put_bytes_non_atomic('mode600', 'test text\n', mode=0600)
240
self.assertTransportMode(t, 'mode600', 0600)
241
t.put_bytes_non_atomic('mode400', 'test text\n', mode=0400)
242
self.assertTransportMode(t, 'mode400', 0400)
244
# The default permissions should be based on the current umask
245
umask = osutils.get_umask()
246
t.put_bytes_non_atomic('nomode', 'test text\n', mode=None)
247
self.assertTransportMode(t, 'nomode', 0666 & ~umask)
249
# We should also be able to set the mode for a parent directory
251
t.put_bytes_non_atomic('dir700/mode664', 'test text\n', mode=0664,
252
dir_mode=0700, create_parent_dir=True)
253
self.assertTransportMode(t, 'dir700', 0700)
254
t.put_bytes_non_atomic('dir770/mode664', 'test text\n', mode=0664,
255
dir_mode=0770, create_parent_dir=True)
256
self.assertTransportMode(t, 'dir770', 0770)
257
t.put_bytes_non_atomic('dir777/mode664', 'test text\n', mode=0664,
258
dir_mode=0777, create_parent_dir=True)
259
self.assertTransportMode(t, 'dir777', 0777)
261
def test_put_file(self):
262
t = self.get_transport()
265
self.assertRaises(TransportNotPossible,
266
t.put_file, 'a', StringIO('some text for a\n'))
269
t.put_file('a', StringIO('some text for a\n'))
270
self.failUnless(t.has('a'))
271
self.check_transport_contents('some text for a\n', t, 'a')
272
# Put also replaces contents
273
t.put_file('a', StringIO('new\ncontents for\na\n'))
274
self.check_transport_contents('new\ncontents for\na\n', t, 'a')
275
self.assertRaises(NoSuchFile,
276
t.put_file, 'path/doesnt/exist/c',
277
StringIO('contents'))
279
def test_put_file_non_atomic(self):
280
t = self.get_transport()
283
self.assertRaises(TransportNotPossible,
284
t.put_file_non_atomic, 'a', StringIO('some text for a\n'))
287
self.failIf(t.has('a'))
288
t.put_file_non_atomic('a', StringIO('some text for a\n'))
289
self.failUnless(t.has('a'))
290
self.check_transport_contents('some text for a\n', t, 'a')
291
# Put also replaces contents
292
t.put_file_non_atomic('a', StringIO('new\ncontents for\na\n'))
293
self.check_transport_contents('new\ncontents for\na\n', t, 'a')
295
# Make sure we can create another file
296
t.put_file_non_atomic('d', StringIO('contents for\nd\n'))
297
# And overwrite 'a' with empty contents
298
t.put_file_non_atomic('a', StringIO(''))
299
self.check_transport_contents('contents for\nd\n', t, 'd')
300
self.check_transport_contents('', t, 'a')
302
self.assertRaises(NoSuchFile, t.put_file_non_atomic, 'no/such/path',
303
StringIO('contents\n'))
304
# Now test the create_parent flag
305
self.assertRaises(NoSuchFile, t.put_file_non_atomic, 'dir/a',
306
StringIO('contents\n'))
307
self.failIf(t.has('dir/a'))
308
t.put_file_non_atomic('dir/a', StringIO('contents for dir/a\n'),
309
create_parent_dir=True)
310
self.check_transport_contents('contents for dir/a\n', t, 'dir/a')
312
# But we still get NoSuchFile if we can't make the parent dir
313
self.assertRaises(NoSuchFile, t.put_file_non_atomic, 'not/there/a',
314
StringIO('contents\n'),
315
create_parent_dir=True)
317
def test_put_file_permissions(self):
319
t = self.get_transport()
323
if not t._can_roundtrip_unix_modebits():
324
# Can't roundtrip, so no need to run this test
326
t.put_file('mode644', StringIO('test text\n'), mode=0644)
327
self.assertTransportMode(t, 'mode644', 0644)
328
t.put_file('mode666', StringIO('test text\n'), mode=0666)
329
self.assertTransportMode(t, 'mode666', 0666)
330
t.put_file('mode600', StringIO('test text\n'), mode=0600)
331
self.assertTransportMode(t, 'mode600', 0600)
332
# Yes, you can put a file such that it becomes readonly
333
t.put_file('mode400', StringIO('test text\n'), mode=0400)
334
self.assertTransportMode(t, 'mode400', 0400)
336
# XXX: put_multi is deprecated, so do we really care anymore?
337
self.applyDeprecated(zero_eleven, t.put_multi,
338
[('mmode644', StringIO('text\n'))], mode=0644)
339
self.assertTransportMode(t, 'mmode644', 0644)
341
# The default permissions should be based on the current umask
342
umask = osutils.get_umask()
343
t.put_file('nomode', StringIO('test text\n'), mode=None)
344
self.assertTransportMode(t, 'nomode', 0666 & ~umask)
346
def test_put_file_non_atomic_permissions(self):
347
t = self.get_transport()
351
if not t._can_roundtrip_unix_modebits():
352
# Can't roundtrip, so no need to run this test
354
t.put_file_non_atomic('mode644', StringIO('test text\n'), mode=0644)
355
self.assertTransportMode(t, 'mode644', 0644)
356
t.put_file_non_atomic('mode666', StringIO('test text\n'), mode=0666)
357
self.assertTransportMode(t, 'mode666', 0666)
358
t.put_file_non_atomic('mode600', StringIO('test text\n'), mode=0600)
359
self.assertTransportMode(t, 'mode600', 0600)
360
# Yes, you can put_file_non_atomic a file such that it becomes readonly
361
t.put_file_non_atomic('mode400', StringIO('test text\n'), mode=0400)
362
self.assertTransportMode(t, 'mode400', 0400)
364
# The default permissions should be based on the current umask
365
umask = osutils.get_umask()
366
t.put_file_non_atomic('nomode', StringIO('test text\n'), mode=None)
367
self.assertTransportMode(t, 'nomode', 0666 & ~umask)
369
# We should also be able to set the mode for a parent directory
372
t.put_file_non_atomic('dir700/mode664', sio, mode=0664,
373
dir_mode=0700, create_parent_dir=True)
374
self.assertTransportMode(t, 'dir700', 0700)
375
t.put_file_non_atomic('dir770/mode664', sio, mode=0664,
376
dir_mode=0770, create_parent_dir=True)
377
self.assertTransportMode(t, 'dir770', 0770)
378
t.put_file_non_atomic('dir777/mode664', sio, mode=0664,
379
dir_mode=0777, create_parent_dir=True)
380
self.assertTransportMode(t, 'dir777', 0777)
382
def test_put_multi(self):
383
t = self.get_transport()
387
self.assertEqual(2, self.applyDeprecated(zero_eleven,
388
t.put_multi, [('a', StringIO('new\ncontents for\na\n')),
389
('d', StringIO('contents\nfor d\n'))]
391
self.assertEqual(list(t.has_multi(['a', 'b', 'c', 'd'])),
392
[True, False, False, True])
136
393
self.check_transport_contents('new\ncontents for\na\n', t, 'a')
137
394
self.check_transport_contents('contents\nfor d\n', t, 'd')
140
t.put_multi(iter([('a', StringIO('diff\ncontents for\na\n')),
141
('d', StringIO('another contents\nfor d\n'))])),
396
self.assertEqual(2, self.applyDeprecated(zero_eleven,
397
t.put_multi, iter([('a', StringIO('diff\ncontents for\na\n')),
398
('d', StringIO('another contents\nfor d\n'))])
143
400
self.check_transport_contents('diff\ncontents for\na\n', t, 'a')
144
401
self.check_transport_contents('another contents\nfor d\n', t, 'd')
146
self.assertRaises(NoSuchFile,
147
t.put, 'path/doesnt/exist/c', StringIO('contents'))
149
def test_put_permissions(self):
150
t = self.get_transport()
154
if not t._can_roundtrip_unix_modebits():
155
# Can't roundtrip, so no need to run this test
157
t.put('mode644', StringIO('test text\n'), mode=0644)
158
self.assertTransportMode(t, 'mode644', 0644)
159
t.put('mode666', StringIO('test text\n'), mode=0666)
160
self.assertTransportMode(t, 'mode666', 0666)
161
t.put('mode600', StringIO('test text\n'), mode=0600)
162
self.assertTransportMode(t, 'mode600', 0600)
163
# Yes, you can put a file such that it becomes readonly
164
t.put('mode400', StringIO('test text\n'), mode=0400)
165
self.assertTransportMode(t, 'mode400', 0400)
166
t.put_multi([('mmode644', StringIO('text\n'))], mode=0644)
167
self.assertTransportMode(t, 'mmode644', 0644)
169
# The default permissions should be based on the current umask
170
umask = osutils.get_umask()
171
t.put('nomode', StringIO('test text\n'), mode=None)
172
self.assertTransportMode(t, 'nomode', 0666 & ~umask)
174
403
def test_mkdir(self):
175
404
t = self.get_transport()
297
525
t = self.get_transport()
299
527
if t.is_readonly():
300
open('a', 'wb').write('diff\ncontents for\na\n')
301
open('b', 'wb').write('contents\nfor b\n')
304
('a', StringIO('diff\ncontents for\na\n')),
305
('b', StringIO('contents\nfor b\n'))
309
self.assertRaises(TransportNotPossible,
310
t.append, 'a', 'add\nsome\nmore\ncontents\n')
311
_append('a', StringIO('add\nsome\nmore\ncontents\n'))
314
t.append('a', StringIO('add\nsome\nmore\ncontents\n')))
316
self.check_transport_contents(
317
'diff\ncontents for\na\nadd\nsome\nmore\ncontents\n',
321
self.assertRaises(TransportNotPossible,
323
[('a', 'and\nthen\nsome\nmore\n'),
324
('b', 'some\nmore\nfor\nb\n')])
325
_append('a', StringIO('and\nthen\nsome\nmore\n'))
326
_append('b', StringIO('some\nmore\nfor\nb\n'))
328
self.assertEqual((43, 15),
329
t.append_multi([('a', StringIO('and\nthen\nsome\nmore\n')),
330
('b', StringIO('some\nmore\nfor\nb\n'))]))
529
t.put_bytes('a', 'diff\ncontents for\na\n')
530
t.put_bytes('b', 'contents\nfor b\n')
532
self.assertEqual(20, self.applyDeprecated(zero_eleven,
533
t.append, 'a', StringIO('add\nsome\nmore\ncontents\n')))
535
self.check_transport_contents(
536
'diff\ncontents for\na\nadd\nsome\nmore\ncontents\n',
539
# And we can create new files, too
540
self.assertEqual(0, self.applyDeprecated(zero_eleven,
541
t.append, 'c', StringIO('some text\nfor a missing file\n')))
542
self.check_transport_contents('some text\nfor a missing file\n',
544
def test_append_file(self):
545
t = self.get_transport()
548
self.assertRaises(TransportNotPossible,
549
t.append_file, 'a', 'add\nsome\nmore\ncontents\n')
551
t.put_bytes('a', 'diff\ncontents for\na\n')
552
t.put_bytes('b', 'contents\nfor b\n')
555
t.append_file('a', StringIO('add\nsome\nmore\ncontents\n')))
557
self.check_transport_contents(
558
'diff\ncontents for\na\nadd\nsome\nmore\ncontents\n',
561
# a file with no parent should fail..
562
self.assertRaises(NoSuchFile,
563
t.append_file, 'missing/path', StringIO('content'))
565
# And we can create new files, too
567
t.append_file('c', StringIO('some text\nfor a missing file\n')))
568
self.check_transport_contents('some text\nfor a missing file\n',
571
def test_append_bytes(self):
572
t = self.get_transport()
575
self.assertRaises(TransportNotPossible,
576
t.append_bytes, 'a', 'add\nsome\nmore\ncontents\n')
579
self.assertEqual(0, t.append_bytes('a', 'diff\ncontents for\na\n'))
580
self.assertEqual(0, t.append_bytes('b', 'contents\nfor b\n'))
583
t.append_bytes('a', 'add\nsome\nmore\ncontents\n'))
585
self.check_transport_contents(
586
'diff\ncontents for\na\nadd\nsome\nmore\ncontents\n',
589
# a file with no parent should fail..
590
self.assertRaises(NoSuchFile,
591
t.append_bytes, 'missing/path', 'content')
593
def test_append_multi(self):
594
t = self.get_transport()
598
t.put_bytes('a', 'diff\ncontents for\na\n'
599
'add\nsome\nmore\ncontents\n')
600
t.put_bytes('b', 'contents\nfor b\n')
602
self.assertEqual((43, 15),
603
t.append_multi([('a', StringIO('and\nthen\nsome\nmore\n')),
604
('b', StringIO('some\nmore\nfor\nb\n'))]))
331
606
self.check_transport_contents(
332
607
'diff\ncontents for\na\n'
333
608
'add\nsome\nmore\ncontents\n'
374
639
'a little bit more\n'
375
640
'some text in a\n',
377
self.check_transport_contents('some text\nfor a missing file\n',
379
642
self.check_transport_contents('missing file r\n', t, 'd')
381
# a file with no parent should fail..
382
if not t.is_readonly():
383
self.assertRaises(NoSuchFile,
384
t.append, 'missing/path',
387
def test_append_file(self):
388
t = self.get_transport()
391
('f1', StringIO('this is a string\nand some more stuff\n')),
392
('f2', StringIO('here is some text\nand a bit more\n')),
393
('f3', StringIO('some text for the\nthird file created\n')),
394
('f4', StringIO('this is a string\nand some more stuff\n')),
395
('f5', StringIO('here is some text\nand a bit more\n')),
396
('f6', StringIO('some text for the\nthird file created\n'))
400
for f, val in contents:
401
open(f, 'wb').write(val.read())
403
t.put_multi(contents)
405
a1 = StringIO('appending to\none\n')
413
self.check_transport_contents(
414
'this is a string\nand some more stuff\n'
415
'appending to\none\n',
418
a2 = StringIO('adding more\ntext to two\n')
419
a3 = StringIO('some garbage\nto put in three\n')
425
t.append_multi([('f2', a2), ('f3', a3)])
429
self.check_transport_contents(
430
'here is some text\nand a bit more\n'
431
'adding more\ntext to two\n',
433
self.check_transport_contents(
434
'some text for the\nthird file created\n'
435
'some garbage\nto put in three\n',
438
# Test that an actual file object can be used with put
447
self.check_transport_contents(
448
'this is a string\nand some more stuff\n'
449
'this is a string\nand some more stuff\n'
450
'appending to\none\n',
459
t.append_multi([('f5', a5), ('f6', a6)])
463
self.check_transport_contents(
464
'here is some text\nand a bit more\n'
465
'here is some text\nand a bit more\n'
466
'adding more\ntext to two\n',
468
self.check_transport_contents(
469
'some text for the\nthird file created\n'
470
'some text for the\nthird file created\n'
471
'some garbage\nto put in three\n',
483
t.append_multi([('a', a6), ('d', a7)])
485
self.check_transport_contents(t.get('f2').read(), t, 'c')
486
self.check_transport_contents(t.get('f3').read(), t, 'd')
488
def test_append_mode(self):
644
def test_append_file_mode(self):
645
"""Check that append accepts a mode parameter"""
489
646
# check append accepts a mode
490
647
t = self.get_transport()
491
648
if t.is_readonly():
493
t.append('f', StringIO('f'), mode=None)
649
self.assertRaises(TransportNotPossible,
650
t.append_file, 'f', StringIO('f'), mode=None)
652
t.append_file('f', StringIO('f'), mode=None)
654
def test_append_bytes_mode(self):
655
# check append_bytes accepts a mode
656
t = self.get_transport()
658
self.assertRaises(TransportNotPossible,
659
t.append_bytes, 'f', 'f', mode=None)
661
t.append_bytes('f', 'f', mode=None)
495
663
def test_delete(self):
496
664
# TODO: Test Transport.delete