@@ -949,6 +949,98 @@ def use_real_fs(self):
949949 return True
950950
951951
952+ class FakeFileOpenWithOpenerTest (FakeFileOpenTestBase ):
953+ def opener (self , path , flags ):
954+ return self .os .open (path , flags )
955+
956+ def test_use_opener_with_read (self ):
957+ file_path = self .make_path ('foo' )
958+ self .create_file (file_path , contents = 'test' )
959+ with self .open (file_path , opener = self .opener ) as f :
960+ assert f .read () == 'test'
961+ with self .assertRaises (OSError ):
962+ f .write ('foo' )
963+
964+ def test_use_opener_with_read_plus (self ):
965+ file_path = self .make_path ('foo' )
966+ self .create_file (file_path , contents = 'test' )
967+ with self .open (file_path , 'r+' , opener = self .opener ) as f :
968+ assert f .read () == 'test'
969+ assert f .write ('bar' ) == 3
970+ with self .open (file_path ) as f :
971+ assert f .read () == 'testbar'
972+
973+ def test_use_opener_with_write (self ):
974+ file_path = self .make_path ('foo' )
975+ self .create_file (file_path , contents = 'foo' )
976+ with self .open (file_path , 'w' , opener = self .opener ) as f :
977+ with self .assertRaises (OSError ):
978+ f .read ()
979+ assert f .write ('bar' ) == 3
980+ with self .open (file_path ) as f :
981+ assert f .read () == 'bar'
982+
983+ def test_use_opener_with_write_plus (self ):
984+ file_path = self .make_path ('foo' )
985+ self .create_file (file_path , contents = 'test' )
986+ with self .open (file_path , 'w+' , opener = self .opener ) as f :
987+ assert f .read () == ''
988+ assert f .write ('bar' ) == 3
989+ with self .open (file_path ) as f :
990+ assert f .read () == 'bar'
991+
992+ def test_use_opener_with_append (self ):
993+ file_path = self .make_path ('foo' )
994+ self .create_file (file_path , contents = 'foo' )
995+ with self .open (file_path , 'a' , opener = self .opener ) as f :
996+ assert f .write ('bar' ) == 3
997+ with self .assertRaises (OSError ):
998+ f .read ()
999+ with self .open (file_path ) as f :
1000+ assert f .read () == 'foobar'
1001+
1002+ def test_use_opener_with_append_plus (self ):
1003+ file_path = self .make_path ('foo' )
1004+ self .create_file (file_path , contents = 'foo' )
1005+ with self .open (file_path , 'a+' , opener = self .opener ) as f :
1006+ assert f .read () == ''
1007+ assert f .write ('bar' ) == 3
1008+ with self .open (file_path ) as f :
1009+ assert f .read () == 'foobar'
1010+
1011+ def test_use_opener_with_exclusive_write (self ):
1012+ file_path = self .make_path ('foo' )
1013+ self .create_file (file_path , contents = 'test' )
1014+ with self .assertRaises (OSError ):
1015+ self .open (file_path , 'x' , opener = self .opener )
1016+
1017+ file_path = self .make_path ('bar' )
1018+ with self .open (file_path , 'x' , opener = self .opener ) as f :
1019+ assert f .write ('bar' ) == 3
1020+ with self .assertRaises (OSError ):
1021+ f .read ()
1022+ with self .open (file_path ) as f :
1023+ assert f .read () == 'bar'
1024+
1025+ def test_use_opener_with_exclusive_plus (self ):
1026+ file_path = self .make_path ('foo' )
1027+ self .create_file (file_path , contents = 'test' )
1028+ with self .assertRaises (OSError ):
1029+ self .open (file_path , 'x+' , opener = self .opener )
1030+
1031+ file_path = self .make_path ('bar' )
1032+ with self .open (file_path , 'x+' , opener = self .opener ) as f :
1033+ assert f .write ('bar' ) == 3
1034+ assert f .read () == ''
1035+ with self .open (file_path ) as f :
1036+ assert f .read () == 'bar'
1037+
1038+
1039+ class RealFileOpenWithOpenerTest (FakeFileOpenWithOpenerTest ):
1040+ def use_real_fs (self ):
1041+ return True
1042+
1043+
9521044@unittest .skipIf (sys .version_info < (3 , 8 ),
9531045 'open_code only present since Python 3.8' )
9541046class FakeFilePatchedOpenCodeTest (FakeFileOpenTestBase ):
0 commit comments