@@ -263,8 +263,10 @@ def test_exclusive_create_binary_file(self):
263263 self .os .mkdir (file_dir )
264264 contents = b"Binary contents"
265265 with self .open (file_path , "xb" ) as fake_file :
266+ self .assertEqual ("xb" , fake_file .mode )
266267 fake_file .write (contents )
267268 with self .open (file_path , "rb" ) as fake_file :
269+ self .assertEqual ("rb" , fake_file .mode )
268270 self .assertEqual (contents , fake_file .read ())
269271
270272 def test_overwrite_existing_file (self ):
@@ -302,11 +304,27 @@ def test_open_with_wplus(self):
302304 self .assertTrue (self .os .path .exists (file_path ))
303305 with self .open (file_path , "r" , encoding = "utf8" ) as fake_file :
304306 self .assertEqual ("old contents" , fake_file .read ())
307+ self .assertEqual ("r" , fake_file .mode )
305308 # actual tests
306309 with self .open (file_path , "w+" , encoding = "utf8" ) as fake_file :
307310 fake_file .write ("new contents" )
308311 fake_file .seek (0 )
309312 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 )
310328
311329 def test_open_with_wplus_truncation (self ):
312330 # set up
@@ -319,6 +337,7 @@ def test_open_with_wplus_truncation(self):
319337 with self .open (file_path , "w+" , encoding = "utf8" ) as fake_file :
320338 fake_file .seek (0 )
321339 self .assertEqual ("" , fake_file .read ())
340+ self .assertEqual ("w+" , fake_file .mode )
322341
323342 def test_open_with_append_flag (self ):
324343 contents = [
@@ -335,6 +354,7 @@ def test_open_with_append_flag(self):
335354 fake_file .read (0 )
336355 with self .assertRaises (io .UnsupportedOperation ):
337356 fake_file .readline ()
357+ self .assertEqual ("a" , fake_file .mode )
338358 expected_len = len ("" .join (contents ))
339359 expected_len += len (contents ) * (len (self .os .linesep ) - 1 )
340360 self .assertEqual (expected_len , fake_file .tell ())
@@ -344,6 +364,22 @@ def test_open_with_append_flag(self):
344364 with self .open (file_path , encoding = "utf8" ) as fake_file :
345365 self .assertEqual (contents + additional_contents , fake_file .readlines ())
346366
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+
347383 def check_append_with_aplus (self ):
348384 file_path = self .make_path ("aplus_file" )
349385 self .create_file (file_path , contents = "old contents" )
@@ -357,6 +393,7 @@ def check_append_with_aplus(self):
357393 self .filesystem , delete_on_close = True
358394 )
359395 with self .open (file_path , "a+" , encoding = "utf8" ) as fake_file :
396+ self .assertEqual ("a+" , fake_file .mode )
360397 self .assertEqual (12 , fake_file .tell ())
361398 fake_file .write ("new contents" )
362399 self .assertEqual (24 , fake_file .tell ())
@@ -391,6 +428,12 @@ def test_read_empty_file_with_aplus(self):
391428 with self .open (file_path , "a+" , encoding = "utf8" ) as fake_file :
392429 self .assertEqual ("" , fake_file .read ())
393430
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+
394437 def test_read_with_rplus (self ):
395438 # set up
396439 file_path = self .make_path ("rplus_file" )
@@ -401,11 +444,27 @@ def test_read_with_rplus(self):
401444 # actual tests
402445 with self .open (file_path , "r+" , encoding = "utf8" ) as fake_file :
403446 self .assertEqual ("old contents here" , fake_file .read ())
447+ self .assertEqual ("r+" , fake_file .mode )
404448 fake_file .seek (0 )
405449 fake_file .write ("new contents" )
406450 fake_file .seek (0 )
407451 self .assertEqual ("new contents here" , fake_file .read ())
408452
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+
409468 def create_with_permission (self , file_path , perm_bits ):
410469 self .create_file (file_path )
411470 self .os .chmod (file_path , perm_bits )
@@ -900,6 +959,7 @@ def test_closing_file_with_different_close_mode(self):
900959 file_obj = self .filesystem .get_object (filename )
901960 with self .open (fd , "wb" , closefd = False ) as fp :
902961 fp .write (b"test" )
962+ self .assertEqual ("wb" , fp .mode )
903963 self .assertTrue (self .filesystem .has_open_file (file_obj ))
904964 self .os .close (fd )
905965 self .assertFalse (self .filesystem .has_open_file (file_obj ))
@@ -1028,6 +1088,7 @@ def test_use_opener_with_exclusive_write(self):
10281088
10291089 file_path = self .make_path ("bar" )
10301090 with self .open (file_path , "x" , encoding = "utf8" , opener = self .opener ) as f :
1091+ self .assertEqual ("x" , f .mode )
10311092 assert f .write ("bar" ) == 3
10321093 with self .assertRaises (OSError ):
10331094 f .read ()
@@ -1042,6 +1103,7 @@ def test_use_opener_with_exclusive_plus(self):
10421103
10431104 file_path = self .make_path ("bar" )
10441105 with self .open (file_path , "x+" , encoding = "utf8" , opener = self .opener ) as f :
1106+ self .assertEqual ("x+" , f .mode )
10451107 assert f .write ("bar" ) == 3
10461108 assert f .read () == ""
10471109 with self .open (file_path , encoding = "utf8" ) as f :
0 commit comments