111
112
self.assertListRaises(NoSuchFile, t.get_multi, ['a', 'b', 'c'])
112
113
self.assertListRaises(NoSuchFile, t.get_multi, iter(['a', 'b', 'c']))
115
def test_get_bytes(self):
116
t = self.get_transport()
118
files = ['a', 'b', 'e', 'g']
119
contents = ['contents of a\n',
124
self.build_tree(files, transport=t, line_endings='binary')
125
self.check_transport_contents('contents of a\n', t, 'a')
127
for content, fname in zip(contents, files):
128
self.assertEqual(content, t.get_bytes(fname))
130
self.assertRaises(NoSuchFile, t.get_bytes, 'c')
114
132
def test_put(self):
115
133
t = self.get_transport()
117
135
if t.is_readonly():
118
self.assertRaises(TransportNotPossible,
119
t.put, 'a', 'some text for a\n')
122
t.put('a', StringIO('some text for a\n'))
123
self.failUnless(t.has('a'))
124
self.check_transport_contents('some text for a\n', t, 'a')
125
# Make sure 'has' is updated
126
self.assertEqual(list(t.has_multi(['a', 'b', 'c', 'd', 'e'])),
127
[True, False, False, False, False])
128
# Put also replaces contents
129
self.assertEqual(t.put_multi([('a', StringIO('new\ncontents for\na\n')),
130
('d', StringIO('contents\nfor d\n'))]),
132
self.assertEqual(list(t.has_multi(['a', 'b', 'c', 'd', 'e'])),
133
[True, False, False, True, False])
138
self.applyDeprecated(zero_eleven, t.put, 'a', 'string\ncontents\n')
139
self.check_transport_contents('string\ncontents\n', t, 'a')
141
self.applyDeprecated(zero_eleven,
142
t.put, 'b', StringIO('file-like\ncontents\n'))
143
self.check_transport_contents('file-like\ncontents\n', t, 'b')
145
def test_put_bytes(self):
146
t = self.get_transport()
149
self.assertRaises(TransportNotPossible,
150
t.put_bytes, 'a', 'some text for a\n')
153
t.put_bytes('a', 'some text for a\n')
154
self.failUnless(t.has('a'))
155
self.check_transport_contents('some text for a\n', t, 'a')
157
# The contents should be overwritten
158
t.put_bytes('a', 'new text for a\n')
159
self.check_transport_contents('new text for a\n', t, 'a')
161
self.assertRaises(NoSuchFile,
162
t.put_bytes, 'path/doesnt/exist/c', 'contents')
164
def test_put_bytes_non_atomic(self):
165
t = self.get_transport()
168
self.assertRaises(TransportNotPossible,
169
t.put_bytes_non_atomic, 'a', 'some text for a\n')
172
self.failIf(t.has('a'))
173
t.put_bytes_non_atomic('a', 'some text for a\n')
174
self.failUnless(t.has('a'))
175
self.check_transport_contents('some text for a\n', t, 'a')
176
# Put also replaces contents
177
t.put_bytes_non_atomic('a', 'new\ncontents for\na\n')
178
self.check_transport_contents('new\ncontents for\na\n', t, 'a')
180
# Make sure we can create another file
181
t.put_bytes_non_atomic('d', 'contents for\nd\n')
182
# And overwrite 'a' with empty contents
183
t.put_bytes_non_atomic('a', '')
184
self.check_transport_contents('contents for\nd\n', t, 'd')
185
self.check_transport_contents('', t, 'a')
187
self.assertRaises(NoSuchFile, t.put_bytes_non_atomic, 'no/such/path',
189
# Now test the create_parent flag
190
self.assertRaises(NoSuchFile, t.put_bytes_non_atomic, 'dir/a',
192
self.failIf(t.has('dir/a'))
193
t.put_bytes_non_atomic('dir/a', 'contents for dir/a\n',
194
create_parent_dir=True)
195
self.check_transport_contents('contents for dir/a\n', t, 'dir/a')
197
# But we still get NoSuchFile if we can't make the parent dir
198
self.assertRaises(NoSuchFile, t.put_bytes_non_atomic, 'not/there/a',
200
create_parent_dir=True)
202
def test_put_bytes_permissions(self):
203
t = self.get_transport()
207
if not t._can_roundtrip_unix_modebits():
208
# Can't roundtrip, so no need to run this test
210
t.put_bytes('mode644', 'test text\n', mode=0644)
211
self.assertTransportMode(t, 'mode644', 0644)
212
t.put_bytes('mode666', 'test text\n', mode=0666)
213
self.assertTransportMode(t, 'mode666', 0666)
214
t.put_bytes('mode600', 'test text\n', mode=0600)
215
self.assertTransportMode(t, 'mode600', 0600)
216
# Yes, you can put_bytes a file such that it becomes readonly
217
t.put_bytes('mode400', 'test text\n', mode=0400)
218
self.assertTransportMode(t, 'mode400', 0400)
220
# The default permissions should be based on the current umask
221
umask = osutils.get_umask()
222
t.put_bytes('nomode', 'test text\n', mode=None)
223
self.assertTransportMode(t, 'nomode', 0666 & ~umask)
225
def test_put_bytes_non_atomic_permissions(self):
226
t = self.get_transport()
230
if not t._can_roundtrip_unix_modebits():
231
# Can't roundtrip, so no need to run this test
233
t.put_bytes_non_atomic('mode644', 'test text\n', mode=0644)
234
self.assertTransportMode(t, 'mode644', 0644)
235
t.put_bytes_non_atomic('mode666', 'test text\n', mode=0666)
236
self.assertTransportMode(t, 'mode666', 0666)
237
t.put_bytes_non_atomic('mode600', 'test text\n', mode=0600)
238
self.assertTransportMode(t, 'mode600', 0600)
239
t.put_bytes_non_atomic('mode400', 'test text\n', mode=0400)
240
self.assertTransportMode(t, 'mode400', 0400)
242
# The default permissions should be based on the current umask
243
umask = osutils.get_umask()
244
t.put_bytes_non_atomic('nomode', 'test text\n', mode=None)
245
self.assertTransportMode(t, 'nomode', 0666 & ~umask)
247
# We should also be able to set the mode for a parent directory
249
t.put_bytes_non_atomic('dir700/mode664', 'test text\n', mode=0664,
250
dir_mode=0700, create_parent_dir=True)
251
self.assertTransportMode(t, 'dir700', 0700)
252
t.put_bytes_non_atomic('dir770/mode664', 'test text\n', mode=0664,
253
dir_mode=0770, create_parent_dir=True)
254
self.assertTransportMode(t, 'dir770', 0770)
255
t.put_bytes_non_atomic('dir777/mode664', 'test text\n', mode=0664,
256
dir_mode=0777, create_parent_dir=True)
257
self.assertTransportMode(t, 'dir777', 0777)
259
def test_put_file(self):
260
t = self.get_transport()
263
self.assertRaises(TransportNotPossible,
264
t.put_file, 'a', StringIO('some text for a\n'))
267
t.put_file('a', StringIO('some text for a\n'))
268
self.failUnless(t.has('a'))
269
self.check_transport_contents('some text for a\n', t, 'a')
270
# Put also replaces contents
271
t.put_file('a', StringIO('new\ncontents for\na\n'))
272
self.check_transport_contents('new\ncontents for\na\n', t, 'a')
273
self.assertRaises(NoSuchFile,
274
t.put_file, 'path/doesnt/exist/c',
275
StringIO('contents'))
277
def test_put_file_non_atomic(self):
278
t = self.get_transport()
281
self.assertRaises(TransportNotPossible,
282
t.put_file_non_atomic, 'a', StringIO('some text for a\n'))
285
self.failIf(t.has('a'))
286
t.put_file_non_atomic('a', StringIO('some text for a\n'))
287
self.failUnless(t.has('a'))
288
self.check_transport_contents('some text for a\n', t, 'a')
289
# Put also replaces contents
290
t.put_file_non_atomic('a', StringIO('new\ncontents for\na\n'))
291
self.check_transport_contents('new\ncontents for\na\n', t, 'a')
293
# Make sure we can create another file
294
t.put_file_non_atomic('d', StringIO('contents for\nd\n'))
295
# And overwrite 'a' with empty contents
296
t.put_file_non_atomic('a', StringIO(''))
297
self.check_transport_contents('contents for\nd\n', t, 'd')
298
self.check_transport_contents('', t, 'a')
300
self.assertRaises(NoSuchFile, t.put_file_non_atomic, 'no/such/path',
301
StringIO('contents\n'))
302
# Now test the create_parent flag
303
self.assertRaises(NoSuchFile, t.put_file_non_atomic, 'dir/a',
304
StringIO('contents\n'))
305
self.failIf(t.has('dir/a'))
306
t.put_file_non_atomic('dir/a', StringIO('contents for dir/a\n'),
307
create_parent_dir=True)
308
self.check_transport_contents('contents for dir/a\n', t, 'dir/a')
310
# But we still get NoSuchFile if we can't make the parent dir
311
self.assertRaises(NoSuchFile, t.put_file_non_atomic, 'not/there/a',
312
StringIO('contents\n'),
313
create_parent_dir=True)
315
def test_put_file_permissions(self):
317
t = self.get_transport()
321
if not t._can_roundtrip_unix_modebits():
322
# Can't roundtrip, so no need to run this test
324
t.put_file('mode644', StringIO('test text\n'), mode=0644)
325
self.assertTransportMode(t, 'mode644', 0644)
326
t.put_file('mode666', StringIO('test text\n'), mode=0666)
327
self.assertTransportMode(t, 'mode666', 0666)
328
t.put_file('mode600', StringIO('test text\n'), mode=0600)
329
self.assertTransportMode(t, 'mode600', 0600)
330
# Yes, you can put a file such that it becomes readonly
331
t.put_file('mode400', StringIO('test text\n'), mode=0400)
332
self.assertTransportMode(t, 'mode400', 0400)
334
# XXX: put_multi is deprecated, so do we really care anymore?
335
self.applyDeprecated(zero_eleven, t.put_multi,
336
[('mmode644', StringIO('text\n'))], mode=0644)
337
self.assertTransportMode(t, 'mmode644', 0644)
339
# The default permissions should be based on the current umask
340
umask = osutils.get_umask()
341
t.put_file('nomode', StringIO('test text\n'), mode=None)
342
self.assertTransportMode(t, 'nomode', 0666 & ~umask)
344
def test_put_file_non_atomic_permissions(self):
345
t = self.get_transport()
349
if not t._can_roundtrip_unix_modebits():
350
# Can't roundtrip, so no need to run this test
352
t.put_file_non_atomic('mode644', StringIO('test text\n'), mode=0644)
353
self.assertTransportMode(t, 'mode644', 0644)
354
t.put_file_non_atomic('mode666', StringIO('test text\n'), mode=0666)
355
self.assertTransportMode(t, 'mode666', 0666)
356
t.put_file_non_atomic('mode600', StringIO('test text\n'), mode=0600)
357
self.assertTransportMode(t, 'mode600', 0600)
358
# Yes, you can put_file_non_atomic a file such that it becomes readonly
359
t.put_file_non_atomic('mode400', StringIO('test text\n'), mode=0400)
360
self.assertTransportMode(t, 'mode400', 0400)
362
# The default permissions should be based on the current umask
363
umask = osutils.get_umask()
364
t.put_file_non_atomic('nomode', StringIO('test text\n'), mode=None)
365
self.assertTransportMode(t, 'nomode', 0666 & ~umask)
367
# We should also be able to set the mode for a parent directory
370
t.put_file_non_atomic('dir700/mode664', sio, mode=0664,
371
dir_mode=0700, create_parent_dir=True)
372
self.assertTransportMode(t, 'dir700', 0700)
373
t.put_file_non_atomic('dir770/mode664', sio, mode=0664,
374
dir_mode=0770, create_parent_dir=True)
375
self.assertTransportMode(t, 'dir770', 0770)
376
t.put_file_non_atomic('dir777/mode664', sio, mode=0664,
377
dir_mode=0777, create_parent_dir=True)
378
self.assertTransportMode(t, 'dir777', 0777)
380
def test_put_multi(self):
381
t = self.get_transport()
385
self.assertEqual(2, self.applyDeprecated(zero_eleven,
386
t.put_multi, [('a', StringIO('new\ncontents for\na\n')),
387
('d', StringIO('contents\nfor d\n'))]
389
self.assertEqual(list(t.has_multi(['a', 'b', 'c', 'd'])),
390
[True, False, False, True])
134
391
self.check_transport_contents('new\ncontents for\na\n', t, 'a')
135
392
self.check_transport_contents('contents\nfor d\n', t, 'd')
138
t.put_multi(iter([('a', StringIO('diff\ncontents for\na\n')),
139
('d', StringIO('another contents\nfor d\n'))])),
394
self.assertEqual(2, self.applyDeprecated(zero_eleven,
395
t.put_multi, iter([('a', StringIO('diff\ncontents for\na\n')),
396
('d', StringIO('another contents\nfor d\n'))])
141
398
self.check_transport_contents('diff\ncontents for\na\n', t, 'a')
142
399
self.check_transport_contents('another contents\nfor d\n', t, 'd')
144
self.assertRaises(NoSuchFile,
145
t.put, 'path/doesnt/exist/c', StringIO('contents'))
147
def test_put_permissions(self):
148
t = self.get_transport()
152
if not t._can_roundtrip_unix_modebits():
153
# Can't roundtrip, so no need to run this test
155
t.put('mode644', StringIO('test text\n'), mode=0644)
156
self.assertTransportMode(t, 'mode644', 0644)
157
t.put('mode666', StringIO('test text\n'), mode=0666)
158
self.assertTransportMode(t, 'mode666', 0666)
159
t.put('mode600', StringIO('test text\n'), mode=0600)
160
self.assertTransportMode(t, 'mode600', 0600)
161
# Yes, you can put a file such that it becomes readonly
162
t.put('mode400', StringIO('test text\n'), mode=0400)
163
self.assertTransportMode(t, 'mode400', 0400)
164
t.put_multi([('mmode644', StringIO('text\n'))], mode=0644)
165
self.assertTransportMode(t, 'mmode644', 0644)
167
# The default permissions should be based on the current umask
168
umask = osutils.get_umask()
169
t.put('nomode', StringIO('test text\n'), mode=None)
170
self.assertTransportMode(t, 'nomode', 0666 & ~umask)
172
401
def test_mkdir(self):
173
402
t = self.get_transport()
295
523
t = self.get_transport()
297
525
if t.is_readonly():
298
open('a', 'wb').write('diff\ncontents for\na\n')
299
open('b', 'wb').write('contents\nfor b\n')
302
('a', StringIO('diff\ncontents for\na\n')),
303
('b', StringIO('contents\nfor b\n'))
307
self.assertRaises(TransportNotPossible,
308
t.append, 'a', 'add\nsome\nmore\ncontents\n')
309
_append('a', StringIO('add\nsome\nmore\ncontents\n'))
312
t.append('a', StringIO('add\nsome\nmore\ncontents\n')))
314
self.check_transport_contents(
315
'diff\ncontents for\na\nadd\nsome\nmore\ncontents\n',
319
self.assertRaises(TransportNotPossible,
321
[('a', 'and\nthen\nsome\nmore\n'),
322
('b', 'some\nmore\nfor\nb\n')])
323
_append('a', StringIO('and\nthen\nsome\nmore\n'))
324
_append('b', StringIO('some\nmore\nfor\nb\n'))
326
self.assertEqual((43, 15),
327
t.append_multi([('a', StringIO('and\nthen\nsome\nmore\n')),
328
('b', StringIO('some\nmore\nfor\nb\n'))]))
527
t.put_bytes('a', 'diff\ncontents for\na\n')
528
t.put_bytes('b', 'contents\nfor b\n')
530
self.assertEqual(20, self.applyDeprecated(zero_eleven,
531
t.append, 'a', StringIO('add\nsome\nmore\ncontents\n')))
533
self.check_transport_contents(
534
'diff\ncontents for\na\nadd\nsome\nmore\ncontents\n',
537
# And we can create new files, too
538
self.assertEqual(0, self.applyDeprecated(zero_eleven,
539
t.append, 'c', StringIO('some text\nfor a missing file\n')))
540
self.check_transport_contents('some text\nfor a missing file\n',
542
def test_append_file(self):
543
t = self.get_transport()
546
self.assertRaises(TransportNotPossible,
547
t.append_file, 'a', 'add\nsome\nmore\ncontents\n')
549
t.put_bytes('a', 'diff\ncontents for\na\n')
550
t.put_bytes('b', 'contents\nfor b\n')
553
t.append_file('a', StringIO('add\nsome\nmore\ncontents\n')))
555
self.check_transport_contents(
556
'diff\ncontents for\na\nadd\nsome\nmore\ncontents\n',
559
# a file with no parent should fail..
560
self.assertRaises(NoSuchFile,
561
t.append_file, 'missing/path', StringIO('content'))
563
# And we can create new files, too
565
t.append_file('c', StringIO('some text\nfor a missing file\n')))
566
self.check_transport_contents('some text\nfor a missing file\n',
569
def test_append_bytes(self):
570
t = self.get_transport()
573
self.assertRaises(TransportNotPossible,
574
t.append_bytes, 'a', 'add\nsome\nmore\ncontents\n')
577
self.assertEqual(0, t.append_bytes('a', 'diff\ncontents for\na\n'))
578
self.assertEqual(0, t.append_bytes('b', 'contents\nfor b\n'))
581
t.append_bytes('a', 'add\nsome\nmore\ncontents\n'))
583
self.check_transport_contents(
584
'diff\ncontents for\na\nadd\nsome\nmore\ncontents\n',
587
# a file with no parent should fail..
588
self.assertRaises(NoSuchFile,
589
t.append_bytes, 'missing/path', 'content')
591
def test_append_multi(self):
592
t = self.get_transport()
596
t.put_bytes('a', 'diff\ncontents for\na\n'
597
'add\nsome\nmore\ncontents\n')
598
t.put_bytes('b', 'contents\nfor b\n')
600
self.assertEqual((43, 15),
601
t.append_multi([('a', StringIO('and\nthen\nsome\nmore\n')),
602
('b', StringIO('some\nmore\nfor\nb\n'))]))
329
604
self.check_transport_contents(
330
605
'diff\ncontents for\na\n'
331
606
'add\nsome\nmore\ncontents\n'
372
637
'a little bit more\n'
373
638
'some text in a\n',
375
self.check_transport_contents('some text\nfor a missing file\n',
377
640
self.check_transport_contents('missing file r\n', t, 'd')
379
# a file with no parent should fail..
380
if not t.is_readonly():
381
self.assertRaises(NoSuchFile,
382
t.append, 'missing/path',
385
def test_append_file(self):
386
t = self.get_transport()
389
('f1', StringIO('this is a string\nand some more stuff\n')),
390
('f2', StringIO('here is some text\nand a bit more\n')),
391
('f3', StringIO('some text for the\nthird file created\n')),
392
('f4', StringIO('this is a string\nand some more stuff\n')),
393
('f5', StringIO('here is some text\nand a bit more\n')),
394
('f6', StringIO('some text for the\nthird file created\n'))
398
for f, val in contents:
399
open(f, 'wb').write(val.read())
401
t.put_multi(contents)
403
a1 = StringIO('appending to\none\n')
411
self.check_transport_contents(
412
'this is a string\nand some more stuff\n'
413
'appending to\none\n',
416
a2 = StringIO('adding more\ntext to two\n')
417
a3 = StringIO('some garbage\nto put in three\n')
423
t.append_multi([('f2', a2), ('f3', a3)])
427
self.check_transport_contents(
428
'here is some text\nand a bit more\n'
429
'adding more\ntext to two\n',
431
self.check_transport_contents(
432
'some text for the\nthird file created\n'
433
'some garbage\nto put in three\n',
436
# Test that an actual file object can be used with put
445
self.check_transport_contents(
446
'this is a string\nand some more stuff\n'
447
'this is a string\nand some more stuff\n'
448
'appending to\none\n',
457
t.append_multi([('f5', a5), ('f6', a6)])
461
self.check_transport_contents(
462
'here is some text\nand a bit more\n'
463
'here is some text\nand a bit more\n'
464
'adding more\ntext to two\n',
466
self.check_transport_contents(
467
'some text for the\nthird file created\n'
468
'some text for the\nthird file created\n'
469
'some garbage\nto put in three\n',
481
t.append_multi([('a', a6), ('d', a7)])
483
self.check_transport_contents(t.get('f2').read(), t, 'c')
484
self.check_transport_contents(t.get('f3').read(), t, 'd')
486
def test_append_mode(self):
642
def test_append_file_mode(self):
643
"""Check that append accepts a mode parameter"""
487
644
# check append accepts a mode
488
645
t = self.get_transport()
489
646
if t.is_readonly():
491
t.append('f', StringIO('f'), mode=None)
647
self.assertRaises(TransportNotPossible,
648
t.append_file, 'f', StringIO('f'), mode=None)
650
t.append_file('f', StringIO('f'), mode=None)
652
def test_append_bytes_mode(self):
653
# check append_bytes accepts a mode
654
t = self.get_transport()
656
self.assertRaises(TransportNotPossible,
657
t.append_bytes, 'f', 'f', mode=None)
659
t.append_bytes('f', 'f', mode=None)
493
661
def test_delete(self):
494
662
# TODO: Test Transport.delete