@@ -1939,18 +1939,32 @@ def test_file_offset(self):
1939
1939
def test_alternate_win_impl (self ):
1940
1940
# On Windows copyfile() uses copyfileobj() for files < 128 MiB,
1941
1941
# else an alternate memoryview()-based implementation.
1942
- with unittest .mock .patch ("shutil._copybinfileobj" ) as m :
1943
- shutil .copyfile (TESTFN , TESTFN2 )
1944
- assert not m .called
1942
+ def os_stat_mocked (path ):
1943
+ # Make shutil believe src file is 128 MiB.
1944
+ if path == TESTFN :
1945
+ mock = unittest .mock .Mock ()
1946
+ mock .st_size = 128 * 1024 * 1024
1947
+ mock .st_mode = os .lstat (path ).st_mode
1948
+ return mock
1949
+ else :
1950
+ return os .lstat (path )
1945
1951
1946
- fname = TESTFN + '-win'
1947
- self .addCleanup (support .unlink , fname )
1948
- write_test_file (fname , 128 * 1024 * 1024 )
1952
+ # Make sure it's not called.
1949
1953
with unittest .mock .patch ("shutil._copybinfileobj" ) as m :
1950
- shutil .copyfile (fname , TESTFN2 )
1951
- assert m .called
1952
- shutil .copyfile (fname , TESTFN2 )
1953
- self .assert_files_eq (fname , TESTFN2 )
1954
+ shutil .copyfile (TESTFN , TESTFN2 )
1955
+ assert not m .called
1956
+ # Make sure it's called.
1957
+ with unittest .mock .patch ('shutil.os.stat' , create = True ,
1958
+ side_effect = os_stat_mocked ) as m1 :
1959
+ with unittest .mock .patch ("shutil._copybinfileobj" ) as m2 :
1960
+ shutil .copyfile (TESTFN , TESTFN2 )
1961
+ assert m1 .called
1962
+ assert m2 .called
1963
+ # Test it.
1964
+ with unittest .mock .patch ('shutil.os.stat' , create = True ,
1965
+ side_effect = os_stat_mocked ) as m1 :
1966
+ shutil .copyfile (TESTFN , TESTFN2 )
1967
+ self .assert_files_eq (TESTFN , TESTFN2 )
1954
1968
1955
1969
1956
1970
class _ZeroCopyFileTest (object ):
0 commit comments