|
| 1 | +"""Tests for the telemetry opt-out flag. |
| 2 | +
|
| 3 | +These cover the environment variable path, which previously read its value from |
| 4 | +the config file instead of from the variable, so `SCRAPEGRAPHAI_TELEMETRY_ENABLED=false` |
| 5 | +left telemetry enabled. |
| 6 | +""" |
| 7 | + |
| 8 | +import configparser |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from scrapegraphai.telemetry.telemetry import ( |
| 13 | + _check_config_and_environ_for_telemetry_flag, |
| 14 | + _parse_bool, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +def _config(**defaults): |
| 19 | + cfg = configparser.ConfigParser() |
| 20 | + cfg["DEFAULT"] = {k: str(v) for k, v in defaults.items()} |
| 21 | + return cfg |
| 22 | + |
| 23 | + |
| 24 | +class TestParseBool: |
| 25 | + @pytest.mark.parametrize("value", ["false", "False", "FALSE", "no", "off", "0", " false "]) |
| 26 | + def test_falsey_spellings(self, value): |
| 27 | + assert _parse_bool(value) is False |
| 28 | + |
| 29 | + @pytest.mark.parametrize("value", ["true", "True", "yes", "on", "1"]) |
| 30 | + def test_truthy_spellings(self, value): |
| 31 | + assert _parse_bool(value) is True |
| 32 | + |
| 33 | + def test_rejects_nonsense(self): |
| 34 | + with pytest.raises(ValueError): |
| 35 | + _parse_bool("maybe") |
| 36 | + |
| 37 | + |
| 38 | +class TestTelemetryFlag: |
| 39 | + def test_defaults_to_the_given_default(self): |
| 40 | + assert _check_config_and_environ_for_telemetry_flag(True, _config()) is True |
| 41 | + |
| 42 | + def test_config_file_can_disable(self): |
| 43 | + cfg = _config(telemetry_enabled="False") |
| 44 | + assert _check_config_and_environ_for_telemetry_flag(True, cfg) is False |
| 45 | + |
| 46 | + def test_env_var_disables_with_no_config_key(self, monkeypatch): |
| 47 | + """The regression. Previously returned True, because the value was read |
| 48 | + out of the config file rather than out of the environment variable.""" |
| 49 | + monkeypatch.setenv("SCRAPEGRAPHAI_TELEMETRY_ENABLED", "false") |
| 50 | + assert _check_config_and_environ_for_telemetry_flag(True, _config()) is False |
| 51 | + |
| 52 | + def test_env_var_overrides_the_config_file(self, monkeypatch): |
| 53 | + monkeypatch.setenv("SCRAPEGRAPHAI_TELEMETRY_ENABLED", "false") |
| 54 | + cfg = _config(telemetry_enabled="True") |
| 55 | + assert _check_config_and_environ_for_telemetry_flag(True, cfg) is False |
| 56 | + |
| 57 | + def test_env_var_can_also_enable(self, monkeypatch): |
| 58 | + monkeypatch.setenv("SCRAPEGRAPHAI_TELEMETRY_ENABLED", "true") |
| 59 | + cfg = _config(telemetry_enabled="False") |
| 60 | + assert _check_config_and_environ_for_telemetry_flag(True, cfg) is True |
| 61 | + |
| 62 | + def test_unparseable_env_var_leaves_the_flag_alone(self, monkeypatch): |
| 63 | + monkeypatch.setenv("SCRAPEGRAPHAI_TELEMETRY_ENABLED", "banana") |
| 64 | + cfg = _config(telemetry_enabled="False") |
| 65 | + assert _check_config_and_environ_for_telemetry_flag(True, cfg) is False |
| 66 | + |
| 67 | + def test_unset_env_var_leaves_the_config_in_charge(self, monkeypatch): |
| 68 | + monkeypatch.delenv("SCRAPEGRAPHAI_TELEMETRY_ENABLED", raising=False) |
| 69 | + cfg = _config(telemetry_enabled="False") |
| 70 | + assert _check_config_and_environ_for_telemetry_flag(True, cfg) is False |
0 commit comments