Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 3 additions & 29 deletions dvc/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,28 +204,9 @@ def dump(self):
json.dump(self.info, fobj)
return fobj.name

@staticmethod
def _is_enabled_config(config):
from dvc.config import Config

core = config.config.get(Config.SECTION_CORE, {})
return core.get(Config.SECTION_CORE_ANALYTICS, True)

@staticmethod
def _get_current_config():
from dvc.config import Config
from dvc.repo import Repo
from dvc.exceptions import NotDvcRepoError

try:
dvc_dir = Repo.find_dvc_dir()
config = Config(dvc_dir)
except NotDvcRepoError:
config = Config(validate=False)
return config

@staticmethod
def is_enabled(cmd=None):
from dvc.config import Config, to_bool
from dvc.command.daemon import CmdDaemonBase

if env2bool("DVC_TEST"):
Expand All @@ -234,15 +215,8 @@ def is_enabled(cmd=None):
if isinstance(cmd, CmdDaemonBase):
return False

config = (
Analytics._get_current_config()
if cmd is None or not hasattr(cmd, "config")
else cmd.config
)

assert config is not None

enabled = Analytics._is_enabled_config(config)
core = Config(validate=False).config.get(Config.SECTION_CORE, {})
enabled = to_bool(core.get(Config.SECTION_CORE_ANALYTICS, "true"))
logger.debug(
"Analytics is {}.".format("enabled" if enabled else "disabled")
)
Expand Down
10 changes: 2 additions & 8 deletions tests/func/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,18 @@ def test(self):
self.assertNotEqual(a.info[a.PARAM_SYSTEM_INFO], {})

@mock.patch.object(os, "getenv", new=_clean_getenv)
@mock.patch(
"dvc.analytics.Analytics._is_enabled_config", return_value=True
)
@mock.patch("requests.post")
def test_send(self, mockpost, _):
def test_send(self, mockpost):
ret = main(["daemon", "analytics", Analytics().dump(), "-v"])
self.assertEqual(ret, 0)

self.assertTrue(mockpost.called)

@mock.patch.object(os, "getenv", new=_clean_getenv)
@mock.patch(
"dvc.analytics.Analytics._is_enabled_config", return_value=True
)
@mock.patch.object(
requests, "post", side_effect=requests.exceptions.RequestException()
)
def test_send_failed(self, mockpost, _):
def test_send_failed(self, mockpost):
ret = main(["daemon", "analytics", Analytics().dump(), "-v"])
self.assertEqual(ret, 0)

Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest

from dvc.analytics import Analytics


@pytest.mark.parametrize(
"config, result",
[
({}, True),
({"analytics": "false"}, False),
({"analytics": "true"}, True),
({"unknown": "broken"}, True),
({"analytics": "false", "unknown": "broken"}, False),
],
)
def test_is_enabled(dvc_repo, config, result, monkeypatch):
configobj = dvc_repo.config._repo_config
configobj["core"] = config
configobj.write()

# reset DVC_TEST env var, which affects `is_enabled()`
monkeypatch.delenv("DVC_TEST")

assert result == Analytics.is_enabled()