|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for |
| 4 | +# license information. |
| 5 | +# ----------------------------------------------------------------------------- |
| 6 | + |
| 7 | +import os |
| 8 | +import tempfile |
| 9 | +import unittest |
| 10 | +from unittest.mock import patch |
| 11 | + |
| 12 | +from azdev.operations.extensions import _invalidate_command_index, _COMMAND_INDEX_FILES |
| 13 | + |
| 14 | + |
| 15 | +class TestInvalidateCommandIndex(unittest.TestCase): |
| 16 | + |
| 17 | + def test_deletes_all_index_files(self): |
| 18 | + """All four command index files should be deleted when they exist.""" |
| 19 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 20 | + for filename in _COMMAND_INDEX_FILES: |
| 21 | + filepath = os.path.join(tmpdir, filename) |
| 22 | + with open(filepath, 'w') as f: |
| 23 | + f.write('{}') |
| 24 | + |
| 25 | + with patch('azdev.operations.extensions.get_azure_config_dir', return_value=tmpdir): |
| 26 | + _invalidate_command_index() |
| 27 | + |
| 28 | + for filename in _COMMAND_INDEX_FILES: |
| 29 | + self.assertFalse(os.path.exists(os.path.join(tmpdir, filename)), |
| 30 | + f'{filename} should have been deleted') |
| 31 | + |
| 32 | + def test_handles_missing_files_gracefully(self): |
| 33 | + """Should not raise when index files do not exist.""" |
| 34 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 35 | + with patch('azdev.operations.extensions.get_azure_config_dir', return_value=tmpdir): |
| 36 | + _invalidate_command_index() # should not raise |
| 37 | + |
| 38 | + def test_handles_partial_files(self): |
| 39 | + """Should delete existing files and skip missing ones without error.""" |
| 40 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 41 | + existing = _COMMAND_INDEX_FILES[0] |
| 42 | + with open(os.path.join(tmpdir, existing), 'w') as f: |
| 43 | + f.write('{}') |
| 44 | + |
| 45 | + with patch('azdev.operations.extensions.get_azure_config_dir', return_value=tmpdir): |
| 46 | + _invalidate_command_index() |
| 47 | + |
| 48 | + self.assertFalse(os.path.exists(os.path.join(tmpdir, existing))) |
| 49 | + |
| 50 | + @patch('azdev.operations.extensions._invalidate_command_index') |
| 51 | + @patch('azdev.operations.extensions.pip_cmd') |
| 52 | + @patch('azdev.operations.extensions.find_files', return_value=['/repo/src/my-ext/setup.py']) |
| 53 | + @patch('azdev.operations.extensions.get_ext_repo_paths', return_value=['/repo']) |
| 54 | + def test_add_extension_calls_invalidate(self, _mock_paths, _mock_find, mock_pip, mock_invalidate): |
| 55 | + """add_extension should call _invalidate_command_index after installing.""" |
| 56 | + from unittest.mock import MagicMock |
| 57 | + mock_pip.return_value = MagicMock(error=None) |
| 58 | + |
| 59 | + from azdev.operations.extensions import add_extension |
| 60 | + add_extension(['my-ext']) |
| 61 | + |
| 62 | + mock_invalidate.assert_called_once() |
| 63 | + |
| 64 | + @patch('azdev.operations.extensions._invalidate_command_index') |
| 65 | + @patch('azdev.operations.extensions.pip_cmd') |
| 66 | + @patch('azdev.operations.extensions.display') |
| 67 | + @patch('azdev.operations.extensions.find_files', return_value=['/repo/src/my-ext/my_ext.egg-info']) |
| 68 | + @patch('azdev.operations.extensions.get_ext_repo_paths', return_value=['/repo']) |
| 69 | + def test_remove_extension_calls_invalidate( |
| 70 | + self, _mock_paths, _mock_find, _mock_display, |
| 71 | + _mock_pip, mock_invalidate): |
| 72 | + """remove_extension should call _invalidate_command_index after removing.""" |
| 73 | + with patch('os.listdir', return_value=[]): |
| 74 | + from azdev.operations.extensions import remove_extension |
| 75 | + remove_extension(['my-ext']) |
| 76 | + |
| 77 | + mock_invalidate.assert_called_once() |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + unittest.main() |
0 commit comments