@@ -263,8 +263,10 @@ def test_exclusive_create_binary_file(self):
263
263
self .os .mkdir (file_dir )
264
264
contents = b"Binary contents"
265
265
with self .open (file_path , "xb" ) as fake_file :
266
+ self .assertEqual ("xb" , fake_file .mode )
266
267
fake_file .write (contents )
267
268
with self .open (file_path , "rb" ) as fake_file :
269
+ self .assertEqual ("rb" , fake_file .mode )
268
270
self .assertEqual (contents , fake_file .read ())
269
271
270
272
def test_overwrite_existing_file (self ):
@@ -302,11 +304,27 @@ def test_open_with_wplus(self):
302
304
self .assertTrue (self .os .path .exists (file_path ))
303
305
with self .open (file_path , "r" , encoding = "utf8" ) as fake_file :
304
306
self .assertEqual ("old contents" , fake_file .read ())
307
+ self .assertEqual ("r" , fake_file .mode )
305
308
# actual tests
306
309
with self .open (file_path , "w+" , encoding = "utf8" ) as fake_file :
307
310
fake_file .write ("new contents" )
308
311
fake_file .seek (0 )
309
312
self .assertTrue ("new contents" , fake_file .read ())
313
+ self .assertEqual ("w+" , fake_file .mode )
314
+
315
+ def test_open_with_wplus_binary (self ):
316
+ file_path = self .make_path ("wplus_file_b" )
317
+ self .create_file (file_path , contents = b"old contents" )
318
+ self .assertTrue (self .os .path .exists (file_path ))
319
+ with self .open (file_path , "rb" ) as fake_file :
320
+ self .assertEqual (b"old contents" , fake_file .read ())
321
+ self .assertEqual ("rb" , fake_file .mode )
322
+ # actual tests
323
+ with self .open (file_path , "wb+" ) as fake_file :
324
+ fake_file .write (b"new contents" )
325
+ fake_file .seek (0 )
326
+ self .assertTrue (b"new contents" , fake_file .read ())
327
+ self .assertEqual ("rb+" , fake_file .mode )
310
328
311
329
def test_open_with_wplus_truncation (self ):
312
330
# set up
@@ -319,6 +337,7 @@ def test_open_with_wplus_truncation(self):
319
337
with self .open (file_path , "w+" , encoding = "utf8" ) as fake_file :
320
338
fake_file .seek (0 )
321
339
self .assertEqual ("" , fake_file .read ())
340
+ self .assertEqual ("w+" , fake_file .mode )
322
341
323
342
def test_open_with_append_flag (self ):
324
343
contents = [
@@ -335,6 +354,7 @@ def test_open_with_append_flag(self):
335
354
fake_file .read (0 )
336
355
with self .assertRaises (io .UnsupportedOperation ):
337
356
fake_file .readline ()
357
+ self .assertEqual ("a" , fake_file .mode )
338
358
expected_len = len ("" .join (contents ))
339
359
expected_len += len (contents ) * (len (self .os .linesep ) - 1 )
340
360
self .assertEqual (expected_len , fake_file .tell ())
@@ -344,6 +364,22 @@ def test_open_with_append_flag(self):
344
364
with self .open (file_path , encoding = "utf8" ) as fake_file :
345
365
self .assertEqual (contents + additional_contents , fake_file .readlines ())
346
366
367
+ def test_open_with_append_flag_binary (self ):
368
+ contents = b"Just some boring stuff... "
369
+ additional_contents = b"some excitement added"
370
+ file_path = self .make_path ("append-binary" )
371
+ self .create_file (file_path , contents = contents )
372
+ with self .open (file_path , "ab" ) as fake_file :
373
+ with self .assertRaises (io .UnsupportedOperation ):
374
+ fake_file .read (0 )
375
+ self .assertEqual ("ab" , fake_file .mode )
376
+ self .assertEqual (len (contents ), fake_file .tell ())
377
+ fake_file .seek (0 )
378
+ self .assertEqual (0 , fake_file .tell ())
379
+ fake_file .write (additional_contents )
380
+ with self .open (file_path , "rb" ) as fake_file :
381
+ self .assertEqual (contents + additional_contents , fake_file .read ())
382
+
347
383
def check_append_with_aplus (self ):
348
384
file_path = self .make_path ("aplus_file" )
349
385
self .create_file (file_path , contents = "old contents" )
@@ -357,6 +393,7 @@ def check_append_with_aplus(self):
357
393
self .filesystem , delete_on_close = True
358
394
)
359
395
with self .open (file_path , "a+" , encoding = "utf8" ) as fake_file :
396
+ self .assertEqual ("a+" , fake_file .mode )
360
397
self .assertEqual (12 , fake_file .tell ())
361
398
fake_file .write ("new contents" )
362
399
self .assertEqual (24 , fake_file .tell ())
@@ -391,6 +428,12 @@ def test_read_empty_file_with_aplus(self):
391
428
with self .open (file_path , "a+" , encoding = "utf8" ) as fake_file :
392
429
self .assertEqual ("" , fake_file .read ())
393
430
431
+ def test_read_empty_file_with_aplus_binary (self ):
432
+ file_path = self .make_path ("aplus_file" )
433
+ with self .open (file_path , "ab+" ) as fake_file :
434
+ self .assertEqual (b"" , fake_file .read ())
435
+ self .assertEqual ("ab+" , fake_file .mode )
436
+
394
437
def test_read_with_rplus (self ):
395
438
# set up
396
439
file_path = self .make_path ("rplus_file" )
@@ -401,11 +444,27 @@ def test_read_with_rplus(self):
401
444
# actual tests
402
445
with self .open (file_path , "r+" , encoding = "utf8" ) as fake_file :
403
446
self .assertEqual ("old contents here" , fake_file .read ())
447
+ self .assertEqual ("r+" , fake_file .mode )
404
448
fake_file .seek (0 )
405
449
fake_file .write ("new contents" )
406
450
fake_file .seek (0 )
407
451
self .assertEqual ("new contents here" , fake_file .read ())
408
452
453
+ def test_read_with_rplus_binary (self ):
454
+ file_path = self .make_path ("rplus_binary" )
455
+ self .create_file (file_path , contents = b"old contents here" )
456
+ self .assertTrue (self .os .path .exists (file_path ))
457
+ with self .open (file_path , "rb" ) as fake_file :
458
+ self .assertEqual (b"old contents here" , fake_file .read ())
459
+
460
+ with self .open (file_path , "rb+" ) as fake_file :
461
+ self .assertEqual (b"old contents here" , fake_file .read ())
462
+ self .assertEqual ("rb+" , fake_file .mode )
463
+ fake_file .seek (0 )
464
+ fake_file .write (b"new contents" )
465
+ fake_file .seek (0 )
466
+ self .assertEqual (b"new contents here" , fake_file .read ())
467
+
409
468
def create_with_permission (self , file_path , perm_bits ):
410
469
self .create_file (file_path )
411
470
self .os .chmod (file_path , perm_bits )
@@ -900,6 +959,7 @@ def test_closing_file_with_different_close_mode(self):
900
959
file_obj = self .filesystem .get_object (filename )
901
960
with self .open (fd , "wb" , closefd = False ) as fp :
902
961
fp .write (b"test" )
962
+ self .assertEqual ("wb" , fp .mode )
903
963
self .assertTrue (self .filesystem .has_open_file (file_obj ))
904
964
self .os .close (fd )
905
965
self .assertFalse (self .filesystem .has_open_file (file_obj ))
@@ -1028,6 +1088,7 @@ def test_use_opener_with_exclusive_write(self):
1028
1088
1029
1089
file_path = self .make_path ("bar" )
1030
1090
with self .open (file_path , "x" , encoding = "utf8" , opener = self .opener ) as f :
1091
+ self .assertEqual ("x" , f .mode )
1031
1092
assert f .write ("bar" ) == 3
1032
1093
with self .assertRaises (OSError ):
1033
1094
f .read ()
@@ -1042,6 +1103,7 @@ def test_use_opener_with_exclusive_plus(self):
1042
1103
1043
1104
file_path = self .make_path ("bar" )
1044
1105
with self .open (file_path , "x+" , encoding = "utf8" , opener = self .opener ) as f :
1106
+ self .assertEqual ("x+" , f .mode )
1045
1107
assert f .write ("bar" ) == 3
1046
1108
assert f .read () == ""
1047
1109
with self .open (file_path , encoding = "utf8" ) as f :
0 commit comments