|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for telemetry_config.py.""" |
| 16 | + |
| 17 | +import json |
| 18 | +import pathlib |
| 19 | +import unittest.mock |
| 20 | + |
| 21 | +from google.adk.utils import _telemetry_config as telemetry_config |
| 22 | +import pytest |
| 23 | + |
| 24 | + |
| 25 | +def test_get_user_config_path(): |
| 26 | + path = telemetry_config.get_user_config_path() |
| 27 | + assert isinstance(path, pathlib.Path) |
| 28 | + assert path.name == "config.json" |
| 29 | + assert path.parent.name == ".adk" |
| 30 | + |
| 31 | + |
| 32 | +def test_read_telemetry_consent_not_exists(tmp_path): |
| 33 | + config_file = tmp_path / "config.json" |
| 34 | + with unittest.mock.patch.object( |
| 35 | + telemetry_config, "get_user_config_path", return_value=config_file |
| 36 | + ): |
| 37 | + assert telemetry_config.read_telemetry_consent() is None |
| 38 | + |
| 39 | + |
| 40 | +def test_read_telemetry_consent_exists_true(tmp_path): |
| 41 | + config_file = tmp_path / "config.json" |
| 42 | + config_file.parent.mkdir(parents=True, exist_ok=True) |
| 43 | + with open(config_file, "w", encoding="utf-8") as f: |
| 44 | + json.dump({"telemetry": True}, f) |
| 45 | + |
| 46 | + with unittest.mock.patch.object( |
| 47 | + telemetry_config, "get_user_config_path", return_value=config_file |
| 48 | + ): |
| 49 | + assert telemetry_config.read_telemetry_consent() is True |
| 50 | + |
| 51 | + |
| 52 | +def test_read_telemetry_consent_exists_false(tmp_path): |
| 53 | + config_file = tmp_path / "config.json" |
| 54 | + config_file.parent.mkdir(parents=True, exist_ok=True) |
| 55 | + with open(config_file, "w", encoding="utf-8") as f: |
| 56 | + json.dump({"telemetry": False}, f) |
| 57 | + |
| 58 | + with unittest.mock.patch.object( |
| 59 | + telemetry_config, "get_user_config_path", return_value=config_file |
| 60 | + ): |
| 61 | + assert telemetry_config.read_telemetry_consent() is False |
| 62 | + |
| 63 | + |
| 64 | +def test_read_telemetry_consent_invalid_json(tmp_path): |
| 65 | + config_file = tmp_path / "config.json" |
| 66 | + config_file.parent.mkdir(parents=True, exist_ok=True) |
| 67 | + with open(config_file, "w", encoding="utf-8") as f: |
| 68 | + f.write("not raw json data") |
| 69 | + |
| 70 | + with unittest.mock.patch.object( |
| 71 | + telemetry_config, "get_user_config_path", return_value=config_file |
| 72 | + ): |
| 73 | + assert telemetry_config.read_telemetry_consent() is None |
| 74 | + |
| 75 | + |
| 76 | +def test_write_telemetry_consent(tmp_path): |
| 77 | + config_file = tmp_path / "config.json" |
| 78 | + with unittest.mock.patch.object( |
| 79 | + telemetry_config, "get_user_config_path", return_value=config_file |
| 80 | + ): |
| 81 | + telemetry_config.write_telemetry_consent(True) |
| 82 | + assert telemetry_config.read_telemetry_consent() is True |
| 83 | + |
| 84 | + # Overwrite |
| 85 | + telemetry_config.write_telemetry_consent(False) |
| 86 | + assert telemetry_config.read_telemetry_consent() is False |
| 87 | + |
| 88 | + with open(config_file, "r", encoding="utf-8") as f: |
| 89 | + data = json.load(f) |
| 90 | + assert data == {"telemetry": False} |
| 91 | + |
| 92 | + |
| 93 | +def test_write_telemetry_consent_raises_on_error(tmp_path): |
| 94 | + config_file = tmp_path / "config.json" |
| 95 | + with unittest.mock.patch.object( |
| 96 | + telemetry_config, "get_user_config_path", return_value=config_file |
| 97 | + ): |
| 98 | + with unittest.mock.patch.object( |
| 99 | + pathlib.Path, "mkdir", side_effect=OSError("Permission denied") |
| 100 | + ): |
| 101 | + with pytest.raises(OSError): |
| 102 | + telemetry_config.write_telemetry_consent(True) |
0 commit comments