From d46a9bff0071e13201892dba6f3e6f9d16487f6c Mon Sep 17 00:00:00 2001 From: Cyril Roelandt Date: Mon, 22 Jul 2024 15:13:03 +0200 Subject: [PATCH] Python 3.13: fix tests Following a recent change in Python[1], mock_open now invokes close() when used as a context manager. This causes some of our tests to fail. This commit rewrites part of these tests to be less dependant on internal Python details. [1] https://github.com/python/cpython/pull/26902 Change-Id: Iad9c0f0bf7f34d5cf209ec10863b28ddde281d7e Signed-off-by: Cyril Roelandt --- glanceclient/tests/unit/test_shell.py | 28 +++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/glanceclient/tests/unit/test_shell.py b/glanceclient/tests/unit/test_shell.py index 4a123ab1..520c600d 100644 --- a/glanceclient/tests/unit/test_shell.py +++ b/glanceclient/tests/unit/test_shell.py @@ -786,15 +786,15 @@ class ShellCacheSchemaTest(testutils.TestCase): client = self.shell._get_versioned_client('2', args) self.shell._cache_schemas(args, client, home_dir=self.cache_dir) - self.assertEqual(12, open.mock_calls.__len__()) + self.assertEqual(len(self.cache_files), len(open.call_args_list)) self.assertEqual(mock.call(self.cache_files[0], 'w'), - open.mock_calls[0]) + open.call_args_list[0]) self.assertEqual(mock.call(self.cache_files[1], 'w'), - open.mock_calls[4]) - actual = json.loads(open.mock_calls[2][1][0]) - self.assertEqual(schema_odict, actual) - actual = json.loads(open.mock_calls[6][1][0]) - self.assertEqual(schema_odict, actual) + open.call_args_list[1]) + actual = json.loads(open().write.call_args_list[0][0][0]) + self.assertEqual(schema_odict, OrderedDict(actual)) + actual = json.loads(open().write.call_args_list[1][0][0]) + self.assertEqual(schema_odict, OrderedDict(actual)) @mock.patch('builtins.open', new=mock.mock_open(), create=True) @mock.patch('os.path.exists', side_effect=[True, False, False, False]) @@ -809,15 +809,15 @@ class ShellCacheSchemaTest(testutils.TestCase): client = self.shell._get_versioned_client('2', args) self.shell._cache_schemas(args, client, home_dir=self.cache_dir) - self.assertEqual(12, open.mock_calls.__len__()) + self.assertEqual(len(self.cache_files), len(open.call_args_list)) self.assertEqual(mock.call(self.cache_files[0], 'w'), - open.mock_calls[0]) + open.call_args_list[0]) self.assertEqual(mock.call(self.cache_files[1], 'w'), - open.mock_calls[4]) - actual = json.loads(open.mock_calls[2][1][0]) - self.assertEqual(schema_odict, actual) - actual = json.loads(open.mock_calls[6][1][0]) - self.assertEqual(schema_odict, actual) + open.call_args_list[1]) + actual = json.loads(open().write.call_args_list[0][0][0]) + self.assertEqual(schema_odict, OrderedDict(actual)) + actual = json.loads(open().write.call_args_list[1][0][0]) + self.assertEqual(schema_odict, OrderedDict(actual)) @mock.patch('builtins.open', new=mock.mock_open(), create=True) @mock.patch('os.path.exists', return_value=True)