analytics: add DVC_ANALYTICS env var (#6448)

This commit is contained in:
Ruslan Kuprieiev
2021-08-17 01:48:38 +03:00
committed by GitHub
parent 451bd3ebc8
commit c2bf0dc713
3 changed files with 43 additions and 3 deletions
+8 -3
View File
@@ -2,6 +2,8 @@ import json
import logging
import os
from .env import DVC_ANALYTICS
logger = logging.getLogger(__name__)
@@ -42,9 +44,12 @@ def is_enabled():
if env2bool("DVC_TEST"):
return False
enabled = to_bool(
Config(validate=False).get("core", {}).get("analytics", "true")
)
enabled = env2bool(DVC_ANALYTICS, None)
if enabled is None:
enabled = to_bool(
Config(validate=False).get("core", {}).get("analytics", "true")
)
logger.debug("Analytics is {}abled.".format("en" if enabled else "dis"))
return enabled
+1
View File
@@ -9,3 +9,4 @@ DVCLIVE_RESUME = "DVCLIVE_RESUME"
DVC_IGNORE_ISATTY = "DVC_IGNORE_ISATTY"
DVC_EXP_GIT_REMOTE = "DVC_EXP_GIT_REMOTE"
DVC_EXP_AUTO_PUSH = "DVC_EXP_AUTO_PUSH"
DVC_ANALYTICS = "DVC_ANALYTICS"
+34
View File
@@ -87,6 +87,40 @@ def test_is_enabled(dvc, config, result, monkeypatch, tmp_global_dir):
# reset DVC_TEST env var, which affects `is_enabled()`
monkeypatch.delenv("DVC_TEST")
monkeypatch.delenv("DVC_ANALYTICS", raising=False)
assert result == analytics.is_enabled()
@pytest.mark.parametrize(
"config, env, result",
[
(None, None, True),
(None, "true", True),
(None, "false", False),
("false", None, False),
("false", "true", True),
("false", "false", False),
("true", None, True),
("true", "true", True),
("true", "false", False),
],
)
def test_is_enabled_env(dvc, config, env, result, monkeypatch, tmp_global_dir):
# reset DVC_TEST env var, which affects `is_enabled()`
monkeypatch.delenv("DVC_TEST")
monkeypatch.delenv("DVC_ANALYTICS", raising=False)
with dvc.config.edit() as conf:
conf["core"] = {}
assert analytics.is_enabled()
if config is not None:
with dvc.config.edit() as conf:
conf["core"] = {"analytics": config}
if env is not None:
monkeypatch.setenv("DVC_ANALYTICS", env)
assert result == analytics.is_enabled()