import unittest from unittest.mock import patch, mock_open import yaml from dependabro.utils import get_terraform_modules class TestGetTerraformModules(unittest.TestCase): @patch( "builtins.open", new_callable=mock_open, read_data="modules:\n - module1\n - module2\n", ) @patch( "dependabro.utils.yaml.safe_load", return_value={"modules": ["module1", "module2"]}, ) def test_returns_modules_from_valid_config(self, mock_yaml_load, mock_file): success, modules = get_terraform_modules("/path/to/config.yaml") self.assertTrue(success) self.assertEqual(modules, ["module1", "module2"]) @patch("builtins.open", new_callable=mock_open, read_data="modules: []\n") @patch("dependabro.utils.yaml.safe_load", return_value={"modules": []}) def test_handles_empty_modules_list(self, mock_yaml_load, mock_file): with self.assertLogs(level="WARNING") as log: success, modules = get_terraform_modules("/path/to/config.yaml") self.assertTrue(success) self.assertEqual(modules, []) self.assertIn("No modules found in the configuration file", log.output[0]) @patch("builtins.open", new_callable=mock_open) @patch("dependabro.utils.yaml.safe_load", side_effect=yaml.YAMLError) def test_handles_yaml_load_error(self, mock_yaml_load, mock_file): with self.assertLogs(level="ERROR") as log: success, modules = get_terraform_modules("/path/to/config.yaml") self.assertFalse(success) self.assertEqual(modules, []) self.assertIn("Error reading configuration file", log.output[0]) @patch("builtins.open", side_effect=IOError("File not found")) def test_handles_file_not_found_error(self, mock_file): with self.assertLogs(level="ERROR") as log: success, modules = get_terraform_modules("/path/to/config.yaml") self.assertFalse(success) self.assertEqual(modules, []) self.assertIn("Error reading configuration file", log.output[0]) if __name__ == "__main__": unittest.main()