import unittest from unittest.mock import mock_open, patch from dependabro.utils import load_module_versions_cache class TestLoadModuleVersionsCache(unittest.TestCase): @patch("os.path.exists", return_value=True) @patch("builtins.open", new_callable=mock_open, read_data='{"module1": "v1.0.0"}') def test_returns_cached_module_versions(self, mock_open, mock_exists): success, module_versions = load_module_versions_cache( "/path/to/cache_file.json" ) self.assertTrue(success) self.assertEqual(module_versions, {"module1": "v1.0.0"}) @patch("os.path.exists", return_value=True) @patch("builtins.open", new_callable=mock_open, read_data="invalid json") def test_handles_corrupted_cache_file(self, mock_open, mock_exists): with self.assertLogs(level="WARNING") as log: success, module_versions = load_module_versions_cache( "/path/to/cache_file.json" ) self.assertFalse(success) self.assertEqual(module_versions, {}) self.assertIn( "Cache file is corrupted. It needs to be refreshed.", log.output[0] ) @patch("os.path.exists", return_value=False) def test_handles_missing_cache_file(self, mock_exists): success, module_versions = load_module_versions_cache( "/path/to/cache_file.json" ) self.assertFalse(success) self.assertEqual(module_versions, {}) if __name__ == "__main__": unittest.main()