From e13801b4a1e5f587ee0581168eea0d42fea6cb66 Mon Sep 17 00:00:00 2001 From: Michal Nasiadka Date: Tue, 20 May 2025 12:17:25 +0200 Subject: 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 --- tests/test_set_config.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'tests') diff --git a/tests/test_set_config.py b/tests/test_set_config.py index 3b528e445..3d4fc5a45 100644 --- a/tests/test_set_config.py +++ b/tests/test_set_config.py @@ -48,15 +48,32 @@ class LoadFromFile(base.BaseTestCase): with mock.patch.object(set_configs, 'open', mo): config = set_configs.load_config() set_configs.copy_config(config) - self.assertEqual([ - mock.call('/var/lib/kolla/config_files/config.json'), - mock.call().__enter__(), - mock.call().read(), - mock.call().__exit__(None, None, None), - mock.call('/run_command', 'w+'), - mock.call().__enter__(), - mock.call().write('/bin/true'), - mock.call().__exit__(None, None, None)], mo.mock_calls) + if sys.version_info >= (3, 13): + calls = [ + mock.call('/var/lib/kolla/config_files/config.json'), + mock.call().__enter__(), + mock.call().read(), + mock.call().__exit__(None, None, None), + mock.call().close(), + mock.call('/run_command', 'w+'), + mock.call().__enter__(), + mock.call().write('/bin/true'), + mock.call().__exit__(None, None, None), + mock.call().close() + ] + else: + calls = [ + mock.call('/var/lib/kolla/config_files/config.json'), + mock.call().__enter__(), + mock.call().read(), + mock.call().__exit__(None, None, None), + mock.call('/run_command', 'w+'), + mock.call().__enter__(), + mock.call().write('/bin/true'), + mock.call().__exit__(None, None, None) + ] + + self.assertEqual(calls, mo.mock_calls) FAKE_CONFIG_FILES = [ -- cgit v1.2.3