Skip to content

Commit f47c011

Browse files
committed
speed up test case by mocking file size instead of creating an actual 128 ;iB file
1 parent 8a3f88e commit f47c011

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

Lib/test/test_shutil.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,18 +1939,32 @@ def test_file_offset(self):
19391939
def test_alternate_win_impl(self):
19401940
# On Windows copyfile() uses copyfileobj() for files < 128 MiB,
19411941
# 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)
19451951

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.
19491953
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)
19541968

19551969

19561970
class _ZeroCopyFileTest(object):

0 commit comments

Comments
 (0)