|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import unittest |
| 4 | +from unittest.mock import patch, MagicMock, mock_open |
| 5 | + |
| 6 | +# Add the project root to sys.path so we can import the hook scripts |
| 7 | +script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 8 | +project_root = os.path.abspath(os.path.join(script_dir, "..")) |
| 9 | +hooks_dir = os.path.join(project_root, ".gemini/hooks") |
| 10 | +sys.path.append(hooks_dir) |
| 11 | + |
| 12 | +import custom_config # noqa: E402 |
| 13 | + |
| 14 | +class TestCustomConfig(unittest.TestCase): |
| 15 | + |
| 16 | + def test_get_version_success(self): |
| 17 | + with patch("subprocess.run") as mocked_run: |
| 18 | + mocked_run.return_value = MagicMock(stdout="2.1.0\n", check=True) |
| 19 | + version = custom_config.get_version("dummy_script.py") |
| 20 | + self.assertEqual(version, "2.1.0") |
| 21 | + mocked_run.assert_called_once() |
| 22 | + |
| 23 | + def test_get_version_failure(self): |
| 24 | + with patch("subprocess.run") as mocked_run: |
| 25 | + mocked_run.side_effect = Exception("failed") |
| 26 | + version = custom_config.get_version("dummy_script.py") |
| 27 | + self.assertEqual(version, "2.0.0") # Fallback |
| 28 | + |
| 29 | + def test_parse_ruby_config(self): |
| 30 | + content = """ |
| 31 | + c.developer_token = 'token123' |
| 32 | + c.client_id = "id456" |
| 33 | + c.client_secret = 'secret789' |
| 34 | + """ |
| 35 | + with patch("builtins.open", mock_open(read_data=content)): |
| 36 | + data = custom_config.parse_ruby_config("dummy.rb") |
| 37 | + self.assertEqual(data["developer_token"], "token123") |
| 38 | + self.assertEqual(data["client_id"], "id456") |
| 39 | + self.assertEqual(data["client_secret"], "secret789") |
| 40 | + |
| 41 | + def test_parse_ini_config(self): |
| 42 | + content = "[DEFAULT]\ndeveloper_token = token123\nclient_id = 'id456'\n" |
| 43 | + with patch("builtins.open", mock_open(read_data=content)): |
| 44 | + data = custom_config.parse_ini_config("dummy.ini") |
| 45 | + self.assertEqual(data["developer_token"], "token123") |
| 46 | + self.assertEqual(data["client_id"], "id456") |
| 47 | + |
| 48 | + def test_parse_properties_config(self): |
| 49 | + content = "api.googleads.developerToken=token123\napi.googleads.clientId=id456\n" |
| 50 | + with patch("builtins.open", mock_open(read_data=content)): |
| 51 | + data = custom_config.parse_properties_config("dummy.properties") |
| 52 | + self.assertEqual(data["developer_token"], "token123") |
| 53 | + self.assertEqual(data["client_id"], "id456") |
| 54 | + |
| 55 | + def test_write_yaml_config_oauth2(self): |
| 56 | + data = { |
| 57 | + "developer_token": "token123", |
| 58 | + "client_id": "id456", |
| 59 | + "client_secret": "secret789", |
| 60 | + "refresh_token": "refresh000" |
| 61 | + } |
| 62 | + with patch("builtins.open", mock_open()) as mocked_file: |
| 63 | + success = custom_config.write_yaml_config(data, "dummy.yaml", "2.1.0") |
| 64 | + self.assertTrue(success) |
| 65 | + handle = mocked_file() |
| 66 | + handle.write.assert_any_call("developer_token: token123\n") |
| 67 | + handle.write.assert_any_call("client_id: id456\n") |
| 68 | + handle.write.assert_any_call("gaada: \"2.1.0\"\n") |
| 69 | + |
| 70 | + def test_write_yaml_config_service_account(self): |
| 71 | + data = { |
| 72 | + "developer_token": "token123", |
| 73 | + "json_key_file_path": "/path/to/key.json", |
| 74 | + "impersonated_email": "user@example.com" |
| 75 | + } |
| 76 | + with patch("builtins.open", mock_open()) as mocked_file: |
| 77 | + success = custom_config.write_yaml_config(data, "dummy.yaml", "2.1.0") |
| 78 | + self.assertTrue(success) |
| 79 | + handle = mocked_file() |
| 80 | + handle.write.assert_any_call("json_key_file_path: /path/to/key.json\n") |
| 81 | + handle.write.assert_any_call("impersonated_email: user@example.com\n") |
| 82 | + # Verify client_id is NOT written |
| 83 | + for call in handle.write.call_args_list: |
| 84 | + self.assertNotIn("client_id:", call[0][0]) |
| 85 | + |
| 86 | + def test_configure_language(self): |
| 87 | + with patch("os.path.exists", return_value=True), \ |
| 88 | + patch("shutil.copy2") as mocked_copy, \ |
| 89 | + patch("builtins.open", mock_open()) as mocked_file: |
| 90 | + success = custom_config.configure_language("Python", "home.yaml", "target.yaml", "2.1.0", is_python=True) |
| 91 | + self.assertTrue(success) |
| 92 | + mocked_copy.assert_called_once_with("home.yaml", "target.yaml") |
| 93 | + handle = mocked_file() |
| 94 | + handle.write.assert_called_with('\ngaada: "2.1.0"\n') |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + unittest.main() |
0 commit comments