Skip to content

Commit e13801b

Browse files
mnasiadkakeuko
authored andcommitted
Fix mock_open() expectations for Python 3.13+
Python 3.13 introduces a behavior change in unittest.mock.mock_open(), which now calls close() when exiting the context manager as described in bpo-44185 [1]. This causes additional mock calls to appear, breaking strict mock_calls assertions. This patch fix tests and add compatibility for Python 3.13. From mock documentation [2]: "bpo-44185: unittest.mock.mock_open() will call the close() method of the file handle mock when it is exiting from the context manager. Patch by Samet Yaslan." [1] https://bugs.python.org/issue44185 [2] https://mock.readthedocs.io/en/latest/changelog.html Change-Id: Ib8e2a8a20a828c4c69deedd307c9b726f51489f8
1 parent 0006320 commit e13801b

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

tests/test_set_config.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,32 @@ def test_load_ok(self):
4848
with mock.patch.object(set_configs, 'open', mo):
4949
config = set_configs.load_config()
5050
set_configs.copy_config(config)
51-
self.assertEqual([
52-
mock.call('/var/lib/kolla/config_files/config.json'),
53-
mock.call().__enter__(),
54-
mock.call().read(),
55-
mock.call().__exit__(None, None, None),
56-
mock.call('/run_command', 'w+'),
57-
mock.call().__enter__(),
58-
mock.call().write('/bin/true'),
59-
mock.call().__exit__(None, None, None)], mo.mock_calls)
51+
if sys.version_info >= (3, 13):
52+
calls = [
53+
mock.call('/var/lib/kolla/config_files/config.json'),
54+
mock.call().__enter__(),
55+
mock.call().read(),
56+
mock.call().__exit__(None, None, None),
57+
mock.call().close(),
58+
mock.call('/run_command', 'w+'),
59+
mock.call().__enter__(),
60+
mock.call().write('/bin/true'),
61+
mock.call().__exit__(None, None, None),
62+
mock.call().close()
63+
]
64+
else:
65+
calls = [
66+
mock.call('/var/lib/kolla/config_files/config.json'),
67+
mock.call().__enter__(),
68+
mock.call().read(),
69+
mock.call().__exit__(None, None, None),
70+
mock.call('/run_command', 'w+'),
71+
mock.call().__enter__(),
72+
mock.call().write('/bin/true'),
73+
mock.call().__exit__(None, None, None)
74+
]
75+
76+
self.assertEqual(calls, mo.mock_calls)
6077

6178

6279
FAKE_CONFIG_FILES = [

0 commit comments

Comments
 (0)